# Basic%20NN
course: Academy — 54-Next-Gen-AI-GenAI-Agents-Future-Trends
module: Academy/54-Next-Gen-AI-GenAI-Agents-Future-Trends
type: notebook
source_url: https://personal-learn.armco.dev/files/Academy/54-Next-Gen-AI-GenAI-Agents-Future-Trends/folders/Session-3_Folder/Basic%20NN.ipynb
---
[cell 2 markdown]
# NN with Keras
[cell 3 markdown]
### Import libraries
[cell 4 markdown]
This notebook was run and tested on Keras version 3.3.3 and Tensorflow 2.16.1
[cell 5 code]
#!pip install tensorflow
# Check keras and tensorflow versions
import tensorflow as tf
from tensorflow import keras
print("Keras version: ", keras.__version__)
print("Tensorflow version: ", tf.__version__)
[cell 6 code]
## Import libraries
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
# import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras import Input
[cell 7 markdown]
### Load data
[cell 8 code]
# The diabetes dataset
diabetes = load_diabetes()
[cell 9 code]
print(diabetes.DESCR)
[cell 10 markdown]
### Prepare input data
[cell 11 code]
# input
X = diabetes.data
# print the type of X to check that it is a numpy array
print("X is a ", type(X))
# Print shape to check rows and columns
print("X has {} rows and {} columns".format(X.shape[0], X.shape[1]))
# Save number of columns as n_cols
n_cols = X.shape[1]
# output
y = diabetes.target
print("First 10 values in y: ", y[:10])
[cell 12 code]
# How many observations are in y?
len(y)
[cell 13 code]
y.shape
[cell 14 markdown]
### Split the data (training/test)
[cell 15 code]
X_train, X_test, y_train, y_test = train_test_split(
X, y,
test_size = 0.3,
random_state = 65)
[cell 16 markdown]
### Create the model's architecture
[cell 17 code]
# Set up the model architecture
model = Sequential()
[cell 18 code]
model.add(Input(shape=(n_cols,)))
# Add the first hidden layer
model.add(Dense(15, activation = 'relu'))
# Add the second hidden layer
model.add(Dense(5, activation = 'relu'))
# Add the output layer
model.add(Dense(1, activation = 'linear'))
[cell 19 code]
print(model.summary())
[cell 20 markdown]
### Compile the model
[cell 21 code]
# Compile the model
model.compile(
optimizer = 'adam',
loss = 'mse',
metrics = ['mse'])
[cell 22 markdown]
### Fit the training data
[cell 23 code]
# shuffle training data
from sklearn.utils import shuffle
X_train2, y_train2 = shuffle(X_train, y_train, random_state=42)
[cell 24 code]
%%time
# Fit the model
history = model.fit(
X_train2,
y_train2,
validation_split = 0.25,
batch_size = 10,
epochs = 300,
verbose = 1)
[cell 25 markdown]
### Create predictions
[cell 26 code]
predictions = model.predict(X_test)
[cell 27 code]
predictions
[cell 28 markdown]
### Evaluate the model
[cell 29 code]
# Calculate test MSE
score = model.evaluate(X_test, y_test)
print('\nTest loss: %.6f' % score[0])
[cell 30 code]
score
[cell 31 code]
# Find RMSE
score[0] ** (1/2)
[cell 32 code]
y.mean()
[cell 33 code]
y.std()
[cell 34 code]
y.min()
[cell 35 code]
y.max()
[cell 36 markdown]
### Visualization of cost
[cell 37 code]
fig, ax = plt.subplots(1, 2, figsize = (18, 6))
fig.subplots_adjust(left = 0.02, right = 0.98, wspace = 0.2)
plt.rcParams.update({'font.size': 14})
# Plot training & validation accuracy values
ax[0].plot(history.history['mse'], label = 'Training')
ax[0].plot(history.history['val_mse'], label = 'Validation')
ax[0].set_title('Model MSE')
ax[0].set_ylabel('MSE')
ax[0].set_xlabel('Epoch')
ax[0].legend()
# Plot training & validation loss values
ax[1].plot(history.history['loss'], label = 'Training')
ax[1].plot(history.history['val_loss'], label = 'Validation')
ax[1].set_title('Model loss')
ax[1].set_ylabel('Loss')
ax[1].set_xlabel('Epoch')
ax[1].legend()
plt.show()
[cell 38 markdown]
### Visualization of residuals
[cell 39 code]
# Calculate R2 and adjusted R2
r2 = r2_score(y_test, predictions)
n, p = X.shape # sample size, number of explanatory variables
adjusted_r2 = 1 - (1 - r2) * ((n - 1) / (n - p - 1))
[cell 40 code]
difference = predictions.flatten() - y_test
d_mean = difference.mean()
d_std = difference.std()
n_pred = len(predictions)
plt.figure(figsize = (15, 6))
plt.scatter(range(n_pred), difference, s = 15)
plt.hlines(d_mean + d_std, 0, n_pred, 'r', lw = 1, label = '$\pm \: std$')
plt.hlines(d_mean, 0, n_pred, label = '$mean$')
plt.hlines(d_mean - d_std, 0, n_pred, 'r', lw = 1)
plt.xlabel('Prediction')
plt.ylabel('$\hat y - y$')
plt.legend()
plt.text(0, difference.max() * 0.95,
'$R^2: %.2f, \: Adjusted \: R^2: %.2f$' % (r2, adjusted_r2),
fontsize = 12)
plt.show()