-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnn_multicategory.py
More file actions
41 lines (33 loc) · 2.16 KB
/
nn_multicategory.py
File metadata and controls
41 lines (33 loc) · 2.16 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
# USAGE
# python3 nn_multicategory.py
# import the necessary packages
from modules.nn import NeuralNetwork
import numpy as np
# construct the multicategory dataset
X = np.array([[-0.55, 1.85], [-0.35, 1.35], [-0.3, 1.69], [-0.28, 2], [-0.23, 2.15], [-0.28, 2.75], [-0.08, 1.7], [-0.08, 1.75], [-0.1, 2.05], [0.18, 1.41], [0.2, 1.75], [0.12, 2.18], [0.16, 2.23], [0.1, 2.4], [0.25, 1.95], [0.27, 2.02], [0.33, 2.01], [0.35, 2.01], [0.34, 1.9], [0.45, 1.74],
[1.8, 1.3], [1.76, 1.42], [1.76, 1.63], [1.77, 2.15], [1.85, 1.65], [1.8, 2.15], [1.92, 1.47], [1.92, 1.8], [1.91, 2.1], [2.05, 1.55], [2.05, 1.87], [2.05, 2.05], [2.12, 1.6], [2.3, 1.94], [2.28, 1.95], [2.26, 2.12], [2.28, 2.48], [2.7, 1.4], [2.57, 1.49], [2.62, 2.12],
[1.61, -0.5], [1.67, -0.08], [1.78, 0.62], [1.78, 0], [1.91, 0.4], [1.89, -0.12], [1.98, -0.18], [2, -0.05], [1.98, 0], [2.06, -0.45], [2.06, -0.4], [2.1, 0], [2.16, -0.15], [2.13, 0.01], [2.18, 0.1], [2.23, 0.35], [2.29, -0.31], [2.39, -0.27], [2.38, 0.08], [2.21, -0.1]])
y = np.array([[1,0,0], [1,0,0], [1,0,0], [1,0,0], [1,0,0], [1,0,0], [1,0,0], [1,0,0], [1,0,0], [1,0,0], [1,0,0], [1,0,0], [1,0,0], [1,0,0], [1,0,0], [1,0,0], [1,0,0], [1,0,0], [1,0,0], [1,0,0], [0,1,0], [0,1,0], [0,1,0], [0,1,0], [0,1,0], [0,1,0], [0,1,0], [0,1,0], [0,1,0], [0,1,0], [0,1,0], [0,1,0], [0,1,0], [0,1,0], [0,1,0], [0,1,0], [0,1,0], [0,1,0], [0,1,0], [0,1,0], [0,0,1], [0,0,1], [0,0,1], [0,0,1], [0,0,1], [0,0,1], [0,0,1], [0,0,1], [0,0,1], [0,0,1], [0,0,1], [0,0,1], [0,0,1], [0,0,1], [0,0,1], [0,0,1], [0,0,1], [0,0,1], [0,0,1], [0,0,1]])
# define our 3-3 neural network and train it
nn = NeuralNetwork([2, 3], alpha=0.1)
nn.fit(X, y, epochs=20)
# now that our network is trained, loop over the multicategory data points
# pred={:.4f}
i=0
for (x, target) in zip(X, y):
# make a prediction on the data point and display the result
# to our console
pred = nn.predict(x)
if i<20:
step = 1 if pred[0][0] > 0.5 else 0
else:
if i<40:
step = 1 if pred[0][1] > 0.5 else 0
else:
step = 1 if pred[0][2] > 0.5 else 0
print("[INFO] data={}, ground-truth={}, pred={}, step={}".format(
x, target, pred[0], step))
i=i+1
#Imprime el vector de pesos
W = nn.weights()
print(W)