Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does TensorFlow handle the differentials for L1 regularization?

Tags:

tensorflow

it seems that you can just declare a cost function by tf.abs() and then pass it down to auto-gradient generation (see https://github.com/nfmcclure/tensorflow_cookbook/blob/master/03_Linear_Regression/04_Loss_Functions_in_Linear_Regressions/04_lin_reg_l1_vs_l2.py)

. but we know abs() is not differentiable.

how is this done in Tensorflow? does it just randomly throw a number in [-1,1] ?

if someone could please point me to the implementation that would be great. Thanks!

(I looked for tensorflow.py in the git, but it does not even exist)

like image 992
teddy teddy Avatar asked Jan 07 '17 07:01

teddy teddy


People also ask

How does TensorFlow do automatic differentiation?

To differentiate automatically, TensorFlow needs to remember what operations happen in what order during the forward pass. Then, during the backward pass, TensorFlow traverses this list of operations in reverse order to compute gradients.

Is TF ABS differentiable?

abs is non-differentiable in principle (discontinuity at 0) but the same applies to tf. nn.

How does TensorFlow compute derivatives?

Tensorflow calculates derivatives using automatic differentiation. This is different from symbolic differentiation and numeric differentiation (aka finite differences). More than a smart math approach, it is a smart programming approach.

What are gradients in TensorFlow?

The gradients are the partial derivatives of the loss with respect to each of the six variables. TensorFlow presents the gradient and the variable of which it is the gradient, as members of a tuple inside a list. We display the shapes of each of the gradients and variables to check that is actually the case.


1 Answers

f(x) = abs(x) is differentiable everywhere, except at x=0. It derivative equals:

abs derivative

So the only question is how tensorflow implements derivative at x=0. You can check this manually:

import tensorflow as tf
x = tf.Variable(0.0)
y = tf.abs(x)
grad = tf.gradients(y, [x])[0]
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(grad))

It prints 0.0.

like image 51
standy Avatar answered Sep 30 '22 17:09

standy