<jupyter_start><jupyter_text>Implementing a Multilayer Perceptron from Scratch<jupyter_code>%matplotlib inline
import d2l
from mxnet import nd
from mxnet.gluon import loss as gloss<jupyter_output><empty_output><jupyter_text>Load the Fashion-MNIST data set<jupyter_code>batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)<jupyter_output><empty_output><jupyter_text>Initialize Model Parameters<jupyter_code>num_inputs, num_outputs, num_hiddens = 784, 10, 256

W1 = nd.random.normal(scale=0.01, shape=(num_inputs, num_hiddens))
b1 = nd.zeros(num_hiddens)
W2 = nd.random.normal(scale=0.01, shape=(num_hiddens, num_outputs))
b2 = nd.zeros(num_outputs)
params = [W1, b1, W2, b2]

for param in params:
    param.attach_grad()<jupyter_output><empty_output><jupyter_text>Activation Function<jupyter_code>def relu(X):
    return nd.maximum(X, 0)<jupyter_output><empty_output><jupyter_text>The model<jupyter_code>def net(X):
    X = X.reshape((-1, num_inputs))
    H = relu(nd.dot(X, W1) + b1)
    return nd.dot(H, W2) + b2<jupyter_output><empty_output><jupyter_text>The Loss Function<jupyter_code>loss = gloss.SoftmaxCrossEntropyLoss()<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>num_epochs, lr = 10, 0.5
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size,
             params, lr)<jupyter_output>epoch 1, loss 0.7868, train acc 0.708, test acc 0.822
epoch 2, loss 0.4831, train acc 0.820, test acc 0.853
epoch 3, loss 0.4295, train acc 0.842, test acc 0.859
epoch 4, loss 0.3930, train acc 0.856, test acc 0.865
epoch 5, loss 0.3663, train acc 0.866, test acc 0.869
epoch 6, loss 0.3520, train acc 0.870, test acc 0.871
epoch 7, loss 0.3368, train acc 0.876, test acc 0.870
epoch 8, loss 0.3236, train acc 0.880, test acc 0.878
epoch 9, loss 0.3129, train acc 0.886, test acc 0.883
epoch 10, loss 0.3067, train acc 0.886, test acc 0.882<jupyter_text>Evaluation<jupyter_code>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])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12010558732952045, 'fraction_numerical': 0.08271007479102507, 'mean_word_length': 5.996923076923077, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '252570', 'license': 'Apache-2.0', 'n_tokens_mistral': 1052, 'n_tokens_neox': 939, 'n_words': 208, 'path': 'slides/2_7/mlp-scratch.ipynb', 'repo_name': 'yang-chenyu104/berkeley-stat-157', 'size': '2273'}