dotnet / TorchSharp

A .NET library that provides access to the library that powers PyTorch.
MIT License
1.37k stars 177 forks source link

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

Closed yueyinqiu closed 7 months ago

yueyinqiu commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

Fixed in v0.102.1.