def predict(net, test_iter):
    """模型预测"""
    X, y = iter(test_iter).next()
    print("X shape:", X.shape)
    print("y shape:", y.shape)
    if isinstance(net, torch.nn.Module):
        net.eval()  # 评估模式,这会关闭dropout
        y_hat = net(X).argmax(dim=1)
        net.train()
    else:  # 自定义的模型, func.__code__.co_varnames:将函数局部变量以元组的形式返回
        if ('is_training' in net.__code__.co_varnames):  # 如果有is_training这个参数
            # 将is_training设置成False
            y_hat = net(X, is_training=False).argmax(dim=1)
        else:
            y_hat = net(X).argmax(dim=1)

    true_labels = get_fashion_mnist_labels(y.numpy())
    predict_labels = get_fashion_mnist_labels(y_hat.numpy())

    titles = [
        true + '\n' + pred for true, pred in zip(true_labels, predict_labels)
    ]
    show_fashion_mnist(X[10:19],
                       titles[10:19],
                       imagepath='../figures/3.13_dropout.jpg')
Ejemplo n.º 2
0
def predict(net, test_iter):
    X, y = iter(test_iter).next()
    y_hat = net(X).argmax(dim=1)

    true_labels = get_fashion_mnist_labels(y.numpy())
    predict_labels = get_fashion_mnist_labels(y_hat.numpy())

    titles = [
        true + '\n' + pred for true, pred in zip(true_labels, predict_labels)
    ]
    show_fashion_mnist(
        X[10:19],
        titles[10:19],
        imagepath='../figures/3.6_softmax-regression-scratch.jpg')
def predict(net, test_iter):
    X, y = iter(test_iter).next()
    print("X shape:", X.shape)
    print("y shape:", y.shape)
    y_hat = net(X).argmax(dim=1)

    true_labels = get_fashion_mnist_labels(y.numpy())
    predict_labels = get_fashion_mnist_labels(y_hat.numpy())

    titles = [
        true + '\n' + pred for true, pred in zip(true_labels, predict_labels)
    ]
    show_fashion_mnist(X[10:19],
                       titles[10:19],
                       imagepath='../figures/3.9_mlp-scratch.jpg')
Ejemplo n.º 4
0
def main():
    # Step 1:获取数据集
    mnist_train = torchvision.datasets.FashionMNIST(
        root='./Datasets/',
        train=True,
        download=True,
        transform=transforms.ToTensor())
    mnist_test = torchvision.datasets.FashionMNIST(
        root='./Datasets/',
        train=False,
        download=True,
        transform=transforms.ToTensor())

    ### begin Unit Test ###
    # output FashionMNIST
    print(type(mnist_train))
    print(len(mnist_train), len(mnist_test))

    feature, label = mnist_train[0]
    print("feature.shape:", feature.shape, '\n' + "feature.dtype:",
          feature.dtype)  # Channel * Height * Width -> torch.Size([1, 28, 28])
    print("label:", label)  # 9 -> torch.float32
    ### end Unit Test ###

    X, y = [], []
    for i in range(10):
        X.append(mnist_train[i][0])  # features
        y.append(mnist_train[i][1])  # labels
    show_fashion_mnist(X, get_fashion_mnist_labels(y), '../figures/test.jpg')

    train_iter = data_iter(batch_size=256, data=mnist_train, shuffle=True)
    test_iter = data_iter(batch_size=256, data=mnist_test, shuffle=False)

    import time
    start = time.time()
    for X, y in train_iter:
        continue
    print("%.2f sec" % (time.time() - start))