栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Mismatch in shape: grad

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mismatch in shape: grad

错误日志

错误一:

Mismatch in shape: grad_output[0] has a shape of torch.Size([2]) and output[0] has a shape of torch.Size([2, 2])

错误二 

expected sequence of length 2 at dim 1 (got 1)

错误原因

在进行 张量 梯度求解时,传入数据维度不对 

错误代码示例
import torch

# 第一步:创建 tensor
x = torch.ones(2,2,requires_grad=True)
print(x)

# 第二步:对 tensor 做处理
y = x**2
print(y)

# 第三步:求梯度
y.backward(torch.tensor([[3,2],[2]],dtype=float))# 这里加了一个类型
print(x.grad)
正确代码示例
import torch

# 第一步:创建 tensor
x = torch.ones(2,2,requires_grad=True)
print(x)

# 第二步:对 tensor 做处理
y = x**2
print(y)

# 第三步:求梯度
y.backward(torch.tensor([[3,2],[2,2]],dtype=float))# 这里加了一个类型
print(x.grad)
运行结果

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/283460.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号