Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If you use plus sign instead of tf.add, will tensorflow still calculate gradients correctly?

If I have a fairly complex equation for the cost like this:

cost = tf.reduce_sum( tf.multiply( y , tf.log(y/abs(yy)))  + \
                      tf.multiply( (1 - y) , tf.log((1-y)/abs(1-yy)) ) )

will tensorflow still figure out the correct backpropagation equations for this? In other words, how can I be sure that the gradients are calculated correctly?

like image 910
A.Razavi Avatar asked Feb 13 '18 13:02

A.Razavi


People also ask

How does TensorFlow gradient work?

Gradient tapesTensorFlow "records" relevant operations executed inside the context of a tf. GradientTape onto a "tape". TensorFlow then uses that tape to compute the gradients of a "recorded" computation using reverse mode differentiation.

What is built function in TensorFlow?

TensorFlow has built in function to create tensors for use in variables. For example, we can create a zero filled tensor of predefined shape using the tf. zeros() function as follows. We can evaluate tensors with calling a run() method on our session.


1 Answers

As described in this anwser, __add__ op (and __mul__ as well btw) are overloaded, therefore

cost = tf.reduce_sum(tf.add(
    tf.multiply( y , tf.log(y/abs(yy))),
    tf.multiply((1 - y) , tf.log((1-y)/abs(1-yy)))
))

is equivalent to

cost = tf.reduce_sum(y * tf.log(y/abs(yy))  + (1 - y) * tf.log((1-y)/abs(1-yy))) 
like image 114
pfm Avatar answered Sep 18 '22 13:09

pfm