Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

paddle.nn.Embedding()与torch.nn.Embedding()的权重对不齐! #68695

Open
openvino-book opened this issue Oct 14, 2024 · 2 comments
Open
Assignees

Comments

@openvino-book
Copy link

bug描述 Describe the Bug

请问如何初始化让paddle.nn.Embedding()与torch.nn.Embedding()的权重可以一样?

PyTorch代码:

import torch
print("PyTorch version:", torch.__version__)
torch.manual_seed(1)

vocab_size = 6
output_dim = 3

embedding_layer = torch.nn.Embedding(vocab_size, output_dim)
print(embedding_layer.weight)
print(embedding_layer(torch.tensor([3])))

Paddle代码:

import paddle
print("PaddlePaddle version:", paddle.__version__)
paddle.seed(1)

vocab_size = 6
output_dim = 3

embedding_layer = paddle.nn.Embedding(vocab_size, output_dim)
print(embedding_layer.weight)
print(embedding_layer(paddle.to_tensor([3])))

二者输出结果完全不一样
1728907832888

其他补充信息 Additional Supplementary Information

No response

@zoooo0820
Copy link
Contributor

参考 #44565 默认初始化的方法有差异

@ILoveAmy
Copy link

ILoveAmy commented Oct 15, 2024

参考 #44565 默认初始化的方法有差异

多谢!~ 原来paddle.nn.Embedding默认的参数初始化,是用的]XavierUniform,:[-x, x]间的均匀分布,其中 x = \sqrt{\frac{6.0}{fan_in + fan_out}}

XavierUniform做参数初始化非常不错!有助于提高训练速度和收敛性,并能增强模型的泛化能力

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants