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

torch.nn.functional.avg_pool1d is not working correctly #1249

Closed
yueyinqiu opened this issue Mar 1, 2024 · 2 comments · Fixed by #1251
Closed

torch.nn.functional.avg_pool1d is not working correctly #1249

yueyinqiu opened this issue Mar 1, 2024 · 2 comments · Fixed by #1251

Comments

@yueyinqiu
Copy link
Contributor

yueyinqiu commented Mar 1, 2024

Hello! I've found that torch.nn.functional.avg_pool1d is not working as expected.

Here is my test code:

using TorchSharp;

var x = torch.zeros(5, 7, 128);
Console.WriteLine(x.metastr());
// [5x7x128], type = Float32, device = cpu

var y1 = torch.nn.functional.avg_pool1d(x, 2);
Console.WriteLine(y1.metastr());
// [5x7x127], type = Float32, device = cpu

var y2 = torch.nn.AvgPool1d(2).call(x);
Console.WriteLine(y2.metastr());
// [5x7x64], type = Float32, device = cpu

And in PyTorch:

import torch

x = torch.zeros(5, 7, 128)
print(x.size())
# torch.Size([5, 7, 128])

y1 = torch.nn.functional.avg_pool1d(x, 2)
print(y1.size())
# torch.Size([5, 7, 64])

y2 = torch.nn.AvgPool1d(2)(x)
print(y2.size())
# torch.Size([5, 7, 64])

There seems to be something going wrong with torch.nn.functional.avg_pool1d, while torch.nn.AvgPool1d still works well.

@shaltielshmid
Copy link
Contributor

shaltielshmid commented Mar 2, 2024

Thank you for reporting! The discrepancy between PyTorch and TorchSharp is the default value assigned to the stride parameter. I submitted a fix for the next version, but until then you can just specify the stride parameter to be the same as the kernelSize parameter to get the desired results.

E.g.,

var y1 = torch.nn.functional.avg_pool1d(x, 2, 2);

@NiklasGustafsson
Copy link
Contributor

Fixed in v0.102.1.

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

Successfully merging a pull request may close this issue.

3 participants