首页 > 其他分享 >RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

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

时间:2023-02-06 19:00:31浏览次数:60  
标签:loss RuntimeError does grad tensors fn

 

 

 

 报错如下:

 

原因: 这里的loss默认的requires_grad是False,因此在backward()处不会计算梯度,导致出错

方法:将loss的requires_grad属性设为True,如下所示:

 

标签:loss,RuntimeError,does,grad,tensors,fn
From: https://www.cnblogs.com/leifzhang/p/17096432.html

相关文章