【发布时间】:2018-08-15 10:11:13
【问题描述】:
有没有办法在 PyTorch 中区分渐变?
例如,我可以在 TensorFlow 中做到这一点:
from pylab import *
import tensorflow as tf
tf.reset_default_graph()
sess = tf.InteractiveSession()
def gradient_descent( loss_fnc, w, max_its, lr):
'''a gradient descent "RNN" '''
for k in range(max_its):
w = w - lr * tf.gradients( loss_fnc(w), w )[0]
return w
lr = tf.Variable( 0.0, dtype=tf.float32)
w = tf.Variable( tf.zeros(10), dtype=tf.float32)
reg = tf.Variable( 1.0, dtype=tf.float32 )
def loss_fnc(w):
return tf.reduce_sum((tf.ones(10) - w)**2) + reg * tf.reduce_sum( w**2 )
w_n = gradient_descent( loss_fnc, w, 10, lr )
sess.run( tf.initialize_all_variables())
# differentiate through the gradient_descent RNN with respnect to the initial weight
print(tf.gradients( w_n, w))
# differentiate through the gradient_descent RNN with respnect to the learning rate
print(tf.gradients( w_n, lr))
输出是
[<tf.Tensor 'gradients_10/AddN_9:0' shape=(10,) dtype=float32>]
[<tf.Tensor 'gradients_11/AddN_9:0' shape=() dtype=float32>]
我如何在 PyTorch 中做类似的事情?
【问题讨论】:
标签: python neural-network deep-learning gradient-descent pytorch