首页 > 解决方案 > 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)

标签: python-3.x

解决方案


如果你正在学习 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()

并直接调用该函数。

这是另一个变体。不确定哪个是最新的或与您需要的兼容。


推荐阅读