-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #421 from kritikaparmar-programmer/rnn
RNN
- Loading branch information
Showing
4 changed files
with
531 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"id": "zwzjFi8-CR4a" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import tensorflow as tf\r\n", | ||
"from tensorflow.keras.models import Sequential\r\n", | ||
"from tensorflow.keras.layers import Dense, Dropout, LSTM" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "yGzOxAAjCVld", | ||
"outputId": "6a0e52cc-7071-4a64-ea60-a2801097915d" | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n", | ||
"11493376/11490434 [==============================] - 0s 0us/step\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"mnist = tf.keras.datasets.mnist\r\n", | ||
"(x_train, y_train), (x_test, y_test) = mnist.load_data()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "GjR_FRu3CYFa", | ||
"outputId": "2ecd447c-79e0-44c4-93d3-0f52fad26eec" | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"(60000, 28, 28)\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(x_train.shape)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": { | ||
"id": "dW_UOayCCYy3" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# normalise data\r\n", | ||
"x_train = x_train/255.0\r\n", | ||
"x_test = x_test/255.0" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": { | ||
"id": "Ev3eg2PcCYps" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"model = Sequential()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": { | ||
"id": "W7OnTS_UCYem" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"model.add(LSTM(128, input_shape = (28, 28), activation='relu', return_sequences=True))\r\n", | ||
"model.add(Dropout(0.2))\r\n", | ||
"\r\n", | ||
"model.add(LSTM(128, activation='relu'))\r\n", | ||
"model.add(Dropout(0.2))\r\n", | ||
"\r\n", | ||
"model.add(Dense(32, activation='relu'))\r\n", | ||
"model.add(Dropout(0.2))\r\n", | ||
"\r\n", | ||
"model.add(Dense(10, activation='softmax'))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 11, | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "-y_DlifKEVUu", | ||
"outputId": "3bfb3e71-d4d8-4267-8d20-ce10c3b173d0" | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Model: \"sequential\"\n", | ||
"_________________________________________________________________\n", | ||
"Layer (type) Output Shape Param # \n", | ||
"=================================================================\n", | ||
"lstm (LSTM) (None, 28, 128) 80384 \n", | ||
"_________________________________________________________________\n", | ||
"dropout (Dropout) (None, 28, 128) 0 \n", | ||
"_________________________________________________________________\n", | ||
"lstm_1 (LSTM) (None, 128) 131584 \n", | ||
"_________________________________________________________________\n", | ||
"dropout_1 (Dropout) (None, 128) 0 \n", | ||
"_________________________________________________________________\n", | ||
"dense (Dense) (None, 32) 4128 \n", | ||
"_________________________________________________________________\n", | ||
"dropout_2 (Dropout) (None, 32) 0 \n", | ||
"_________________________________________________________________\n", | ||
"dense_1 (Dense) (None, 10) 330 \n", | ||
"=================================================================\n", | ||
"Total params: 216,426\n", | ||
"Trainable params: 216,426\n", | ||
"Non-trainable params: 0\n", | ||
"_________________________________________________________________\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"model.summary()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": { | ||
"id": "6O9qZ5WRCl2d" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"optimizer = tf.keras.optimizers.Adam(lr=0.001, decay=0.00005)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": { | ||
"id": "BMz_PlqqCn3Q" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"model.compile(loss='sparse_categorical_crossentropy', \r\n", | ||
" optimizer=optimizer,\r\n", | ||
" metrics=['accuracy'])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "ZGyzfMlYCq5e", | ||
"outputId": "e755ae08-d56b-4fac-8abd-ae991363409d" | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Epoch 1/5\n", | ||
"1875/1875 [==============================] - 101s 54ms/step - loss: 0.2994 - accuracy: 0.9152 - val_loss: 0.1330 - val_accuracy: 0.9612\n", | ||
"Epoch 2/5\n", | ||
"1875/1875 [==============================] - 99s 53ms/step - loss: 0.1400 - accuracy: 0.9634 - val_loss: 0.0844 - val_accuracy: 0.9764\n", | ||
"Epoch 3/5\n", | ||
"1875/1875 [==============================] - 102s 54ms/step - loss: 0.0969 - accuracy: 0.9743 - val_loss: 0.0621 - val_accuracy: 0.9812\n", | ||
"Epoch 4/5\n", | ||
"1875/1875 [==============================] - 100s 53ms/step - loss: 0.0791 - accuracy: 0.9793 - val_loss: 0.0698 - val_accuracy: 0.9811\n", | ||
"Epoch 5/5\n", | ||
"1875/1875 [==============================] - 99s 53ms/step - loss: 0.0643 - accuracy: 0.9830 - val_loss: 0.0502 - val_accuracy: 0.9850\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"<tensorflow.python.keras.callbacks.History at 0x7f40fc0f4e80>" | ||
] | ||
}, | ||
"execution_count": 10, | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"model.fit(x_train, y_train, epochs=5, \r\n", | ||
" validation_data=(x_test, y_test))" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"colab": { | ||
"name": "RNN.ipynb", | ||
"provenance": [] | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
Oops, something went wrong.