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.

270 lines
7.7 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": null,
  6. "id": "13fa2f64",
  7. "metadata": {},
  8. "outputs": [],
  9. "source": [
  10. "# In this example, we used the TensorFlow library to load the MNIST data,\n",
  11. "# define an MLP model with three dense layers, compile the model, train it\n",
  12. "# for 10 epochs, evaluate it on the test set, and make predictions on\n",
  13. "# the test set. Finally, we plot some examples of the predictions made\n",
  14. "# by the model."
  15. ]
  16. },
  17. {
  18. "cell_type": "code",
  19. "execution_count": null,
  20. "id": "1c4405f8",
  21. "metadata": {},
  22. "outputs": [],
  23. "source": [
  24. "import tensorflow as tf\n",
  25. "from tensorflow import keras\n",
  26. "import numpy as np\n",
  27. "import matplotlib.pyplot as plt\n",
  28. "(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()"
  29. ]
  30. },
  31. {
  32. "cell_type": "code",
  33. "execution_count": null,
  34. "id": "1e7e58fb",
  35. "metadata": {},
  36. "outputs": [],
  37. "source": [
  38. "# Normalize the pixel values to be between 0 and 1\n",
  39. "x_train = x_train / 255\n",
  40. "x_test = x_test / 255"
  41. ]
  42. },
  43. {
  44. "cell_type": "code",
  45. "execution_count": null,
  46. "id": "3d8a7370",
  47. "metadata": {},
  48. "outputs": [],
  49. "source": [
  50. "# Flatten the 2D images into 1D arrays\n",
  51. "x_train = x_train.reshape(x_train.shape[0], -1)\n",
  52. "x_test = x_test.reshape(x_test.shape[0], -1)"
  53. ]
  54. },
  55. {
  56. "cell_type": "code",
  57. "execution_count": null,
  58. "id": "c2df0f54",
  59. "metadata": {},
  60. "outputs": [],
  61. "source": [
  62. "# Convert the labels into one-hot encoded arrays\n",
  63. "y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)\n",
  64. "y_test = tf.keras.utils.to_categorical(y_test, num_classes=10)"
  65. ]
  66. },
  67. {
  68. "cell_type": "code",
  69. "execution_count": null,
  70. "id": "e2b249ea",
  71. "metadata": {},
  72. "outputs": [],
  73. "source": [
  74. "# Define the model\n",
  75. "# The number of parameters depends on the shapes and sizes of the layers.\n",
  76. "# In the given model, the first layer Dense(512, activation='relu',\n",
  77. "# input_shape=(784,)) has 784 input nodes and 512 output nodes. Therefore,\n",
  78. "# the number of parameters in this layer would be (784 * 512) + 512 = 401920,\n",
  79. "# where the +512 term is for the bias terms.\n",
  80. "# The second layer also has 512 input nodes and 512 output nodes, which makes\n",
  81. "# 512512 = 262,144 parameters. The third and last layer has 512 input nodes\n",
  82. "# and 10 output nodes, which makes 512*10 = 5,120 parameters.\n",
  83. "model = tf.keras.models.Sequential()\n",
  84. "model.add(tf.keras.layers.Dense(512, activation='relu', input_shape=(784,)))\n",
  85. "model.add(tf.keras.layers.Dense(512, activation='relu'))\n",
  86. "model.add(tf.keras.layers.Dense(10, activation='softmax'))"
  87. ]
  88. },
  89. {
  90. "cell_type": "code",
  91. "execution_count": null,
  92. "id": "bab8730a",
  93. "metadata": {},
  94. "outputs": [],
  95. "source": [
  96. "# over come overfitting by regularization\n",
  97. "#model = tf.keras.models.Sequential([\n",
  98. "# tf.keras.layers.Dense(64, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001),input_shape=(784,)),\n",
  99. "# tf.keras.layers.Dense(64, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001)),\n",
  100. "# tf.keras.layers.Dense(10, activation='softmax')\n",
  101. "#])"
  102. ]
  103. },
  104. {
  105. "cell_type": "code",
  106. "execution_count": null,
  107. "id": "e3223c61",
  108. "metadata": {},
  109. "outputs": [],
  110. "source": [
  111. "# Compile the model\n",
  112. "model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])"
  113. ]
  114. },
  115. {
  116. "cell_type": "code",
  117. "execution_count": null,
  118. "id": "51e7758f",
  119. "metadata": {},
  120. "outputs": [],
  121. "source": [
  122. "# Train the model and record the history\n",
  123. "history = model.fit(x_train, y_train, epochs=10, batch_size=64, validation_data=(x_test, y_test))"
  124. ]
  125. },
  126. {
  127. "cell_type": "code",
  128. "execution_count": null,
  129. "id": "e3240069",
  130. "metadata": {},
  131. "outputs": [],
  132. "source": [
  133. "# Get the weights of the Dense layer\n",
  134. "# plot the weights as a heatmap or image, where the weights are represented\n",
  135. "# as pixel values.\n",
  136. "# model.layers[2].get_weights()[0] returns only the weights of the third\n",
  137. "# layer. If you wanted to get the biases, you would use\n",
  138. "# model.layers[2].get_weights()[1].\n",
  139. "dense_weights = model.layers[2].get_weights()[0]\n",
  140. "\n",
  141. "# Plot the weights as a heatmap\n",
  142. "fig, ax = plt.subplots(figsize=(12, 36))\n",
  143. "im = ax.imshow(dense_weights, cmap='coolwarm')\n",
  144. "plt.colorbar(im, ax=ax)\n",
  145. "ax.set_title('Weights in the Output Layer')\n",
  146. "ax.set_xlabel('Neurons in the Output Layer')\n",
  147. "ax.set_ylabel('Neurons in the Previous Layer')\n",
  148. "plt.show()"
  149. ]
  150. },
  151. {
  152. "cell_type": "code",
  153. "execution_count": null,
  154. "id": "13118686",
  155. "metadata": {},
  156. "outputs": [],
  157. "source": [
  158. "# Evaluate the model on the test set\n",
  159. "test_loss, test_acc = model.evaluate(x_test, y_test)\n",
  160. "print('Test accuracy:', test_acc)"
  161. ]
  162. },
  163. {
  164. "cell_type": "code",
  165. "execution_count": null,
  166. "id": "40f35fe5",
  167. "metadata": {},
  168. "outputs": [],
  169. "source": [
  170. "# Plot loss and accuracy\n",
  171. "plt.figure(figsize=(12, 4))\n",
  172. "\n",
  173. "# Plot the loss and accuracy for training and validation data\n",
  174. "plt.subplot(1, 2, 1)\n",
  175. "plt.plot(history.history['loss'], label='training loss')\n",
  176. "plt.plot(history.history['val_loss'], label='validation loss')\n",
  177. "plt.xlabel('Epoch')\n",
  178. "plt.ylabel('Loss')\n",
  179. "plt.legend()\n",
  180. "\n",
  181. "plt.subplot(1, 2, 2)\n",
  182. "plt.plot(history.history['accuracy'])\n",
  183. "plt.plot(history.history['val_accuracy'])\n",
  184. "plt.xlabel('Epoch')\n",
  185. "plt.ylabel('Accuracy')\n",
  186. "plt.legend()\n",
  187. "\n",
  188. "plt.show()\n",
  189. "\n"
  190. ]
  191. },
  192. {
  193. "cell_type": "code",
  194. "execution_count": null,
  195. "id": "e882afea",
  196. "metadata": {},
  197. "outputs": [],
  198. "source": [
  199. "# Plot a confusion matrix of the test set predictions\n",
  200. "test_preds = np.argmax(model.predict(x_test), axis=1)\n",
  201. "conf_mat = tf.math.confusion_matrix(y_test.argmax(axis=1), test_preds)\n",
  202. "plt.imshow(conf_mat, cmap=\"Blues\")\n",
  203. "plt.xlabel(\"Predicted labels\")\n",
  204. "plt.ylabel(\"True labels\")\n",
  205. "plt.xticks(np.arange(10))\n",
  206. "plt.yticks(np.arange(10))\n",
  207. "plt.colorbar()\n",
  208. "plt.show()"
  209. ]
  210. },
  211. {
  212. "cell_type": "code",
  213. "execution_count": null,
  214. "id": "6176ce1e",
  215. "metadata": {},
  216. "outputs": [],
  217. "source": [
  218. "# Make predictions on the test set\n",
  219. "y_pred = model.predict(x_test)\n",
  220. "y_pred = np.argmax(y_pred, axis=1)"
  221. ]
  222. },
  223. {
  224. "cell_type": "code",
  225. "execution_count": null,
  226. "id": "3635ded5",
  227. "metadata": {},
  228. "outputs": [],
  229. "source": [
  230. "# Plot some examples from the test set and their predictions\n",
  231. "fig, axes = plt.subplots(4, 4, figsize=(14, 14))\n",
  232. "for i, ax in enumerate(axes.ravel()):\n",
  233. " ax.matshow(x_test[i].reshape(28, 28), cmap='gray')\n",
  234. " ax.set_title(\"True: %d\\nPredict: %d\" % (np.argmax(y_test[i]), y_pred[i]))\n",
  235. " ax.axis(\"off\")\n",
  236. "\n",
  237. "plt.show()"
  238. ]
  239. },
  240. {
  241. "cell_type": "code",
  242. "execution_count": null,
  243. "id": "71f1cb93",
  244. "metadata": {},
  245. "outputs": [],
  246. "source": []
  247. }
  248. ],
  249. "metadata": {
  250. "kernelspec": {
  251. "display_name": "Python 3 (ipykernel)",
  252. "language": "python",
  253. "name": "python3"
  254. },
  255. "language_info": {
  256. "codemirror_mode": {
  257. "name": "ipython",
  258. "version": 3
  259. },
  260. "file_extension": ".py",
  261. "mimetype": "text/x-python",
  262. "name": "python",
  263. "nbconvert_exporter": "python",
  264. "pygments_lexer": "ipython3",
  265. "version": "3.8.16"
  266. }
  267. },
  268. "nbformat": 4,
  269. "nbformat_minor": 5
  270. }