<jupyter_start><jupyter_text>MXNet (Using Gluon)<jupyter_code>import mxnet as mx
from mxnet import nd, autograd, gluon
import matplotlib.pyplot as plt

data_ctx = mx.cpu()
model_ctx = mx.gpu()<jupyter_output><empty_output><jupyter_text>Getting the dataset<jupyter_code>def transform(data, label):
    return data.astype('float32')/255.0, label.astype('float32')

mnist_train = gluon.data.vision.MNIST(train = True, transform = transform)
mnist_test = gluon.data.vision.MNIST(train = False, transform = transform)
image, label = mnist_train[0]
image.shape, label.shape
plt.imshow(image.reshape((28, 28)).asnumpy())
plt.title(label)
plt.show()<jupyter_output><empty_output><jupyter_text>Defining data loader<jupyter_code>batch_size = 64
training_data = gluon.data.DataLoader(mnist_train, batch_size = batch_size, shuffle = True)
testing_data = gluon.data.DataLoader(mnist_test, batch_size = batch_size, shuffle = False)<jupyter_output><empty_output><jupyter_text>Building the model<jupyter_code>model = gluon.nn.Dense(10)
model.initialize(mx.init.Normal(sigma = 1.0), model_ctx)<jupyter_output><empty_output><jupyter_text>Defining our objective function<jupyter_code>categoricalcrossentropy = gluon.loss.SoftmaxCrossEntropyLoss()<jupyter_output><empty_output><jupyter_text>Defining our parameter optimizer<jupyter_code>optimizer = gluon.Trainer(model.collect_params(), 'sgd', {'learning_rate': 0.1})<jupyter_output><empty_output><jupyter_text>Defining our accuracy function<jupyter_code>def accuracy(predictions, labels):
    return nd.mean(predictions.argmax(1) == labels)<jupyter_output><empty_output><jupyter_text>Training the  model<jupyter_code>epochs = 10
num_batches = 60000/batch_size
losses = []
accs = []

for epoch in range(epochs):
    cumulative_loss = 0
    cumulative_acc = 0
    for features, labels in training_data:
        features = features.as_in_context(model_ctx)
        labels = labels.as_in_context(model_ctx)
        with autograd.record():
            output = model(features)
            loss = categoricalcrossentropy(output, labels)
        loss.backward()
        optimizer.step(batch_size)
        cumulative_loss += loss.mean()
        cumulative_acc += accuracy(output, labels)
    cost = cumulative_loss.asscalar()/num_batches
    acc = cumulative_acc.asscalar()/num_batches
    print(f'Epoch: {epoch} Loss: {cost} Accuracy: {acc}')
    losses.append(cost)
    accs.append(acc)<jupyter_output>Epoch: 0 Loss: 1.9556088541666667 Accuracy: 0.6740333333333334
Epoch: 1 Loss: 0.8585227213541666 Accuracy: 0.8203833333333334
Epoch: 2 Loss: 0.707626953125 Accuracy: 0.8469166666666667
Epoch: 3 Loss: 0.6295673177083333 Accuracy: 0.8603333333333333
Epoch: 4 Loss: 0.5791789713541666 Accuracy: 0.8693166666666666
Epoch: 5 Loss: 0.5421420247395833 Accuracy: 0.8756
Epoch: 6 Loss: 0.5148118489583333 Accuracy: 0.8800833333333333
Epoch: 7 Loss: 0.4915766927083333 Accuracy: 0.8842333333333333
Epoch: 8 Loss: 0.47307737630208335 Accuracy: 0.8876
Epoch: 9 Loss: 0.4568736328125 Accuracy: 0.8904333333333333<jupyter_text>Lets see test accuracy<jupyter_code>acc = 0.0
batches = 0
for features, labels in testing_data:
    features = features.as_in_context(model_ctx).reshape((-1, 784))
    labels = labels.as_in_context(model_ctx)
    predictions = model(features)
    acc += accuracy(predictions, labels)
    batches += 1
print(f'Test Accuracy: {acc.asscalar()/batches}')
def plot(losses = losses, accs = accs, model = model, samples = 5):
    plt.plot(losses, 'r')
    plt.title('Loss during Training')
    plt.xlabel('Epoch')
    plt.ylabel('Loss')
    plt.show()
    
    plt.plot(accs, 'g')
    plt.title('Accuracy during Training')
    plt.xlabel('Epoch')
    plt.ylabel('Accuracy')
    plt.show()
    
    for i, batch in enumerate(mnist_test):
        features = batch[0].as_in_context(model_ctx).reshape((1, 784))
        labels = batch[1]
        predictions = model(features).argmax(1).asscalar()
        plt.imshow(features.reshape((28, 28)).asnumpy())
        plt.title(f'Predicted: {predictions}, Target: {labels}')
        plt.show()
        if i == samples:
            break
plot(samples = 10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12097162097162097, 'fraction_numerical': 0.0897065897065897, 'mean_word_length': 5.062682215743441, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '568252', 'license': 'MIT', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1491, 'n_words': 271, 'path': 'MXNet Gluon Implementations/03. Multiclass_Softmax_Classification_MXNet_(Using_Gluon).ipynb', 'repo_name': 'braindotai/MXNet-Gluon-Tutorials', 'size': '4158'}