Differentiate gradients(区分梯度)
本文介绍了区分梯度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在 PyTorch 中区分梯度?
Is there a way to differentiate gradients in PyTorch?
例如,我可以在 TensorFlow 中执行此操作:
For example, I can do this in 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 中做类似的事情?
How would I do something similar in PyTorch?
推荐答案
你只需要使用 torch.autograd.grad 函数,它和 tf.gradients 完全一样代码>.
You just need to use the function torch.autograd.grad it does exactly the same as tf.gradients.
所以在 pytorch 中,这将是:
So in pytorch this would be:
from torch.autograd import Variable, grad
import torch
def gradient_descent( loss_fnc, w, max_its, lr):
'''a gradient descent "RNN" '''
for k in range(max_its):
w = w - lr * grad( loss_fnc(w), w )
return w
lr = Variable(torch.zeros(1), , requires_grad=True)
w = Variable( torch.zeros(10), requires_grad=True)
reg = Variable( torch.ones(1) , requires_grad=True)
def loss_fnc(w):
return torch.sum((Variable(torch.ones(10)) - w)**2) + reg * torch.sum( w**2 )
w_n = gradient_descent( loss_fnc, w, 10, lr )
# differentiate through the gradient_descent RNN with respnect to the initial weight
print(grad( w_n, w))
# differentiate through the gradient_descent RNN with respnect to the learning rate
print(grad( w_n, lr))
这篇关于区分梯度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:区分梯度
基础教程推荐
猜你喜欢
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 包装空间模型 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
