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.

177 lines
4.8 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "id": "0b7d591a",
  6. "metadata": {},
  7. "source": [
  8. "\n",
  9. " An example of the minimzer usage in tensorflow\n",
  10. " the loss function is plotted and the result in terms of a line\n"
  11. ]
  12. },
  13. {
  14. "cell_type": "code",
  15. "execution_count": null,
  16. "id": "270932f3",
  17. "metadata": {},
  18. "outputs": [],
  19. "source": [
  20. "import numpy as np\n",
  21. "import matplotlib.pyplot as plt\n",
  22. "import tensorflow as tf"
  23. ]
  24. },
  25. {
  26. "cell_type": "code",
  27. "execution_count": null,
  28. "id": "77cd99a8",
  29. "metadata": {},
  30. "outputs": [],
  31. "source": [
  32. "# Define the training data\n",
  33. "train_X = np.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,\n",
  34. " 7.042,10.791,5.313,7.997,5.654,9.27,3.1])\n",
  35. "train_Y = np.asarray([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,\n",
  36. " 2.827,3.465,1.65,2.904,2.42,2.94,1.3])\n"
  37. ]
  38. },
  39. {
  40. "cell_type": "markdown",
  41. "id": "f39cbcd9",
  42. "metadata": {},
  43. "source": [
  44. "The input to the model is represented by the train_X \n",
  45. "Y_train represents the target or the truth values for the training data\n",
  46. "The model will recieve train_X and make predictions on the weights\n",
  47. "The difference between these predictions and the actual target values\n",
  48. "train_Y will be used to update the weights and minimize the loss function."
  49. ]
  50. },
  51. {
  52. "cell_type": "code",
  53. "execution_count": null,
  54. "id": "ed8449c3",
  55. "metadata": {},
  56. "outputs": [],
  57. "source": [
  58. "# Define the model to a simple linear regression with only one dense layer and\n",
  59. "# no activation function for the first layer all train_X points are input\n",
  60. "\n",
  61. "# model = tf.keras.models.Sequential([\n",
  62. "# tf.keras.layers.Dense(1, input_shape=[1])\n",
  63. "#])"
  64. ]
  65. },
  66. {
  67. "cell_type": "code",
  68. "execution_count": null,
  69. "id": "71e072b4",
  70. "metadata": {},
  71. "outputs": [],
  72. "source": [
  73. "# This model has 2 dense layers the first with relu activation\n",
  74. "# and the 2nd layer has 1 output unit and uses the default\n",
  75. "# linear activation function.\n",
  76. "\n",
  77. "model = tf.keras.models.Sequential([\n",
  78. " tf.keras.layers.Dense(17, activation='relu',input_shape=[1]),\n",
  79. " tf.keras.layers.Dense(1)\n",
  80. "])"
  81. ]
  82. },
  83. {
  84. "cell_type": "code",
  85. "execution_count": null,
  86. "id": "5fabf184",
  87. "metadata": {},
  88. "outputs": [],
  89. "source": [
  90. "# different optimizer methods can be enabled\n",
  91. "\n",
  92. "model.compile(optimizer=tf.keras.optimizers.Adam(0.01), loss='mean_squared_error')\n",
  93. "#model.compile(optimizer=tf.keras.optimizers.SGD(0.01), loss='mean_squared_error')\n",
  94. "#model.compile(optimizer=tf.keras.optimizers.Adagrad(learning_rate=0.01), loss='mean_squared_error')\n",
  95. "#model.compile(optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.01), loss='mean_squared_error')\n",
  96. "#model.compile(optimizer=tf.keras.optimizers.Ftrl(learning_rate=0.015), loss='mean_squared_error')"
  97. ]
  98. },
  99. {
  100. "cell_type": "code",
  101. "execution_count": null,
  102. "id": "22c4124f",
  103. "metadata": {},
  104. "outputs": [],
  105. "source": [
  106. "# Train the model and access training parameters\n",
  107. "history = model.fit(train_X, train_Y, epochs=60)\n",
  108. "print(history.params)"
  109. ]
  110. },
  111. {
  112. "cell_type": "code",
  113. "execution_count": null,
  114. "id": "46615960",
  115. "metadata": {},
  116. "outputs": [],
  117. "source": [
  118. "# Get the weights of the Dense layer\n",
  119. "weights = model.layers[0].get_weights()\n",
  120. "# Print the weight matrix and bias vector\n",
  121. "print('Weight matrix shape:', weights[0].shape)\n",
  122. "print('Bias vector shape:', weights[1].shape)\n",
  123. "print (weights[0])"
  124. ]
  125. },
  126. {
  127. "cell_type": "code",
  128. "execution_count": null,
  129. "id": "da12fc5b",
  130. "metadata": {},
  131. "outputs": [],
  132. "source": [
  133. "# Plot the loss function\n",
  134. "plt.plot(history.history['loss'])\n",
  135. "plt.title(\"Loss Function\")\n",
  136. "plt.xlabel(\"Epoch\")\n",
  137. "plt.ylabel(\"Loss\")\n",
  138. "plt.show()\n",
  139. "\n",
  140. "# Plot the input data and the predicted values\n",
  141. "plt.plot(train_X, train_Y, 'ro', label=\"Original Data\")\n",
  142. "plt.plot(train_X, model.predict(train_X), label=\"Predicted\")\n",
  143. "plt.legend()\n",
  144. "plt.show()"
  145. ]
  146. },
  147. {
  148. "cell_type": "code",
  149. "execution_count": null,
  150. "id": "60417d5f",
  151. "metadata": {},
  152. "outputs": [],
  153. "source": []
  154. }
  155. ],
  156. "metadata": {
  157. "kernelspec": {
  158. "display_name": "Python 3 (ipykernel)",
  159. "language": "python",
  160. "name": "python3"
  161. },
  162. "language_info": {
  163. "codemirror_mode": {
  164. "name": "ipython",
  165. "version": 3
  166. },
  167. "file_extension": ".py",
  168. "mimetype": "text/x-python",
  169. "name": "python",
  170. "nbconvert_exporter": "python",
  171. "pygments_lexer": "ipython3",
  172. "version": "3.8.16"
  173. }
  174. },
  175. "nbformat": 4,
  176. "nbformat_minor": 5
  177. }