Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
YStrano
GitHub Repository: YStrano/DataScience_GA
Path: blob/master/april_18/lessons/lesson-19-flex/19-Neural Networks-Solutions.ipynb
1904 views
Kernel: Python [Root]

Neural Networks with Keras

from __future__ import print_function import random import numpy as np import pandas as pd from math import sin # pip install keras from keras.models import Sequential from keras.layers.core import Dense, Activation from keras.optimizers import SGD # Stochastic Gradient Descent from sklearn.metrics import accuracy_score, confusion_matrix, mean_squared_error import sklearn.cross_validation as cv import matplotlib.pyplot as plt %matplotlib inline plt.rcParams["figure.figsize"] = (8, 8)

Neural Network Regression

Polynomial Regression

Let's train a neural network on a few different shapes. First we start with a polynomial (a cubic).

# Create some data def f(x): return x ** 3 - 5 * x + 12 + random.random() X = np.linspace(-1, 1, 1000).reshape(-1, 1) y = np.array(list(map(f, X))) print(X.shape, y.shape)
(1000, 1) (1000, 1)
# Define a Feed Forward NN model = Sequential() model.add(Dense(input_dim=1, output_dim=5)) model.add(Activation('tanh')) model.add(Dense(input_dim=5, output_dim=1)) model.add(Activation('linear')) # lr: learning rate model.compile(loss='mse', optimizer=SGD(lr=0.01))
# Train the model print('Training...') loss = model.fit(X, y, nb_epoch=500, validation_split=0.1, batch_size=128, verbose=False) loss.history['loss'][-1] print("Done")
Training... Done
# Plot the predictions predictions = model.predict(X) plt.scatter(X, y) plt.plot(X, predictions, color='r') plt.show() print("MSE", mean_squared_error(predictions, y))
Image in a Jupyter notebook
MSE 0.0847152073854

Sine Regression

# Sine data X = np.linspace(0, 2 * np.pi, 500).reshape(-1,1) y = np.sin(X) print(X.shape, y.shape)
(500, 1) (500, 1)
# Create the model model = Sequential() model.add(Dense(input_dim=1, output_dim=5)) model.add(Activation('tanh')) model.add(Dense(input_dim=5, output_dim=1)) model.add(Activation('linear')) # lr: learning rate model.compile(loss='mse', optimizer=SGD(lr=0.1))

Train the Model

print('Training..') loss = model.fit(X, y, nb_epoch=150, validation_split=0.1, batch_size=128, verbose=False) print(loss.history['loss'][-1]) print('Complete')
Training.. 0.0141807238509 Complete
# Plot the predictions predictions = model.predict(X) plt.scatter(X, y) plt.plot(X, predictions, color='r') plt.show() print("MSE", mean_squared_error(predictions, y))
Image in a Jupyter notebook
MSE 0.0756651529904
# Plot the error over time plt.scatter(range(len(loss.history['loss'])), loss.history['loss']) # plt.scatter(range(len(loss.history['val_loss'])), loss.history['val_loss'], color='red') plt.xlabel('Epoch') plt.ylabel('MSE') plt.title('MSE by Epoch')
<matplotlib.text.Text at 0x7fa6843cb898>
Image in a Jupyter notebook

Train longer

If we train for more epochs, we can get a better regression.

X = np.linspace(0, 2 * np.pi, 1000).reshape(-1,1) y = np.sin(X) print(X.shape, y.shape) model = Sequential() model.add(Dense(input_dim=1, output_dim=5)) model.add(Activation('tanh')) model.add(Dense(input_dim=5, output_dim=1)) model.add(Activation('linear')) # lr: learning rate model.compile(loss='mse', optimizer=SGD(lr=0.05)) print('Training..') loss = model.fit(X, y, nb_epoch=15000, validation_split=0.1, batch_size=128, verbose=False) print(loss.history['loss'][-1]) print('Complete') # Plot predictions = model.predict(X) plt.scatter(X, y) plt.plot(X, predictions, color='r') plt.show() print("MSE", mean_squared_error(predictions, y))
(1000, 1) (1000, 1) Training.. 0.00057795047718 Complete
Image in a Jupyter notebook
MSE 0.00133023607819

We can take a closer look at the error per training epoch.

# Plot the error over time plt.scatter(range(len(loss.history['loss'])), loss.history['loss']) plt.xlabel('Epoch') plt.ylabel('MSE') plt.title('MSE by Epoch')
<matplotlib.text.Text at 0x7fa684022358>
Image in a Jupyter notebook
### Exercise: Perform regression the following data Hints: * Try adding a hidden layer * Try lowering the learning rate and using more epochs
def f(x): return x ** 2 * np.sin(x**2) # Sine data X = np.linspace(2, np.pi, 1000).reshape(-1,1) y = np.array(list(map(f, X))) print(X.shape, y.shape)
(1000, 1) (1000, 1)
## Solution model = Sequential() model.add(Dense(input_dim=1, output_dim=5)) model.add(Activation('tanh')) model.add(Dense(input_dim=5, output_dim=5)) model.add(Activation('tanh')) model.add(Dense(input_dim=5, output_dim=1)) model.add(Activation('linear')) # lr: learning rate model.compile(loss='mse', optimizer=SGD(lr=0.005)) print('Training..') loss = model.fit(X, y, nb_epoch=10000, validation_split=0.1, batch_size=128, verbose=False) print(loss.history['loss'][-1]) print('Complete') # Plot predictions = model.predict(X) plt.scatter(X, y) plt.plot(X, predictions, color='r') plt.show() print("MSE", mean_squared_error(predictions, y))
Training.. 0.0916458962692 Complete
Image in a Jupyter notebook
MSE 1.28391965568

Classification

We'll start with the Iris dataset (of course).

import sklearn.datasets as datasets iris = datasets.load_iris() X = iris.data y = iris.target # Break each output into indicator cols y_cat = pd.get_dummies(y).values print(X.shape, y_cat.shape)
(150, 4) (150, 3)
# Define a model model = Sequential() # input_dim = number of neurons in previous layer. # output_dim = number of neurons in current layer. # First layer - input_dim=k features. model.add(Dense(input_dim=4, output_dim=4)) model.add(Activation("tanh")) # Output layer - output_dim=# of output per point (in y). # Use 'softmax' for class probability. 'linear' for regression model.add(Dense(input_dim=4, output_dim=3)) model.add(Activation("softmax")) # Uses Mean Squared Error and Stochastic Gradient Descent model.compile(loss='mse', optimizer=SGD(lr=0.01))
# Train the model print('Training...') loss = model.fit(X, y_cat, validation_split=0.1, nb_epoch=5000, batch_size=16, verbose=False) print(loss.history['loss'][-1]) # displays MSE at last iteration print("Training complete")
Training... 0.102203034196 Training complete
# Model evaluation pred_y = model.predict(X, verbose=False) preds = model.predict_classes(X, verbose=False) print('ACCURACY: ', accuracy_score(y, preds)) print('CONFUSION MATRIX:\n', confusion_matrix(y, preds))
ACCURACY: 0.666666666667 CONFUSION MATRIX: [[50 0 0] [ 0 50 0] [ 0 50 0]]
# Plot the error over time plt.scatter(range(len(loss.history['loss'])), loss.history['loss']) plt.xlabel('Epoch') plt.ylabel('MSE') plt.title('MSE by Epoch')
<matplotlib.text.Text at 0x7fa681fbc0b8>
Image in a Jupyter notebook

Abalone data set

columns = ["Sex", "Length", "Diameter", "Height", "Whole Weight", "Shucked weight", "Viscera weight", "Shell weight", "Rings" ] df = pd.read_csv("abalone.data", names=columns) df.head()
import seaborn as sns sns.pairplot(data=df, vars=columns[1:], hue="Sex") plt.show()
Image in a Jupyter notebook
d = {'M': 0, 'F': 1, 'I': 2} df["Sex"] = df["Sex"].apply(lambda x: d[x])
X = np.array(df[columns[1:]]) y = np.array(df["Sex"]) y_cat = pd.get_dummies(y).values print(X.shape, y_cat.shape)
(4177, 8) (4177, 3)
# Define a model model = Sequential() # input_dim = number of neurons in previous layer. # output_dim = number of neurons in current layer. # First layer - input_dim=k features. model.add(Dense(input_dim=8, output_dim=6)) model.add(Activation("tanh")) model.add(Dense(input_dim=6, output_dim=6)) model.add(Activation("tanh")) # Output layer - output_dim=# of output per point (in y). # Use 'softmax' for class probability. 'linear' for regression model.add(Dense(input_dim=6, output_dim=3)) model.add(Activation("softmax")) # Uses Mean Squared Error and Stochastic Gradient Descent model.compile(loss='mse', optimizer=SGD(lr=0.1))
# Train the model print('Training...') loss = model.fit(X, y_cat, validation_split=0.1, nb_epoch=1000, batch_size=16, verbose=False) print(loss.history['loss'][-1]) # displays MSE at last iteration print("Training complete")
Training... 0.173390359594 Training complete
# Model evaluation pred_y = model.predict(X, verbose=False) preds = model.predict_classes(X, verbose=False) print('ACCURACY: ', accuracy_score(y, preds)) print('CONFUSION MATRIX:\n', confusion_matrix(y, preds)) # Plot the error over time plt.scatter(range(len(loss.history['loss'])), loss.history['loss']) plt.xlabel('Epoch') plt.ylabel('MSE') plt.title('MSE by Epoch')
ACCURACY: 0.550873832894 CONFUSION MATRIX: [[1260 3 265] [1157 1 149] [ 302 0 1040]]
<matplotlib.text.Text at 0x7fa67fea9940>
Image in a Jupyter notebook

Exercise

Classify the following data (source). You'll need to translate the classes into integers and make dummies. Design a neural network to classify the data and evaluate the results.

names = "fLength fWidth fSize fConc fConc1 fAsym fM3Long fM3Trans fAlpha fDist class".split() df = pd.read_csv("magic04.data", names=names) df.head()
d = {'g': 0, 'h': 1} df["class"] = df["class"].apply(lambda x: d[x])
X = np.array(df[df.columns[:-1]]) y = np.array(df["class"]) y_cat = pd.get_dummies(y).values print(X.shape, y_cat.shape)
(19020, 10) (19020, 2)
# Define a model model = Sequential() # input_dim = number of neurons in previous layer. # output_dim = number of neurons in current layer. # First layer - input_dim=k features. model.add(Dense(input_dim=10, output_dim=6)) model.add(Activation("tanh")) model.add(Dense(input_dim=6, output_dim=6)) model.add(Activation("tanh")) # Output layer - output_dim=# of output per point (in y). # Use 'softmax' for class probability. 'linear' for regression model.add(Dense(input_dim=6, output_dim=2)) model.add(Activation("softmax")) # Uses Mean Squared Error and Stochastic Gradient Descent model.compile(loss='mse', optimizer=SGD(lr=0.01))
# Train the model print('Training...') loss = model.fit(X, y_cat, validation_split=0.2, nb_epoch=1000, batch_size=256, verbose=False) print(loss.history['loss'][-1]) # displays MSE at last iteration print("Training complete")
Training... 0.108597032569 Training complete
# Model evaluation pred_y = model.predict(X, verbose=False) preds = model.predict_classes(X, verbose=False) print('ACCURACY: ', accuracy_score(y, preds)) print('CONFUSION MATRIX:\n', confusion_matrix(y, preds)) # Plot the error over time plt.scatter(range(len(loss.history['loss'])), loss.history['loss']) plt.xlabel('Epoch') plt.ylabel('MSE') plt.title('MSE by Epoch')
ACCURACY: 0.799158780231 CONFUSION MATRIX: [[11670 662] [ 3158 3530]]
<matplotlib.text.Text at 0x7fa68421df98>
Image in a Jupyter notebook