maxpool long数据类型报错

报错

RuntimeError: "max_pool2d" not implemented for 'Long'

源码:

python 复制代码
import torch
from torch import nn
from torch.nn import MaxPool2d

input = torch.tensor([[1, 2, 0, 3, 1],
                      [0, 1, 2, 3, 1],
                      [1, 2, 1, 0, 0],
                      [5, 2, 3, 1, 1],
                      [2, 1, 0, 1, 1]])

input = torch.reshape(input, (-1, 1, 5, 5))

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=True)
    def forward(self,input):
        output = self.maxpool1(input)
        return output

tudui = Tudui()
output = tudui(input)
print(output)

错误是说,max_pool2d不能操作long数据类型。也就是我们需要修改input的数据类型。

对input的tensor进行修改,增加一句

python 复制代码
dtype=torch.float32
python 复制代码
input = torch.tensor([[1, 2, 0, 3, 1],
                      [0, 1, 2, 3, 1],
                      [1, 2, 1, 0, 0],
                      [5, 2, 3, 1, 1],
                      [2, 1, 0, 1, 1]], dtype=torch.float32)

修改后代码:

python 复制代码
import torch
from torch import nn
from torch.nn import MaxPool2d

input = torch.tensor([[1, 2, 0, 3, 1],
                      [0, 1, 2, 3, 1],
                      [1, 2, 1, 0, 0],
                      [5, 2, 3, 1, 1],
                      [2, 1, 0, 1, 1]], dtype=torch.float32)

input = torch.reshape(input, (-1, 1, 5, 5))

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=True)
    def forward(self,input):
        output = self.maxpool1(input)
        return output

tudui = Tudui()
output = tudui(input)
print(output)

运行结果:

运行结果中的数据类型都变成了float。

相关推荐
爱笑的眼睛1113 分钟前
PyTorch自动微分:超越基础,深入动态计算图与工程实践
java·人工智能·python·ai
Daily Mirror27 分钟前
Day33 类的装饰器
python
web3.088899942 分钟前
震坤行商品详情数据接口可以用于获取商品详情信息
python·计算机网络
Salt_07281 小时前
DAY32 类的定义和方法
开发语言·python·算法·机器学习
Nick_zcy1 小时前
新能源汽车推荐系统分享
爬虫·python·汽车·推荐算法
未来影子1 小时前
Java领域构建Agent新杀入一匹黑马(agentscope-java)
java·开发语言·python
IT阳晨。1 小时前
【CNN与卷积神经网络(吴恩达)】卷积神经网络学习笔记
笔记·深度学习·神经网络·cnn
Better Bench1 小时前
ThinkStation PGX 与 RTX 4090 深度学习性能对比测试报告
人工智能·深度学习·显卡·联想·4090·计算性能·pgx
管理大亨1 小时前
ELK的操作应用
开发语言·python·elk