What is the purpose of the Tensorflow Gradient Tape?

前端 未结 3 1480
天涯浪人
天涯浪人 2020-12-12 14:39

I watched the Tensorflow Developer\'s summit video on Eager Execution in Tensorflow, and the presenter gave an introduction to \"Gradient Tape.\" Now I understand that Gradi

3条回答
  •  被撕碎了的回忆
    2020-12-12 15:00

    Having worked on this for a while, after posting the initial question, I have a better sense of where Gradient Tape is useful. Seems like the most useful application of Gradient Tap is when you design a custom layer in your keras model for example--or equivalently designing a custom training loop for your model.

    If you have a custom layer, you can define exactly how the operations occur within that layer, including the gradients that are computed and also calculating the amount of loss that is accumulated.

    So Gradient tape will just give you direct access to the individual gradients that are in the layer.

    Here is an example from Aurelien Geron's 2nd edition book on Tensorflow.

    Say you have a function you want as your activation.

     def f(w1, w2):
         return 3 * w1 ** 2 + 2 * w1 * w2
    

    Now if you want to take derivatives of this function with respec to w1 and w2:

    w1, w2 = tf.Variable(5.), tf.Variable(3.)
    with tf.GradientTape() as tape:
        z = f(w1, w2)
    
    gradients = tape.gradient(z, [w1, w2])
    

    So the optimizer will calculate the gradient and give you access to those values. Then you can double them, square them, triple them, etc., whatever you like. Whatever you choose to do, then you can add those adjusted gradients to the loss calculation for the backpropagation step, etc.

提交回复
热议问题