How do I get the gradient of the loss at a TensorFlow variable?

后端 未结 2 544
慢半拍i
慢半拍i 2020-12-23 16:18

The feature I\'m after is to be able to tell what the gradient of a given variable is with respect to my error function given some data.

One way to do this would be

相关标签:
2条回答
  • 2020-12-23 16:55

    In TensorFlow 2.0 you can use GradientTape to achieve this. GradientTape records the gradients of any computation that happens in the context of that. Below is an example of how you might do that.

    import tensorflow as tf
    
    # Here goes the neural network weights as tf.Variable
    x = tf.Variable(3.0)
    
    # TensorFlow operations executed within the context of
    # a GradientTape are  recorded for differentiation 
    with tf.GradientTape() as tape:
      # Doing the computation in the context of the gradient tape
      # For example computing loss
      y = x ** 2 
    
    # Getting the gradient of network weights w.r.t. loss
    dy_dx = tape.gradient(y, x) 
    print(dy_dx)  # Returns 6
    
    0 讨论(0)
  • 2020-12-23 17:07

    The tf.gradients() function allows you to compute the symbolic gradient of one tensor with respect to one or more other tensors—including variables. Consider the following simple example:

    data = tf.placeholder(tf.float32)
    var = tf.Variable(...)              # Must be a tf.float32 or tf.float64 variable.
    loss = some_function_of(var, data)  # some_function_of() returns a `Tensor`.
    
    var_grad = tf.gradients(loss, [var])[0]
    

    You can then use this symbolic gradient to evaluate the gradient in some specific point (data):

    sess = tf.Session()
    
    var_grad_val = sess.run(var_grad, feed_dict={data: ...})
    
    0 讨论(0)
提交回复
热议问题