Skip to content

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

Closed

Description

@yueyinqiu

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.

Activity

  1. shaltielshmid commented on Mar 2, 2024

    @shaltielshmid
    Contributor

    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);
  2. NiklasGustafsson commented on Mar 5, 2024

    @NiklasGustafsson

    Fixed in v0.102.1.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions