{"cells":[{"metadata":{"trusted":true,"_uuid":"378381c0bf90655d7c260af4ee1793acf64bf2e2"},"cell_type":"code","source":"from keras.datasets import imdb\nfrom keras import layers, models, optimizers\nimport numpy as np\nimport matplotlib.pyplot as plt\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2679d7b8cf7e05945735fcc05b2626840873b184"},"cell_type":"code","source":"def vectorize_sequence(sequences, dimension=1000):\n    results = np.zeros((len(sequences), dimension))\n    for i, sequence in enumerate(sequences):\n        results[i,sequence] = 1.\n    return results\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c5cc0e73cafbbcdfbc2b92684bacbf4e8077af38"},"cell_type":"code","source":"(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words = 1000)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fa38aadb4037a9a3deffafcddf99d270222e390b"},"cell_type":"code","source":"# print(\"Max index in train data: {max([max(sequence) for sequence in train_data])}\")\n# print(\"Train label: train_labels[0] = {train_labels[0]})\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d5f301824a6f10a5e430c085268cdc60f5e34c46"},"cell_type":"code","source":"# print({len(train_data[0])})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8b1eaa315fd4777d63bd22541e90cf2e0bdd4f85"},"cell_type":"code","source":"word_index = imdb.get_word_index() # maps words to index, commonly called as w_to_i\nreverse_word_index = dict( [ (value, key) for (key, value) in word_index.items()] ) # this is just opposite, i_to_w\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4273742381df153025ad9276d442b0bc66346d92"},"cell_type":"code","source":"decode_review = ' '.join( [reverse_word_index.get(i - 3, '?') for i in train_data[0] ])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"867cb57d546ff5db552edd94fcaa8f199b222d68"},"cell_type":"code","source":"# print(f\"Encoded Review: \\n {train_data[0]} \\n\")\nprint(decode_review)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e6838888539d3fad486396cd2ed38305a9d8f171"},"cell_type":"code","source":"x_train = vectorize_sequence(train_data) # mapping one hot encoding to a particular index\nx_test = vectorize_sequence(test_data) # mapping one hot encoding to a particular index","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d331ed3f15b4866734a0490fd62a9072aaf982be"},"cell_type":"code","source":"y_train = np.asarray(train_labels).astype('float32')\ny_test = np.asarray(test_labels).astype('float32')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e0fe6d67f86f74be8fd09fef009a89ad3ed484a7"},"cell_type":"code","source":"# print(\"A data point appears like\" (x_train[0]))\n# print(\"Actual appears as: {y_train[0]}\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6422441e9189a7903cfb71b1e28affd947ef4bee"},"cell_type":"code","source":"# Validation separation\n\nx_val = x_train[:1000]\npartial_x_train = x_train[1000:]\n\ny_val = y_train[:1000]\npartial_y_train = y_train[1000:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f9f1f30820ab9cf17e2db8f2fa44d8f6fbedba19"},"cell_type":"code","source":"# Model definition\n\nmodel = models.Sequential()\nmodel.add(layers.Dense(16, activation='relu', input_shape=(1000,), name=\"Input_Layer\"))\nmodel.add(layers.Dense(16, activation='relu', name=\"Hidden_Layer_1\"))\nmodel.add(layers.Dense(1, activation='sigmoid', name=\"Output_Layer\"))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d3ab657935e1ddbb1a7fd7e08c69782a5dc47765"},"cell_type":"code","source":"print(model.summary())\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9e92749ce30fa7819aa196d29f34217f44f6045d"},"cell_type":"code","source":"# Compiling the model\n\nmodel.compile(optimizer=optimizers.RMSprop(lr = 0.001),\n              loss='binary_crossentropy',\n              metrics=['accuracy'])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cad7a7380144ec3b700cb062a51e0bd5c19225de"},"cell_type":"code","source":"history = model.fit(partial_x_train,\n                    partial_y_train,\n                    epochs = 4,\n                    batch_size = 128,\n                    validation_data = (x_val, y_val))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"16c38443b894f0433d52b06b2201f6effd847c85"},"cell_type":"code","source":"history_dict = history.history\nacc = history_dict[\"acc\"]\nval_acc = history_dict[\"val_acc\"]\nloss_values = history_dict['loss']\nval_loss_values = history_dict['val_loss']\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"88a7a12fe9764e9d23e5f1a9edf79391362fcfc2"},"cell_type":"code","source":"print (loss_values)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e88ee46058a88dd3a780c4870523d5a7352d0526"},"cell_type":"code","source":"epochs = range(1, len(acc) + 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"813f31e567a463b8040656f25657a5586bd3f42e"},"cell_type":"code","source":"print (epochs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b561154b26a126d003a5e3e640c1fdc55639e07b"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.figure(1)\nplt.subplot(211)\nplt.plot(epochs, loss_values, 'bo', label='Training Loss')\nplt.plot(epochs, val_loss_values, 'b', label='Validation Loss')\nplt.title('Training and validation loss')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"85eda9566d79d1bdedca990a18272d9802dec732"},"cell_type":"code","source":"plt.subplot(212)\nplt.plot(epochs, acc, 'bo', label='Training acc')\nplt.plot(epochs, val_acc, 'b', label = 'Validation acc')\nplt.title('Training and validation accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d6047a104bfc08e9047f85ab4d7bc33c087f453e"},"cell_type":"code","source":"# Running Predict\ntest = x_test[1].reshape(1, -1)\nprint(f\"Model's Prediction: {model.predict(test)[0]}\")\nprint(f\"Actual: {y_test[1]}\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"e3def365559b2ee24d84fd1f3dc26d5b557c61e8"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}