diff --git a/dl/RNN/.ipynb_checkpoints/RNN-checkpoint.ipynb b/dl/RNN/.ipynb_checkpoints/RNN-checkpoint.ipynb new file mode 100644 index 000000000..5000bf835 --- /dev/null +++ b/dl/RNN/.ipynb_checkpoints/RNN-checkpoint.ipynb @@ -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": [ + "" + ] + }, + "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 +} diff --git a/dl/RNN/RNN.ipynb b/dl/RNN/RNN.ipynb new file mode 100644 index 000000000..5000bf835 --- /dev/null +++ b/dl/RNN/RNN.ipynb @@ -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": [ + "" + ] + }, + "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 +} diff --git a/dl/RNN/requirements.txt b/dl/RNN/requirements.txt new file mode 100644 index 000000000..4cff948f3 --- /dev/null +++ b/dl/RNN/requirements.txt @@ -0,0 +1,5 @@ +1 keras == 2.3.1 +2 scikit-learn == 0.22.1 +3 matplotlib == 3.1.3 +4 numpy == 1.18.1 +5 pandas == 1.0.3 \ No newline at end of file diff --git a/dl/RNN/rnn.py b/dl/RNN/rnn.py new file mode 100644 index 000000000..7d532388e --- /dev/null +++ b/dl/RNN/rnn.py @@ -0,0 +1,32 @@ +import tensorflow as tf +from tensorflow.keras.models import Sequential +from tensorflow.keras.layers import Dense, Dropout, LSTM + +def rnn_model(input_shape, x_train, y_train, x_test, y_test, epochs=0, loss='sparse_categorical_crossentropy', metric='accuracy', lr=0): + model = Sequential([ + + LSTM(128, input_shape=input_shape, activation='relu', return_sequences=True), + Dropout(0.2), + + LSTM(128, activation='relu'), + Dropout(0.2), + + Dense(32, activation='relu'), + Dropout(0.2), + + Dense(10, activation='softmax') + + ]) + print(model.summary()) + + + optimizer = tf.keras.optimizers.Adam(lr=lr, decay=0.00005) + model.compile(loss=loss, + optimizer=optimizer, + metrics=[metric]) + + model.fit(x_train, y_train, epochs=epochs, validation_data=(x_test, y_test)) + + +# example +# rnn_model((28, 28),x_train, y_train, x_test, y_test, epochs=5, loss='sparse_categorical_crossentropy', metric='accuracy', lr=0.01) \ No newline at end of file