Search code examples
pythontensorflowdeep-learningcost-based-optimizer

Tensorflow cost function


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.


Solution

  • tf.reduce_sum(cost) will sum all of the values in the matrix.

    https://www.tensorflow.org/api_docs/python/tf/reduce_sum