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。

相关推荐
千里码aicood8 小时前
计算机大数据、人工智能与智能系统开发定制开发
大数据·人工智能·深度学习·决策树·机器学习·森林树
ID_1800790547310 小时前
基于 Python 的 Cdiscount 商品详情 API 调用与 JSON 核心字段解析(含多规格 SKU 提取)
开发语言·python·json
Q_Q51100828510 小时前
python+django/flask+vue的大健康养老公寓管理系统
spring boot·python·django·flask·node.js
我是哈哈hh10 小时前
【Python数据分析】Numpy总结
开发语言·python·数据挖掘·数据分析·numpy·python数据分析
Michelle802310 小时前
24大数据 14-2 函数练习
开发语言·python
qq_3814549910 小时前
Python学习技巧
开发语言·python·学习
学生高德10 小时前
小模型结合大模型的加速方法关键笔记
人工智能·深度学习·机器学习
Ndmzi10 小时前
Matlab编程技巧:自定义Simulink菜单(理解补充)
前端·javascript·python
dagouaofei11 小时前
AI生成个性化年终总结PPT
人工智能·python·powerpoint
Aspect of twilight12 小时前
深度学习各种优化器详解
人工智能·深度学习