在使用pytorch在对MNIST数据集进行预览时,出现了TypeError: 'module' object is not callable的错误:

上报错信息图如下:



从图中可以看出,报错位置为第35行,也就是如下位置的错误:

images, labels = next(iter(data_loader_train))

在经过多次的检查发现,引起MNIST数据集无法显现的问题不是由于这一行所引起的,而是由于缺少了对图片进行处理,在加载数据代码的前添加上如下的代码:

transform = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda x: x.repeat(3,1,1)),
transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
])
  • 此时问题就已经解决了
  • 下面完整的代码贴出来:
 1.获取手写数字的训练集和测试集
# 2.root 存放下载的数据集的路径
# 3.transform用于指定导入数据集需要对数据进行哪种操作
# 4.train是指定在数据集下完成后需要载入数据哪部分
import torch
import torchvision
import matplotlib.pyplot as plt
from torchvision import *
from torchvision import transforms
from torch.autograd import Variable transform = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda x: x.repeat(3,1,1)),
transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
]) # 修改的位置 # 获取数据
data_train=datasets.MNIST(root='./MNIST_Model/',
transform=transforms,
train=True, # true训练集
download=True)
data_test=datasets.MNIST(root='./MNIST_Model/',
transform=transforms,
train=False) # false测试集
# 装载数据
data_loader_train=torch.utils.data.DataLoader(dataset=data_train,
batch_size=64, # 处理批次的大小(一次处理的数据大小)
shuffle=True) # 将处理的数据集合打乱
data_loader_test=torch.utils.data.DataLoader(dataset=data_test,
batch_size=64,
shuffle=True)
# 装载好数据之后,进行预览
images, labels = next(iter(data_loader_train))
img = torchvision.utils.make_grid(images) img = img.numpy().transpose(1, 2, 0)
std = [0.5, 0.5, 0.5]
mean = [0.5, 0.5, 0.5]
img = img * std + mean
plt.imshow(img)
plt.show() #这一行非常关键,不写这一行的代码的话,其预览的图片是无法展示出来的

最终的结果如图所示:

TypeError: 'module' object is not callable (pytorch在进行MNIST数据集预览时出现的错误)的更多相关文章

  1. TypeError: 'module' object is not callable cp fromhttp://blog.csdn.net/huang9012/article/details/17417133

    程序代码  class Person:      #constructor      def __init__(self,name,sex):           self.Name = name   ...

  2. Python TypeError: 'module' object is not callable 原因分析

    今天尝试使用pprint进行输出,语句为 >>>import pprint >>>pprint(people) 结果报错,TypeError: 'module' o ...

  3. PyCharm+selenium环境搭建报错:Traceback (most recent call last): TypeError: 'module' object is not callable

    环境搭建好后,代码如下: from selenium import webdriverdriver = webdriver.chrome()driver.get("http://www.ba ...

  4. python import 错误 TypeError: 'module' object is not callable

    python import 错误 TypeError: 'module' object is not callable 在这里,有 Person.py test.py; 在 test.py 里面 im ...

  5. TypeError: 'module' object is not callable 原因分析

    程序代码 class Person: #constructor def __init__(self,name,sex): self.Name = name self.Sex = sex def ToS ...

  6. pip安装pillow——死循环:[WinError5] & [TypeError:'module' object is not callable]

    1.这次本来要安装个pillow,记得以前装了的,怎么这次就不行了.然后,下意识的使用:pip3 install pillow. 发现报错: [TypeError:'module' object is ...

  7. python -- TypeError: 'module' object is not callable

    文件: 代码: import pprintmessge = 'It was a bringht cold day in April,and the clocks were striking thrir ...

  8. pip install 报错 TypeError: 'module' object is not callable

    $ pip install filetype Traceback (most recent call last): File "/usr/local/bin/pip2", line ...

  9. django TypeError: 'module' object is not callable

    原因:导入模块时直接把模块当函数使用 from rest_framework import reverse #import reverse module @api_view(("GET&qu ...

  10. python 报错——Python TypeError: 'module' object is not callable 原因分析

    原因分析:Python导入模块的方法有两种: import module 和 from module import 区别是前者所有导入的东西使用时需加上模块名的限定,而后者则不需要 例: >&g ...

随机推荐

  1. idea 热部署插件 JRebel 安装

    idea 热部署插件 JRebel 安装 1.安装 直接在idea 插件搜索安装 JRebel and XRebel 安装,安装后需要破解才能使用 2.破解 破解原来需要远程连接服务器破解或者下载源码 ...

  2. Solution Set -「CF 1490」

    「CF 1490A」Dense Array Link. 显然不满足的 adjacent elements 之间一直加 \(\min\times2,\min\times4,\cdots,\min\tim ...

  3. open3d -- voxel_down_sample

    官网文档 parameter: Input: open3d.geometry.Pointcloud点云类 voxel_size: 体素单位长度 Return: 处理后的点云类 Description: ...

  4. 其它——Apache-ab压力测试工具使用

    文章目录 一 介绍 二 安装 2.1 windows安装 2.2 Linux安装 三 使用 四 参数介绍 一 介绍 Apache Benchmark(简称ab) 是Apache安装包中自带的压力测试工 ...

  5. 程序员必备:使用AI工具通义千问,没有广告没有假链接,比搜索引擎要便捷多了,回答还算满意

    我提的问题是:使用Python连接Oracle,cx_Oracle和oracledb那个效率更高,功能更全,请列出他们的区别和实例代码 回复是: 以下是使用Python连接Oracle时,cx_Ora ...

  6. 43道Python经典案例题(有答案)

    1.有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? for x in range(0,5): for y in range(0,5): for z in range( ...

  7. Jackson--FastJson--XStream--代码执行&&反序列化

    Jackson--FastJson--XStream--代码执行&&反序列化 Jackson代码执行 (CVE-2020-8840) 影响范围 2.0.0 <= FasterXM ...

  8. linux 查找命令(whatis,free,df,top)

    whatis whatis命令是用于查询一个命令执行什么功能,并将查询结果打印到终端上,等同于 man -f: # whatis grep grep (1) - print lines matchin ...

  9. MIT协议原文及中文翻译

    MIT协议原文及翻译 参考链接 原文: Copyright ( C ) Permission is hereby granted, free of charge, to any person obta ...

  10. ELK-日志收集-Kibana WEB安全认证

    1.ELK收集MYSQL日志实战: 日志收集存放目录位置: /usr/local/logstash/config/etc/   1)日志采集-存入redis缓存数据库:mysql-redis.conf ...