-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex00_firstModel.py
executable file
·74 lines (62 loc) · 2.16 KB
/
ex00_firstModel.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#coding=utf-8
''' Import theano and numpy '''
import theano
import numpy as np
#execfile('00_readingInput.py')
exec(open("./00_readingInput.py").read())
''' Import keras to build a DL model '''
from keras.models import Sequential
from keras.layers.core import Dense, Activation
print('Building a model whose loss function is categorical_crossentropy')
''' For categorical_crossentropy '''
model = Sequential()
# (Do!) 加入 hidden layer of 128 neurons 與指定 input_dim=200
# 用 'sigmoid' 當作 activation function
model.add(Dense(128, input_dim=200))
model.add(Activation('sigmoid'))
# (Do!) 加入 hidden layer of 256 neurons
# 使用 'sigmoid' 當作 activation function
model.add(Dense(256))
model.add(Activation('sigmoid'))
# (Do!) 加入 output layer of 5 neurons
# 使用 'softmax' 當作 activation function
model.add(Dense(5))
model.add(Activation('softmax'))
model.summary()
''' Set up the optimizer '''
from keras.optimizers import SGD, Adam, RMSprop, Adagrad
sgd = SGD(lr=0.01,momentum=0.0,decay=0.0,nesterov=False)
''' Compile model with specified loss and optimizer '''
# (Do!) 指定 loss function
model.compile( loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
''' set the size of mini-batch and number of epochs'''
batch_size = 16
nb_epoch = 30
'''Fit models and use validation_split=0.1 '''
# (Do!) 指定 batch_size, nb_epoch, shuffle or not
# 與 validation_split 的比例
history = model.fit( X_train, #X_train, Y_train,
Y_train, # output
batch_size=16, # batch_size
nb_epoch=30, # nb_epoch
shuffle=True, # shuffle
validation_split=0.1, # validation_split
verbose=0)
'''Access the loss and accuracy in every epoch'''
loss = history.history.get('loss')
acc = history.history.get('acc')
''' Visualize the loss and accuracy of both models'''
import matplotlib.pyplot as plt
plt.figure(0)
plt.subplot(121)
plt.plot(range(len(loss)), loss,label='CE')
plt.title('Loss')
plt.legend(loc='upper left')
plt.subplot(122)
plt.plot(range(len(acc)), acc,label='CE')
plt.title('Accuracy')
plt.savefig('00_firstModel.png',dpi=300,format='png')
plt.close()
print('Result saved into 00_lossFuncSelection.png')