CrossEntropyLoss, Paddle报错,Torch不报错
收藏
from paddle import nn
import paddle
pre = paddle.to_tensor([[ [[1.4146, 0.1446],[1.8990, -1.1429]],[[0.9828, -1.8893],[-1.5256, -1.8724]],[[-1.2521, 1.8987],[-0.2531, -0.1878]]]],'float32')
mask1 = paddle.to_tensor([[[1,2],[2,0]]])
ce_loss = nn.CrossEntropyLoss()
ce_loss(pre,mask1)
pre 维度 [1,3,2,2]
mask1 维度[1,2,2]
用Torch 得到的结果是1.2123
用paddle得到的错误是
----> 3 ce_loss(pre,mask1)
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py in __call__(self, *inputs, **kwargs)
915
916 def __call__(self, *inputs, **kwargs):
--> 917 return self._dygraph_call_func(*inputs, **kwargs)
918
919 def forward(self, *inputs, **kwargs):
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py in _dygraph_call_func(self, *inputs, **kwargs)
905 self._built = True
906
--> 907 outputs = self.forward(*inputs, **kwargs)
908
909 for forward_post_hook in self._forward_post_hooks.values():
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/nn/layer/loss.py in forward(self, input, label)
404 axis=self.axis,
405 use_softmax=self.use_softmax,
--> 406 name=self.name)
407
408 return ret
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/nn/functional/loss.py in cross_entropy(input, label, weight, ignore_index, reduction, soft_label, axis, use_softmax, name)
1660 if label_max >= input.shape[axis]:
1661 raise ValueError("label should not out of bound, but got{}".
-> 1662 format(label_max))
1663
1664 _, out = _C_ops.softmax_with_cross_entropy(
ValueError: label should not out of bound, but gotTensor(shape=[1], dtype=int64, place=CUDAPlace(0), stop_gradient=True,
[2])
0
收藏
请登录后评论
这个不能硬套,你得看看paddle的函数对应的入参是什么要求