<jupyter_start><jupyter_text><jupyter_code>!pip install mxnet
!pip install d2l
import d2l
from mxnet import autograd, nd, gluon, init
from mxnet.gluon import nn, loss as gloss
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
# Define the model and init its parameters
net = nn.Sequential()
net.add(nn.Dense(10))
net.initialize(init.Normal(sigma=0.01))

# Unify the softmax function with the cross-entropy loss, for stability/efficiency purposes
loss = gluon.loss.SoftmaxCrossEntropyLoss()

# Optimization algorithm
trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.1})

# Accuracy evaluation
def accuracy(y_hat, y):
  """ from d2l """
  return (y_hat.argmax(axis=1) == y.astype('float32')).sum().asscalar()

def evaluate_accuracy(net, data_iter):
  """ from d2l """
  metric = d2l.Accumulator(2) # num_corrected_example, num_examples
  for X, y in data_iter:
    y = y.astype('float32')
    metric.add(accuracy(net(X), y), y.size)
  return metric[0]/metric[1]

# Training
def train_epoch_ch3(net, train_iter, loss, updater):
  """ from d2l """
  metric = d2l.Accumulator(3)  # train_loss_sum, train_acc_sum, num_examples
  if isinstance(updater, gluon.Trainer):
    updater = updater.step
  for X, y in train_iter:
    # Compute gradients and update parameters
    with autograd.record():
      y_hat = net(X)
      l = loss(y_hat, y)
    l.backward()
    updater(X.shape[0])
    metric.add(l.sum().asscalar(), accuracy(y_hat, y), y.size)
  # Return training loss and training accuracy
  return metric[0]/metric[2], metric[1]/metric[2], 

def train_ch3(net, train_iter, test_iter, loss, num_epochs, updater):
  """ from d2l """
  trains, test_accs = [], []
  animator = d2l.Animator(xlabel='epoch', 
                          xlim=[1, num_epochs], 
                          ylim=[0.3, 0.9],
                          legend=['train loss', 'train acc', 'test acc'])
  for epoch in range(num_epochs):
    train_metrics = train_epoch_ch3(net, train_iter, loss, updater)
    test_acc = evaluate_accuracy(net, test_iter)
    animator.add(epoch+1, train_metrics+(test_acc,))

num_epochs = 10
train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)
# Prediction
def predict_ch3(net, test_iter, n=6):
  for X, y in test_iter:
    break
  trues = d2l.get_fashion_mnist_labels(y.asnumpy())
  preds = d2l.get_fashion_mnist_labels(net(X).argmax(axis=1).asnumpy())
  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])

predict_ch3(net, test_iter)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13831417624521072, 'fraction_numerical': 0.022988505747126436, 'mean_word_length': 4.296146044624747, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '431932', 'license': 'MIT', 'n_tokens_mistral': 1007, 'n_tokens_neox': 985, 'n_words': 213, 'path': 'Linear_Regression/softmax-regression.ipynb', 'repo_name': 'mattia-sandrini/deep-learning-book', 'size': '2610'}