import sys sys.path.insert(0, '..') import d2l import torch import torch.nn as nn batch_size = 256 train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size) class Reshape(torch.nn.Module): def forward(self, x): return x.view(-1, 784) net = nn.Sequential(Reshape(), nn.Linear(784, 10)) def init_weights(m): if type(m) == nn.Linear: torch.nn.init.normal_(m.weight, std=0.01) net.apply(init_weights) loss = nn.CrossEntropyLoss() num_epochs = 10 lr = 0.1 d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, lr)
from mxnet import gluon, init, npx from mxnet.gluon import nn npx.set_np() batch_size = 256 train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size) net = nn.Sequential() net.add(nn.Dense(10)) net.initialize(init.Normal(sigma=0.01)) loss = gluon.loss.SoftmaxCrossEntropyLoss() trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.1}) num_epochs = 2 d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer) 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() predict_ch3(net, test_iter) net.save_parameters('softmax.params')
def relu(X): return np.maximum(X, 0) def net(X): X = X.reshape(-1, num_inputs) H = relu(np.dot(X, W1) + b1) return np.dot(H, W2) + b2 loss = gluon.loss.SoftmaxCrossEntropyLoss() num_epochs, lr = 10, 0.5 d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, lambda batch_size: d2l.sgd(params, lr, batch_size)) 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() predict_ch3(net, test_iter)
def cross_entropy(y_hat, y): return -np.log(y_hat[range(len(y_hat)), y]) accuracy = d2l.evaluate_accuracy(net, test_iter) print("accuracy of the untrained model : {}".format(accuracy)) num_epochs, lr = 2, 0.1 def updater(b_size): return d2l.sgd([W, b], lr, b_size) d2l.train_ch3(net, train_iter, test_iter, cross_entropy, num_epochs, updater) def predict_ch3(net, test_iter, n=6): # @save for X, y in test_iter: break trues = d2l.get_fashion_mnist_labels(y) preds = d2l.get_fashion_mnist_labels(net(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() predict_ch3(net, test_iter)
def net(X): X = X.reshape((-1, num_inputs)) H1 = (nd.dot(X, W1) + b1).relu() if autograd.is_training(): H1 = dropout(H1, drop_prob1) H2 = (nd.dot(H1, W2) + b2).relu() if autograd.is_training(): H2 = dropout(H2, drop_prob2) return nd.dot(H2, W3) + b3 num_epochs, lr, batch_size = 10, 0.5, 256 loss = gloss.SoftmaxCrossEntropyLoss() train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size) d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, params, lr) ############################ ## Concise Implementation ## ############################ net = nn.Sequential() net.add(nn.Dense(256, activation="relu"), nn.Dropout(drop_prob1), nn.Dense(256, activation="relu"), nn.Dropout(drop_prob2), nn.Dense(10)) net.initialize(init.Normal(sigma=0.01)) trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': lr}) d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, None, None, trainer)