x = torch.ones(1) # 默认requires_grad = False
print x.requires_grad # False
y = torch.ones(1) #y的requires_grad标志也是False
z = x + y # z的requires_grad也是False, 所以如果试图对z做梯度反传,会抛出Error

z.backward()

RuntimeError: element 0 of tensors does not require grad and does not
have a grad_fn

w = torch.zeros(1, requires_grad=True) # 创建 requires_grad=True 的Tensor
print w.requires_grad #True

total = w + z

print total.requires_grad#True

total.backward()

print w.grad#tensor([ 1.])
print z.grad == x.grad == y.grad == None #True

GitHub 加速计划 / eleme / element
54.06 K
14.63 K
下载
A Vue.js 2.0 UI Toolkit for Web
最近提交(Master分支:3 个月前 )
c345bb45 7 个月前
a07f3a59 * Update transition.md * Update table.md * Update transition.md * Update table.md * Update transition.md * Update table.md * Update table.md * Update transition.md * Update popover.md 7 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐