forked from krystianity/keras-serving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
56 lines (41 loc) · 1 KB
/
train.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
from __future__ import print_function
import numpy as np
import os
import shutil
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import SGD
from keras import backend as K
K.set_learning_phase(0)
# training set
x = np.array([
[0,0],
[0,1],
[1,0],
[1,1]
])
y = np.array([
[0],
[1],
[1],
[0]
])
# build model
model = Sequential()
model.add(Dense(8, input_dim=2))
model.add(Activation("tanh"))
model.add(Dense(1))
model.add(Activation("sigmoid"))
sgd = SGD(lr=0.1)
model.compile(loss="binary_crossentropy", optimizer=sgd, metrics=["accuracy"])
# train model
model.fit(x, y, batch_size=1, epochs=1000)
print(model.predict_proba(x))
# write model to json file
if os.path.isdir("./result"):
shutil.rmtree("./result")
os.makedirs("./result")
model_json = model.to_json()
with open("result/model.json", "w") as json_file: json_file.write(model_json)
model.save_weights("result/model.h5")
print("Saved model to disk")