-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex04_optimizerSelection.py
executable file
·82 lines (70 loc) · 2.42 KB
/
ex04_optimizerSelection.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
75
76
77
78
79
80
81
82
#coding=utf-8
''' Import theano and numpy '''
import theano
import numpy as np
exec(open("./00_readingInput.py").read())
''' set the size of mini-batch and number of epochs'''
batch_size = 16
nb_epoch = 30
''' Import keras to build a DL model '''
from keras.models import Sequential
from keras.layers.core import Dense, Activation
print('Building a model whose optimizer=adam, activation function=softplus')
model_adam = Sequential()
model_adam.add(Dense(128, input_dim=200))
model_adam.add(Activation('softplus'))
model_adam.add(Dense(256))
model_adam.add(Activation('softplus'))
model_adam.add(Dense(5))
model_adam.add(Activation('softmax'))
''' Setting optimizer as Adam '''
from keras.optimizers import Adam
# (Do!) 請在 compile model_adam 時使用 'Adam' 當作 optimizer
model_adam.compile(loss='categorical_crossentropy',
optimizer='Adam',
metrics=['accuracy'])
'''Fit models and use validation_split=0.1 '''
history_adam = model_adam.fit(X_train, Y_train,
batch_size=batch_size,
nb_epoch=nb_epoch,
verbose=0,
shuffle=True,
validation_split=0.1)
loss_adam= history_adam.history.get('loss')
acc_adam = history_adam.history.get('acc')
print('Building a model whose optimizer=sgd, activation function=softplus')
model_sgd = Sequential()
model_sgd.add(Dense(128, input_dim=200))
model_sgd.add(Activation('softplus'))
model_sgd.add(Dense(256))
model_sgd.add(Activation('softplus'))
model_sgd.add(Dense(5))
model_sgd.add(Activation('softmax'))
''' Setting optimizer as SGD '''
model_sgd.compile(loss='categorical_crossentropy',
optimizer='SGD',
metrics=['accuracy'])
history_sgd = model_sgd.fit(X_train, Y_train,
batch_size=batch_size,
nb_epoch=nb_epoch,
verbose=0,
shuffle=True,
validation_split=0.1)
loss_sgd= history_sgd.history.get('loss')
acc_sgd = history_sgd.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_adam)), loss_adam,label='Adam')
plt.plot(range(len(loss_sgd)), loss_sgd,label='SGD')
plt.title('Loss')
plt.legend(loc='upper left')
plt.subplot(122)
plt.plot(range(len(acc_adam)), acc_adam,label='Adam')
plt.plot(range(len(acc_sgd)), acc_sgd,label='SGD')
plt.title('Accuracy')
# plt.show()
plt.savefig('04_optimizerSelection.png',dpi=300,format='png')
plt.close()
print('Result saved into 04_optimizerSelection.png')