<jupyter_start><jupyter_text>In this example, we will train a neural network using sinnn on the Bike Sharing Dataset<jupyter_code>!pip install sinnn
import numpy as np
import urllib.request
from numpy import genfromtxt
from sinnn.Model import Model
from sinnn.Layers import Dense, ReLU
from sinnn.Losses import MSE
from sinnn.utils import save_model, load_model<jupyter_output><empty_output><jupyter_text>Downloading the dataset<jupyter_code>urllib.request.urlretrieve("https://archive.ics.uci.edu/ml/machine-learning-databases/00275/Bike-Sharing-Dataset.zip", "Bike-Sharing-Dataset")<jupyter_output><empty_output><jupyter_text>Expanding the dataset<jupyter_code>!unzip Bike-Sharing-Dataset<jupyter_output>Archive:  Bike-Sharing-Dataset
replace Readme.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: a
error:  invalid response [a]
replace Readme.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
  inflating: Readme.txt              
  inflating: day.csv                 
  inflating: hour.csv<jupyter_text>After viewing the csv, we can see that the first row and coloumn (row and colums headings) needs to be removed from the data. We also remove the second column (date) as the year month and day are already available in later coloumns.<jupyter_code>bike_data = genfromtxt('hour.csv', delimiter=',')[1:, 2:]<jupyter_output><empty_output><jupyter_text>The data is shuffled before partitioning into train and test<jupyter_code>np.random.shuffle(bike_data)
x_train, y_train, x_test, y_test = bike_data[:9600, :-1], bike_data[:9600, -1:], bike_data[9600:, :-1], bike_data[9600:, -1:]<jupyter_output><empty_output><jupyter_text>A model object in intantiated with MSE loss_function. We add a neural network with 100 neurons in the first hidden layer, followed by ReLU, followed by 200 neurons in second hidden layer, again followed by ReLu. The output layer contains just 1 neuron and its activation will predict labels. The network is trained over 10 epochs with a batch size of 32.<jupyter_code>model = Model(loss_function=MSE())
model.add(Dense(100), ReLU(), Dense(200), ReLU(), Dense(1))
model.train(x_train, y_train, 32, 10, (x_test, y_test))<jupyter_output>{'Epochs': 0, 'train_loss': 69387.9892051373, 'validation_loss': 71735.89503205878}
{'Epochs': 1, 'train_loss': 0.14338706155367886, 'validation_loss': 0.14458997274158625}
{'Epochs': 2, 'train_loss': 0.060812382974511504, 'validation_loss': 0.06160691837671004}
{'Epochs': 3, 'train_loss': 0.031050337708793466, 'validation_loss': 0.03156223057181988}
{'Epochs': 4, 'train_loss': 0.01782567683906422, 'validation_loss': 0.018142769425156312}
{'Epochs': 5, 'train_loss': 0.010936234228141372, 'validation_loss': 0.011110760226808956}
{'Epochs': 6, 'train_loss': 0.007236848933563557, 'validation_loss': 0.007330854723268884}
{'Epochs': 7, 'train_loss': 0.005126745418580676, 'validation_loss': 0.005176672322206588}
{'Epochs': 8, 'train_loss': 0.0038213336319642285, 'validation_loss': 0.0038477783243219613}
{'Epochs': 9, 'train_loss': 0.0029834369676421855, 'validation_loss': 0.00299840727107661}
{'Epochs': 10, 'train_loss': 0.00243094787238311, 'validation_loss': 0.002440080854853425}<jupyter_text>After training the model we save the model immediately to prevent loss of data.<jupyter_code>save_model(model)<jupyter_output><empty_output><jupyter_text>Model is loaded and all metrics plotted (For MSE only loss in supported.)<jupyter_code>import matplotlib.pyplot as plt

model = load_model()
for metric in model.train_log:
    for key in model.train_log[metric]:
        plt.plot(model.train_log[metric][key], label=key)
    plt.xlabel('Epochs')
    plt.ylabel(metric)
    plt.legend()
    plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12177922430160022, 'fraction_numerical': 0.12693246541903988, 'mean_word_length': 6.69937369519833, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '459540', 'license': 'MIT', 'n_tokens_mistral': 1659, 'n_tokens_neox': 1348, 'n_words': 327, 'path': 'examples/regression_using_sinnn.ipynb', 'repo_name': 'sohaibimran7/sinnn', 'size': '3687'}