maxpool long数据类型报错

报错

RuntimeError: “max_pool2d” not implemented for ‘Long’

在这里插入图片描述
源码:

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进行修改,增加一句

dtype=torch.float32
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)

修改后代码:

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. spark 写入 hudi时数据类型

    2024-04-10 07:26:02       59 阅读
  2. spark写入数据

    2024-04-10 07:26:02       61 阅读
  3. Oracle修改Number类型精度:ORA-01440

    2024-04-10 07:26:02       42 阅读
  4. npm insall无效的依赖类型:别名(alias)

    2024-04-10 07:26:02       41 阅读
  5. Mybatis plus update PG json 类型 解决

    2024-04-10 07:26:02       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-10 07:26:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-10 07:26:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-10 07:26:02       87 阅读
  4. Python语言-面向对象

    2024-04-10 07:26:02       96 阅读

热门阅读

  1. MySQL-系统及自定义变量

    2024-04-10 07:26:02       43 阅读
  2. LeetCode题练习与总结:排列序列--60

    2024-04-10 07:26:02       46 阅读
  3. Linux中MySQL测试环境搭建主主集群

    2024-04-10 07:26:02       42 阅读
  4. MySQL View 视图

    2024-04-10 07:26:02       33 阅读
  5. 计算机视觉(CV)技术的优势和挑战

    2024-04-10 07:26:02       33 阅读
  6. Swift入门

    2024-04-10 07:26:02       40 阅读