python-3.x - ValueError Traceback(最近一次调用最后一次)在()
问题描述
我正在尝试建立一个模型来识别图像中的手写数字。我已经编写了一个代码,现在我想查看我的模型的预测,但我卡住了,它向我显示了一个错误,例如:
ModuleNotFoundError:没有名为“helper”的模块。
下面是代码;
%matplotlib inline
import helper
images,labels=next(iter(trainloader))
img=images[0].view(1,784)
with torch.no_grad():
logits=model.forward(img)
ps=F.softmax(logits,dim=1)
helper.view_classify(img.view(1,28,28),ps)
解决方案
如果你正在学习 Udacity 课程,这是他们编写的一个模块。您可以简单地放入此代码,
import matplotlib.pyplot as plt
import numpy as np
def view_classify(img, ps, version="MNIST"):
''' Function for viewing an image and it's predicted classes.
'''
ps = ps.data.numpy().squeeze()
fig, (ax1, ax2) = plt.subplots(figsize=(6,9), ncols=2)
ax1.imshow(img.resize_(1, 28, 28).numpy().squeeze())
ax1.axis('off')
ax2.barh(np.arange(10), ps)
ax2.set_aspect(0.1)
ax2.set_yticks(np.arange(10))
if version == "MNIST":
ax2.set_yticklabels(np.arange(10))
elif version == "Fashion":
ax2.set_yticklabels(['T-shirt/top',
'Trouser',
'Pullover',
'Dress',
'Coat',
'Sandal',
'Shirt',
'Sneaker',
'Bag',
'Ankle Boot'], size='small');
ax2.set_title('Class Probability')
ax2.set_xlim(0, 1.1)
plt.tight_layout()
并直接调用该函数。
这是另一个变体。不确定哪个是最新的或与您需要的兼容。
推荐阅读
- c# - 如示例所示创建 xml 标头
- ios - iOS SWIFT - 检测从中央关闭的外围设备
- python - FastAPI JWT 令牌无效
- vuejs3 - 将变量从 Vue 3 组件传递到另一个文件而不使用 Vuex
- sql - 通过数据库链接启动和获取从一个 SQL 服务器到另一个 SQL 服务器的备份文件
- react-native - 在 react native 中按下默认的 react native 选择器时如何调用函数
- css - 确保在 Selenium 中触发悬停媒体查询
- amazon-web-services - 运行粘合作业时出现 AWS 错误 无法清理 XML
- iis - IIS 中同一域上的多个绑定到子文件夹
- python - 创建 python 输出的图像