forked from neokarn/computer_vision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example6_1_Keras_Basic_ANN.py
59 lines (43 loc) · 1.41 KB
/
example6_1_Keras_Basic_ANN.py
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
from keras.models import Model
from keras.layers import Input, Dense
#from keras.utils import plot_model
import numpy as np
#Create model
input = Input(shape=(3,))
hidden = Dense(4, activation='tanh')(input)
output = Dense(1, activation='sigmoid')(hidden) #Binary Classification
model = Model(inputs=input, outputs=output)
model.compile(optimizer='sgd',
loss='binary_crossentropy',
metrics=['accuracy'])
model.summary()
#for plot_model
#install Graphviz (https://www.graphviz.org/)
#set PATH to Graphviz\bin (eg. C:\Program Files (x86)\Graphviz2.38\bin)
#install pydot (https://pypi.org/project/pydot/)
#conda install -c anaconda pydot
#pip install pydot
#plot_model(model, to_file='model1.png')
#Train model
x_train = np.asarray([[1,0,1],
[4, 2, 0],
[-4, 0, 1],
[1,2,3],
[-1,-2,3],
[0,-1,3],
[1, 0, 0],
[-1, 0, -2],
[4, -2, 7],
[-1,-1,4]])
y_train = np.asarray([1,1,0,1,0,1,0,0,1,0])
model.fit(x_train, y_train, epochs=200, batch_size=10)
#Test model
x_test = np.asarray([[1,5,1],
[5,-1,3],
[-5,0,1],
[0,0,0]])
y_test = np.asarray([1,1,0,0])
y_pred = model.predict(x_test)
print(y_pred)
score = model.evaluate(x_test, y_test)
print(score)