-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel1.py
58 lines (48 loc) · 1.65 KB
/
model1.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
# モデル1
#
# 中間層1層
#
import time
from datetime import datetime
from load_data import load
from saver import save_arch, save_history
from plotter import plot_hist, plot_model_arch
import pickle
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import SGD
from keras.callbacks import ModelCheckpoint
# 変数
model_name = 'model1'
nb_epoch = 100
validation_split = 0.2
lr = 0.01
momentum = 0.9
nesterov = True
loss_method = 'mean_squared_error'
arch_path = 'model/' + model_name + '-arch-' + str(nb_epoch) + '.json'
weights_path = 'model/' + model_name + '-weights-' + str(nb_epoch) + '.hdf5'
# データ読み込み
X, y = load()
# モデル
model = Sequential()
model.add(Dense(100, input_dim=9216))
model.add(Activation('relu'))
model.add(Dense(30))
save_arch(model, arch_path) # モデルを保存しておく
# トレーニングの準備
checkpoint_collback = ModelCheckpoint(filepath = weights_path,
monitor='val_loss',
save_best_only=True,
mode='auto')
sgd = SGD(lr=lr, momentum=momentum, nesterov=nesterov)
model.compile(loss=loss_method, optimizer=sgd)
# トレーニング
start_time = time.time()
print('start_time: %s' % (datetime.now()))
hist = model.fit(X, y, nb_epoch=nb_epoch, validation_split=validation_split, callbacks=[checkpoint_collback])
print('end_time: %s, duracion(min): %d' % (datetime.now(), int(time.time()-start_time) / 60))
# プロットしてファイルとして保存する
# plot_hist(hist, model_name)
# plot_model_arch(model, model_name)
save_history(hist, model_name)