Reputation: 255
I have an input dataset x with shape (10,1000), 10 inputs and 1000 lines and a output y with (1,1000), 1 output and 1000 lines.
The cost function I defined is
cost = tf.square(Y - prediction, name="cost")
The prediction is a single predicted output value and Y is the placeholder of output values. I used the code below to get the value of cost.
cost_value = sess.run(cost, feed_dict ={ X: x, Y : y })
Then the output cost function value is a (1000,1000) matrix since the feed of Y is a (1,1000) vector.
The question is how could I make a cost function that calculate the cost in a number instant of a matrix without looping all the inputs line by line.
Upvotes: 2
Views: 8994
Reputation: 222889
Also tf.reduce_sum(cost)
will do what you want, I think it is better to use tf.reduce_mean()
. Here are a few reasons why:
nan
by overflowingUpvotes: 2
Reputation: 2497
tf.reduce_sum(cost)
will sum all of the values in the matrix.
https://www.tensorflow.org/api_docs/python/tf/reduce_sum
Upvotes: 3