Spaces:
Sleeping
Sleeping
File size: 26,782 Bytes
51649e7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Movie sentiment model - GRU"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"from numpy import asarray\n",
"from numpy import zeros\n",
"import tensorflow as tf\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.metrics import classification_report, confusion_matrix, accuracy_score\n",
"import pickle"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def getGloveEmbeddings(glovefolderpath):\n",
" print(\"---------------------- Getting Glove Embeddings -------------------------\\n\")\n",
" embeddings_dictionary = dict()\n",
" glove_file = open(f\"{glovefolderpath}\", encoding=\"utf8\")\n",
" for line in glove_file:\n",
" records = line.split()\n",
" word = records[0]\n",
" vector_dimensions = asarray(records[1:], dtype='float32')\n",
" embeddings_dictionary [word] = vector_dimensions\n",
" glove_file.close()\n",
" print(\"---------------------- -------------------------\\n\")\n",
" return embeddings_dictionary\n",
"\n",
"\n",
"glove_folder=r'D:/STUDY/Sem3/deeplearning/glove.6B/glove.6B.100d.txt'"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"maxlen = 100"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"dataset = pd.read_csv('movie_data.csv')\n",
"\n",
"X = dataset['review'].values\n",
"y = dataset['sentiment'].values"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"tokeniser = tf.keras.preprocessing.text.Tokenizer()\n",
"tokeniser.fit_on_texts(X_train)\n",
"\n",
"\n",
"# Save the tokenizer using pickle\n",
"with open('tokenizer_movie_gru.pickle', 'wb') as handle:\n",
" pickle.dump(tokeniser, handle, protocol=pickle.HIGHEST_PROTOCOL)\n",
"\n",
"\n",
"X_train = tokeniser.texts_to_sequences(X_train)\n",
"X_test = tokeniser.texts_to_sequences(X_test)\n",
"vocab_size = len(tokeniser.word_index) + 1\n",
"\n",
"X_train = tf.keras.preprocessing.sequence.pad_sequences(X_train, padding='post', maxlen=maxlen)\n",
"X_test = tf.keras.preprocessing.sequence.pad_sequences(X_test, padding='post', maxlen=maxlen)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"---------------------- Getting Glove Embeddings -------------------------\n",
"\n",
"---------------------- -------------------------\n",
"\n"
]
}
],
"source": [
"embeddings_dictionary=getGloveEmbeddings(glove_folder)\n",
"embedding_matrix = zeros((vocab_size, maxlen))\n",
"for word, index in tokeniser.word_index.items():\n",
" embedding_vector = embeddings_dictionary.get(word)\n",
" if embedding_vector is not None:\n",
" embedding_matrix[index] = embedding_vector"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"sequential\"\n",
"_________________________________________________________________\n",
" Layer (type) Output Shape Param # \n",
"=================================================================\n",
" embedding (Embedding) (None, 100, 100) 10591700 \n",
" \n",
" gru (GRU) (None, 100) 60600 \n",
" \n",
" dense (Dense) (None, 1) 101 \n",
" \n",
"=================================================================\n",
"Total params: 10652401 (40.64 MB)\n",
"Trainable params: 60701 (237.11 KB)\n",
"Non-trainable params: 10591700 (40.40 MB)\n",
"_________________________________________________________________\n",
"None\n"
]
}
],
"source": [
"model=tf.keras.models.Sequential([\n",
" tf.keras.layers.Embedding(input_dim=vocab_size,output_dim= maxlen, weights=[embedding_matrix], input_length=maxlen , trainable=False),\n",
" tf.keras.layers.GRU(maxlen),\n",
" tf.keras.layers.Dense(1, activation='sigmoid')\n",
" ]) \n",
" \n",
"print(model.summary())\n",
"\n",
"early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', mode='auto', patience=10)\n",
"\n",
"model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/100\n",
"766/766 [==============================] - 41s 51ms/step - loss: 0.5112 - accuracy: 0.7408 - val_loss: 0.4424 - val_accuracy: 0.7906\n",
"Epoch 2/100\n",
"766/766 [==============================] - 39s 51ms/step - loss: 0.3865 - accuracy: 0.8234 - val_loss: 0.3741 - val_accuracy: 0.8330\n",
"Epoch 3/100\n",
"766/766 [==============================] - 38s 49ms/step - loss: 0.3418 - accuracy: 0.8473 - val_loss: 0.3578 - val_accuracy: 0.8444\n",
"Epoch 4/100\n",
"766/766 [==============================] - 39s 51ms/step - loss: 0.3104 - accuracy: 0.8653 - val_loss: 0.3519 - val_accuracy: 0.8446\n",
"Epoch 5/100\n",
"766/766 [==============================] - 40s 52ms/step - loss: 0.2721 - accuracy: 0.8819 - val_loss: 0.3361 - val_accuracy: 0.8510\n",
"Epoch 6/100\n",
"766/766 [==============================] - 40s 52ms/step - loss: 0.2412 - accuracy: 0.8972 - val_loss: 0.3429 - val_accuracy: 0.8540\n",
"Epoch 7/100\n",
"766/766 [==============================] - 41s 54ms/step - loss: 0.2082 - accuracy: 0.9143 - val_loss: 0.3459 - val_accuracy: 0.8570\n",
"Epoch 8/100\n",
"766/766 [==============================] - 61s 79ms/step - loss: 0.1683 - accuracy: 0.9329 - val_loss: 0.4076 - val_accuracy: 0.8528\n",
"Epoch 9/100\n",
"766/766 [==============================] - 59s 78ms/step - loss: 0.1338 - accuracy: 0.9495 - val_loss: 0.4233 - val_accuracy: 0.8490\n",
"Epoch 10/100\n",
"766/766 [==============================] - 56s 73ms/step - loss: 0.0984 - accuracy: 0.9636 - val_loss: 0.4878 - val_accuracy: 0.8514\n",
"Epoch 11/100\n",
"766/766 [==============================] - 56s 73ms/step - loss: 0.0725 - accuracy: 0.9753 - val_loss: 0.5296 - val_accuracy: 0.8413\n",
"Epoch 12/100\n",
"766/766 [==============================] - 56s 73ms/step - loss: 0.0513 - accuracy: 0.9831 - val_loss: 0.5957 - val_accuracy: 0.8437\n",
"Epoch 13/100\n",
"766/766 [==============================] - 56s 73ms/step - loss: 0.0390 - accuracy: 0.9879 - val_loss: 0.6976 - val_accuracy: 0.8336\n",
"Epoch 14/100\n",
"766/766 [==============================] - 61s 80ms/step - loss: 0.0334 - accuracy: 0.9895 - val_loss: 0.7144 - val_accuracy: 0.8468\n",
"Epoch 15/100\n",
"766/766 [==============================] - 63s 82ms/step - loss: 0.0264 - accuracy: 0.9913 - val_loss: 0.7993 - val_accuracy: 0.8417\n",
"Epoch 16/100\n",
"766/766 [==============================] - 61s 79ms/step - loss: 0.0285 - accuracy: 0.9907 - val_loss: 0.8220 - val_accuracy: 0.8445\n",
"Epoch 17/100\n",
"766/766 [==============================] - 57s 74ms/step - loss: 0.0257 - accuracy: 0.9915 - val_loss: 0.8161 - val_accuracy: 0.8396\n"
]
}
],
"source": [
"history=model.fit(x=X_train,\n",
" y=y_train,\n",
" epochs=100,\n",
" callbacks=[early_stop],\n",
" validation_split=0.3\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"def c_report(y_true, y_pred):\n",
" print(\"Classification Report\")\n",
" print(classification_report(y_true, y_pred))\n",
" acc_sc = accuracy_score(y_true, y_pred)\n",
" print(f\"Accuracy : {str(round(acc_sc,2)*100)}\")\n",
" return acc_sc\n",
"\n",
"def plot_confusion_matrix(y_true, y_pred):\n",
" mtx = confusion_matrix(y_true, y_pred)\n",
" sns.heatmap(mtx, annot=True, fmt='d', linewidths=.5, cmap=\"Blues\", cbar=False)\n",
" plt.ylabel('True label')\n",
" plt.xlabel('Predicted label')\n",
" plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"469/469 [==============================] - 9s 18ms/step\n",
"Classification Report\n",
" precision recall f1-score support\n",
"\n",
" 0 0.82 0.87 0.85 7443\n",
" 1 0.86 0.81 0.84 7557\n",
"\n",
" accuracy 0.84 15000\n",
" macro avg 0.84 0.84 0.84 15000\n",
"weighted avg 0.84 0.84 0.84 15000\n",
"\n",
"Accuracy : 84.0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YhXgAAgFGIFxht0qRJqlixogoUKKB69erpu+++8/VIAK7T119/rbZt2yo0NFQ2m01Lly719Ui4yRAvMNaiRYs0cOBADR8+XNu2bVOtWrXUqlUr/frrr74eDcB1SE1NVa1atTRp0iRfj4KbFG+VhrHq1aune+65RxMnTpQkORwOhYWFqW/fvho6dKiPpwPgDTabTR9//LE6dOjg61FwE+HKC4yUkZGhrVu3qkWLFs5t+fLlU4sWLbR582YfTgYAyGvEC4x08uRJZWVlqXTp0i7bS5curcTERB9NBQC4EYgXAABgFOIFRipRooT8/PyUlJTksj0pKUllypTx0VQAgBuBeIGRAgICVKdOHa1evdq5zeFwaPXq1apfv74PJwMA5LX8vh4AuFYDBw5UVFSU7r77btWtW1cTJkxQamqqunXr5uvRAFyHlJQU7d271/n4wIED2rFjh4oVK6YKFSr4cDLcLHirNIw2ceJEjR07VomJiapdu7ZiY2NVr149X48F4DqsW7dOzZo1y7Y9KipKcXFxN34g3HSIFwAAYBTueQEAAEYhXgAAgFGIFwAAYBTiBQAAGIV4AQAARiFeAACAUYgXAABgFOIFAAAYhXgB4HVdu3ZVhw4dnI+bNm2q55577obPsW7dOtlsNp0+fTrHY2w2m5YuXer2mq+++qpq1659XXMdPHhQNptNO3bsuK51gL8q4gX4i+jatatsNptsNpsCAgIUERGhkSNH6sKFC3n+3B999JFGjRrl1rHuBAeAvza+mBH4C2ndurVmzZql9PR0ffrpp+rdu7f8/f01bNiwbMdmZGQoICDAK89brFgxr6wDABJXXoC/FLvdrjJlyig8PFzPPPOMWrRooWXLlkn630s9o0ePVmhoqKpWrSpJOnLkiDp16qQiRYqoWLFiat++vQ4ePOhcMysrSwMHDlSRIkVUvHhxPf/887r8K9Muf9koPT1dL7zwgsLCwmS32xUREaH3339fBw8edH4hX9GiRWWz2dS1a1dJksPhUExMjCpVqqTAwEDVqlVLH374ocvzfPrpp6pSpYoCAwPVrFkzlznd9cILL6hKlSoKCgpS5cqVFR0drczMzGzHTZs2TWFhYQoKClKnTp105swZl/0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YEFFRES4ffxdd92lRYsWqVSpUtmuPlxStmxZffvtt2rcuLGki1cYtm7dqrvuuuuKx9esWVMOh0NfffWVWrRokW3/pSs/WVlZzm01atSQ3W7X4cOHc7xiU716defNx5d88803Vz/JP9m0aZPCw8P10ksvObcdOnQo23GHDx/W8ePHFRoa6nyefPnyqWrVqipdurRCQ0O1f/9+de7c2aPnB+AebtgFkKPOnTurRIkSat++vdavX68DBw5o3bp16tevn44ePSpJ6t+/v9544w0tXbpUu3fv1rPPPpvrZ7RUrFhRUVFR6t69u5YuXepcc/HixZKk8PBw2Ww2rVixQr/99ptSUlIUHByswYMHa8CAAZo9e7b27dunbdu26d1333XeBPv000/rl19+0ZAhQ5SQkKD58+crLi7Oo/O9/fbbdfjwYS1cuFD79u1TbGzsFW8+LlCggKKiorRz506tX79e/fr1U6dOnVSmTBlJ0ogRIxQTE6PY2Fjt2bNHP/74o2bNmqW3337bo3kAXBnxAiBHQUFB+vrrr1WhQgV17NhR1atXV48ePZSWlua8EjNo0CD9+9//VlRUlOrXr6/g4GA99NBDua47ZcoUPfLII3r22WdVrVo19erVS6mpqZKkcuXKacSIERo6dKhKly6tPn36SJJGjRql6OhoxcTEqHr16mrdurVWrlypSpUqSbp4H8qSJUu0dOlS1apVS1OnTtXrr7/u0fm2a9dOAwYMUJ8+fVS7dm1t2rRJ0dHR2Y6LiIhQx44d9eCDD6ply5a68847Xd4K3bNnT82YMUOzZs1SzZo11aRJE8XFxTlnBXB9bFZOd9UBAADchLjyAgAAjEK8AAAAoxAvAADAKMQLAAAwCvECAACMQrwAAACjEC8AAMAoxAsAADAK8QIAAIxCvAAAAKMQLwAAwCj/B/Ut7r+e94ZGAAAAAElFTkSuQmCC",
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"preds = (model.predict(X_test) > 0.5).astype(\"int32\")\n",
"c_report(y_test, preds)\n",
"plot_confusion_matrix(y_test, preds)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"d:\\STUDY\\Sem3\\deeplearning\\DLENV\\lib\\site-packages\\keras\\src\\engine\\training.py:3079: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.\n",
" saving_api.save_model(\n"
]
}
],
"source": [
"# Save the model\n",
"model.save(\"gru_movie_model.h5\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# Load the saved model\n",
"gru_movie_model = tf.keras.models.load_model('gru_movie_model.h5')\n",
"\n",
"# Function to predict sentiment for a given review\n",
"def gru_predict_sentiment(review):\n",
" sequence = tokeniser.texts_to_sequences([review])\n",
" sequence = tf.keras.preprocessing.sequence.pad_sequences(sequence, padding='post', maxlen=maxlen)\n",
" prediction = gru_movie_model.predict(sequence)\n",
" if prediction > 0.5:\n",
" return \"Positive\"\n",
" else:\n",
" return \"Negative\""
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1/1 [==============================] - 1s 571ms/step\n",
"Review: This movie was fantastic! I loved every bit of it. \n",
"The sentiment is predicted as: Positive\n"
]
}
],
"source": [
"# Test the model prediction\n",
"example_review = \"This movie was fantastic! I loved every bit of it.\"\n",
"prediction_result = gru_predict_sentiment(example_review)\n",
"print(f\"Review: {example_review} \\nThe sentiment is predicted as: {prediction_result}\")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1/1 [==============================] - 0s 51ms/step\n",
"Review: This movie was very bad! I hated every bit of it. \n",
"The sentiment is predicted as: Negative\n"
]
}
],
"source": [
"# Test the model prediction\n",
"example_review = \"This movie was very bad! I hated every bit of it.\"\n",
"prediction_result = gru_predict_sentiment(example_review)\n",
"print(f\"Review: {example_review} \\nThe sentiment is predicted as: {prediction_result}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "DLENV",
"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.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|