Files
FYS-STK4155/doc/LectureNotes/_build/html/_sources/chapter13.ipynb
T
Morten Hjorth-Jensen 31fc8fcf81 updating notes
2025-08-05 17:31:55 +02:00

1874 lines
67 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "6d2db899",
"metadata": {
"editable": true
},
"source": [
"<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)\n",
"doconce format html chapter13.do.txt -->"
]
},
{
"cell_type": "markdown",
"id": "f4908a97",
"metadata": {
"editable": true
},
"source": [
"# Recurrent neural networks: Overarching view\n",
"\n",
"Till now our focus has been, including convolutional neural networks\n",
"as well, on feedforward neural networks. The output or the activations\n",
"flow only in one direction, from the input layer to the output layer.\n",
"\n",
"A recurrent neural network (RNN) looks very much like a feedforward\n",
"neural network, except that it also has connections pointing\n",
"backward. \n",
"\n",
"RNNs are used to analyze time series data such as stock prices, and\n",
"tell you when to buy or sell. In autonomous driving systems, they can\n",
"anticipate car trajectories and help avoid accidents. More generally,\n",
"they can work on sequences of arbitrary lengths, rather than on\n",
"fixed-sized inputs like all the nets we have discussed so far. For\n",
"example, they can take sentences, documents, or audio samples as\n",
"input, making them extremely useful for natural language processing\n",
"systems such as automatic translation and speech-to-text.\n",
"\n",
"More to text to be added"
]
},
{
"cell_type": "markdown",
"id": "43cb4913",
"metadata": {
"editable": true
},
"source": [
"## A simple example"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "cf6b9dab",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"\n",
"# Start importing packages\n",
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import tensorflow as tf\n",
"from tensorflow.keras import datasets, layers, models\n",
"from tensorflow.keras.layers import Input\n",
"from tensorflow.keras.models import Model, Sequential \n",
"from tensorflow.keras.layers import Dense, SimpleRNN, LSTM, GRU\n",
"from tensorflow.keras import optimizers \n",
"from tensorflow.keras import regularizers \n",
"from tensorflow.keras.utils import to_categorical \n",
"\n",
"\n",
"\n",
"# convert into dataset matrix\n",
"def convertToMatrix(data, step):\n",
" X, Y =[], []\n",
" for i in range(len(data)-step):\n",
" d=i+step \n",
" X.append(data[i:d,])\n",
" Y.append(data[d,])\n",
" return np.array(X), np.array(Y)\n",
"\n",
"step = 4\n",
"N = 1000 \n",
"Tp = 800 \n",
"\n",
"t=np.arange(0,N)\n",
"x=np.sin(0.02*t)+2*np.random.rand(N)\n",
"df = pd.DataFrame(x)\n",
"df.head()\n",
"\n",
"plt.plot(df)\n",
"plt.show()\n",
"\n",
"values=df.values\n",
"train,test = values[0:Tp,:], values[Tp:N,:]\n",
"\n",
"# add step elements into train and test\n",
"test = np.append(test,np.repeat(test[-1,],step))\n",
"train = np.append(train,np.repeat(train[-1,],step))\n",
" \n",
"trainX,trainY =convertToMatrix(train,step)\n",
"testX,testY =convertToMatrix(test,step)\n",
"trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))\n",
"testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))\n",
"\n",
"model = Sequential()\n",
"model.add(SimpleRNN(units=32, input_shape=(1,step), activation=\"relu\"))\n",
"model.add(Dense(8, activation=\"relu\")) \n",
"model.add(Dense(1))\n",
"model.compile(loss='mean_squared_error', optimizer='rmsprop')\n",
"model.summary()\n",
"\n",
"model.fit(trainX,trainY, epochs=100, batch_size=16, verbose=2)\n",
"trainPredict = model.predict(trainX)\n",
"testPredict= model.predict(testX)\n",
"predicted=np.concatenate((trainPredict,testPredict),axis=0)\n",
"\n",
"trainScore = model.evaluate(trainX, trainY, verbose=0)\n",
"print(trainScore)\n",
"\n",
"index = df.index.values\n",
"plt.plot(index,df)\n",
"plt.plot(index,predicted)\n",
"plt.axvline(df.index[Tp], c=\"r\")\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "897a47c7",
"metadata": {
"editable": true
},
"source": [
"## An extrapolation example\n",
"\n",
"The following code provides an example of how recurrent neural\n",
"networks can be used to extrapolate to unknown values of physics data\n",
"sets. Specifically, the data sets used in this program come from\n",
"a quantum mechanical many-body calculation of energies as functions of the number of particles."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "6776ae2a",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"\n",
"# For matrices and calculations\n",
"import numpy as np\n",
"# For machine learning (backend for keras)\n",
"import tensorflow as tf\n",
"# User-friendly machine learning library\n",
"# Front end for TensorFlow\n",
"import tensorflow.keras\n",
"# Different methods from Keras needed to create an RNN\n",
"# This is not necessary but it shortened function calls \n",
"# that need to be used in the code.\n",
"from tensorflow.keras import datasets, layers, models\n",
"from tensorflow.keras.layers import Input\n",
"from tensorflow.keras import regularizers\n",
"from tensorflow.keras.models import Model, Sequential\n",
"from tensorflow.keras.layers import Dense, SimpleRNN, LSTM, GRU\n",
"# For timing the code\n",
"from timeit import default_timer as timer\n",
"# For plotting\n",
"import matplotlib.pyplot as plt\n",
"\n",
"\n",
"# The data set\n",
"datatype='VaryDimension'\n",
"X_tot = np.arange(2, 42, 2)\n",
"y_tot = np.array([-0.03077640549, -0.08336233266, -0.1446729567, -0.2116753732, -0.2830637392, -0.3581341341, -0.436462435, -0.5177783846,\n",
"\t-0.6019067271, -0.6887363571, -0.7782028952, -0.8702784034, -0.9649652536, -1.062292565, -1.16231451, \n",
"\t-1.265109911, -1.370782966, -1.479465113, -1.591317992, -1.70653767])"
]
},
{
"cell_type": "markdown",
"id": "35227d36",
"metadata": {
"editable": true
},
"source": [
"The way the recurrent neural networks are trained in this program\n",
"differs from how machine learning algorithms are usually trained.\n",
"Typically a machine learning algorithm is trained by learning the\n",
"relationship between the x data and the y data. In this program, the\n",
"recurrent neural network will be trained to recognize the relationship\n",
"in a sequence of y values. This is type of data formatting is\n",
"typically used time series forcasting, but it can also be used in any\n",
"extrapolation (time series forecasting is just a specific type of\n",
"extrapolation along the time axis). This method of data formatting\n",
"does not use the x data and assumes that the y data are evenly spaced.\n",
"\n",
"For a standard machine learning algorithm, the training data has the\n",
"form of (x,y) so the machine learning algorithm learns to assiciate a\n",
"y value with a given x value. This is useful when the test data has x\n",
"values within the same range as the training data. However, for this\n",
"application, the x values of the test data are outside of the x values\n",
"of the training data and the traditional method of training a machine\n",
"learning algorithm does not work as well. For this reason, the\n",
"recurrent neural network is trained on sequences of y values of the\n",
"form ((y1, y2), y3), so that the network is concerned with learning\n",
"the pattern of the y data and not the relation between the x and y\n",
"data. As long as the pattern of y data outside of the training region\n",
"stays relatively stable compared to what was inside the training\n",
"region, this method of training can produce accurate extrapolations to\n",
"y values far removed from the training data set."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7dc577d1",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"# FORMAT_DATA\n",
"def format_data(data, length_of_sequence = 2): \n",
" \"\"\"\n",
" Inputs:\n",
" data(a numpy array): the data that will be the inputs to the recurrent neural\n",
" network\n",
" length_of_sequence (an int): the number of elements in one iteration of the\n",
" sequence patter. For a function approximator use length_of_sequence = 2.\n",
" Returns:\n",
" rnn_input (a 3D numpy array): the input data for the recurrent neural network. Its\n",
" dimensions are length of data - length of sequence, length of sequence, \n",
" dimnsion of data\n",
" rnn_output (a numpy array): the training data for the neural network\n",
" Formats data to be used in a recurrent neural network.\n",
" \"\"\"\n",
"\n",
" X, Y = [], []\n",
" for i in range(len(data)-length_of_sequence):\n",
" # Get the next length_of_sequence elements\n",
" a = data[i:i+length_of_sequence]\n",
" # Get the element that immediately follows that\n",
" b = data[i+length_of_sequence]\n",
" # Reshape so that each data point is contained in its own array\n",
" a = np.reshape (a, (len(a), 1))\n",
" X.append(a)\n",
" Y.append(b)\n",
" rnn_input = np.array(X)\n",
" rnn_output = np.array(Y)\n",
"\n",
" return rnn_input, rnn_output\n",
"\n",
"\n",
"# ## Defining the Recurrent Neural Network Using Keras\n",
"# \n",
"# The following method defines a simple recurrent neural network in keras consisting of one input layer, one hidden layer, and one output layer.\n",
"\n",
"def rnn(length_of_sequences, batch_size = None, stateful = False):\n",
" \"\"\"\n",
" Inputs:\n",
" length_of_sequences (an int): the number of y values in \"x data\". This is determined\n",
" when the data is formatted\n",
" batch_size (an int): Default value is None. See Keras documentation of SimpleRNN.\n",
" stateful (a boolean): Default value is False. See Keras documentation of SimpleRNN.\n",
" Returns:\n",
" model (a Keras model): The recurrent neural network that is built and compiled by this\n",
" method\n",
" Builds and compiles a recurrent neural network with one hidden layer and returns the model.\n",
" \"\"\"\n",
" # Number of neurons in the input and output layers\n",
" in_out_neurons = 1\n",
" # Number of neurons in the hidden layer\n",
" hidden_neurons = 200\n",
" # Define the input layer\n",
" inp = Input(batch_shape=(batch_size, \n",
" length_of_sequences, \n",
" in_out_neurons)) \n",
" # Define the hidden layer as a simple RNN layer with a set number of neurons and add it to \n",
" # the network immediately after the input layer\n",
" rnn = SimpleRNN(hidden_neurons, \n",
" return_sequences=False,\n",
" stateful = stateful,\n",
" name=\"RNN\")(inp)\n",
" # Define the output layer as a dense neural network layer (standard neural network layer)\n",
" #and add it to the network immediately after the hidden layer.\n",
" dens = Dense(in_out_neurons,name=\"dense\")(rnn)\n",
" # Create the machine learning model starting with the input layer and ending with the \n",
" # output layer\n",
" model = Model(inputs=[inp],outputs=[dens])\n",
" # Compile the machine learning model using the mean squared error function as the loss \n",
" # function and an Adams optimizer.\n",
" model.compile(loss=\"mean_squared_error\", optimizer=\"adam\") \n",
" return model"
]
},
{
"cell_type": "markdown",
"id": "1978b6a1",
"metadata": {
"editable": true
},
"source": [
"## Predicting New Points With A Trained Recurrent Neural Network"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "d4ad417a",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"def test_rnn (x1, y_test, plot_min, plot_max):\n",
" \"\"\"\n",
" Inputs:\n",
" x1 (a list or numpy array): The complete x component of the data set\n",
" y_test (a list or numpy array): The complete y component of the data set\n",
" plot_min (an int or float): the smallest x value used in the training data\n",
" plot_max (an int or float): the largest x valye used in the training data\n",
" Returns:\n",
" None.\n",
" Uses a trained recurrent neural network model to predict future points in the \n",
" series. Computes the MSE of the predicted data set from the true data set, saves\n",
" the predicted data set to a csv file, and plots the predicted and true data sets w\n",
" while also displaying the data range used for training.\n",
" \"\"\"\n",
" # Add the training data as the first dim points in the predicted data array as these\n",
" # are known values.\n",
" y_pred = y_test[:dim].tolist()\n",
" # Generate the first input to the trained recurrent neural network using the last two \n",
" # points of the training data. Based on how the network was trained this means that it\n",
" # will predict the first point in the data set after the training data. All of the \n",
" # brackets are necessary for Tensorflow.\n",
" next_input = np.array([[[y_test[dim-2]], [y_test[dim-1]]]])\n",
" # Save the very last point in the training data set. This will be used later.\n",
" last = [y_test[dim-1]]\n",
"\n",
" # Iterate until the complete data set is created.\n",
" for i in range (dim, len(y_test)):\n",
" # Predict the next point in the data set using the previous two points.\n",
" next = model.predict(next_input)\n",
" # Append just the number of the predicted data set\n",
" y_pred.append(next[0][0])\n",
" # Create the input that will be used to predict the next data point in the data set.\n",
" next_input = np.array([[last, next[0]]], dtype=np.float64)\n",
" last = next\n",
"\n",
" # Print the mean squared error between the known data set and the predicted data set.\n",
" print('MSE: ', np.square(np.subtract(y_test, y_pred)).mean())\n",
" # Save the predicted data set as a csv file for later use\n",
" name = datatype + 'Predicted'+str(dim)+'.csv'\n",
" np.savetxt(name, y_pred, delimiter=',')\n",
" # Plot the known data set and the predicted data set. The red box represents the region that was used\n",
" # for the training data.\n",
" fig, ax = plt.subplots()\n",
" ax.plot(x1, y_test, label=\"true\", linewidth=3)\n",
" ax.plot(x1, y_pred, 'g-.',label=\"predicted\", linewidth=4)\n",
" ax.legend()\n",
" # Created a red region to represent the points used in the training data.\n",
" ax.axvspan(plot_min, plot_max, alpha=0.25, color='red')\n",
" plt.show()\n",
"\n",
"# Check to make sure the data set is complete\n",
"assert len(X_tot) == len(y_tot)\n",
"\n",
"# This is the number of points that will be used in as the training data\n",
"dim=12\n",
"\n",
"# Separate the training data from the whole data set\n",
"X_train = X_tot[:dim]\n",
"y_train = y_tot[:dim]\n",
"\n",
"\n",
"# Generate the training data for the RNN, using a sequence of 2\n",
"rnn_input, rnn_training = format_data(y_train, 2)\n",
"\n",
"\n",
"# Create a recurrent neural network in Keras and produce a summary of the \n",
"# machine learning model\n",
"model = rnn(length_of_sequences = rnn_input.shape[1])\n",
"model.summary()\n",
"\n",
"# Start the timer. Want to time training+testing\n",
"start = timer()\n",
"# Fit the model using the training data genenerated above using 150 training iterations and a 5%\n",
"# validation split. Setting verbose to True prints information about each training iteration.\n",
"hist = model.fit(rnn_input, rnn_training, batch_size=None, epochs=150, \n",
" verbose=True,validation_split=0.05)\n",
"\n",
"for label in [\"loss\",\"val_loss\"]:\n",
" plt.plot(hist.history[label],label=label)\n",
"\n",
"plt.ylabel(\"loss\")\n",
"plt.xlabel(\"epoch\")\n",
"plt.title(\"The final validation loss: {}\".format(hist.history[\"val_loss\"][-1]))\n",
"plt.legend()\n",
"plt.show()\n",
"\n",
"# Use the trained neural network to predict more points of the data set\n",
"test_rnn(X_tot, y_tot, X_tot[0], X_tot[dim-1])\n",
"# Stop the timer and calculate the total time needed.\n",
"end = timer()\n",
"print('Time: ', end-start)"
]
},
{
"cell_type": "markdown",
"id": "e2cad4fc",
"metadata": {
"editable": true
},
"source": [
"Changing the size of the recurrent neural network and its parameters\n",
"can drastically change the results you get from the model. The below\n",
"code takes the simple recurrent neural network from above and adds a\n",
"second hidden layer, changes the number of neurons in the hidden\n",
"layer, and explicitly declares the activation function of the hidden\n",
"layers to be a sigmoid function. The loss function and optimizer can\n",
"also be changed but are kept the same as the above network. These\n",
"parameters can be tuned to provide the optimal result from the\n",
"network. For some ideas on how to improve the performance of a\n",
"[recurrent neural network](https://danijar.com/tips-for-training-recurrent-neural-networks)."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c39f1516",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"def rnn_2layers(length_of_sequences, batch_size = None, stateful = False):\n",
" \"\"\"\n",
" Inputs:\n",
" length_of_sequences (an int): the number of y values in \"x data\". This is determined\n",
" when the data is formatted\n",
" batch_size (an int): Default value is None. See Keras documentation of SimpleRNN.\n",
" stateful (a boolean): Default value is False. See Keras documentation of SimpleRNN.\n",
" Returns:\n",
" model (a Keras model): The recurrent neural network that is built and compiled by this\n",
" method\n",
" Builds and compiles a recurrent neural network with two hidden layers and returns the model.\n",
" \"\"\"\n",
" # Number of neurons in the input and output layers\n",
" in_out_neurons = 1\n",
" # Number of neurons in the hidden layer, increased from the first network\n",
" hidden_neurons = 500\n",
" # Define the input layer\n",
" inp = Input(batch_shape=(batch_size, \n",
" length_of_sequences, \n",
" in_out_neurons)) \n",
" # Create two hidden layers instead of one hidden layer. Explicitly set the activation\n",
" # function to be the sigmoid function (the default value is hyperbolic tangent)\n",
" rnn1 = SimpleRNN(hidden_neurons, \n",
" return_sequences=True, # This needs to be True if another hidden layer is to follow\n",
" stateful = stateful, activation = 'sigmoid',\n",
" name=\"RNN1\")(inp)\n",
" rnn2 = SimpleRNN(hidden_neurons, \n",
" return_sequences=False, activation = 'sigmoid',\n",
" stateful = stateful,\n",
" name=\"RNN2\")(rnn1)\n",
" # Define the output layer as a dense neural network layer (standard neural network layer)\n",
" #and add it to the network immediately after the hidden layer.\n",
" dens = Dense(in_out_neurons,name=\"dense\")(rnn2)\n",
" # Create the machine learning model starting with the input layer and ending with the \n",
" # output layer\n",
" model = Model(inputs=[inp],outputs=[dens])\n",
" # Compile the machine learning model using the mean squared error function as the loss \n",
" # function and an Adams optimizer.\n",
" model.compile(loss=\"mean_squared_error\", optimizer=\"adam\") \n",
" return model\n",
"\n",
"# Check to make sure the data set is complete\n",
"assert len(X_tot) == len(y_tot)\n",
"\n",
"# This is the number of points that will be used in as the training data\n",
"dim=12\n",
"\n",
"# Separate the training data from the whole data set\n",
"X_train = X_tot[:dim]\n",
"y_train = y_tot[:dim]\n",
"\n",
"\n",
"# Generate the training data for the RNN, using a sequence of 2\n",
"rnn_input, rnn_training = format_data(y_train, 2)\n",
"\n",
"\n",
"# Create a recurrent neural network in Keras and produce a summary of the \n",
"# machine learning model\n",
"model = rnn_2layers(length_of_sequences = 2)\n",
"model.summary()\n",
"\n",
"# Start the timer. Want to time training+testing\n",
"start = timer()\n",
"# Fit the model using the training data genenerated above using 150 training iterations and a 5%\n",
"# validation split. Setting verbose to True prints information about each training iteration.\n",
"hist = model.fit(rnn_input, rnn_training, batch_size=None, epochs=150, \n",
" verbose=True,validation_split=0.05)\n",
"\n",
"\n",
"# This section plots the training loss and the validation loss as a function of training iteration.\n",
"# This is not required for analyzing the couple cluster data but can help determine if the network is\n",
"# being overtrained.\n",
"for label in [\"loss\",\"val_loss\"]:\n",
" plt.plot(hist.history[label],label=label)\n",
"\n",
"plt.ylabel(\"loss\")\n",
"plt.xlabel(\"epoch\")\n",
"plt.title(\"The final validation loss: {}\".format(hist.history[\"val_loss\"][-1]))\n",
"plt.legend()\n",
"plt.show()\n",
"\n",
"# Use the trained neural network to predict more points of the data set\n",
"test_rnn(X_tot, y_tot, X_tot[0], X_tot[dim-1])\n",
"# Stop the timer and calculate the total time needed.\n",
"end = timer()\n",
"print('Time: ', end-start)"
]
},
{
"cell_type": "markdown",
"id": "842c7602",
"metadata": {
"editable": true
},
"source": [
"## Other Types of Recurrent Neural Networks\n",
"\n",
"Besides a simple recurrent neural network layer, there are two other\n",
"commonly used types of recurrent neural network layers: Long Short\n",
"Term Memory (LSTM) and Gated Recurrent Unit (GRU). For a short\n",
"introduction to these layers see <https://medium.com/mindboard/lstm-vs-gru-experimental-comparison-955820c21e8b>\n",
"and <https://medium.com/mindboard/lstm-vs-gru-experimental-comparison-955820c21e8b>.\n",
"\n",
"The first network created below is similar to the previous network,\n",
"but it replaces the SimpleRNN layers with LSTM layers. The second\n",
"network below has two hidden layers made up of GRUs, which are\n",
"preceeded by two dense (feeddorward) neural network layers. These\n",
"dense layers \"preprocess\" the data before it reaches the recurrent\n",
"layers. This architecture has been shown to improve the performance\n",
"of recurrent neural networks (see the link above and also\n",
"<https://arxiv.org/pdf/1807.02857.pdf>."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "6f0e9b62",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"def lstm_2layers(length_of_sequences, batch_size = None, stateful = False):\n",
" \"\"\"\n",
" Inputs:\n",
" length_of_sequences (an int): the number of y values in \"x data\". This is determined\n",
" when the data is formatted\n",
" batch_size (an int): Default value is None. See Keras documentation of SimpleRNN.\n",
" stateful (a boolean): Default value is False. See Keras documentation of SimpleRNN.\n",
" Returns:\n",
" model (a Keras model): The recurrent neural network that is built and compiled by this\n",
" method\n",
" Builds and compiles a recurrent neural network with two LSTM hidden layers and returns the model.\n",
" \"\"\"\n",
" # Number of neurons on the input/output layer and the number of neurons in the hidden layer\n",
" in_out_neurons = 1\n",
" hidden_neurons = 250\n",
" # Input Layer\n",
" inp = Input(batch_shape=(batch_size, \n",
" length_of_sequences, \n",
" in_out_neurons)) \n",
" # Hidden layers (in this case they are LSTM layers instead if SimpleRNN layers)\n",
" rnn= LSTM(hidden_neurons, \n",
" return_sequences=True,\n",
" stateful = stateful,\n",
" name=\"RNN\", use_bias=True, activation='tanh')(inp)\n",
" rnn1 = LSTM(hidden_neurons, \n",
" return_sequences=False,\n",
" stateful = stateful,\n",
" name=\"RNN1\", use_bias=True, activation='tanh')(rnn)\n",
" # Output layer\n",
" dens = Dense(in_out_neurons,name=\"dense\")(rnn1)\n",
" # Define the midel\n",
" model = Model(inputs=[inp],outputs=[dens])\n",
" # Compile the model\n",
" model.compile(loss='mean_squared_error', optimizer='adam') \n",
" # Return the model\n",
" return model\n",
"\n",
"def dnn2_gru2(length_of_sequences, batch_size = None, stateful = False):\n",
" \"\"\"\n",
" Inputs:\n",
" length_of_sequences (an int): the number of y values in \"x data\". This is determined\n",
" when the data is formatted\n",
" batch_size (an int): Default value is None. See Keras documentation of SimpleRNN.\n",
" stateful (a boolean): Default value is False. See Keras documentation of SimpleRNN.\n",
" Returns:\n",
" model (a Keras model): The recurrent neural network that is built and compiled by this\n",
" method\n",
" Builds and compiles a recurrent neural network with four hidden layers (two dense followed by\n",
" two GRU layers) and returns the model.\n",
" \"\"\" \n",
" # Number of neurons on the input/output layers and hidden layers\n",
" in_out_neurons = 1\n",
" hidden_neurons = 250\n",
" # Input layer\n",
" inp = Input(batch_shape=(batch_size, \n",
" length_of_sequences, \n",
" in_out_neurons)) \n",
" # Hidden Dense (feedforward) layers\n",
" dnn = Dense(hidden_neurons/2, activation='relu', name='dnn')(inp)\n",
" dnn1 = Dense(hidden_neurons/2, activation='relu', name='dnn1')(dnn)\n",
" # Hidden GRU layers\n",
" rnn1 = GRU(hidden_neurons, \n",
" return_sequences=True,\n",
" stateful = stateful,\n",
" name=\"RNN1\", use_bias=True)(dnn1)\n",
" rnn = GRU(hidden_neurons, \n",
" return_sequences=False,\n",
" stateful = stateful,\n",
" name=\"RNN\", use_bias=True)(rnn1)\n",
" # Output layer\n",
" dens = Dense(in_out_neurons,name=\"dense\")(rnn)\n",
" # Define the model\n",
" model = Model(inputs=[inp],outputs=[dens])\n",
" # Compile the mdoel\n",
" model.compile(loss='mean_squared_error', optimizer='adam') \n",
" # Return the model\n",
" return model\n",
"\n",
"# Check to make sure the data set is complete\n",
"assert len(X_tot) == len(y_tot)\n",
"\n",
"# This is the number of points that will be used in as the training data\n",
"dim=12\n",
"\n",
"# Separate the training data from the whole data set\n",
"X_train = X_tot[:dim]\n",
"y_train = y_tot[:dim]\n",
"\n",
"\n",
"# Generate the training data for the RNN, using a sequence of 2\n",
"rnn_input, rnn_training = format_data(y_train, 2)\n",
"\n",
"\n",
"# Create a recurrent neural network in Keras and produce a summary of the \n",
"# machine learning model\n",
"# Change the method name to reflect which network you want to use\n",
"model = dnn2_gru2(length_of_sequences = 2)\n",
"model.summary()\n",
"\n",
"# Start the timer. Want to time training+testing\n",
"start = timer()\n",
"# Fit the model using the training data genenerated above using 150 training iterations and a 5%\n",
"# validation split. Setting verbose to True prints information about each training iteration.\n",
"hist = model.fit(rnn_input, rnn_training, batch_size=None, epochs=150, \n",
" verbose=True,validation_split=0.05)\n",
"\n",
"\n",
"# This section plots the training loss and the validation loss as a function of training iteration.\n",
"# This is not required for analyzing the couple cluster data but can help determine if the network is\n",
"# being overtrained.\n",
"for label in [\"loss\",\"val_loss\"]:\n",
" plt.plot(hist.history[label],label=label)\n",
"\n",
"plt.ylabel(\"loss\")\n",
"plt.xlabel(\"epoch\")\n",
"plt.title(\"The final validation loss: {}\".format(hist.history[\"val_loss\"][-1]))\n",
"plt.legend()\n",
"plt.show()\n",
"\n",
"# Use the trained neural network to predict more points of the data set\n",
"test_rnn(X_tot, y_tot, X_tot[0], X_tot[dim-1])\n",
"# Stop the timer and calculate the total time needed.\n",
"end = timer()\n",
"print('Time: ', end-start)\n",
"\n",
"\n",
"# ### Training Recurrent Neural Networks in the Standard Way (i.e. learning the relationship between the X and Y data)\n",
"# \n",
"# 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.\n",
"\n",
"# Check to make sure the data set is complete\n",
"assert len(X_tot) == len(y_tot)\n",
"\n",
"# This is the number of points that will be used in as the training data\n",
"dim=12\n",
"\n",
"# Separate the training data from the whole data set\n",
"X_train = X_tot[:dim]\n",
"y_train = y_tot[:dim]\n",
"\n",
"# Reshape the data for Keras specifications\n",
"X_train = X_train.reshape((dim, 1))\n",
"y_train = y_train.reshape((dim, 1))\n",
"\n",
"\n",
"# Create a recurrent neural network in Keras and produce a summary of the \n",
"# machine learning model\n",
"# Set the sequence length to 1 for regular data formatting \n",
"model = rnn(length_of_sequences = 1)\n",
"model.summary()\n",
"\n",
"# Start the timer. Want to time training+testing\n",
"start = timer()\n",
"# Fit the model using the training data genenerated above using 150 training iterations and a 5%\n",
"# validation split. Setting verbose to True prints information about each training iteration.\n",
"hist = model.fit(X_train, y_train, batch_size=None, epochs=150, \n",
" verbose=True,validation_split=0.05)\n",
"\n",
"\n",
"# This section plots the training loss and the validation loss as a function of training iteration.\n",
"# This is not required for analyzing the couple cluster data but can help determine if the network is\n",
"# being overtrained.\n",
"for label in [\"loss\",\"val_loss\"]:\n",
" plt.plot(hist.history[label],label=label)\n",
"\n",
"plt.ylabel(\"loss\")\n",
"plt.xlabel(\"epoch\")\n",
"plt.title(\"The final validation loss: {}\".format(hist.history[\"val_loss\"][-1]))\n",
"plt.legend()\n",
"plt.show()\n",
"\n",
"# Use the trained neural network to predict the remaining data points\n",
"X_pred = X_tot[dim:]\n",
"X_pred = X_pred.reshape((len(X_pred), 1))\n",
"y_model = model.predict(X_pred)\n",
"y_pred = np.concatenate((y_tot[:dim], y_model.flatten()))\n",
"\n",
"# Plot the known data set and the predicted data set. The red box represents the region that was used\n",
"# for the training data.\n",
"fig, ax = plt.subplots()\n",
"ax.plot(X_tot, y_tot, label=\"true\", linewidth=3)\n",
"ax.plot(X_tot, y_pred, 'g-.',label=\"predicted\", linewidth=4)\n",
"ax.legend()\n",
"# Created a red region to represent the points used in the training data.\n",
"ax.axvspan(X_tot[0], X_tot[dim], alpha=0.25, color='red')\n",
"plt.show()\n",
"\n",
"# Stop the timer and calculate the total time needed.\n",
"end = timer()\n",
"print('Time: ', end-start)"
]
},
{
"cell_type": "markdown",
"id": "0752ba7f",
"metadata": {
"editable": true
},
"source": [
"# Generative Models\n",
"\n",
"**Generative models** describe a class of statistical models that are a contrast\n",
"to **discriminative models**. Informally we say that generative models can\n",
"generate new data instances while discriminative models discriminate between\n",
"different kinds of data instances. A generative model could generate new photos\n",
"of animals that look like 'real' animals while a discriminative model could tell\n",
"a dog from a cat. More formally, given a data set $x$ and a set of labels /\n",
"targets $y$. Generative models capture the joint probability $p(x, y)$, or\n",
"just $p(x)$ if there are no labels, while discriminative models capture the\n",
"conditional probability $p(y | x)$. Discriminative models generally try to draw\n",
"boundaries in the data space (often high dimensional), while generative models\n",
"try to model how data is placed throughout the space.\n",
"\n",
"**Note**: this material is thanks to Linus Ekstrøm."
]
},
{
"cell_type": "markdown",
"id": "784138f8",
"metadata": {
"editable": true
},
"source": [
"## Generative Adversarial Networks\n",
"\n",
"**Generative Adversarial Networks** are a type of unsupervised machine learning\n",
"algorithm proposed by [Goodfellow et. al](https://arxiv.org/pdf/1406.2661.pdf)\n",
"in 2014 (short and good article).\n",
"\n",
"The simplest formulation of\n",
"the model is based on a game theoretic approach, *zero sum game*, where we pit\n",
"two neural networks against one another. We define two rival networks, one\n",
"generator $g$, and one discriminator $d$. The generator directly produces\n",
"samples"
]
},
{
"cell_type": "markdown",
"id": "a42f89ee",
"metadata": {
"editable": true
},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto1\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
" x = g(z; \\theta^{(g)})\n",
"\\label{_auto1} \\tag{1}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "abe7212f",
"metadata": {
"editable": true
},
"source": [
"The discriminator attempts to distinguish between samples drawn from the\n",
"training data and samples drawn from the generator. In other words, it tries to\n",
"tell the difference between the fake data produced by $g$ and the actual data\n",
"samples we want to do prediction on. The discriminator outputs a probability\n",
"value given by"
]
},
{
"cell_type": "markdown",
"id": "0821eaee",
"metadata": {
"editable": true
},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto2\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
" d(x; \\theta^{(d)})\n",
"\\label{_auto2} \\tag{2}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "55e0ccf6",
"metadata": {
"editable": true
},
"source": [
"indicating the probability that $x$ is a real training example rather than a\n",
"fake sample the generator has generated. The simplest way to formulate the\n",
"learning process in a generative adversarial network is a zero-sum game, in\n",
"which a function"
]
},
{
"cell_type": "markdown",
"id": "f37ece14",
"metadata": {
"editable": true
},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto3\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
" v(\\theta^{(g)}, \\theta^{(d)})\n",
"\\label{_auto3} \\tag{3}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "d6d6d5fa",
"metadata": {
"editable": true
},
"source": [
"determines the reward for the discriminator, while the generator gets the\n",
"conjugate reward"
]
},
{
"cell_type": "markdown",
"id": "3c74b45c",
"metadata": {
"editable": true
},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto4\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
" -v(\\theta^{(g)}, \\theta^{(d)})\n",
"\\label{_auto4} \\tag{4}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "605ac8c9",
"metadata": {
"editable": true
},
"source": [
"During learning both of the networks maximize their own reward function, so that\n",
"the generator gets better and better at tricking the discriminator, while the\n",
"discriminator gets better and better at telling the difference between the fake\n",
"and real data. The generator and discriminator alternate on which one trains at\n",
"one time (i.e. for one epoch). In other words, we keep the generator constant\n",
"and train the discriminator, then we keep the discriminator constant to train\n",
"the generator and repeat. It is this back and forth dynamic which lets GANs\n",
"tackle otherwise intractable generative problems. As the generator improves with\n",
" training, the discriminator's performance gets worse because it cannot easily\n",
" tell the difference between real and fake. If the generator ends up succeeding\n",
" perfectly, the the discriminator will do no better than random guessing i.e.\n",
" 50\\%. This progression in the training poses a problem for the convergence\n",
" criteria for GANs. The discriminator feedback gets less meaningful over time,\n",
" if we continue training after this point then the generator is effectively\n",
" training on junk data which can undo the learning up to that point. Therefore,\n",
" we stop training when the discriminator starts outputting $1/2$ everywhere.\n",
"\n",
"At convergence we have"
]
},
{
"cell_type": "markdown",
"id": "cfec7462",
"metadata": {
"editable": true
},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto5\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
" g^* = \\underset{g}{\\mathrm{argmin}}\\hspace{2pt}\n",
" \\underset{d}{\\mathrm{max}}v(\\theta^{(g)}, \\theta^{(d)})\n",
"\\label{_auto5} \\tag{5}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "37c65d4c",
"metadata": {
"editable": true
},
"source": [
"The default choice for $v$ is"
]
},
{
"cell_type": "markdown",
"id": "c868a092",
"metadata": {
"editable": true
},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto6\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
" v(\\theta^{(g)}, \\theta^{(d)}) = \\mathbb{E}_{x\\sim p_\\mathrm{data}}\\log d(x)\n",
" + \\mathbb{E}_{x\\sim p_\\mathrm{model}}\n",
" \\log (1 - d(x))\n",
"\\label{_auto6} \\tag{6}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "ad465af3",
"metadata": {
"editable": true
},
"source": [
"The main motivation for the design of GANs is that the learning process requires\n",
"neither approximate inference (variational autoencoders for example) nor\n",
"approximation of a partition function. In the case where"
]
},
{
"cell_type": "markdown",
"id": "27858a4e",
"metadata": {
"editable": true
},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto7\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
" \\underset{d}{\\mathrm{max}}v(\\theta^{(g)}, \\theta^{(d)})\n",
"\\label{_auto7} \\tag{7}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "86006023",
"metadata": {
"editable": true
},
"source": [
"is convex in $\\theta^{(g)} then the procedure is guaranteed to converge and is\n",
"asymptotically consistent\n",
"( [Seth Lloyd on QuGANs](https://arxiv.org/pdf/1804.09139.pdf) ).\n",
"\n",
"This is in\n",
"general not the case and it is possible to get situations where the training\n",
"process never converges because the generator and discriminator chase one\n",
"another around in the parameter space indefinitely. A much deeper discussion on\n",
"the currently open research problem of GAN convergence is available\n",
"[here](https://www.deeplearningbook.org/contents/generative_models.html). To\n",
"anyone interested in learning more about GANs it is a highly recommended read.\n",
"Direct quote: \"In this best-performing formulation, the generator aims to\n",
"increase the log probability that the discriminator makes a mistake, rather than\n",
"aiming to decrease the log probability that the discriminator makes the correct\n",
"prediction.\" [Another interesting read](https://arxiv.org/abs/1701.00160)"
]
},
{
"cell_type": "markdown",
"id": "2fee38bd",
"metadata": {
"editable": true
},
"source": [
"## Writing Our First Generative Adversarial Network\n",
"Let us now move on to actually implementing a GAN in tensorflow. We will study\n",
"the performance of our GAN on the MNIST dataset. This code is based on and\n",
"adapted from the\n",
"[google tutorial](https://www.tensorflow.org/tutorials/generative/dcgan)\n",
"\n",
"First we import our libraries"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "004a0b53",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"import os\n",
"import time\n",
"import numpy as np\n",
"import tensorflow as tf\n",
"import matplotlib.pyplot as plt\n",
"from tensorflow.keras import layers\n",
"from tensorflow.keras.utils import plot_model"
]
},
{
"cell_type": "markdown",
"id": "353af161",
"metadata": {
"editable": true
},
"source": [
"Next we define our hyperparameters and import our data the usual way"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "8cbaf16a",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"BUFFER_SIZE = 60000\n",
"BATCH_SIZE = 256\n",
"EPOCHS = 30\n",
"\n",
"data = tf.keras.datasets.mnist.load_data()\n",
"(train_images, train_labels), (test_images, test_labels) = data\n",
"train_images = np.reshape(train_images, (train_images.shape[0],\n",
" 28,\n",
" 28,\n",
" 1)).astype('float32')\n",
"\n",
"# we normalize between -1 and 1\n",
"train_images = (train_images - 127.5) / 127.5\n",
"training_dataset = tf.data.Dataset.from_tensor_slices(\n",
" train_images).shuffle(BUFFER_SIZE).batch(BATCH_SIZE)"
]
},
{
"cell_type": "markdown",
"id": "822b8cc7",
"metadata": {
"editable": true
},
"source": [
"### MNIST and GANs\n",
"\n",
"Let's have a quick look"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "52b5965c",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"plt.imshow(train_images[0], cmap='Greys')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "21c5199c",
"metadata": {
"editable": true
},
"source": [
"Now we define our two models. This is where the 'magic' happens. There are a\n",
"huge amount of possible formulations for both models. A lot of engineering and\n",
"trial and error can be done here to try to produce better performing models. For\n",
"more advanced GANs this is by far the step where you can 'make or break' a\n",
"model.\n",
"\n",
"We start with the generator. As stated in the introductory text the generator\n",
"$g$ upsamples from a random sample to the shape of what we want to predict. In\n",
"our case we are trying to predict MNIST images ($28\\times 28$ pixels)."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "356759c7",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"def generator_model():\n",
" \"\"\"\n",
" The generator uses upsampling layers tf.keras.layers.Conv2DTranspose() to\n",
" produce an image from a random seed. We start with a Dense layer taking this\n",
" random sample as an input and subsequently upsample through multiple\n",
" convolutional layers.\n",
" \"\"\"\n",
"\n",
" # we define our model\n",
" model = tf.keras.Sequential()\n",
"\n",
"\n",
" # adding our input layer. Dense means that every neuron is connected and\n",
" # the input shape is the shape of our random noise. The units need to match\n",
" # in some sense the upsampling strides to reach our desired output shape.\n",
" # we are using 100 random numbers as our seed\n",
" model.add(layers.Dense(units=7*7*BATCH_SIZE,\n",
" use_bias=False,\n",
" input_shape=(100, )))\n",
" # we normalize the output form the Dense layer\n",
" model.add(layers.BatchNormalization())\n",
" # and add an activation function to our 'layer'. LeakyReLU avoids vanishing\n",
" # gradient problem\n",
" model.add(layers.LeakyReLU())\n",
" model.add(layers.Reshape((7, 7, BATCH_SIZE)))\n",
" assert model.output_shape == (None, 7, 7, BATCH_SIZE)\n",
" # even though we just added four keras layers we think of everything above\n",
" # as 'one' layer\n",
"\n",
" # next we add our upscaling convolutional layers\n",
" model.add(layers.Conv2DTranspose(filters=128,\n",
" kernel_size=(5, 5),\n",
" strides=(1, 1),\n",
" padding='same',\n",
" use_bias=False))\n",
" model.add(layers.BatchNormalization())\n",
" model.add(layers.LeakyReLU())\n",
" assert model.output_shape == (None, 7, 7, 128)\n",
"\n",
" model.add(layers.Conv2DTranspose(filters=64,\n",
" kernel_size=(5, 5),\n",
" strides=(2, 2),\n",
" padding='same',\n",
" use_bias=False))\n",
" model.add(layers.BatchNormalization())\n",
" model.add(layers.LeakyReLU())\n",
" assert model.output_shape == (None, 14, 14, 64)\n",
"\n",
" model.add(layers.Conv2DTranspose(filters=1,\n",
" kernel_size=(5, 5),\n",
" strides=(2, 2),\n",
" padding='same',\n",
" use_bias=False,\n",
" activation='tanh'))\n",
" assert model.output_shape == (None, 28, 28, 1)\n",
"\n",
" return model"
]
},
{
"cell_type": "markdown",
"id": "854bcd6b",
"metadata": {
"editable": true
},
"source": [
"And there we have our 'simple' generator model. Now we move on to defining our\n",
"discriminator model $d$, which is a convolutional neural network based image\n",
"classifier."
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "41473304",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"def discriminator_model():\n",
" \"\"\"\n",
" The discriminator is a convolutional neural network based image classifier\n",
" \"\"\"\n",
"\n",
" # we define our model\n",
" model = tf.keras.Sequential()\n",
" model.add(layers.Conv2D(filters=64,\n",
" kernel_size=(5, 5),\n",
" strides=(2, 2),\n",
" padding='same',\n",
" input_shape=[28, 28, 1]))\n",
" model.add(layers.LeakyReLU())\n",
" # adding a dropout layer as you do in conv-nets\n",
" model.add(layers.Dropout(0.3))\n",
"\n",
"\n",
" model.add(layers.Conv2D(filters=128,\n",
" kernel_size=(5, 5),\n",
" strides=(2, 2),\n",
" padding='same'))\n",
" model.add(layers.LeakyReLU())\n",
" # adding a dropout layer as you do in conv-nets\n",
" model.add(layers.Dropout(0.3))\n",
"\n",
" model.add(layers.Flatten())\n",
" model.add(layers.Dense(1))\n",
"\n",
" return model"
]
},
{
"cell_type": "markdown",
"id": "353af567",
"metadata": {
"editable": true
},
"source": [
"Let us take a look at our models."
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "f899d4e3",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"generator = generator_model()\n",
"plot_model(generator, show_shapes=True, rankdir='LR')"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "87ef384b",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"discriminator = discriminator_model()\n",
"plot_model(discriminator, show_shapes=True, rankdir='LR')"
]
},
{
"cell_type": "markdown",
"id": "b2bef82d",
"metadata": {
"editable": true
},
"source": [
"Next we need a few helper objects we will use in training"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "e397847a",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=True)\n",
"generator_optimizer = tf.keras.optimizers.Adam(1e-4)\n",
"discriminator_optimizer = tf.keras.optimizers.Adam(1e-4)"
]
},
{
"cell_type": "markdown",
"id": "db3396cc",
"metadata": {
"editable": true
},
"source": [
"The first object, *cross_entropy* is our loss function and the two others are\n",
"our optimizers. Notice we use the same learning rate for both $g$ and $d$. This\n",
"is because they need to improve their accuracy at approximately equal speeds to\n",
"get convergence (not necessarily exactly equal). Now we define our loss\n",
"functions"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "931eaced",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"def generator_loss(fake_output):\n",
" loss = cross_entropy(tf.ones_like(fake_output), fake_output)\n",
"\n",
" return loss"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "0c4a44bb",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"def discriminator_loss(real_output, fake_output):\n",
" real_loss = cross_entropy(tf.ones_like(real_output), real_output)\n",
" fake_loss = cross_entropy(tf.zeros_liks(fake_output), fake_output)\n",
" total_loss = real_loss + fake_loss\n",
"\n",
" return total_loss"
]
},
{
"cell_type": "markdown",
"id": "fcf8f066",
"metadata": {
"editable": true
},
"source": [
"Next we define a kind of seed to help us compare the learning process over\n",
"multiple training epochs."
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "eea2bbee",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"noise_dimension = 100\n",
"n_examples_to_generate = 16\n",
"seed_images = tf.random.normal([n_examples_to_generate, noise_dimension])"
]
},
{
"cell_type": "markdown",
"id": "94a6e341",
"metadata": {
"editable": true
},
"source": [
"Now we have everything we need to define our training step, which we will apply\n",
"for every step in our training loop. Notice the @tf.function flag signifying\n",
"that the function is tensorflow 'compiled'. Removing this flag doubles the\n",
"computation time."
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "8d48470b",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"@tf.function\n",
"def train_step(images):\n",
" noise = tf.random.normal([BATCH_SIZE, noise_dimension])\n",
"\n",
" with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:\n",
" generated_images = generator(noise, training=True)\n",
"\n",
" real_output = discriminator(images, training=True)\n",
" fake_output = discriminator(generated_images, training=True)\n",
"\n",
" gen_loss = generator_loss(fake_output)\n",
" disc_loss = discriminator_loss(real_output, fake_output)\n",
"\n",
" gradients_of_generator = gen_tape.gradient(gen_loss,\n",
" generator.trainable_variables)\n",
" gradients_of_discriminator = disc_tape.gradient(disc_loss,\n",
" discriminator.trainable_variables)\n",
" generator_optimizer.apply_gradients(zip(gradients_of_generator,\n",
" generator.trainable_variables))\n",
" discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator,\n",
" discriminator.trainable_variables))\n",
"\n",
" return gen_loss, disc_loss"
]
},
{
"cell_type": "markdown",
"id": "62015b88",
"metadata": {
"editable": true
},
"source": [
"Next we define a helper function to produce an output over our training epochs\n",
"to see the predictive progression of our generator model. **Note**: I am including\n",
"this code here, but comment it out in the training loop."
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "b189ed96",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"def generate_and_save_images(model, epoch, test_input):\n",
" # we're making inferences here\n",
" predictions = model(test_input, training=False)\n",
"\n",
" fig = plt.figure(figsize=(4, 4))\n",
"\n",
" for i in range(predictions.shape[0]):\n",
" plt.subplot(4, 4, i+1)\n",
" plt.imshow(predictions[i, :, :, 0] * 127.5 + 127.5, cmap='gray')\n",
" plt.axis('off')\n",
"\n",
" plt.savefig(f'./images_from_seed_images/image_at_epoch_{str(epoch).zfill(3)}.png')\n",
" plt.close()\n",
" #plt.show()"
]
},
{
"cell_type": "markdown",
"id": "ba2c82d5",
"metadata": {
"editable": true
},
"source": [
"Setting up checkpoints to periodically save our model during training so that\n",
"everything is not lost even if the program were to somehow terminate while\n",
"training."
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "a0e2fc8a",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"# Setting up checkpoints to save model during training\n",
"checkpoint_dir = './training_checkpoints'\n",
"checkpoint_prefix = os.path.join(checkpoint_dir, 'ckpt')\n",
"checkpoint = tf.train.Checkpoint(generator_optimizer=generator_optimizer,\n",
" discriminator_optimizer=discriminator_optimizer,\n",
" generator=generator,\n",
" discriminator=discriminator)"
]
},
{
"cell_type": "markdown",
"id": "4d93a0f7",
"metadata": {
"editable": true
},
"source": [
"Now we define our training loop"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "a1275556",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"def train(dataset, epochs):\n",
" generator_loss_list = []\n",
" discriminator_loss_list = []\n",
"\n",
" for epoch in range(epochs):\n",
" start = time.time()\n",
"\n",
" for image_batch in dataset:\n",
" gen_loss, disc_loss = train_step(image_batch)\n",
" generator_loss_list.append(gen_loss.numpy())\n",
" discriminator_loss_list.append(disc_loss.numpy())\n",
"\n",
" #generate_and_save_images(generator, epoch + 1, seed_images)\n",
"\n",
" if (epoch + 1) % 15 == 0:\n",
" checkpoint.save(file_prefix=checkpoint_prefix)\n",
"\n",
" print(f'Time for epoch {epoch} is {time.time() - start}')\n",
"\n",
" #generate_and_save_images(generator, epochs, seed_images)\n",
"\n",
" loss_file = './data/lossfile.txt'\n",
" with open(loss_file, 'w') as outfile:\n",
" outfile.write(str(generator_loss_list))\n",
" outfile.write('\\n')\n",
" outfile.write('\\n')\n",
" outfile.write(str(discriminator_loss_list))\n",
" outfile.write('\\n')\n",
" outfile.write('\\n')"
]
},
{
"cell_type": "markdown",
"id": "6ff3a75a",
"metadata": {
"editable": true
},
"source": [
"To train simply call this function. **Warning**: this might take a long time so\n",
"there is a folder of a pretrained network already included in the repository."
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "371ed41a",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"train(train_dataset, EPOCHS)"
]
},
{
"cell_type": "markdown",
"id": "654399f1",
"metadata": {
"editable": true
},
"source": [
"Now to avoid having to train and everything, which will take a while depending\n",
"on your computer setup we now load in the model which produced the above gif."
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "dec4b560",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))\n",
"restored_generator = checkpoint.generator\n",
"restored_discriminator = checkpoint.discriminator\n",
"\n",
"print(restored_generator)\n",
"print(restored_discriminator)"
]
},
{
"cell_type": "markdown",
"id": "296bfa5c",
"metadata": {
"editable": true
},
"source": [
"We have successfully loaded in our latest model. Let us now play around a bit\n",
"and see what kind of things we can learn about this model. Our generator takes\n",
"an array of 100 numbers. One idea can be to try to systematically change our\n",
"input. Let us try and see what we get"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "eecfbb1f",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"def generate_latent_points(number=100, scale_means=1, scale_stds=1):\n",
" latent_dim = 100\n",
" means = scale_means * tf.linspace(-1, 1, num=latent_dim)\n",
" stds = scale_stds * tf.linspace(-1, 1, num=latent_dim)\n",
" latent_space_value_range = tf.random.normal([number, latent_dim],\n",
" means,\n",
" stds,\n",
" dtype=tf.float64)\n",
"\n",
" return latent_space_value_range\n",
"\n",
"def generate_images(latent_points):\n",
" # notice we set training to false because we are making inferences\n",
" generated_images = restored_generator.predict(latent_points)\n",
"\n",
" return generated_images"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "333a593d",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"def plot_result(generated_images, number=100):\n",
" # obviously this assumes sqrt number is an int\n",
" fig, axs = plt.subplots(int(np.sqrt(number)), int(np.sqrt(number)),\n",
" figsize=(10, 10))\n",
"\n",
" for i in range(int(np.sqrt(number))):\n",
" for j in range(int(np.sqrt(number))):\n",
" axs[i, j].imshow(generated_images[i*j], cmap='Greys')\n",
" axs[i, j].axis('off')\n",
"\n",
" plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "2f5f0154",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"generated_images = generate_images(generate_latent_points())\n",
"plot_result(generated_images)"
]
},
{
"cell_type": "markdown",
"id": "ff581bf2",
"metadata": {
"editable": true
},
"source": [
"We see that the generator generates images that look like MNIST\n",
"numbers: $1, 4, 7, 9$. Let's try to tweak it a bit more to see if we are able\n",
"to generate a similar plot where we generate every MNIST number. Let us now try\n",
"to 'move' a bit around in the latent space. **Note**: decrease the plot number if\n",
"these following cells take too long to run on your computer."
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "d3617ad8",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"plot_number = 225\n",
"\n",
"generated_images = generate_images(generate_latent_points(number=plot_number,\n",
" scale_means=5,\n",
" scale_stds=1))\n",
"plot_result(generated_images, number=plot_number)\n",
"\n",
"generated_images = generate_images(generate_latent_points(number=plot_number,\n",
" scale_means=-5,\n",
" scale_stds=1))\n",
"plot_result(generated_images, number=plot_number)\n",
"\n",
"generated_images = generate_images(generate_latent_points(number=plot_number,\n",
" scale_means=1,\n",
" scale_stds=5))\n",
"plot_result(generated_images, number=plot_number)"
]
},
{
"cell_type": "markdown",
"id": "1a074f93",
"metadata": {
"editable": true
},
"source": [
"Again, we have found something interesting. *Moving* around using our means\n",
"takes us from digit to digit, while *moving* around using our standard\n",
"deviations seem to increase the number of different digits! In the last image\n",
"above, we can barely make out every MNIST digit. Let us make on last plot using\n",
"this information by upping the standard deviation of our Gaussian noises."
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "4ef8937d",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"plot_number = 400\n",
"generated_images = generate_images(generate_latent_points(number=plot_number,\n",
" scale_means=1,\n",
" scale_stds=10))\n",
"plot_result(generated_images, number=plot_number)"
]
},
{
"cell_type": "markdown",
"id": "385a2d0a",
"metadata": {
"editable": true
},
"source": [
"A pretty cool result! We see that our generator indeed has learned a\n",
"distribution which qualitatively looks a whole lot like the MNIST dataset.\n",
"\n",
"Another interesting way to explore the latent space of our generator model is by\n",
"interpolating between the MNIST digits. This section is largely based on\n",
"[this excellent blogpost](https://machinelearningmastery.com/how-to-interpolate-and-perform-vector-arithmetic-with-faces-using-a-generative-adversarial-network/)\n",
"by Jason Brownlee.\n",
"\n",
"So let us start by defining a function to interpolate between two points in the\n",
"latent space."
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "57de87b8",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"def interpolation(point_1, point_2, n_steps=10):\n",
" ratios = np.linspace(0, 1, num=n_steps)\n",
" vectors = []\n",
" for i, ratio in enumerate(ratios):\n",
" vectors.append(((1.0 - ratio) * point_1 + ratio * point_2))\n",
"\n",
" return tf.stack(vectors)"
]
},
{
"cell_type": "markdown",
"id": "cfb76bb6",
"metadata": {
"editable": true
},
"source": [
"Now we have all we need to do our interpolation analysis."
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "e25decef",
"metadata": {
"collapsed": false,
"editable": true
},
"outputs": [],
"source": [
"plot_number = 100\n",
"latent_points = generate_latent_points(number=plot_number)\n",
"results = None\n",
"for i in range(0, 2*np.sqrt(plot_number), 2):\n",
" interpolated = interpolation(latent_points[i], latent_points[i+1])\n",
" generated_images = generate_images(interpolated)\n",
"\n",
" if results is None:\n",
" results = generated_images\n",
" else:\n",
" results = tf.stack((results, generated_images))\n",
"\n",
"plot_results(results, plot_number)"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}