<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from sklearn.datasets import make_regression
seed = np.random.seed(99)
n = 100000
X, y = make_regression(n_samples=n, 
                       n_features=4, 
                       n_informative=3, 
                       n_targets=1, 
                       bias=19.9, 
                       effective_rank=None, 
                       tail_strength=3.5, 
                       noise=9.9, 
                       shuffle=True, 
                       coef=False, 
                       random_state=seed)
from sklearn.preprocessing import StandardScaler

Xscaler = StandardScaler()
yscaler = StandardScaler()

X = Xscaler.fit_transform(X)
y = np.reshape(y, (-1,1))
y = yscaler.fit_transform(y)

#Chop off a little data for the validation.
val_size = int(n/10)
X_val = X[-val_size:]
y_val = y[-val_size:]
X = X[:-val_size]
y = y[:-val_size]
X = pd.DataFrame(X)
plt.plot(X.index, X.values)
X.head()
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=seed)
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

#Uses tpu. Much faster training. 
tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()

# instantiate a distribution strategy
tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)

# instantiating the model in the strategy scope creates the model on the TPU
with tpu_strategy.scope():
    model = Sequential()
    model.add(Dense(3, activation='relu'))
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='adam')
history = model.fit(X_train, y_train, epochs=10, batch_size=64, validation_data=(X_val, y_val))
'''
Note to future self:
The loss is calculated using Mean squared error which is the average 
squared difference between the estimated values and the actual value
'''
train_mse = model.evaluate(X_train, y_train, verbose=2)
test_mse = model.evaluate(X_test, y_test, verbose=2)
print('MSE Train: %.3f, MSE Test: %.3f' % (train_mse, test_mse))
plt.title('Loss / Mean Squared Error')
plt.plot(history.history['loss'], label='train')
plt.plot(history.history['val_loss'], label='test')
plt.legend()
plt.show()
model.summary()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11092436974789915, 'fraction_numerical': 0.014705882352941176, 'mean_word_length': 3.4338919925512106, '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': '138182', 'license': 'MIT', 'n_tokens_mistral': 841, 'n_tokens_neox': 779, 'n_words': 190, 'path': 'Mini_NN_for_regression.ipynb', 'repo_name': 'Coalbear/Neural-Networks', 'size': '2380'}