Reputation: 5102
I am defining a max pool layer and passing in an integer tensor as follows.
max_pool = nn.MaxPool2d(3, stride=2)
max_pool(torch.IntTensor(3,5,5).random_(0, 10))
It throws the following error:
RuntimeError: _thnn_max_pool2d_with_indices_forward not supported on CPUType for Int
Upvotes: 1
Views: 1502
Reputation: 5102
I used Float tensors and it was fixed,
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.]]])
Upvotes: 1
Reputation: 46331
I rewrote your the example:
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)
Instead of FloatTensor
you can use just Tensor, since it is float 32-bit by default.
max_pool = nn.MaxPool2d(3, stride=2)
t = torch.Tensor(3,5,5).uniform_(0, 10)
print(t)
max_pool(t)
Out:
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.]]])
In the second example I also used uniform_
function. You may guess the output.
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]]])
Upvotes: 1
Reputation: 114786
As the error message suggests, nn.MaxPoll2d
only supports floating point input tensors.
You'll need to cast your input int tensor to torch.float
before applying the pooling.
Upvotes: 2