forked from neokarn/computer_vision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example6_4_validation.py
49 lines (38 loc) · 1.24 KB
/
example6_4_validation.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
from keras.models import Sequential
from keras.layers import Input, Dense
from keras.utils import to_categorical
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import numpy as np
#Create model by using sequential structure
model = Sequential()
model.add(Dense(5, input_dim=5, activation='tanh'))
model.add(Dense(5, activation='tanh'))
model.add(Dense(3, activation='softmax'))
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.summary()
#Read data from file
data = np.asarray([[float(num) for num in line.split(',')] for line in open('data.csv')])
#Train Model
x_train = data[0:100,0:5]
y_train = data[0:100,5]
y_train = to_categorical(y_train)
x_val = data[100:120,0:5]
y_val = data[100:120,5]
y_val = to_categorical(y_val)
h = model.fit(x_train, y_train,
epochs=1000, batch_size=5,
validation_data=(x_val,y_val))
plt.plot(h.history['acc'])
plt.plot(h.history['val_acc'])
plt.legend(['train', 'val'])
#Test Model
x_test = data[120:,0:5]
y_test = data[120:,5]
y_pred = model.predict(x_test)
y_pred = np.argmax(y_pred,axis = -1)
cm = confusion_matrix(y_test, y_pred)
print(cm)
plt.show()