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。

相关推荐
纪伊路上盛名在1 分钟前
Accurate structure prediction of biomolecular interactions with AlphaFold 3
深度学习·阅读·文献·结构·蛋白质
无敌的黑星星8 分钟前
Java8 CompletableFuture 实战指南
linux·前端·python
StockTV18 分钟前
印度股票实时数据 NSE和BSE的实时行情、K 线及指数数据
java·开发语言·spring boot·python
chaofan98018 分钟前
GPT-5.5 领衔 Image 2.0:像素级控制时代,AI 绘图告别开盲盒
开发语言·人工智能·python·gpt·自动化·api
七颗糖很甜29 分钟前
“十五五”气象发展规划:聚焦五大核心任务
大数据·python·算法
爱码小白39 分钟前
Python 异常处理 完整学习笔记
开发语言·python
芝士就是力量啊 ೄ೨1 小时前
Python如何编写一个简单的类
开发语言·python
胖虎喜欢静香1 小时前
从零到一快速实现 Mini DeepResearch
人工智能·python·开源
qq_392690661 小时前
Redis怎样应对Redis集群整体宕机带来的雪崩
jvm·数据库·python
β添砖java1 小时前
深度学习(11)数值稳定+模型初始化、激活函数
人工智能·深度学习