Answer To: { "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "LSTM_Tutorial.ipynb",...
Sandeep Kumar answered on Apr 16 2021
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"kernelspec": {
"name": "python394jvsc74a57bd081118431cc388d258ed977b65143603a98f8ad6ed776c173758a3af876bc6de9",
"display_name": "Python 3.9.4 64-bit"
},
"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.9.4-final"
},
"colab": {
"name": "IMDB movie review Modeling DNN CNN LSTM Keras.ipynb",
"provenance": []
}
},
"cells": [
{
"cell_type": "code",
"metadata": {
"id": "yJDfTCHvULWg"
},
"source": [
"from keras.datasets import imdb\n",
"%matplotlib inline\n",
"import numpy as np\n",
"import pandas as pd\n",
"from matplotlib import cm\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"import os\n",
"import time"
],
"execution_count": 28,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "0Lf43x1oULW7"
},
"source": [
"from keras.preprocessing import sequence\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense, Dropout, Activation\n",
"from keras.layers import Embedding\n",
"from keras.layers import Conv1D, GlobalMaxPooling1D\n",
"from keras.callbacks import EarlyStopping\n",
"from keras import models"
],
"execution_count": 29,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 0
},
"id": "9Z2RhfpPULW-",
"outputId": "dffec856-2810-49de-b253-5c0d3bb524e6"
},
"source": [
"\n",
"(X_train, y_train), (X_test, y_test) = imdb.load_data()\n",
"X = np.concatenate((X_train, X_test), axis=0)\n",
"y = np.concatenate((y_train, y_test), axis=0)"
],
"execution_count": 30,
"outputs": [
{
"output_type": "stream",
"text": [
":6: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray\n",
"/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/datasets/imdb.py:159: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray\n",
" x_train, y_train = np.array(xs[:idx]), np.array(labels[:idx])\n",
"/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/datasets/imdb.py:160: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray\n",
" x_test, y_test = np.array(xs[idx:]), np.array(labels[idx:])\n"
],
"name": "stderr"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 0
},
"id": "JJXh9NkDULW_",
"outputId": "27ee5cf9-43e0-4501-acdb-532c3e635253"
},
"source": [
"##training data shape review\n",
"print(\"Training data: \")\n",
"print(X.shape)\n",
"print(y.shape)\n",
"print(\"Classes: \")\n",
"print(np.unique(y))"
],
"execution_count": 31,
"outputs": [
{
"output_type": "stream",
"text": [
"Training data: \n",
"(50000,)\n",
"(50000,)\n",
"Classes: \n",
"[0 1]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 0
},
"id": "GnKYq1bVULXB",
"outputId": "0adaa77d-9b67-4e58-ad7d-1f62da898acb"
},
"source": [
"print(\"Number of words: \")\n",
"print(len(np.unique(np.hstack(X))))"
],
"execution_count": 32,
"outputs": [
{
"output_type": "stream",
"text": [
"Number of words: \n",
"88585\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 299
},
"id": "ffaOSrvEULXC",
"outputId": "10a0455a-d45f-4a7b-8c0c-dc1456211e71"
},
"source": [
"print(\"Review length: \")\n",
"result = [len(x) for x in X]\n",
"print(\"Mean %.2f words (%f)\" % (np.mean(result), np.std(result)))\n",
"# plot review length\n",
"plt.boxplot(result)\n",
"plt.show()"
],
"execution_count": 33,
"outputs": [
{
"output_type": "stream",
"text": [
"Review length: \n",
"Mean 234.76 words (172.911495)\n"
],
"name": "stdout"
},
{
"output_type": "display_data",
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAD4CAYAAAAAczaOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAUb0lEQVR4nO3db2xV953n8fc3xn+Ek+F/UTZOlipiR2YsTVK5aaXhwbqrzb8nYZ60caopAhQWqVjMkoRk4gfpzgg0QjuMqNUNzQi3QRocRZoZijbJUBZZqqxOZ+K0UUrwVEEdKCb8SyBtZGQM9m8fcKAm4c89xvj4ct4v6eqe+73n3vu9D/j48Du/87uRUkKSVA53FN2AJGnqGPqSVCKGviSViKEvSSVi6EtSicwouoHrmT9/flq0aFHRbUhSVXnnnXc+SiktuNpz0zr0Fy1aRH9/f9FtSFJViYjD13rO4R1JKhFDX5JKxNCXpBIx9CWpRAx9SSqRG4Z+RNwbEb0RcSAi3o+IdVn9OxFxNCLezW6Pj3vNX0TEwYj4VUQ8Mq7+aFY7GBEv3JqvJN1aPT09tLS0UFNTQ0tLCz09PUW3JFWskimbF4BnUko/j4i7gHciYm/23N+mlP73+J0jYgnwJPBHwH8C/l9E/Jfs6e8B/x0YBN6OiN0ppQOT8UWkqdDT00NnZyfbt29n6dKl9PX1sWrVKgDa29sL7k66sRse6aeUjqWUfp5tfwoMAPdc5yVPAK+llM6llP4DOAg8lN0OppR+nVIaAV7L9pWqxsaNG9m+fTttbW3U1tbS1tbG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