From 99937282d10f45c1b89fe6d13398bc0e637ba22b Mon Sep 17 00:00:00 2001 From: Mirko Birbaumer <mirko.birbaumer@hslu.ch> Date: Tue, 26 Apr 2022 14:59:06 +0000 Subject: [PATCH] Commenting of exercises --- .../Exercises Block 2 - Neural Networks.ipynb | 44 +++++++++++++++++++ .../Solutions to Exercises - Block 2.ipynb | 15 +------ 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/notebooks/Block_2/Exercises Block 2 - Neural Networks.ipynb b/notebooks/Block_2/Exercises Block 2 - Neural Networks.ipynb index 683552c..f0aacd7 100644 --- a/notebooks/Block_2/Exercises Block 2 - Neural Networks.ipynb +++ b/notebooks/Block_2/Exercises Block 2 - Neural Networks.ipynb @@ -700,6 +700,50 @@ "plt.plot(x_pred, y_pred)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(model.get_weights())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "w_val = model.get_weights()[0]\n", + "b_val = model.get_weights()[1]\n", + "plt.plot(data[:,0], data[:,1], 'o')\n", + "plt.axis([40, 85, -0.1, 1.2])\n", + "x_pred = np.linspace(40,85)\n", + "x_pred = np.resize(x_pred,[len(x_pred),1])\n", + "y_pred = 1 / (1 + np.exp(-x_pred*w_val - b_val))\n", + "plt.plot(x_pred, y_pred)\n", + "\n", + "# predicted probabilities\n", + "p_1 = 1 / (1 + np.exp(-x*w_val - b_val))\n", + "\n", + "# cross-entropy loss function\n", + "cross_entropy = -np.mean(y * np.log(p_1) + (1-y) * np.log(1-p_1))\n", + "print(\"Cross-entropy: \", cross_entropy)\n", + "y_pred = np.round(p_1, decimals=0).astype('int')\n", + "accuracy = np.mean(y==y_pred)\n", + "print(\"Accuracy: \", accuracy)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "model.evaluate(x, y)" + ] + }, { "cell_type": "markdown", "metadata": {}, diff --git a/notebooks/Block_2/Solutions to Exercises - Block 2.ipynb b/notebooks/Block_2/Solutions to Exercises - Block 2.ipynb index 1d12dd3..cac74e5 100644 --- a/notebooks/Block_2/Solutions to Exercises - Block 2.ipynb +++ b/notebooks/Block_2/Solutions to Exercises - Block 2.ipynb @@ -792,20 +792,9 @@ }, { "cell_type": "code", - "execution_count": 92, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "<keras.callbacks.History at 0x7ff9e419c450>" - ] - }, - "execution_count": 92, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "l0 = tf.keras.layers.Dense(units=1, activation = tf.nn.sigmoid, input_shape=[1])\n", "model = tf.keras.Sequential([l0])\n", -- GitLab