From 662ab4d81813e76fc039e64eb6ba2a0f88dc625b Mon Sep 17 00:00:00 2001 From: Claudio Scheer Date: Tue, 31 Mar 2020 02:23:12 -0300 Subject: [PATCH] Perceptron neural network to handle OR operator --- perceptron-or/.gitignore | 1 + perceptron-or/README.md | 3 +++ perceptron-or/main.py | 41 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 perceptron-or/.gitignore create mode 100644 perceptron-or/README.md create mode 100644 perceptron-or/main.py diff --git a/perceptron-or/.gitignore b/perceptron-or/.gitignore new file mode 100644 index 0000000..722d5e7 --- /dev/null +++ b/perceptron-or/.gitignore @@ -0,0 +1 @@ +.vscode diff --git a/perceptron-or/README.md b/perceptron-or/README.md new file mode 100644 index 0000000..ef4e0cb --- /dev/null +++ b/perceptron-or/README.md @@ -0,0 +1,3 @@ +# About + +This is an example of a perceptron neural network that can learn how to deal with the OR operator. This neural network cannot handle XOR operator, because XOR cannot be expressed as a linear function like OR. \ No newline at end of file diff --git a/perceptron-or/main.py b/perceptron-or/main.py new file mode 100644 index 0000000..b852d0d --- /dev/null +++ b/perceptron-or/main.py @@ -0,0 +1,41 @@ +import numpy as np + +# np.random.seed(0) + +train_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) +train_data_y = np.array([-1, 1, 1, 1]) + + +def threshold_activation_function(x): + return -1 if x < 0 else 1 + + +# Add the bias to the input data. +# size = 4x3 +train_data = np.insert(train_data, 0, 1, axis=1) + +epochs = 1000 +random_weight_threshold = 0.5 +learning_rate = 0.001 + +# Synapses that will be learned. +# size = 3x1 +weigths = np.random.uniform(-random_weight_threshold, random_weight_threshold, 3) + +for epoch in range(epochs): + # Go through all the data. + for t in zip(train_data, train_data_y): + # Predict the y-value based on the weights. + predicted_y = threshold_activation_function(weigths.dot(t[0])) + # The size of the input data is the size of the weights that must be updated. + for entry in range(3): + weigths[entry] = weigths[entry] - ( + learning_rate * (predicted_y - t[1]) * t[0][entry] + ) + +# Test on training data if the output is correct. +# In this case, there is no data to test. +for t in zip(train_data, train_data_y): + predicted_y = threshold_activation_function(weigths.dot(t[0])) + print(t) + print(predicted_y) -- GitLab