Machine Learning Kurs im Rahmen der Studierendentage im SS 2023
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
6.7 KiB

2 years ago
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": null,
  6. "id": "6c180d4b",
  7. "metadata": {},
  8. "outputs": [],
  9. "source": [
  10. "# Exercise 3\n",
  11. "# fashion mnist data\n",
  12. "# MLP model with two hidden layers, each with a ReLU activation function.\n",
  13. "# Input data is flattened to a 1D array and passed to the model."
  14. ]
  15. },
  16. {
  17. "cell_type": "code",
  18. "execution_count": null,
  19. "id": "b0e31b9c",
  20. "metadata": {},
  21. "outputs": [],
  22. "source": [
  23. "import tensorflow as tf\n",
  24. "from tensorflow import keras\n",
  25. "import matplotlib.pyplot as plt\n",
  26. "import numpy as np"
  27. ]
  28. },
  29. {
  30. "cell_type": "code",
  31. "execution_count": null,
  32. "id": "1ae1412e",
  33. "metadata": {},
  34. "outputs": [],
  35. "source": [
  36. "# Load the MNIST Fashion dataset\n",
  37. "(x_train, y_train), (x_test, y_test) = keras.datasets.fashion_mnist.load_data()"
  38. ]
  39. },
  40. {
  41. "cell_type": "code",
  42. "execution_count": null,
  43. "id": "f8814914",
  44. "metadata": {},
  45. "outputs": [],
  46. "source": [
  47. "# Normalize pixel values to between 0 and 1\n",
  48. "x_train = x_train.astype(\"float32\") / 255.0\n",
  49. "x_test = x_test.astype(\"float32\") / 255.0"
  50. ]
  51. },
  52. {
  53. "cell_type": "code",
  54. "execution_count": null,
  55. "id": "2810da39",
  56. "metadata": {},
  57. "outputs": [],
  58. "source": [
  59. "# MNIST dataset images have a shape of (28, 28). The images are flattened\n",
  60. "# into a 1D array of length 784 \n",
  61. "x_train = x_train.reshape(-1, 784)\n",
  62. "x_test = x_test.reshape(-1, 784)"
  63. ]
  64. },
  65. {
  66. "cell_type": "code",
  67. "execution_count": null,
  68. "id": "96f7ff8a",
  69. "metadata": {},
  70. "outputs": [],
  71. "source": [
  72. "# The model is defined here with three dense (fully connected) layers\n",
  73. "# The first layer is a Dense layer with 128 units and a ReLU activation\n",
  74. "# function with an input shape of (784,). This layer serves as the input\n",
  75. "# layer of the model.\n",
  76. "# The second layer is also a Dense layer with 64 units and a ReLU activation\n",
  77. "# function. This layer takes the output of the previous layer as input, and\n",
  78. "# applies a non-linear transformation to it to produce a new set of features\n",
  79. "# that the next layer can use.\n",
  80. "# The third is another Dense layer, one for each class in the output. The\n",
  81. "# output is raw scores or logits for each class since there is no activation\n",
  82. "# function . This layer is responsible for producing the final output of the\n",
  83. "# model, which can then be used to make predictions.\n",
  84. "# With Dropout(0.2) 20 % of the input is randomly droped, this should reduce overfitting\n",
  85. "model = keras.Sequential([\n",
  86. " keras.layers.Dense(128, activation='relu', input_shape=(784,)),\n",
  87. " # keras.layers.Dropout(0.2),\n",
  88. " keras.layers.Dense(64, activation='relu'),\n",
  89. " keras.layers.Dense(10)\n",
  90. "])\n"
  91. ]
  92. },
  93. {
  94. "cell_type": "code",
  95. "execution_count": null,
  96. "id": "a3fe609c",
  97. "metadata": {},
  98. "outputs": [],
  99. "source": [
  100. "# Compile the model\n",
  101. "# adam = specifies the optimizer to use during training\n",
  102. "# loss function to use during training, SparseCategoricalCrossentropy loss\n",
  103. "# is commonly used for multi-class classification problems.\n",
  104. "# from_logits=True indicates that the model's output is a raw score\n",
  105. "# for each class and not a probability distribution.\n",
  106. "model.compile(optimizer='adam',\n",
  107. " loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n",
  108. " metrics=['accuracy'])"
  109. ]
  110. },
  111. {
  112. "cell_type": "code",
  113. "execution_count": null,
  114. "id": "cf6c978d",
  115. "metadata": {},
  116. "outputs": [],
  117. "source": [
  118. "# Train the model\n",
  119. "history = model.fit(x_train, y_train, epochs=10, validation_split=0.2)"
  120. ]
  121. },
  122. {
  123. "cell_type": "code",
  124. "execution_count": null,
  125. "id": "97fc2313",
  126. "metadata": {
  127. "scrolled": true
  128. },
  129. "outputs": [],
  130. "source": [
  131. "# Evaluate the model on the test set\n",
  132. "test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)\n",
  133. "print(\"Test accuracy:\", test_acc)"
  134. ]
  135. },
  136. {
  137. "cell_type": "code",
  138. "execution_count": null,
  139. "id": "ef5f19d0",
  140. "metadata": {},
  141. "outputs": [],
  142. "source": [
  143. "# Plot the training and validation accuracy and loss over time\n",
  144. "plt.figure(figsize=(10, 4))\n",
  145. "plt.subplot(1, 2, 1)\n",
  146. "plt.plot(history.history[\"accuracy\"])\n",
  147. "plt.plot(history.history[\"val_accuracy\"])\n",
  148. "plt.title(\"Model accuracy\")\n",
  149. "plt.ylabel(\"Accuracy\")\n",
  150. "plt.xlabel(\"Epoch\")\n",
  151. "plt.legend([\"Train\", \"Validation\"], loc=\"lower right\")\n",
  152. "\n",
  153. "plt.subplot(1, 2, 2)\n",
  154. "plt.plot(history.history[\"loss\"])\n",
  155. "plt.plot(history.history[\"val_loss\"])\n",
  156. "plt.title(\"Model loss\")\n",
  157. "plt.ylabel(\"Loss\")\n",
  158. "plt.xlabel(\"Epoch\")\n",
  159. "plt.legend([\"Train\", \"Validation\"], loc=\"upper right\")\n",
  160. "\n",
  161. "plt.show()"
  162. ]
  163. },
  164. {
  165. "cell_type": "code",
  166. "execution_count": null,
  167. "id": "c0ebddc4",
  168. "metadata": {},
  169. "outputs": [],
  170. "source": [
  171. "# Plot a confusion matrix of the test set predictions\n",
  172. "test_preds = np.argmax(model.predict(x_test), axis=1)\n",
  173. "conf_mat = tf.math.confusion_matrix(y_test, test_preds)\n",
  174. "plt.imshow(conf_mat, cmap=\"Blues\")\n",
  175. "plt.xlabel(\"Predicted labels\")\n",
  176. "plt.ylabel(\"True labels\")\n",
  177. "plt.xticks(np.arange(10))\n",
  178. "plt.yticks(np.arange(10))\n",
  179. "plt.colorbar()\n",
  180. "plt.show()"
  181. ]
  182. },
  183. {
  184. "cell_type": "code",
  185. "execution_count": null,
  186. "id": "9175d533",
  187. "metadata": {
  188. "scrolled": true
  189. },
  190. "outputs": [],
  191. "source": [
  192. "# Make predictions on the test set\n",
  193. "y_pred = model.predict(x_test)\n",
  194. "y_pred = np.argmax(y_pred, axis=1)\n",
  195. "\n",
  196. "# Plot some examples from the test set and their predictions\n",
  197. "fig, axes = plt.subplots(4, 4, figsize=(18, 18))\n",
  198. "for i, ax in enumerate(axes.ravel()):\n",
  199. " ax.matshow(x_test[i].reshape(28, 28), cmap='gray')\n",
  200. " ax.set_title(\"True: %d\\nPredict: %d\" % (y_test[i], y_pred[i]))\n",
  201. " ax.axis(\"off\")\n",
  202. "\n",
  203. "plt.show()"
  204. ]
  205. },
  206. {
  207. "cell_type": "code",
  208. "execution_count": null,
  209. "id": "4a6e85be",
  210. "metadata": {},
  211. "outputs": [],
  212. "source": []
  213. }
  214. ],
  215. "metadata": {
  216. "kernelspec": {
  217. "display_name": "Python 3 (ipykernel)",
  218. "language": "python",
  219. "name": "python3"
  220. },
  221. "language_info": {
  222. "codemirror_mode": {
  223. "name": "ipython",
  224. "version": 3
  225. },
  226. "file_extension": ".py",
  227. "mimetype": "text/x-python",
  228. "name": "python",
  229. "nbconvert_exporter": "python",
  230. "pygments_lexer": "ipython3",
  231. "version": "3.8.16"
  232. }
  233. },
  234. "nbformat": 4,
  235. "nbformat_minor": 5
  236. }