From 3222ada0558043cb9b3ad2aa5093898d8820dcad Mon Sep 17 00:00:00 2001 From: krishnaw14 Date: Thu, 15 Mar 2018 22:53:20 +0530 Subject: [PATCH 1/5] Correction in the formula for mean square error --- neural_nets.ipynb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/neural_nets.ipynb b/neural_nets.ipynb index a6bb6f43b..9c5db9a56 100644 --- a/neural_nets.ipynb +++ b/neural_nets.ipynb @@ -82,7 +82,7 @@ "\n", "In both the Perceptron and the Neural Network, we are using the Backpropagation algorithm to train our weights. Basically it achieves that by propagating the errors from our last layer into our first layer, this is why it is called Backpropagation. In order to use Backpropagation, we need a cost function. This function is responsible for indicating how good our neural network is for a given example. One common cost function is the *Mean Squared Error* (MSE). This cost function has the following format:\n", "\n", - "$$MSE=\\frac{1}{2} \\sum_{i=1}^{n}(y - \\hat{y})^{2}$$\n", + "$$MSE=\\frac{1}{n} \\sum_{i=1}^{n}(y - \\hat{y})^{2}$$\n", "\n", "Where `n` is the number of training examples, $\\hat{y}$ is our prediction and $y$ is the correct prediction for the example.\n", "\n", @@ -221,14 +221,14 @@ "language_info": { "codemirror_mode": { "name": "ipython", - "version": 3 + "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.5.3" + "pygments_lexer": "ipython2", + "version": "2.7.14" } }, "nbformat": 4, From 26eaaf6c5fb93a6f1d7ccc39a75ca5ff60ca7287 Mon Sep 17 00:00:00 2001 From: krishnaw14 Date: Fri, 16 Mar 2018 01:35:58 +0530 Subject: [PATCH 2/5] Added cross-entropy loss --- learning.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/learning.py b/learning.py index 32cf73d81..4772a6128 100644 --- a/learning.py +++ b/learning.py @@ -21,6 +21,10 @@ def euclidean_distance(X, Y): return math.sqrt(sum((x - y)**2 for x, y in zip(X, Y))) +def cross_entropy_loss(X,Y): + n=len(X) + return (-1.0/n)*sum(x*math.log(y)+(1-x)*math.log(1-y) for x,y in zip(X,Y) ) + def rms_error(X, Y): return math.sqrt(ms_error(X, Y)) From 2ffd60cf6cd7d89e97e1f49e55f008c033735755 Mon Sep 17 00:00:00 2001 From: krishnaw14 Date: Fri, 16 Mar 2018 01:36:28 +0530 Subject: [PATCH 3/5] Test case for cross-entropy loss --- tests/test_learning.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_learning.py b/tests/test_learning.py index 6afadc282..cae405c9a 100644 --- a/tests/test_learning.py +++ b/tests/test_learning.py @@ -18,6 +18,16 @@ def test_euclidean(): distance = euclidean_distance([0, 0, 0], [0, 0, 0]) assert distance == 0 +def test_cross_entropy(): + loss=cross_entropy_loss([1,0], [0,9, 0.3]) + assert round(loss,2)==0.23 + + loss=cross_entropy_loss([1,0,0,1], [0.9,0.3,0.5,0.75]) + assert round(loss,2)==0.36 + + loss=cross_entropy_loss([1,0,0,1,1,0,1,1], [0.9,0.3,0.5,0.75,0.85,0.14,0.93,0.79]) + assert round(loss,2)==0.26 + def test_rms_error(): assert rms_error([2, 2], [2, 2]) == 0 From b9464f37aab37885f32d93fc1c0110483902ac52 Mon Sep 17 00:00:00 2001 From: Krishna Wadhwani Date: Fri, 16 Mar 2018 02:12:15 +0530 Subject: [PATCH 4/5] Decimal point mistake --- tests/test_learning.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_learning.py b/tests/test_learning.py index cae405c9a..ac34648b5 100644 --- a/tests/test_learning.py +++ b/tests/test_learning.py @@ -19,7 +19,7 @@ def test_euclidean(): assert distance == 0 def test_cross_entropy(): - loss=cross_entropy_loss([1,0], [0,9, 0.3]) + loss=cross_entropy_loss([1,0], [0.9, 0.3]) assert round(loss,2)==0.23 loss=cross_entropy_loss([1,0,0,1], [0.9,0.3,0.5,0.75]) From 7637470b9361a6017ca53d345c904a8b6d86a8a3 Mon Sep 17 00:00:00 2001 From: Krishna Wadhwani Date: Fri, 16 Mar 2018 09:40:48 +0530 Subject: [PATCH 5/5] Added spaces around = and == --- tests/test_learning.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_learning.py b/tests/test_learning.py index ac34648b5..ec3a2f188 100644 --- a/tests/test_learning.py +++ b/tests/test_learning.py @@ -19,14 +19,14 @@ def test_euclidean(): assert distance == 0 def test_cross_entropy(): - loss=cross_entropy_loss([1,0], [0.9, 0.3]) - assert round(loss,2)==0.23 + loss = cross_entropy_loss([1,0], [0.9, 0.3]) + assert round(loss,2) == 0.23 - loss=cross_entropy_loss([1,0,0,1], [0.9,0.3,0.5,0.75]) - assert round(loss,2)==0.36 + loss = cross_entropy_loss([1,0,0,1], [0.9,0.3,0.5,0.75]) + assert round(loss,2) == 0.36 - loss=cross_entropy_loss([1,0,0,1,1,0,1,1], [0.9,0.3,0.5,0.75,0.85,0.14,0.93,0.79]) - assert round(loss,2)==0.26 + loss = cross_entropy_loss([1,0,0,1,1,0,1,1], [0.9,0.3,0.5,0.75,0.85,0.14,0.93,0.79]) + assert round(loss,2) == 0.26 def test_rms_error():