Yingchao Xiong
Yingchao Xiong

Reputation: 255

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.

Upvotes: 2

Views: 8994

Answers (2)

Salvador Dali
Salvador Dali

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:

  • you get consistent loss independent of your matrix size. On average you will get reduce_sum 4 times bigger for a two times bigger matrix
  • less chances you will get nan by overflowing

Upvotes: 2

c2huc2hu
c2huc2hu

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

Related Questions