-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperceptron.py
More file actions
106 lines (71 loc) · 2.64 KB
/
perceptron.py
File metadata and controls
106 lines (71 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-
"""PERCEPTRON
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1Usd8JvlUB4HVYvSS3TRwkf_9M9Cr-doC
# ANN - Artificial Neural Network
Usar a arquitetura perceptron para resolver o problema de portas lógicas
"""
from sklearn.linear_model import Perceptron
import numpy as np
"""## Logic gate: AND"""
data = [[0, 0], [0, 1], [1, 0], [1, 1]]
target = [0, 0, 0, 1]
classifier = Perceptron(max_iter=50000)
classifier.fit(data, target)
print('Accuracy: ', classifier.score(data, target))
predictions = classifier.predict(data)
print('Predictions: ', predictions)
"""## Logic gate: OR"""
data = [[0, 0], [0, 1], [1, 0], [1, 1]]
target = [0, 1, 1, 1]
classifier = Perceptron(max_iter=50000)
classifier.fit(data, target)
print('Accuracy: ', classifier.score(data, target))
predictions = classifier.predict(data)
print('Predictions: ', predictions)
"""## Logic gate: XOR"""
# WITH A SINGLE PERCEPTRON
data = [[0, 0], [0, 1], [1, 0], [1, 1]]
target = [0, 1, 1, 0]
classifier = Perceptron(max_iter=999000000) # 999 million
classifier.fit(data, target)
print('Accuracy: ', classifier.score(data, target))
predictions = classifier.predict(data)
print('Predictions: ', predictions)
# WITH TWO PERCEPTRONS
data = [[0, 0], [0, 1], [1, 0], [1, 1]]
targetAND = [0, 0, 0, 1]
targetOR = [0, 1, 1, 1]
targetXOR = [0, 1, 1, 0]
# aprender AND
classifierAND = Perceptron(max_iter=500000)
classifierAND.fit(data, targetAND)
print('Accuracy AND: ', classifierAND.score(data, targetAND))
# aprender OR
classifierOR = Perceptron(max_iter=500000)
classifierOR.fit(data, targetOR)
print('Accuracy OR: ', classifierOR.score(data, targetOR))
first_layer_output = np.column_stack([
classifierOR.predict(data), # OR output
classifierAND.predict(data) # AND output
])
classifierXOR = Perceptron(max_iter=999000000)
classifierXOR.fit(first_layer_output, targetXOR)
# Predict XOR
xor_predictions = classifierXOR.predict(first_layer_output)
print('Accuracy XOR: ', classifierXOR.score(first_layer_output, targetXOR))
print("XOR Predictions:", xor_predictions)
"""## Logic gate: XOR with MLP"""
from sklearn.neural_network import MLPClassifier
data = [[0, 0], [0, 1], [1, 0], [1, 1]]
target = [0, 1, 1, 0]
# Create an MLP classifier with one hidden layer of 2 neurons
mlp_classifier = MLPClassifier(hidden_layer_sizes=(2,), activation='logistic', solver='lbfgs', max_iter=10000)
# Train the classifier
mlp_classifier.fit(data, target)
# Evaluate the classifier
print('Accuracy: ', mlp_classifier.score(data, target))
# Make predictions
predictions = mlp_classifier.predict(data)
print('Predictions: ', predictions)