PaddlePaddle / Paddle

PArallel Distributed Deep LEarning: Machine Learning Framework from Industrial Practice (『飞桨』核心框架,深度学习&机器学习高性能单机、分布式训练和跨平台部署)
http://www.paddlepaddle.org/
Apache License 2.0
22.08k stars 5.55k forks source link

同样的操作有些结果和pytorch不一样,有些一样 #47813

Closed DLlearn closed 8 months ago

DLlearn commented 1 year ago

bug描述 Describe the Bug

交叉熵损失同样输入,在不加label smooth和加label_smooth时结果与pytorch相同;加与不加类别权重weight都与pytorch相同,但在同加上类别权重和label smooth时结果不一样,麻烦确认一下。代码如下:

import torch 
import torch.nn as tnn
import paddle 
import paddle.nn as pnn
import copy
import numpy as np

inputs = np.array([0,1,0,0,0,1],dtype=np.float32)
outputs= np.array([0,1],dtype=np.int64)
weights=np.array([1,2,3],dtype=np.float32)

tinputs = torch.from_numpy(inputs)
toutputs = torch.from_numpy(outputs)
tinputs = tinputs.view((1,3,2))
toutputs = toutputs.view((1,2))
tweight = torch.from_numpy(weights)
tce = tnn.CrossEntropyLoss(weight=tweight,label_smoothing=0.1)
tloss=tce(tinputs,toutputs)
print(tloss.numpy())  
# 输出1.5991

pinputs = paddle.to_tensor(inputs)
poutputs = paddle.to_tensor(outputs)
pinputs = pinputs.reshape((1,3,2)).transpose((0,2,1))
poutputs = poutputs.reshape((1,2))
poutputs = pnn.functional.one_hot(poutputs,num_classes=3)
poutputs = pnn.functional.label_smooth(poutputs,epsilon=0.1)
pweight = paddle.to_tensor(copy.deepcopy(weights))
pce = pnn.CrossEntropyLoss(weight=pweights,soft_label=True)
ploss=pce(pinputs,poutputs)
print(ploss.numpy())
#输出 [1.5481]

其他补充信息 Additional Supplementary Information

No response

paddle-bot[bot] commented 1 year ago

您好,我们已经收到了您的问题,会安排技术人员尽快解答您的问题,请耐心等待。请您再次检查是否提供了清晰的问题描述、复现代码、环境&版本、报错信息等。同时,您也可以通过查看官网API文档常见问题历史IssueAI社区来寻求解答。祝您生活愉快~

Hi! We've received your issue and please be patient to get responded. We will arrange technicians to answer your questions as soon as possible. Please make sure that you have posted enough message to demo your request. You may also check out the APIFAQGithub Issue and AI community to get the answer.Have a nice day!

pangyoki commented 1 year ago

你好,已经复现了你的问题,我找相关同学咨询下。

paddle-bot[bot] commented 8 months ago

Since you haven\'t replied for more than a year, we have closed this issue/pr. If the problem is not solved or there is a follow-up one, please reopen it at any time and we will continue to follow up. 由于您超过一年未回复,我们将关闭这个issue/pr。 若问题未解决或有后续问题,请随时重新打开,我们会继续跟进。