Pytorch的runtime error

RuntimeError: bool value of Tensor with more than one value is ambiguous

运行下面这段代码的时候出错了,后来网上搜说改成 if w1.grad is not None:  可以通过。  

#===========PyTorch: Autograd==============
#====A PyTorch Variable is a node in a computational graph===
#==============x.data is a Tensor
#=============x.grad is a Variable of gradients(same shape as x.data)==
#========x.grad.data is a Tensor of gradients===========================
import torch 
from torch.autograd import Variable
N,D_in, H,D_out = 64, 1000, 100, 10
x = Variable(torch.randn(N, D_in), requires_grad=False)
y = Variable(torch.randn(N, D_out), requires_grad=False)
w1 = Variable(torch.randn(D_in, H), requires_grad=True)
w2 = Variable(torch.randn(H,D_out), requires_grad=True)

learning_rate = 1e-6
for t in range(500):
  y_pred = x.mm(w1).clamp(min=0).mm(w2)
  loss = ((y_pred-y).pow(2).sum())
  #should be if w1.grad is not None:
  if w1.grad:
    w1.grad.data.zero_()
  if w2.grad: 
    w2.grad.data.zero_()
  loss.backward()
  
  w1.data -= learning_rate * w1.grad.data
  w2.data -= learning_rate * w2.grad.data
  print(loss)

 

转载于:https://www.cnblogs.com/Shinered/p/10762783.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值