+ + +
+
+ + + +
+ + +
+

Week 45, Recurrent Neural Networks

+

Morten Hjorth-Jensen, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

+

Date: November 6-10

+
+

Plan for week 45

+

Material for the active learning sessions on Tuesday and Wednesday.

+ +

Material for the lecture on Thursday November 9, 2023.

+ +
+
+

Material for the lab sessions, additional ways to present classification results and other practicalities

+
+
+

Searching for Optimal Regularization Parameters \(\lambda\)

+

In project 1, when using Ridge and Lasso regression, we end up +searching for the optimal parameter \(\lambda\) which minimizes our +selected scores (MSE or \(R2\) values for example). The brute force +approach, as discussed in the code here for Ridge regression, consists +in evaluating the MSE as function of different \(\lambda\) values. +Based on these calculations, one tries then to determine the value of the hyperparameter \(\lambda\) +which results in optimal scores (for example the smallest MSE or an \(R2=1\)).

+
+
+
%matplotlib inline
+
+import numpy as np
+import pandas as pd
+import matplotlib.pyplot as plt
+from sklearn.model_selection import train_test_split
+from sklearn import linear_model
+
+def MSE(y_data,y_model):
+    n = np.size(y_model)
+    return np.sum((y_data-y_model)**2)/n
+# A seed just to ensure that the random numbers are the same for every run.
+# Useful for eventual debugging.
+np.random.seed(2021)
+
+n = 100
+x = np.random.rand(n)
+y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.randn(n)
+
+Maxpolydegree = 5
+X = np.zeros((n,Maxpolydegree-1))
+
+for degree in range(1,Maxpolydegree): #No intercept column
+    X[:,degree-1] = x**(degree)
+
+# We split the data in test and training data
+X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
+
+# Decide which values of lambda to use
+nlambdas = 500
+MSERidgePredict = np.zeros(nlambdas)
+lambdas = np.logspace(-4, 2, nlambdas)
+for i in range(nlambdas):
+    lmb = lambdas[i]
+    RegRidge = linear_model.Ridge(lmb)
+    RegRidge.fit(X_train,y_train)
+    ypredictRidge = RegRidge.predict(X_test)
+    MSERidgePredict[i] = MSE(y_test,ypredictRidge)
+
+# Now plot the results
+plt.figure()
+plt.plot(np.log10(lambdas), MSERidgePredict, 'g--', label = 'MSE SL Ridge Test')
+plt.xlabel('log10(lambda)')
+plt.ylabel('MSE')
+plt.legend()
+plt.show()
+
+
+
+
+_images/week45_5_0.png +
+
+

Here we have performed a rather data greedy calculation as function of the regularization parameter \(\lambda\). There is no resampling here. The latter can easily be added by employing the function RidgeCV instead of just calling the Ridge function. For RidgeCV we need to pass the array of \(\lambda\) values. +By inspecting the figure we can in turn determine which is the optimal regularization parameter. +This becomes however less functional in the long run.

+
+ + +
+

Wisconsin Cancer Data

+

We show here how we can use a simple regression case on the breast +cancer data using Logistic regression as our algorithm for +classification.

+
+
+
import matplotlib.pyplot as plt
+import numpy as np
+from sklearn.model_selection import  train_test_split 
+from sklearn.datasets import load_breast_cancer
+from sklearn.linear_model import LogisticRegression
+
+# Load the data
+cancer = load_breast_cancer()
+
+X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
+print(X_train.shape)
+print(X_test.shape)
+# Logistic Regression
+logreg = LogisticRegression(solver='lbfgs')
+logreg.fit(X_train, y_train)
+print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test)))
+
+
+
+
+
(426, 30)
+(143, 30)
+Test set accuracy with Logistic Regression: 0.94
+
+
+
/Users/mhjensen/miniforge3/envs/myenv/lib/python3.9/site-packages/sklearn/linear_model/_logistic.py:814: ConvergenceWarning: lbfgs failed to converge (status=1):
+STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
+
+Increase the number of iterations (max_iter) or scale the data as shown in:
+    https://scikit-learn.org/stable/modules/preprocessing.html
+Please also refer to the documentation for alternative solver options:
+    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
+  n_iter_i = _check_optimize_result(
+
+
+
+
+
+
+

Using the correlation matrix

+

In addition to the above scores, we could also study the covariance (and the correlation matrix). +We use Pandas to compute the correlation matrix.

+
+
+
import matplotlib.pyplot as plt
+import numpy as np
+from sklearn.model_selection import  train_test_split 
+from sklearn.datasets import load_breast_cancer
+from sklearn.linear_model import LogisticRegression
+cancer = load_breast_cancer()
+import pandas as pd
+# Making a data frame
+cancerpd = pd.DataFrame(cancer.data, columns=cancer.feature_names)
+
+fig, axes = plt.subplots(15,2,figsize=(10,20))
+malignant = cancer.data[cancer.target == 0]
+benign = cancer.data[cancer.target == 1]
+ax = axes.ravel()
+
+for i in range(30):
+    _, bins = np.histogram(cancer.data[:,i], bins =50)
+    ax[i].hist(malignant[:,i], bins = bins, alpha = 0.5)
+    ax[i].hist(benign[:,i], bins = bins, alpha = 0.5)
+    ax[i].set_title(cancer.feature_names[i])
+    ax[i].set_yticks(())
+ax[0].set_xlabel("Feature magnitude")
+ax[0].set_ylabel("Frequency")
+ax[0].legend(["Malignant", "Benign"], loc ="best")
+fig.tight_layout()
+plt.show()
+
+import seaborn as sns
+correlation_matrix = cancerpd.corr().round(1)
+# use the heatmap function from seaborn to plot the correlation matrix
+# annot = True to print the values inside the square
+plt.figure(figsize=(15,8))
+sns.heatmap(data=correlation_matrix, annot=True)
+plt.show()
+
+
+
+
+_images/week45_15_0.png +_images/week45_15_1.png +
+
+
+
+

Discussing the correlation data

+

In the above example we note two things. In the first plot we display +the overlap of benign and malignant tumors as functions of the various +features in the Wisconsing breast cancer data set. We see that for +some of the features we can distinguish clearly the benign and +malignant cases while for other features we cannot. This can point to +us which features may be of greater interest when we wish to classify +a benign or not benign tumour.

+

In the second figure we have computed the so-called correlation +matrix, which in our case with thirty features becomes a \(30\times 30\) +matrix.

+

We constructed this matrix using pandas via the statements

+
+
+
cancerpd = pd.DataFrame(cancer.data, columns=cancer.feature_names)
+
+
+
+
+

and then

+
+
+
correlation_matrix = cancerpd.corr().round(1)
+
+
+
+
+

Diagonalizing this matrix we can in turn say something about which +features are of relevance and which are not. This leads us to +the classical Principal Component Analysis (PCA) theorem with +applications. This will be discussed later this semester (week 43).

+
+
+

Other measures in classification studies: Cancer Data again

+
+
+
import matplotlib.pyplot as plt
+import numpy as np
+from sklearn.model_selection import  train_test_split 
+from sklearn.datasets import load_breast_cancer
+from sklearn.linear_model import LogisticRegression
+
+# Load the data
+cancer = load_breast_cancer()
+
+X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
+print(X_train.shape)
+print(X_test.shape)
+# Logistic Regression
+logreg = LogisticRegression(solver='lbfgs')
+logreg.fit(X_train, y_train)
+
+from sklearn.preprocessing import LabelEncoder
+from sklearn.model_selection import cross_validate
+#Cross validation
+accuracy = cross_validate(logreg,X_test,y_test,cv=10)['test_score']
+print(accuracy)
+print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test)))
+
+import scikitplot as skplt
+y_pred = logreg.predict(X_test)
+skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True)
+plt.show()
+y_probas = logreg.predict_proba(X_test)
+skplt.metrics.plot_roc(y_test, y_probas)
+plt.show()
+skplt.metrics.plot_cumulative_gain(y_test, y_probas)
+plt.show()
+
+
+
+
+
(426, 30)
+(143, 30)
+[1.         0.86666667 1.         0.92857143 1.         0.85714286
+ 1.         0.92857143 0.92857143 1.        ]
+Test set accuracy with Logistic Regression: 0.94
+
+
+
/Users/mhjensen/miniforge3/envs/myenv/lib/python3.9/site-packages/sklearn/linear_model/_logistic.py:814: ConvergenceWarning: lbfgs failed to converge (status=1):
+STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
+
+Increase the number of iterations (max_iter) or scale the data as shown in:
+    https://scikit-learn.org/stable/modules/preprocessing.html
+Please also refer to the documentation for alternative solver options:
+    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
+  n_iter_i = _check_optimize_result(
+/Users/mhjensen/miniforge3/envs/myenv/lib/python3.9/site-packages/sklearn/linear_model/_logistic.py:814: ConvergenceWarning: lbfgs failed to converge (status=1):
+STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
+
+Increase the number of iterations (max_iter) or scale the data as shown in:
+    https://scikit-learn.org/stable/modules/preprocessing.html
+Please also refer to the documentation for alternative solver options:
+    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
+  n_iter_i = _check_optimize_result(
+/Users/mhjensen/miniforge3/envs/myenv/lib/python3.9/site-packages/sklearn/linear_model/_logistic.py:814: ConvergenceWarning: lbfgs failed to converge (status=1):
+STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
+
+Increase the number of iterations (max_iter) or scale the data as shown in:
+    https://scikit-learn.org/stable/modules/preprocessing.html
+Please also refer to the documentation for alternative solver options:
+    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
+  n_iter_i = _check_optimize_result(
+/Users/mhjensen/miniforge3/envs/myenv/lib/python3.9/site-packages/sklearn/linear_model/_logistic.py:814: ConvergenceWarning: lbfgs failed to converge (status=1):
+STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
+
+Increase the number of iterations (max_iter) or scale the data as shown in:
+    https://scikit-learn.org/stable/modules/preprocessing.html
+Please also refer to the documentation for alternative solver options:
+    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
+  n_iter_i = _check_optimize_result(
+/Users/mhjensen/miniforge3/envs/myenv/lib/python3.9/site-packages/sklearn/linear_model/_logistic.py:814: ConvergenceWarning: lbfgs failed to converge (status=1):
+STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
+
+Increase the number of iterations (max_iter) or scale the data as shown in:
+    https://scikit-learn.org/stable/modules/preprocessing.html
+Please also refer to the documentation for alternative solver options:
+    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
+  n_iter_i = _check_optimize_result(
+/Users/mhjensen/miniforge3/envs/myenv/lib/python3.9/site-packages/sklearn/linear_model/_logistic.py:814: ConvergenceWarning: lbfgs failed to converge (status=1):
+STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
+
+Increase the number of iterations (max_iter) or scale the data as shown in:
+    https://scikit-learn.org/stable/modules/preprocessing.html
+Please also refer to the documentation for alternative solver options:
+    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
+  n_iter_i = _check_optimize_result(
+/Users/mhjensen/miniforge3/envs/myenv/lib/python3.9/site-packages/sklearn/linear_model/_logistic.py:814: ConvergenceWarning: lbfgs failed to converge (status=1):
+STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
+
+Increase the number of iterations (max_iter) or scale the data as shown in:
+    https://scikit-learn.org/stable/modules/preprocessing.html
+Please also refer to the documentation for alternative solver options:
+    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
+  n_iter_i = _check_optimize_result(
+/Users/mhjensen/miniforge3/envs/myenv/lib/python3.9/site-packages/sklearn/linear_model/_logistic.py:814: ConvergenceWarning: lbfgs failed to converge (status=1):
+STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
+
+Increase the number of iterations (max_iter) or scale the data as shown in:
+    https://scikit-learn.org/stable/modules/preprocessing.html
+Please also refer to the documentation for alternative solver options:
+    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
+  n_iter_i = _check_optimize_result(
+/Users/mhjensen/miniforge3/envs/myenv/lib/python3.9/site-packages/sklearn/linear_model/_logistic.py:814: ConvergenceWarning: lbfgs failed to converge (status=1):
+STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
+
+Increase the number of iterations (max_iter) or scale the data as shown in:
+    https://scikit-learn.org/stable/modules/preprocessing.html
+Please also refer to the documentation for alternative solver options:
+    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
+  n_iter_i = _check_optimize_result(
+/Users/mhjensen/miniforge3/envs/myenv/lib/python3.9/site-packages/sklearn/linear_model/_logistic.py:814: ConvergenceWarning: lbfgs failed to converge (status=1):
+STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
+
+Increase the number of iterations (max_iter) or scale the data as shown in:
+    https://scikit-learn.org/stable/modules/preprocessing.html
+Please also refer to the documentation for alternative solver options:
+    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
+  n_iter_i = _check_optimize_result(
+/Users/mhjensen/miniforge3/envs/myenv/lib/python3.9/site-packages/sklearn/linear_model/_logistic.py:814: ConvergenceWarning: lbfgs failed to converge (status=1):
+STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
+
+Increase the number of iterations (max_iter) or scale the data as shown in:
+    https://scikit-learn.org/stable/modules/preprocessing.html
+Please also refer to the documentation for alternative solver options:
+    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
+  n_iter_i = _check_optimize_result(
+
+
+_images/week45_22_2.png +_images/week45_22_3.png +_images/week45_22_4.png +
+
+
+
+

Material for Lecture Thursday November 9

+
+
+

Recurrent neural networks (RNNs): Overarching view

+

Till now our focus has been, including convolutional neural networks +as well, on feedforward neural networks. The output or the activations +flow only in one direction, from the input layer to the output layer.

+

A recurrent neural network (RNN) looks very much like a feedforward +neural network, except that it also has connections pointing +backward.

+

RNNs are used to analyze time series data such as stock prices, and +tell you when to buy or sell. In autonomous driving systems, they can +anticipate car trajectories and help avoid accidents. More generally, +they can work on sequences of arbitrary lengths, rather than on +fixed-sized inputs like all the nets we have discussed so far. For +example, they can take sentences, documents, or audio samples as +input, making them extremely useful for natural language processing +systems such as automatic translation and speech-to-text.

+
+
+

A simple example

+
+
+
# Start importing packages
+import pandas as pd
+import numpy as np
+import matplotlib.pyplot as plt
+import tensorflow as tf
+from tensorflow.keras import datasets, layers, models
+from tensorflow.keras.layers import Input
+from tensorflow.keras.models import Model, Sequential 
+from tensorflow.keras.layers import Dense, SimpleRNN, LSTM, GRU
+from tensorflow.keras import optimizers     
+from tensorflow.keras import regularizers           
+from tensorflow.keras.utils import to_categorical 
+
+
+
+# convert into dataset matrix
+def convertToMatrix(data, step):
+ X, Y =[], []
+ for i in range(len(data)-step):
+  d=i+step  
+  X.append(data[i:d,])
+  Y.append(data[d,])
+ return np.array(X), np.array(Y)
+
+step = 4
+N = 1000    
+Tp = 800    
+
+t=np.arange(0,N)
+x=np.sin(0.02*t)+2*np.random.rand(N)
+df = pd.DataFrame(x)
+df.head()
+
+values=df.values
+train,test = values[0:Tp,:], values[Tp:N,:]
+
+# add step elements into train and test
+test = np.append(test,np.repeat(test[-1,],step))
+train = np.append(train,np.repeat(train[-1,],step))
+ 
+trainX,trainY =convertToMatrix(train,step)
+testX,testY =convertToMatrix(test,step)
+trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
+testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
+
+model = Sequential()
+model.add(SimpleRNN(units=32, input_shape=(1,step), activation="relu"))
+model.add(Dense(8, activation="relu")) 
+model.add(Dense(1))
+model.compile(loss='mean_squared_error', optimizer='rmsprop')
+model.summary()
+
+model.fit(trainX,trainY, epochs=100, batch_size=16, verbose=2)
+trainPredict = model.predict(trainX)
+testPredict= model.predict(testX)
+predicted=np.concatenate((trainPredict,testPredict),axis=0)
+
+trainScore = model.evaluate(trainX, trainY, verbose=0)
+print(trainScore)
+plt.plot(df)
+plt.plot(predicted)
+plt.show()
+
+
+
+
+
Metal device set to: Apple M1
+
+
+
Model: "sequential"
+
+
+
_________________________________________________________________
+
+
+
 Layer (type)                Output Shape              Param #   
+
+
+
=================================================================
+
+
+
 simple_rnn (SimpleRNN)      (None, 32)                1184      
+
+
+
                                                                 
+
+
+
 dense (Dense)               (None, 8)                 264       
+
+
+
                                                                 
+
+
+
 dense_1 (Dense)             (None, 1)                 9         
+
+
+
                                                                 
+
+
+
=================================================================
+
+
+
Total params: 1,457
+
+
+
Trainable params: 1,457
+
+
+
Non-trainable params: 0
+
+
+
_________________________________________________________________
+
+
+
Epoch 1/100
+
+
+
2023-11-06 06:26:15.664586: W tensorflow/core/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz
+
+
+
50/50 - 3s - loss: 0.4727 - 3s/epoch - 66ms/step
+
+
+
Epoch 2/100
+
+
+
50/50 - 0s - loss: 0.3966 - 471ms/epoch - 9ms/step
+
+
+
Epoch 3/100
+
+
+
50/50 - 0s - loss: 0.3929 - 470ms/epoch - 9ms/step
+
+
+
Epoch 4/100
+
+
+
50/50 - 0s - loss: 0.3901 - 468ms/epoch - 9ms/step
+
+
+
Epoch 5/100
+
+
+
50/50 - 0s - loss: 0.3920 - 467ms/epoch - 9ms/step
+
+
+
Epoch 6/100
+
+
+
50/50 - 0s - loss: 0.3910 - 469ms/epoch - 9ms/step
+
+
+
Epoch 7/100
+
+
+
50/50 - 0s - loss: 0.3911 - 470ms/epoch - 9ms/step
+
+
+
Epoch 8/100
+
+
+
50/50 - 0s - loss: 0.3896 - 469ms/epoch - 9ms/step
+
+
+
Epoch 9/100
+
+
+
50/50 - 0s - loss: 0.3913 - 467ms/epoch - 9ms/step
+
+
+
Epoch 10/100
+
+
+
50/50 - 0s - loss: 0.3858 - 467ms/epoch - 9ms/step
+
+
+
Epoch 11/100
+
+
+
50/50 - 0s - loss: 0.3884 - 465ms/epoch - 9ms/step
+
+
+
Epoch 12/100
+
+
+
50/50 - 0s - loss: 0.3899 - 468ms/epoch - 9ms/step
+
+
+
Epoch 13/100
+
+
+
50/50 - 0s - loss: 0.3868 - 467ms/epoch - 9ms/step
+
+
+
Epoch 14/100
+
+
+
50/50 - 0s - loss: 0.3853 - 467ms/epoch - 9ms/step
+
+
+
Epoch 15/100
+
+
+
50/50 - 0s - loss: 0.3889 - 465ms/epoch - 9ms/step
+
+
+
Epoch 16/100
+
+
+
50/50 - 0s - loss: 0.3858 - 468ms/epoch - 9ms/step
+
+
+
Epoch 17/100
+
+
+
50/50 - 0s - loss: 0.3873 - 465ms/epoch - 9ms/step
+
+
+
Epoch 18/100
+
+
+
50/50 - 0s - loss: 0.3879 - 466ms/epoch - 9ms/step
+
+
+
Epoch 19/100
+
+
+
50/50 - 0s - loss: 0.3870 - 468ms/epoch - 9ms/step
+
+
+
Epoch 20/100
+
+
+
50/50 - 0s - loss: 0.3842 - 467ms/epoch - 9ms/step
+
+
+
Epoch 21/100
+
+
+
50/50 - 0s - loss: 0.3847 - 468ms/epoch - 9ms/step
+
+
+
Epoch 22/100
+
+
+
50/50 - 0s - loss: 0.3846 - 469ms/epoch - 9ms/step
+
+
+
Epoch 23/100
+
+
+
50/50 - 0s - loss: 0.3837 - 470ms/epoch - 9ms/step
+
+
+
Epoch 24/100
+
+
+
50/50 - 0s - loss: 0.3855 - 466ms/epoch - 9ms/step
+
+
+
Epoch 25/100
+
+
+
50/50 - 0s - loss: 0.3846 - 467ms/epoch - 9ms/step
+
+
+
Epoch 26/100
+
+
+
50/50 - 0s - loss: 0.3842 - 468ms/epoch - 9ms/step
+
+
+
Epoch 27/100
+
+
+
50/50 - 0s - loss: 0.3835 - 463ms/epoch - 9ms/step
+
+
+
Epoch 28/100
+
+
+
50/50 - 0s - loss: 0.3839 - 461ms/epoch - 9ms/step
+
+
+
Epoch 29/100
+
+
+
50/50 - 0s - loss: 0.3827 - 462ms/epoch - 9ms/step
+
+
+
Epoch 30/100
+
+
+
50/50 - 0s - loss: 0.3827 - 463ms/epoch - 9ms/step
+
+
+
Epoch 31/100
+
+
+
50/50 - 0s - loss: 0.3810 - 462ms/epoch - 9ms/step
+
+
+
Epoch 32/100
+
+
+
50/50 - 0s - loss: 0.3812 - 457ms/epoch - 9ms/step
+
+
+
Epoch 33/100
+
+
+
50/50 - 0s - loss: 0.3814 - 460ms/epoch - 9ms/step
+
+
+
Epoch 34/100
+
+
+
50/50 - 0s - loss: 0.3828 - 466ms/epoch - 9ms/step
+
+
+
Epoch 35/100
+
+
+
50/50 - 0s - loss: 0.3816 - 461ms/epoch - 9ms/step
+
+
+
Epoch 36/100
+
+
+
50/50 - 0s - loss: 0.3822 - 463ms/epoch - 9ms/step
+
+
+
Epoch 37/100
+
+
+
50/50 - 0s - loss: 0.3813 - 462ms/epoch - 9ms/step
+
+
+
Epoch 38/100
+
+
+
50/50 - 0s - loss: 0.3803 - 465ms/epoch - 9ms/step
+
+
+
Epoch 39/100
+
+
+
50/50 - 0s - loss: 0.3801 - 466ms/epoch - 9ms/step
+
+
+
Epoch 40/100
+
+
+
50/50 - 0s - loss: 0.3815 - 467ms/epoch - 9ms/step
+
+
+
Epoch 41/100
+
+
+
50/50 - 0s - loss: 0.3806 - 464ms/epoch - 9ms/step
+
+
+
Epoch 42/100
+
+
+
50/50 - 0s - loss: 0.3801 - 464ms/epoch - 9ms/step
+
+
+
Epoch 43/100
+
+
+
50/50 - 0s - loss: 0.3791 - 469ms/epoch - 9ms/step
+
+
+
Epoch 44/100
+
+
+
50/50 - 0s - loss: 0.3803 - 468ms/epoch - 9ms/step
+
+
+
Epoch 45/100
+
+
+
50/50 - 0s - loss: 0.3779 - 468ms/epoch - 9ms/step
+
+
+
Epoch 46/100
+
+
+
50/50 - 0s - loss: 0.3776 - 466ms/epoch - 9ms/step
+
+
+
Epoch 47/100
+
+
+
50/50 - 0s - loss: 0.3778 - 472ms/epoch - 9ms/step
+
+
+
Epoch 48/100
+
+
+
50/50 - 0s - loss: 0.3790 - 465ms/epoch - 9ms/step
+
+
+
Epoch 49/100
+
+
+
50/50 - 0s - loss: 0.3750 - 466ms/epoch - 9ms/step
+
+
+
Epoch 50/100
+
+
+
50/50 - 0s - loss: 0.3782 - 468ms/epoch - 9ms/step
+
+
+
Epoch 51/100
+
+
+
50/50 - 0s - loss: 0.3773 - 469ms/epoch - 9ms/step
+
+
+
Epoch 52/100
+
+
+
50/50 - 0s - loss: 0.3768 - 466ms/epoch - 9ms/step
+
+
+
Epoch 53/100
+
+
+
50/50 - 0s - loss: 0.3760 - 467ms/epoch - 9ms/step
+
+
+
Epoch 54/100
+
+
+
---------------------------------------------------------------------------
+KeyboardInterrupt                         Traceback (most recent call last)
+Input In [9], in <cell line: 53>()
+     50 model.compile(loss='mean_squared_error', optimizer='rmsprop')
+     51 model.summary()
+---> 53 model.fit(trainX,trainY, epochs=100, batch_size=16, verbose=2)
+     54 trainPredict = model.predict(trainX)
+     55 testPredict= model.predict(testX)
+
+File ~/miniforge3/envs/myenv/lib/python3.9/site-packages/keras/utils/traceback_utils.py:64, in filter_traceback.<locals>.error_handler(*args, **kwargs)
+     62 filtered_tb = None
+     63 try:
+---> 64   return fn(*args, **kwargs)
+     65 except Exception as e:  # pylint: disable=broad-except
+     66   filtered_tb = _process_traceback_frames(e.__traceback__)
+
+File ~/miniforge3/envs/myenv/lib/python3.9/site-packages/keras/engine/training.py:1384, in Model.fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
+   1377 with tf.profiler.experimental.Trace(
+   1378     'train',
+   1379     epoch_num=epoch,
+   1380     step_num=step,
+   1381     batch_size=batch_size,
+   1382     _r=1):
+   1383   callbacks.on_train_batch_begin(step)
+-> 1384   tmp_logs = self.train_function(iterator)
+   1385   if data_handler.should_sync:
+   1386     context.async_wait()
+
+File ~/miniforge3/envs/myenv/lib/python3.9/site-packages/tensorflow/python/util/traceback_utils.py:150, in filter_traceback.<locals>.error_handler(*args, **kwargs)
+    148 filtered_tb = None
+    149 try:
+--> 150   return fn(*args, **kwargs)
+    151 except Exception as e:
+    152   filtered_tb = _process_traceback_frames(e.__traceback__)
+
+File ~/miniforge3/envs/myenv/lib/python3.9/site-packages/tensorflow/python/eager/def_function.py:915, in Function.__call__(self, *args, **kwds)
+    912 compiler = "xla" if self._jit_compile else "nonXla"
+    914 with OptionalXlaContext(self._jit_compile):
+--> 915   result = self._call(*args, **kwds)
+    917 new_tracing_count = self.experimental_get_tracing_count()
+    918 without_tracing = (tracing_count == new_tracing_count)
+
+File ~/miniforge3/envs/myenv/lib/python3.9/site-packages/tensorflow/python/eager/def_function.py:947, in Function._call(self, *args, **kwds)
+    944   self._lock.release()
+    945   # In this case we have created variables on the first call, so we run the
+    946   # defunned version which is guaranteed to never create variables.
+--> 947   return self._stateless_fn(*args, **kwds)  # pylint: disable=not-callable
+    948 elif self._stateful_fn is not None:
+    949   # Release the lock early so that multiple threads can perform the call
+    950   # in parallel.
+    951   self._lock.release()
+
+File ~/miniforge3/envs/myenv/lib/python3.9/site-packages/tensorflow/python/eager/function.py:2956, in Function.__call__(self, *args, **kwargs)
+   2953 with self._lock:
+   2954   (graph_function,
+   2955    filtered_flat_args) = self._maybe_define_function(args, kwargs)
+-> 2956 return graph_function._call_flat(
+   2957     filtered_flat_args, captured_inputs=graph_function.captured_inputs)
+
+File ~/miniforge3/envs/myenv/lib/python3.9/site-packages/tensorflow/python/eager/function.py:1853, in ConcreteFunction._call_flat(self, args, captured_inputs, cancellation_manager)
+   1849 possible_gradient_type = gradients_util.PossibleTapeGradientTypes(args)
+   1850 if (possible_gradient_type == gradients_util.POSSIBLE_GRADIENT_TYPES_NONE
+   1851     and executing_eagerly):
+   1852   # No tape is watching; skip to running the function.
+-> 1853   return self._build_call_outputs(self._inference_function.call(
+   1854       ctx, args, cancellation_manager=cancellation_manager))
+   1855 forward_backward = self._select_forward_and_backward_functions(
+   1856     args,
+   1857     possible_gradient_type,
+   1858     executing_eagerly)
+   1859 forward_function, args_with_tangents = forward_backward.forward()
+
+File ~/miniforge3/envs/myenv/lib/python3.9/site-packages/tensorflow/python/eager/function.py:499, in _EagerDefinedFunction.call(self, ctx, args, cancellation_manager)
+    497 with _InterpolateFunctionError(self):
+    498   if cancellation_manager is None:
+--> 499     outputs = execute.execute(
+    500         str(self.signature.name),
+    501         num_outputs=self._num_outputs,
+    502         inputs=args,
+    503         attrs=attrs,
+    504         ctx=ctx)
+    505   else:
+    506     outputs = execute.execute_with_cancellation(
+    507         str(self.signature.name),
+    508         num_outputs=self._num_outputs,
+   (...)
+    511         ctx=ctx,
+    512         cancellation_manager=cancellation_manager)
+
+File ~/miniforge3/envs/myenv/lib/python3.9/site-packages/tensorflow/python/eager/execute.py:54, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
+     52 try:
+     53   ctx.ensure_initialized()
+---> 54   tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
+     55                                       inputs, attrs, num_outputs)
+     56 except core._NotOkStatusException as e:
+     57   if name is not None:
+
+KeyboardInterrupt: 
+
+
+
+
+
+

RNNs

+

RNNs are very powerful, because they +combine two properties:

+
    +
  1. Distributed hidden state that allows them to store a lot of information about the past efficiently.

  2. +
  3. Non-linear dynamics that allows them to update their hidden state in complicated ways.

  4. +
+

With enough neurons and time, RNNs +can compute anything that can be +computed by your computer!

+
+
+
+

Basic layout

+ + +

Figure 1:

+
+

We need to specify the initial activity state of all the hidden and output units

+
    +
  1. We could just fix these initial states to have some default value like 0.5.

  2. +
  3. But it is better to treat the initial states as learned parameters.

  4. +
  5. We learn them in the same way as we learn the weights.

  6. +
+
    +
  • Start off with an initial random guess for the initial states.

  • +
+

a. At the end of each training sequence, backpropagate through time all the way to the initial states to get the gradient of the error function with respect to each initial state.

+

b. Adjust the initial states by following the negative gradient.

+
+
+

We can specify inputs in several ways

+
    +
  1. Specify the initial states of all the units.

  2. +
  3. Specify the initial states of a subset of the units.

  4. +
  5. Specify the states of the same subset of the units at every time step.

  6. +
+

This is the natural way to model most sequential data.

+
+
+

We can specify targets in several ways

+
    +
  1. Specify desired final activities of all the units

  2. +
  3. Specify desired activities of all units for the last few steps

  4. +
+
    +
  • Good for learning attractors

  • +
  • It is easy to add in extra error derivatives as we backpropagate.

    +
      +
    • Specify the desired activity of a subset of the units.

    • +
    +
  • +
  • The other units are input or hidden units.

  • +
+ + +

Figure 1:

+ + + +

Figure 1:

+ + + +

Figure 1:

+ + + +

Figure 1:

+
+
+

Backpropagation through time

+

We can think of the recurrent net as a layered, feed-forward +net with shared weights and then train the feed-forward net +with weight constraints.

+

We can also think of this training algorithm in the time domain:

+
    +
  1. The forward pass builds up a stack of the activities of all the units at each time step.

  2. +
  3. The backward pass peels activities off the stack to compute the error derivatives at each time step.

  4. +
  5. After the backward pass we add together the derivatives at all the different times for each weight.

  6. +
+
+
+

The backward pass is linear

+
    +
  1. There is a big difference between the forward and backward passes.

  2. +
  3. In the forward pass we use squashing functions (like the logistic) to prevent the activity vectors from exploding.

  4. +
  5. The backward pass, is completely linear. If you double the error derivatives at the final layer, all the error derivatives will double.

  6. +
+

The forward pass determines the slope of the linear function used for +backpropagating through each neuron

+ + +

Figure 1:

+ + + +

Figure 1:

+ + + +

Figure 1:

+ + + +

Figure 1:

+ + + +

Figure 1:

+ + + +

Figure 1:

+ + + +

Figure 1:

+
+
+
+

The problem of exploding or vanishing gradients

+
    +
  • What happens to the magnitude of the gradients as we backpropagate through many layers?

  • +
+

a. If the weights are small, the gradients shrink exponentially.

+

b. If the weights are big the gradients grow exponentially.

+
    +
  • Typical feed-forward neural nets can cope with these exponential effects because they only have a few hidden layers.

  • +
  • In an RNN trained on long sequences (e.g. 100 time steps) the gradients can easily explode or vanish.

  • +
+

a. We can avoid this by initializing the weights very carefully.

+
    +
  • Even with good initial weights, its very hard to detect that the current target output depends on an input from many time-steps ago.

  • +
+

RNNs have difficulty dealing with long-range dependencies.

+
+
+

Four effective ways to learn an RNN

+
    +
  1. Long Short Term Memory Make the RNN out of little modules that are designed to remember values for a long time.

  2. +
  3. Hessian Free Optimization: Deal with the vanishing gradients problem by using a fancy optimizer that can detect directions with a tiny gradient but even smaller curvature.

  4. +
  5. Echo State Networks: Initialize the input a hidden and hidden-hidden and output-hidden connections very carefully so that the hidden state has a huge reservoir of weakly coupled oscillators which can be selectively driven by the input.

  6. +
+
    +
  • ESNs only need to learn the hidden-output connections.

  • +
+
    +
  1. Good initialization with momentum Initialize like in Echo State Networks, but then learn all of the connections using momentum

  2. +
+
+

Long Short Term Memory (LSTM)

+

LSTM uses a memory cell for +modeling long-range dependencies and avoid vanishing gradient +problems.

+
    +
  1. Introduced by Hochreiter and Schmidhuber (1997) who solved the problem of getting an RNN to remember things for a long time (like hundreds of time steps).

  2. +
  3. They designed a memory cell using logistic and linear units with multiplicative interactions.

  4. +
  5. Information gets into the cell whenever its “write” gate is on.

  6. +
  7. The information stays in the cell so long as its keep gate is on.

  8. +
  9. Information can be read from the cell by turning on its read gate.

  10. +
+
+
+

Implementing a memory cell in a neural network

+

To preserve information for a long time in +the activities of an RNN, we use a circuit +that implements an analog memory cell.

+
    +
  1. A linear unit that has a self-link with a weight of 1 will maintain its state.

  2. +
  3. Information is stored in the cell by activating its write gate.

  4. +
  5. Information is retrieved by activating the read gate.

  6. +
  7. We can backpropagate through this circuit because logistics are have nice derivatives.

  8. +
+ + +

Figure 1:

+ + + +

Figure 1:

+ + + +

Figure 1:

+ + + +

Figure 1:

+ + + +

Figure 1:

+ + + +

Figure 1:

+ + + +

Figure 1:

+ + + +

Figure 1:

+ + + +

Figure 1:

+ + + +

Figure 1:

+
+
+
+

An extrapolation example

+

The following code provides an example of how recurrent neural +networks can be used to extrapolate to unknown values of physics data +sets. Specifically, the data sets used in this program come from +a quantum mechanical many-body calculation of energies as functions of the number of particles.

+
+
+
# For matrices and calculations
+import numpy as np
+# For machine learning (backend for keras)
+import tensorflow as tf
+# User-friendly machine learning library
+# Front end for TensorFlow
+import tensorflow.keras
+# Different methods from Keras needed to create an RNN
+# This is not necessary but it shortened function calls 
+# that need to be used in the code.
+from tensorflow.keras import datasets, layers, models
+from tensorflow.keras.layers import Input
+from tensorflow.keras import regularizers
+from tensorflow.keras.models import Model, Sequential
+from tensorflow.keras.layers import Dense, SimpleRNN, LSTM, GRU
+# For timing the code
+from timeit import default_timer as timer
+# For plotting
+import matplotlib.pyplot as plt
+
+
+# The data set
+datatype='VaryDimension'
+X_tot = np.arange(2, 42, 2)
+y_tot = np.array([-0.03077640549, -0.08336233266, -0.1446729567, -0.2116753732, -0.2830637392, -0.3581341341, -0.436462435, -0.5177783846,
+	-0.6019067271, -0.6887363571, -0.7782028952, -0.8702784034, -0.9649652536, -1.062292565, -1.16231451, 
+	-1.265109911, -1.370782966, -1.479465113, -1.591317992, -1.70653767])
+
+
+
+
+
+
+

Formatting the Data

+

The way the recurrent neural networks are trained in this program +differs from how machine learning algorithms are usually trained. +Typically a machine learning algorithm is trained by learning the +relationship between the x data and the y data. In this program, the +recurrent neural network will be trained to recognize the relationship +in a sequence of y values. This is type of data formatting is +typically used time series forcasting, but it can also be used in any +extrapolation (time series forecasting is just a specific type of +extrapolation along the time axis). This method of data formatting +does not use the x data and assumes that the y data are evenly spaced.

+

For a standard machine learning algorithm, the training data has the +form of (x,y) so the machine learning algorithm learns to assiciate a +y value with a given x value. This is useful when the test data has x +values within the same range as the training data. However, for this +application, the x values of the test data are outside of the x values +of the training data and the traditional method of training a machine +learning algorithm does not work as well. For this reason, the +recurrent neural network is trained on sequences of y values of the +form ((y1, y2), y3), so that the network is concerned with learning +the pattern of the y data and not the relation between the x and y +data. As long as the pattern of y data outside of the training region +stays relatively stable compared to what was inside the training +region, this method of training can produce accurate extrapolations to +y values far removed from the training data set.

+ + + + + +
+
+
# FORMAT_DATA
+def format_data(data, length_of_sequence = 2):  
+    """
+        Inputs:
+            data(a numpy array): the data that will be the inputs to the recurrent neural
+                network
+            length_of_sequence (an int): the number of elements in one iteration of the
+                sequence patter.  For a function approximator use length_of_sequence = 2.
+        Returns:
+            rnn_input (a 3D numpy array): the input data for the recurrent neural network.  Its
+                dimensions are length of data - length of sequence, length of sequence, 
+                dimnsion of data
+            rnn_output (a numpy array): the training data for the neural network
+        Formats data to be used in a recurrent neural network.
+    """
+
+    X, Y = [], []
+    for i in range(len(data)-length_of_sequence):
+        # Get the next length_of_sequence elements
+        a = data[i:i+length_of_sequence]
+        # Get the element that immediately follows that
+        b = data[i+length_of_sequence]
+        # Reshape so that each data point is contained in its own array
+        a = np.reshape (a, (len(a), 1))
+        X.append(a)
+        Y.append(b)
+    rnn_input = np.array(X)
+    rnn_output = np.array(Y)
+
+    return rnn_input, rnn_output
+
+
+# ## Defining the Recurrent Neural Network Using Keras
+# 
+# The following method defines a simple recurrent neural network in keras consisting of one input layer, one hidden layer, and one output layer.
+
+def rnn(length_of_sequences, batch_size = None, stateful = False):
+    """
+        Inputs:
+            length_of_sequences (an int): the number of y values in "x data".  This is determined
+                when the data is formatted
+            batch_size (an int): Default value is None.  See Keras documentation of SimpleRNN.
+            stateful (a boolean): Default value is False.  See Keras documentation of SimpleRNN.
+        Returns:
+            model (a Keras model): The recurrent neural network that is built and compiled by this
+                method
+        Builds and compiles a recurrent neural network with one hidden layer and returns the model.
+    """
+    # Number of neurons in the input and output layers
+    in_out_neurons = 1
+    # Number of neurons in the hidden layer
+    hidden_neurons = 200
+    # Define the input layer
+    inp = Input(batch_shape=(batch_size, 
+                length_of_sequences, 
+                in_out_neurons))  
+    # Define the hidden layer as a simple RNN layer with a set number of neurons and add it to 
+    # the network immediately after the input layer
+    rnn = SimpleRNN(hidden_neurons, 
+                    return_sequences=False,
+                    stateful = stateful,
+                    name="RNN")(inp)
+    # Define the output layer as a dense neural network layer (standard neural network layer)
+    #and add it to the network immediately after the hidden layer.
+    dens = Dense(in_out_neurons,name="dense")(rnn)
+    # Create the machine learning model starting with the input layer and ending with the 
+    # output layer
+    model = Model(inputs=[inp],outputs=[dens])
+    # Compile the machine learning model using the mean squared error function as the loss 
+    # function and an Adams optimizer.
+    model.compile(loss="mean_squared_error", optimizer="adam")  
+    return model
+
+
+
+
+
+
+

Predicting New Points With A Trained Recurrent Neural Network

+
+
+
def test_rnn (x1, y_test, plot_min, plot_max):
+    """
+        Inputs:
+            x1 (a list or numpy array): The complete x component of the data set
+            y_test (a list or numpy array): The complete y component of the data set
+            plot_min (an int or float): the smallest x value used in the training data
+            plot_max (an int or float): the largest x valye used in the training data
+        Returns:
+            None.
+        Uses a trained recurrent neural network model to predict future points in the 
+        series.  Computes the MSE of the predicted data set from the true data set, saves
+        the predicted data set to a csv file, and plots the predicted and true data sets w
+        while also displaying the data range used for training.
+    """
+    # Add the training data as the first dim points in the predicted data array as these
+    # are known values.
+    y_pred = y_test[:dim].tolist()
+    # Generate the first input to the trained recurrent neural network using the last two 
+    # points of the training data.  Based on how the network was trained this means that it
+    # will predict the first point in the data set after the training data.  All of the 
+    # brackets are necessary for Tensorflow.
+    next_input = np.array([[[y_test[dim-2]], [y_test[dim-1]]]])
+    # Save the very last point in the training data set.  This will be used later.
+    last = [y_test[dim-1]]
+
+    # Iterate until the complete data set is created.
+    for i in range (dim, len(y_test)):
+        # Predict the next point in the data set using the previous two points.
+        next = model.predict(next_input)
+        # Append just the number of the predicted data set
+        y_pred.append(next[0][0])
+        # Create the input that will be used to predict the next data point in the data set.
+        next_input = np.array([[last, next[0]]], dtype=np.float64)
+        last = next
+
+    # Print the mean squared error between the known data set and the predicted data set.
+    print('MSE: ', np.square(np.subtract(y_test, y_pred)).mean())
+    # Save the predicted data set as a csv file for later use
+    name = datatype + 'Predicted'+str(dim)+'.csv'
+    np.savetxt(name, y_pred, delimiter=',')
+    # Plot the known data set and the predicted data set.  The red box represents the region that was used
+    # for the training data.
+    fig, ax = plt.subplots()
+    ax.plot(x1, y_test, label="true", linewidth=3)
+    ax.plot(x1, y_pred, 'g-.',label="predicted", linewidth=4)
+    ax.legend()
+    # Created a red region to represent the points used in the training data.
+    ax.axvspan(plot_min, plot_max, alpha=0.25, color='red')
+    plt.show()
+
+# Check to make sure the data set is complete
+assert len(X_tot) == len(y_tot)
+
+# This is the number of points that will be used in as the training data
+dim=12
+
+# Separate the training data from the whole data set
+X_train = X_tot[:dim]
+y_train = y_tot[:dim]
+
+
+# Generate the training data for the RNN, using a sequence of 2
+rnn_input, rnn_training = format_data(y_train, 2)
+
+
+# Create a recurrent neural network in Keras and produce a summary of the 
+# machine learning model
+model = rnn(length_of_sequences = rnn_input.shape[1])
+model.summary()
+
+# Start the timer.  Want to time training+testing
+start = timer()
+# Fit the model using the training data genenerated above using 150 training iterations and a 5%
+# validation split.  Setting verbose to True prints information about each training iteration.
+hist = model.fit(rnn_input, rnn_training, batch_size=None, epochs=150, 
+                 verbose=True,validation_split=0.05)
+
+for label in ["loss","val_loss"]:
+    plt.plot(hist.history[label],label=label)
+
+plt.ylabel("loss")
+plt.xlabel("epoch")
+plt.title("The final validation loss: {}".format(hist.history["val_loss"][-1]))
+plt.legend()
+plt.show()
+
+# Use the trained neural network to predict more points of the data set
+test_rnn(X_tot, y_tot, X_tot[0], X_tot[dim-1])
+# Stop the timer and calculate the total time needed.
+end = timer()
+print('Time: ', end-start)
+
+
+
+
+
+
+

Other Things to Try

+

Changing the size of the recurrent neural network and its parameters +can drastically change the results you get from the model. The below +code takes the simple recurrent neural network from above and adds a +second hidden layer, changes the number of neurons in the hidden +layer, and explicitly declares the activation function of the hidden +layers to be a sigmoid function. The loss function and optimizer can +also be changed but are kept the same as the above network. These +parameters can be tuned to provide the optimal result from the +network. For some ideas on how to improve the performance of a +recurrent neural network.

+
+
+
def rnn_2layers(length_of_sequences, batch_size = None, stateful = False):
+    """
+        Inputs:
+            length_of_sequences (an int): the number of y values in "x data".  This is determined
+                when the data is formatted
+            batch_size (an int): Default value is None.  See Keras documentation of SimpleRNN.
+            stateful (a boolean): Default value is False.  See Keras documentation of SimpleRNN.
+        Returns:
+            model (a Keras model): The recurrent neural network that is built and compiled by this
+                method
+        Builds and compiles a recurrent neural network with two hidden layers and returns the model.
+    """
+    # Number of neurons in the input and output layers
+    in_out_neurons = 1
+    # Number of neurons in the hidden layer, increased from the first network
+    hidden_neurons = 500
+    # Define the input layer
+    inp = Input(batch_shape=(batch_size, 
+                length_of_sequences, 
+                in_out_neurons))  
+    # Create two hidden layers instead of one hidden layer.  Explicitly set the activation
+    # function to be the sigmoid function (the default value is hyperbolic tangent)
+    rnn1 = SimpleRNN(hidden_neurons, 
+                    return_sequences=True,  # This needs to be True if another hidden layer is to follow
+                    stateful = stateful, activation = 'sigmoid',
+                    name="RNN1")(inp)
+    rnn2 = SimpleRNN(hidden_neurons, 
+                    return_sequences=False, activation = 'sigmoid',
+                    stateful = stateful,
+                    name="RNN2")(rnn1)
+    # Define the output layer as a dense neural network layer (standard neural network layer)
+    #and add it to the network immediately after the hidden layer.
+    dens = Dense(in_out_neurons,name="dense")(rnn2)
+    # Create the machine learning model starting with the input layer and ending with the 
+    # output layer
+    model = Model(inputs=[inp],outputs=[dens])
+    # Compile the machine learning model using the mean squared error function as the loss 
+    # function and an Adams optimizer.
+    model.compile(loss="mean_squared_error", optimizer="adam")  
+    return model
+
+# Check to make sure the data set is complete
+assert len(X_tot) == len(y_tot)
+
+# This is the number of points that will be used in as the training data
+dim=12
+
+# Separate the training data from the whole data set
+X_train = X_tot[:dim]
+y_train = y_tot[:dim]
+
+
+# Generate the training data for the RNN, using a sequence of 2
+rnn_input, rnn_training = format_data(y_train, 2)
+
+
+# Create a recurrent neural network in Keras and produce a summary of the 
+# machine learning model
+model = rnn_2layers(length_of_sequences = 2)
+model.summary()
+
+# Start the timer.  Want to time training+testing
+start = timer()
+# Fit the model using the training data genenerated above using 150 training iterations and a 5%
+# validation split.  Setting verbose to True prints information about each training iteration.
+hist = model.fit(rnn_input, rnn_training, batch_size=None, epochs=150, 
+                 verbose=True,validation_split=0.05)
+
+
+# This section plots the training loss and the validation loss as a function of training iteration.
+# This is not required for analyzing the couple cluster data but can help determine if the network is
+# being overtrained.
+for label in ["loss","val_loss"]:
+    plt.plot(hist.history[label],label=label)
+
+plt.ylabel("loss")
+plt.xlabel("epoch")
+plt.title("The final validation loss: {}".format(hist.history["val_loss"][-1]))
+plt.legend()
+plt.show()
+
+# Use the trained neural network to predict more points of the data set
+test_rnn(X_tot, y_tot, X_tot[0], X_tot[dim-1])
+# Stop the timer and calculate the total time needed.
+end = timer()
+print('Time: ', end-start)
+
+
+
+
+
+
+

Other Types of Recurrent Neural Networks

+

Besides a simple recurrent neural network layer, there are two other +commonly used types of recurrent neural network layers: Long Short +Term Memory (LSTM) and Gated Recurrent Unit (GRU). For a short +introduction to these layers see https://medium.com/mindboard/lstm-vs-gru-experimental-comparison-955820c21e8b +and https://medium.com/mindboard/lstm-vs-gru-experimental-comparison-955820c21e8b.

+

The first network created below is similar to the previous network, +but it replaces the SimpleRNN layers with LSTM layers. The second +network below has two hidden layers made up of GRUs, which are +preceeded by two dense (feeddorward) neural network layers. These +dense layers “preprocess” the data before it reaches the recurrent +layers. This architecture has been shown to improve the performance +of recurrent neural networks (see the link above and also +https://arxiv.org/pdf/1807.02857.pdf.

+
+
+
def lstm_2layers(length_of_sequences, batch_size = None, stateful = False):
+    """
+        Inputs:
+            length_of_sequences (an int): the number of y values in "x data".  This is determined
+                when the data is formatted
+            batch_size (an int): Default value is None.  See Keras documentation of SimpleRNN.
+            stateful (a boolean): Default value is False.  See Keras documentation of SimpleRNN.
+        Returns:
+            model (a Keras model): The recurrent neural network that is built and compiled by this
+                method
+        Builds and compiles a recurrent neural network with two LSTM hidden layers and returns the model.
+    """
+    # Number of neurons on the input/output layer and the number of neurons in the hidden layer
+    in_out_neurons = 1
+    hidden_neurons = 250
+    # Input Layer
+    inp = Input(batch_shape=(batch_size, 
+                length_of_sequences, 
+                in_out_neurons)) 
+    # Hidden layers (in this case they are LSTM layers instead if SimpleRNN layers)
+    rnn= LSTM(hidden_neurons, 
+                    return_sequences=True,
+                    stateful = stateful,
+                    name="RNN", use_bias=True, activation='tanh')(inp)
+    rnn1 = LSTM(hidden_neurons, 
+                    return_sequences=False,
+                    stateful = stateful,
+                    name="RNN1", use_bias=True, activation='tanh')(rnn)
+    # Output layer
+    dens = Dense(in_out_neurons,name="dense")(rnn1)
+    # Define the midel
+    model = Model(inputs=[inp],outputs=[dens])
+    # Compile the model
+    model.compile(loss='mean_squared_error', optimizer='adam')  
+    # Return the model
+    return model
+
+def dnn2_gru2(length_of_sequences, batch_size = None, stateful = False):
+    """
+        Inputs:
+            length_of_sequences (an int): the number of y values in "x data".  This is determined
+                when the data is formatted
+            batch_size (an int): Default value is None.  See Keras documentation of SimpleRNN.
+            stateful (a boolean): Default value is False.  See Keras documentation of SimpleRNN.
+        Returns:
+            model (a Keras model): The recurrent neural network that is built and compiled by this
+                method
+        Builds and compiles a recurrent neural network with four hidden layers (two dense followed by
+        two GRU layers) and returns the model.
+    """    
+    # Number of neurons on the input/output layers and hidden layers
+    in_out_neurons = 1
+    hidden_neurons = 250
+    # Input layer
+    inp = Input(batch_shape=(batch_size, 
+                length_of_sequences, 
+                in_out_neurons)) 
+    # Hidden Dense (feedforward) layers
+    dnn = Dense(hidden_neurons/2, activation='relu', name='dnn')(inp)
+    dnn1 = Dense(hidden_neurons/2, activation='relu', name='dnn1')(dnn)
+    # Hidden GRU layers
+    rnn1 = GRU(hidden_neurons, 
+                    return_sequences=True,
+                    stateful = stateful,
+                    name="RNN1", use_bias=True)(dnn1)
+    rnn = GRU(hidden_neurons, 
+                    return_sequences=False,
+                    stateful = stateful,
+                    name="RNN", use_bias=True)(rnn1)
+    # Output layer
+    dens = Dense(in_out_neurons,name="dense")(rnn)
+    # Define the model
+    model = Model(inputs=[inp],outputs=[dens])
+    # Compile the mdoel
+    model.compile(loss='mean_squared_error', optimizer='adam')  
+    # Return the model
+    return model
+
+# Check to make sure the data set is complete
+assert len(X_tot) == len(y_tot)
+
+# This is the number of points that will be used in as the training data
+dim=12
+
+# Separate the training data from the whole data set
+X_train = X_tot[:dim]
+y_train = y_tot[:dim]
+
+
+# Generate the training data for the RNN, using a sequence of 2
+rnn_input, rnn_training = format_data(y_train, 2)
+
+
+# Create a recurrent neural network in Keras and produce a summary of the 
+# machine learning model
+# Change the method name to reflect which network you want to use
+model = dnn2_gru2(length_of_sequences = 2)
+model.summary()
+
+# Start the timer.  Want to time training+testing
+start = timer()
+# Fit the model using the training data genenerated above using 150 training iterations and a 5%
+# validation split.  Setting verbose to True prints information about each training iteration.
+hist = model.fit(rnn_input, rnn_training, batch_size=None, epochs=150, 
+                 verbose=True,validation_split=0.05)
+
+
+# This section plots the training loss and the validation loss as a function of training iteration.
+# This is not required for analyzing the couple cluster data but can help determine if the network is
+# being overtrained.
+for label in ["loss","val_loss"]:
+    plt.plot(hist.history[label],label=label)
+
+plt.ylabel("loss")
+plt.xlabel("epoch")
+plt.title("The final validation loss: {}".format(hist.history["val_loss"][-1]))
+plt.legend()
+plt.show()
+
+# Use the trained neural network to predict more points of the data set
+test_rnn(X_tot, y_tot, X_tot[0], X_tot[dim-1])
+# Stop the timer and calculate the total time needed.
+end = timer()
+print('Time: ', end-start)
+
+
+# ### Training Recurrent Neural Networks in the Standard Way (i.e. learning the relationship between the X and Y data)
+# 
+# Finally, comparing the performace of a recurrent neural network using the standard data formatting to the performance of the network with time sequence data formatting shows the benefit of this type of data formatting with extrapolation.
+
+# Check to make sure the data set is complete
+assert len(X_tot) == len(y_tot)
+
+# This is the number of points that will be used in as the training data
+dim=12
+
+# Separate the training data from the whole data set
+X_train = X_tot[:dim]
+y_train = y_tot[:dim]
+
+# Reshape the data for Keras specifications
+X_train = X_train.reshape((dim, 1))
+y_train = y_train.reshape((dim, 1))
+
+
+# Create a recurrent neural network in Keras and produce a summary of the 
+# machine learning model
+# Set the sequence length to 1 for regular data formatting 
+model = rnn(length_of_sequences = 1)
+model.summary()
+
+# Start the timer.  Want to time training+testing
+start = timer()
+# Fit the model using the training data genenerated above using 150 training iterations and a 5%
+# validation split.  Setting verbose to True prints information about each training iteration.
+hist = model.fit(X_train, y_train, batch_size=None, epochs=150, 
+                 verbose=True,validation_split=0.05)
+
+
+# This section plots the training loss and the validation loss as a function of training iteration.
+# This is not required for analyzing the couple cluster data but can help determine if the network is
+# being overtrained.
+for label in ["loss","val_loss"]:
+    plt.plot(hist.history[label],label=label)
+
+plt.ylabel("loss")
+plt.xlabel("epoch")
+plt.title("The final validation loss: {}".format(hist.history["val_loss"][-1]))
+plt.legend()
+plt.show()
+
+# Use the trained neural network to predict the remaining data points
+X_pred = X_tot[dim:]
+X_pred = X_pred.reshape((len(X_pred), 1))
+y_model = model.predict(X_pred)
+y_pred = np.concatenate((y_tot[:dim], y_model.flatten()))
+
+# Plot the known data set and the predicted data set.  The red box represents the region that was used
+# for the training data.
+fig, ax = plt.subplots()
+ax.plot(X_tot, y_tot, label="true", linewidth=3)
+ax.plot(X_tot, y_pred, 'g-.',label="predicted", linewidth=4)
+ax.legend()
+# Created a red region to represent the points used in the training data.
+ax.axvspan(X_tot[0], X_tot[dim], alpha=0.25, color='red')
+plt.show()
+
+# Stop the timer and calculate the total time needed.
+end = timer()
+print('Time: ', end-start)
+
+
+
+
+
+
+ + + + +
+ + + + + +
+
+
+

+ + By Morten Hjorth-Jensen
+ + © Copyright 2021.
+

+
+