def predict_ch3(model, t_i, n=6): # @save for X, y in t_i: break trues = d2l.get_fashion_mnist_labels(y) preds = d2l.get_fashion_mnist_labels(model(X).argmax(axis=1)) titles = [true + '\n' + pred for true, pred in zip(trues, preds)] d2l.show_images(X[0:n].reshape(n, 28, 28), 1, n, titles=titles[0:n]) plt.show()
y_hat = net(X) l = loss(y_hat, y).sum() l.backward() if trainer is None: d2l.sgd(params, lr, batch_size) else: trainer.step(batch_size) y = y.astype('float32') train_l_sum += l.asscalar() train_acc_sum += (y_hat.argmax(axis=1) == y).sum().asscalar() n += y.size test_acc = evaluate_accuracy(test_iter, net) print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f' % (epoch + 1, train_l_sum / n, train_acc_sum / n, test_acc)) train_ch3(net, train_iter, test_iter, cross_entropy, num_epochs, batch_size, [W, b], lr) for X, y in test_iter: break true_labels = d2l.get_fashion_mnist_labels(y.asnumpy()) pred_labels = d2l.get_fashion_mnist_labels(net(X).argmax(axis=1).asnumpy()) titles = [ truelabel + '\n' + predlabel for truelabel, predlabel in zip(true_labels, pred_labels) ] d2l.show_fashion_mnist(X[0:9], titles[0:9])
import sys sys.path.insert(0, '..') import d2l from mxnet.gluon import data as gdata import sys import time mnist_train = gdata.vision.FashionMNIST(train=True) mnist_test = gdata.vision.FashionMNIST(train=False) feature, label = mnist_train[0] X, y = mnist_train[0:9] d2l.show_fashion_mnist(X, d2l.get_fashion_mnist_labels(y)) batch_size = 256 transformer = gdata.vision.transforms.ToTensor() if sys.platform.startswith('win'): # 0 means no additional processes are needed to speed up the reading of # data num_workers = 0 else: num_workers = 4 train_iter = gdata.DataLoader(mnist_train.transform_first(transformer), batch_size, shuffle=True, num_workers=num_workers)