<jupyter_start><jupyter_text>Intro to KerasWe will do our first neural networks.<jupyter_code>import matplotlib.pyplot as plt
from tensorflow import keras
from sklearn.datasets import load_digits
X, y  = load_digits(return_X_y=True)
X.shape  # .shape returns the dimensions of the array
y.shape
img = X[0].reshape(8,8)
img
X[0]
plt.imshow(img, cmap='gray')
y[0]<jupyter_output><empty_output><jupyter_text>**Goal:** Create a model that estimates the class to which one digit belongs. As estimator, we will use neural networks.- Separate in train and test data.- Determine a structure for the model.- Specify some performance criteria.- Create model on train data.- Validate / sanity check on the model using the test data.  Split into train and test data- Should be randomly split to avoid bias on the data.- Random seed should be specified.<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, random_state=123, train_size=0.8)
X_train.shape
X_test.shape<jupyter_output><empty_output><jupyter_text>Define the structure of the model<jupyter_code># First way of specifying the model
model = keras.models.Sequential()
model.add(keras.layers.Dense(4, input_shape=(64,),activation='relu')) # 1 layer, 4 neurons
model.add(keras.layers.Dense(10, activation='softmax'))
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 4)                 260       
_________________________________________________________________
dense_1 (Dense)              (None, 10)                50        
=================================================================
Total params: 310
Trainable params: 310
Non-trainable params: 0
_________________________________________________________________<jupyter_text>Specify performance criteria<jupyter_code>model.compile(loss="sparse_categorical_crossentropy",  # metric for classification in more than 2 classes
              metrics=["accuracy"]
             )<jupyter_output><empty_output><jupyter_text>Train our model in the training data<jupyter_code>history = model.fit(X_train, y_train, epochs=100, validation_split=0.1)
import pandas as pd
pd.DataFrame(history.history)[['loss', 'val_loss']].plot()
import numpy as np
pd.DataFrame(history.history)[['loss', 'val_loss']].apply(np.log).plot()<jupyter_output><empty_output><jupyter_text>Validate the model we trained<jupyter_code>y_pred_proba = model.predict(X_test)
y_pred = np.argmax(y_pred_proba, axis=1)
y_pred
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, y_pred)
idxs = (y_test == 1) & (y_pred == 7)
idxs[6]
plt.imshow(X_test[6].reshape(8,8), cmap='gray')
model.fit()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13941480206540446, 'fraction_numerical': 0.015834767641996556, 'mean_word_length': 5.5895691609977325, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '650330', 'license': 'MIT', 'n_tokens_mistral': 907, 'n_tokens_neox': 858, 'n_words': 228, 'path': 'Keras Overview.ipynb', 'repo_name': 'jpmaldonado/np-tensorflow', 'size': '2905'}