在Pytorch中对整数张量执行最大池化

时间:2019-06-25 07:28:46

标签: pytorch

我正在定义一个最大池层,并按如下所示传递整数张量。

var coords = [{
    City: "Hong Kong",
    lat: "22.667790",
    long: "-111.909905"
  },
  {
    City: "Atlanta",
    lat: "22.958769",
    long: "-111.948939"
  },
  {
    City: "Paris",
    lat: "23.989803",
    long: "-112.989850"
  },
  {
    City: "Sydney",
    lat: "22.001118",
    long: "-111.939433"
  },
  {
    City: "Hong Kong",
    lat: "22.667790",
    long: "-111.909905"
  }
];



origin = {
  lat: "22.611009",
  long: "-111.967870"
};

function getDistance(lat1, lon1, lat2, lon2) {
  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(lat2 - lat1); // deg2rad below
  var dLon = deg2rad(lon2 - lon1);
  var a =
    Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
    Math.sin(dLon / 2) * Math.sin(dLon / 2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  var d = R * c; // Distance in km
  return d;

}


function deg2rad(deg) {
  return deg * (Math.PI / 180);
}



var matching = coords.filter(function(coord) {
  return getDistance(coord.lat, coord.long, origin.lat, origin.long) < 10;
});

console.info(matching);

它引发以下错误:

flutter clean

3 个答案:

答案 0 :(得分:2)

如错误消息所示,nn.MaxPoll2d仅支持浮点输入张量。
您需要在应用池之前将输入int张量转换为torch.float

答案 1 :(得分:1)

PyTorch已针对与浮点数进行了优化。

我重写了您的示例:

import torch.nn as nn
max_pool = nn.MaxPool2d(3, stride=2)
t = torch.Tensor(3,5,5).random_(0, 10)
print(t)
max_pool(t)

您可以仅使用Tensor代替FloatTensor,因为默认情况下它是浮点数32位。

max_pool = nn.MaxPool2d(3, stride=2)
t = torch.Tensor(3,5,5).uniform_(0, 10)
print(t)
max_pool(t)

出局:

tensor([[[1., 4., 3., 8., 9.],
         [7., 4., 6., 3., 8.],
         [1., 3., 5., 8., 7.],
         [0., 1., 5., 3., 8.],
         [3., 9., 1., 4., 8.]],

        [[8., 8., 9., 6., 8.],
         [0., 3., 3., 5., 5.],
         [6., 8., 7., 2., 7.],
         [2., 0., 9., 2., 1.],
         [8., 0., 6., 3., 9.]],

        [[7., 3., 5., 6., 7.],
         [7., 3., 1., 7., 1.],
         [7., 1., 7., 2., 5.],
         [6., 8., 2., 8., 8.],
         [9., 7., 3., 4., 6.]]])

tensor([[[7., 9.],
         [9., 8.]],

        [[9., 9.],
         [9., 9.]],

        [[7., 7.],
         [9., 8.]]])

在第二个示例中,我还使用了uniform_函数。您可能会猜测输出。

tensor([[[4.9505, 4.3413, 2.0268, 0.0171, 5.8553],
         [4.7359, 0.6695, 3.8030, 4.8984, 1.6336],
         [1.0383, 2.5309, 8.2504, 9.2204, 7.0429],
         [7.3840, 7.7813, 8.3955, 3.9352, 2.5984],
         [2.5878, 7.4873, 7.9175, 5.5030, 1.3033]],

        [[8.8296, 8.3914, 1.7067, 6.5193, 7.6584],
         [7.3535, 3.8681, 7.2349, 7.2388, 6.0021],
         [1.9144, 2.0320, 9.7701, 0.6756, 2.4237],
         [5.1340, 1.1434, 5.9940, 2.5115, 0.8283],
         [7.2698, 2.9935, 7.4333, 6.1474, 2.2367]],

        [[6.8579, 5.7366, 6.6372, 1.0188, 0.8168],
         [8.1572, 2.3252, 8.5032, 2.8171, 5.5800],
         [4.3219, 7.7060, 4.2497, 7.4305, 7.7767],
         [3.8406, 4.8675, 9.8044, 2.7436, 7.7930],
         [8.3616, 4.9148, 4.3417, 7.2583, 8.1779]]])

tensor([[[8.2504, 9.2204],
         [8.3955, 9.2204]],

        [[9.7701, 9.7701],
         [9.7701, 9.7701]],

        [[8.5032, 8.5032],
         [9.8044, 9.8044]]])

答案 2 :(得分:1)

我使用了Float张量,它是固定的,

max_pool = nn.MaxPool2d(3, stride=1)
a = torch.FloatTensor(3,5,5).random_(0, 10)
print(a)
tensor([[[2., 8., 6., 8., 3.],
     [6., 6., 7., 6., 6.],
     [2., 0., 8., 8., 8.],
     [2., 0., 3., 5., 7.],
     [9., 7., 8., 2., 1.]],

    [[1., 8., 6., 7., 3.],
     [0., 1., 2., 9., 4.],
     [1., 2., 5., 0., 1.],
     [8., 2., 8., 3., 1.],
     [5., 4., 0., 5., 2.]],

    [[1., 6., 2., 6., 1.],
     [4., 0., 0., 6., 6.],
     [4., 2., 2., 3., 2.],
     [1., 0., 1., 7., 1.],
     [8., 1., 0., 5., 4.]]])
max_pool(a)
tensor([[[8., 8., 8.],
     [8., 8., 8.],
     [9., 8., 8.]],

    [[8., 9., 9.],
     [8., 9., 9.],
     [8., 8., 8.]],

    [[6., 6., 6.],
     [4., 7., 7.],
     [8., 7., 7.]]])