<jupyter_start><jupyter_text>Initializing Model Parameters<jupyter_code># implement an MLP with one hidden layer and 256 hidden units
num_inputs, num_outputs, num_hiddens = 784, 10, 256

W1 = np.random.normal(scale=0.01, size=(num_inputs, num_hiddens))
b1 = np.zeros(num_hiddens)
W2 = np.random.normal(scale=0.01, size=(num_hiddens, num_outputs))
b2 = np.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># implement the ReLU activation
def relu(X):
    return np.maximum(X, 0)<jupyter_output><empty_output><jupyter_text>Model<jupyter_code># implement the model
def net(X):
    X = X.reshape((-1, num_inputs))
    H = relu(np.dot(X, W1) + b1)
    return np.dot(H, W2) + b2<jupyter_output><empty_output><jupyter_text>Loss Function<jupyter_code># leverage the integrated function from high-level APIs for calculating the softmax and cross-entropy loss
loss = gluon.loss.SoftmaxCrossEntropyLoss()<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>!pip uninstall matplotlib
!pip install --upgrade matplotlib
# MPLs training loop
num_epochs, lr = 10, 0.1
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs,
              lambda batch_size: d2l.sgd(params, lr, batch_size))
# apply it on some test data to evaluate the learned model
d2l.predict_ch3(net, test_iter)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11134453781512606, 'fraction_numerical': 0.028011204481792718, 'mean_word_length': 6.328205128205128, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '255457', 'license': 'Unlicense', 'n_tokens_mistral': 569, 'n_tokens_neox': 540, 'n_words': 121, 'path': 'Week 9/4.2. Implementation of Multilayer Perceptrons from Scratch.ipynb', 'repo_name': 'Hanif-2610/Machine-Learning-Homework-Project', 'size': '1428'}