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.

240 lines
6.8 KiB

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. "plt.imshow(dense_weights, cmap='coolwarm')\n",
  143. "plt.colorbar()\n",
  144. "plt.title('weights in the output layer')\n",
  145. "plt.show()"
  146. ]
  147. },
  148. {
  149. "cell_type": "code",
  150. "execution_count": null,
  151. "id": "13118686",
  152. "metadata": {},
  153. "outputs": [],
  154. "source": [
  155. "# Evaluate the model on the test set\n",
  156. "test_loss, test_acc = model.evaluate(x_test, y_test)\n",
  157. "print('Test accuracy:', test_acc)"
  158. ]
  159. },
  160. {
  161. "cell_type": "code",
  162. "execution_count": null,
  163. "id": "40f35fe5",
  164. "metadata": {},
  165. "outputs": [],
  166. "source": [
  167. "# Plot loss and accuracy\n",
  168. "plt.figure(figsize=(12, 4))\n",
  169. "\n",
  170. "# Plot the loss and accuracy for training and validation data\n",
  171. "plt.subplot(1, 2, 1)\n",
  172. "plt.plot(history.history['loss'], label='training loss')\n",
  173. "plt.plot(history.history['val_loss'], label='validation loss')\n",
  174. "plt.xlabel('Epoch')\n",
  175. "plt.ylabel('Loss')\n",
  176. "plt.legend()\n",
  177. "\n",
  178. "plt.subplot(1, 2, 2)\n",
  179. "plt.plot(history.history['accuracy'])\n",
  180. "plt.plot(history.history['val_accuracy'])\n",
  181. "plt.xlabel('Epoch')\n",
  182. "plt.ylabel('Accuracy')\n",
  183. "plt.legend()\n",
  184. "\n",
  185. "plt.show()\n",
  186. "\n"
  187. ]
  188. },
  189. {
  190. "cell_type": "code",
  191. "execution_count": null,
  192. "id": "6176ce1e",
  193. "metadata": {},
  194. "outputs": [],
  195. "source": [
  196. "# Make predictions on the test set\n",
  197. "y_pred = model.predict(x_test)\n",
  198. "y_pred = np.argmax(y_pred, axis=1)"
  199. ]
  200. },
  201. {
  202. "cell_type": "code",
  203. "execution_count": null,
  204. "id": "3635ded5",
  205. "metadata": {},
  206. "outputs": [],
  207. "source": [
  208. "# Plot some examples from the test set and their predictions\n",
  209. "fig, axes = plt.subplots(4, 4, figsize=(14, 14))\n",
  210. "for i, ax in enumerate(axes.ravel()):\n",
  211. " ax.matshow(x_test[i].reshape(28, 28), cmap='gray')\n",
  212. " ax.set_title(\"True: %d\\nPredict: %d\" % (np.argmax(y_test[i]), y_pred[i]))\n",
  213. " ax.axis(\"off\")\n",
  214. "\n",
  215. "plt.show()"
  216. ]
  217. }
  218. ],
  219. "metadata": {
  220. "kernelspec": {
  221. "display_name": "Python 3 (ipykernel)",
  222. "language": "python",
  223. "name": "python3"
  224. },
  225. "language_info": {
  226. "codemirror_mode": {
  227. "name": "ipython",
  228. "version": 3
  229. },
  230. "file_extension": ".py",
  231. "mimetype": "text/x-python",
  232. "name": "python",
  233. "nbconvert_exporter": "python",
  234. "pygments_lexer": "ipython3",
  235. "version": "3.8.16"
  236. }
  237. },
  238. "nbformat": 4,
  239. "nbformat_minor": 5
  240. }