Last active
March 5, 2023 12:50
-
-
Save Thomascountz/77670d1fd621364bc41a7094563a7b9c to your computer and use it in GitHub Desktop.
Perceptron in Python v.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
MIT License | |
Copyright (c) 2018 Thomas Countz | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
""" | |
import numpy as np | |
class Perceptron(object): | |
def __init__(self, no_of_inputs, threshold=100, learning_rate=0.01): | |
self.threshold = threshold | |
self.learning_rate = learning_rate | |
self.weights = np.zeros(no_of_inputs + 1) | |
def predict(self, inputs): | |
summation = np.dot(inputs, self.weights[1:]) + self.weights[0] | |
if summation > 0: | |
activation = 1 | |
else: | |
activation = 0 | |
return activation | |
def train(self, training_inputs, labels): | |
for _ in range(self.threshold): | |
for inputs, label in zip(training_inputs, labels): | |
prediction = self.predict(inputs) | |
self.weights[1:] += self.learning_rate * (label - prediction) * inputs | |
self.weights[0] += self.learning_rate * (label - prediction) |
How can this be modified for OR?
How can this be modified for OR?
To train for OR
, you don't need to modify the code above. Instead, you update the training input such that the labels match the result of a logical OR
and then train the model just as you would with AND
.
import numpy as np
from perceptron import Perceptron
training_inputs = []
training_inputs.append(np.array([1, 1]))
training_inputs.append(np.array([1, 0]))
training_inputs.append(np.array([0, 1]))
training_inputs.append(np.array([0, 0]))
labels = np.array([1, 1, 1, 0])
perceptron = Perceptron(2)
perceptron.train(training_inputs, labels)
inputs = np.array([1, 1])
perceptron.predict(inputs)
#=> 1
inputs = np.array([0, 1])
perceptron.predict(inputs)
#=> 1
inputs = np.array([0, 0])
perceptron.predict(inputs)
#=> 0
This demonstrates the flexibility of the perceptron/machine learning algorithms; they are adaptable general-purpose solvers!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you for clearing that up.