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。

相关推荐
m0_6464299744 分钟前
LangGraph 学习笔记(一):StateGraph 与状态管理
python·langgraph
Dmz丶1 小时前
前端转 AI 100 天 Day 57:RAG 项目实战(六)——上线后迭代,从“能用”到“越用越好”
python
小小-猿1 小时前
用 Python 写昇腾算子:pyasc 是怎么回事
开发语言·python·昇腾·cann·pyasc
蓝蓝的代码人2 小时前
用 Python、Qt 和 Vue 做一个 Windows 股票看盘工具,观潮的行情小窗与桌面提醒
vue.js·python·qt·开源·桌面应用
老唐7772 小时前
具身智能视觉感知详解:从图像识别到机器人环境理解,核心技术、算法架构与应用实践
人工智能·深度学习·算法·机器人·大模型·具身智能
富贵儿9983 小时前
NL2SQL 实践手记 10:从命令行到网页查询——把整个系列串起来
数据库·python·sql·ai·自然语言处理·langchain
fengkai45453 小时前
Python对接LangChain智能体开发(Tool工具调用与 Agent智能体)
运维·开发语言·python·langchain·llm·ai agent·tool
小白的高手之路3 小时前
【LeetCode 热题 100】560. 和为 K 的子数组|前缀和、哈希表(Python)
数据结构·笔记·python·算法·leetcode·力扣·散列表
专注仿真3 小时前
仿真智能体训练(评估篇):鲁棒性/泛化测试算法详解与实战
人工智能·python·算法·afsim智能体训练·评估算法·鲁棒性算法·泛化测试算法
SummerJokes3 小时前
Java 集合 List 全套实验|从基础增删改查到实体类存储
java·python·list