<jupyter_start><jupyter_text>Amazon Sentiment Data<jupyter_code>%load_ext autoreload
%autoreload 2
import lxmls.readers.sentiment_reader as srs
from lxmls.deep_learning.utils import AmazonData
corpus = srs.SentimentCorpus("books")
data = AmazonData(corpus=corpus)<jupyter_output><empty_output><jupyter_text>Implement Pytorch Forward pass As the final exercise today implement the log `forward()` method in     lxmls/deep_learning/pytorch_models/mlp.pyUse the previous exercise as reference. After you have completed this you can run both systems for comparison.<jupyter_code># Model
geometry = [corpus.nr_features, 20, 2]
activation_functions = ['sigmoid', 'softmax']

# Optimization
learning_rate = 0.05
num_epochs = 10
batch_size = 30
import numpy as np
from lxmls.deep_learning.pytorch_models.mlp import PytorchMLP
model = PytorchMLP(
    geometry=geometry,
    activation_functions=activation_functions,
    learning_rate=learning_rate
)
# Get batch iterators for train and test
train_batches = data.batches('train', batch_size=batch_size)
test_set = data.batches('test', batch_size=None)[0]

# Epoch loop
for epoch in range(num_epochs):

    # Batch loop
    for batch in train_batches:
        model.update(input=batch['input'], output=batch['output'])

    # Prediction for this epoch
    hat_y = model.predict(input=test_set['input'])

    # Evaluation
    accuracy = 100*np.mean(hat_y == test_set['output'])

    # Inform user
    print("Epoch %d: accuracy %2.2f %%" % (epoch+1, accuracy))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09947643979057591, 'fraction_numerical': 0.011780104712041885, 'mean_word_length': 5.647826086956521, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '149', 'license': 'MIT', 'n_tokens_mistral': 523, 'n_tokens_neox': 505, 'n_words': 123, 'path': 'labs/notebooks/non_linear_classifiers/exercise_4.ipynb', 'repo_name': 'mpc97/lxmls', 'size': '1528'}