From 54598b3af73ef375dff5edfb24fdddd6387fa751 Mon Sep 17 00:00:00 2001
From: Michael Mutote <130656746+mr1Michael@users.noreply.github.com>
Date: Thu, 16 Nov 2023 18:24:31 +0100
Subject: [PATCH] 22202956 - Threshold Perceptron completed, skeleton of linear
 created

---
 Reinforcement_Learning/Perceptrons.py      | 45 ++++++++--------------
 Reinforcement_Learning/Solution_Testing.py | 19 +++++++++
 2 files changed, 36 insertions(+), 28 deletions(-)
 create mode 100644 Reinforcement_Learning/Solution_Testing.py

diff --git a/Reinforcement_Learning/Perceptrons.py b/Reinforcement_Learning/Perceptrons.py
index 76aeb17..07325c1 100644
--- a/Reinforcement_Learning/Perceptrons.py
+++ b/Reinforcement_Learning/Perceptrons.py
@@ -2,10 +2,14 @@ import numpy as np
 import Training_data
 
 rng = np.random.default_rng(123)
-TEACHDATA = 90000
-TESTDATA = 1
+TEACHDATA = 10000
+TESTDATA = 1000
 # ETA = 0.5
-T_NUMBER = 3
+T_NUMBER = 0
+
+
+def linear_act(val):
+    return val
 
 
 def threshold(val):
@@ -16,45 +20,30 @@ class Perceptron:
     def __init__(self, input_count, activation=threshold):
         self.input_count = input_count
         self.activation = activation
-        self.weights = rng.random(input_count + 1) * 0.3
+        self.weights = rng.random(input_count + 1)
 
     def train(self, ETA):
         teach_data = Training_data.make_testset(TEACHDATA)
-        for i in rng.permutation(TEACHDATA):
+        for i in range(TEACHDATA):
             old_weights = np.copy(self.weights)
             for j in rng.permutation(len(teach_data)):
                 T = 1 if j == T_NUMBER else 0
                 ix = np.insert(teach_data[j][i].ravel(), 0, 1)
-                # for wx in rng.permutation(len(ix)):
                 RI = self.activation(ix.dot(self.weights))
-                if not RI == T:
+                if RI != T:
                     delta = ETA * (T - self.activation(ix.dot(self.weights))) * ix
                     self.weights = self.weights + delta
-            if np.linalg.norm(old_weights - self.weights) <= 0.00000000001:
+                # print(self.weights[0], self.weights[1], self.weights[2], self.weights[3], self.weights[4], self.weights[5], self.weights[6])
+            if np.linalg.norm(old_weights - self.weights) == 0.00:
                 return self.weights
         return self.weights
 
     def test(self):
         test_data = Training_data.make_testset(TESTDATA)
         # print(test_data)
-        res = []
-        for number in test_data:
-            # print(number[0])
-            ix = np.insert(number[0].ravel(), 0, 1)
-            res.append(self.activation(ix.dot(self.weights)))
+        res = [0 for _ in range(len(test_data))]
+        for number in range(len(test_data)):
+            for sample in test_data[number]:
+                ix = np.insert(sample.ravel(), 0, 1)
+                res[number] = res[number] + (self.activation(ix.dot(self.weights)))
         return res
-
-
-def test_function():
-    input_count = 20  # the number of elements in each input array
-    ETA_list = [0.0001, 0.001, 0.01, 0.1, 0.5, 1, 1.5, 2]  # the list of values for ETA
-    results = []  # the list of results for each ETA value
-    for ETA in ETA_list:
-        p = Perceptron(input_count)  # create a perceptron instance
-        p.train(ETA)  # train the perceptron with the given ETA value
-        output = p.test()  # test the perceptron on a single test pattern
-        results.append((ETA, output))  # append the ETA value and the output to the results list
-    return results  # return the results list
-
-
-print(test_function())  # print the results list
diff --git a/Reinforcement_Learning/Solution_Testing.py b/Reinforcement_Learning/Solution_Testing.py
new file mode 100644
index 0000000..334b49e
--- /dev/null
+++ b/Reinforcement_Learning/Solution_Testing.py
@@ -0,0 +1,19 @@
+import Perceptrons
+
+
+def test_function(ETA):
+    input_count = 20
+
+    results = []
+    p = Perceptrons.Perceptron(input_count)
+    p.train(ETA)
+    output = p.test()
+    results.append((ETA, output))
+    return results
+
+
+for ETA in ([0.05, 0.1, 0.2, 0.4, 0.75, 1, 2, 5]):  # the list of values for ETA
+    for i in range(5):
+        res = test_function(ETA)
+        print(res)  # print the results list
+    print("\n\n")
-- 
GitLab