• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Jun 20th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import numpy as np
2.
3. class Perceptron(object):
4.
5.     def __init__(self, no_of_inputs, threshold=100, learning_rate=0.01):
6.         self.threshold = threshold
7.         self.learning_rate = learning_rate
8.         self.weights = np.zeros(no_of_inputs + 1)
9.
10.     def predict(self, inputs):
11.         summation = np.dot(inputs, self.weights[1:]) + self.weights[0]
12.         if summation > 0:
13.           activation = 1
14.         else:
15.           activation = 0
16.         return activation
17.
18.     def train(self, training_inputs, labels):
19.         for _ in range(self.threshold):
20.             for inputs, label in zip(training_inputs, labels):
21.                 prediction = self.predict(inputs)
22.                 self.weights[1:] += self.learning_rate * (label - prediction) * inputs
23.                 self.weights[0] += self.learning_rate * (label - prediction)
24.
25. training_inputs = []
26. training_inputs.append(np.array([1, 1]))
27. training_inputs.append(np.array([1, 0]))
28. training_inputs.append(np.array([0, 1]))
29. training_inputs.append(np.array([0, 0]))
30.
31. labels = np.array([1, 0, 0, 0])
32.
33. perceptron = Perceptron(2)
34. perceptron.train(training_inputs, labels)
35.
36. inputs = np.array([1, 1])
37. print(perceptron.predict(inputs))
38. #=> 1
39.
40. inputs = np.array([0, 1])
41. print(perceptron.predict(inputs))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top