forked from serengil/tensorflow-101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKerasModelRestoration.py
74 lines (58 loc) · 1.94 KB
/
KerasModelRestoration.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
import tensorflow as tf
import numpy as np
from keras.models import Sequential
from keras.models import load_model
from keras.models import model_from_json
from keras.layers.core import Dense, Activation
from keras.utils import np_utils
#----------------------------
train = False
load_all_model = True #if train is False
#----------------------------
#preparing data for Exclusive OR (XOR)
attributes = [
#x1, x2
[0 ,0]
, [0, 1]
, [1, 0]
, [1, 1]
]
labels = [
#is_0, is_1 -> only a column can be 1 in labels variable
[1, 0]
, [0, 1]
, [0, 1]
, [1, 0]
]
#transforming attributes and labels matrixes to numpy
data = np.array(attributes, 'int64')
target = np.array(labels, 'int64')
#----------------------------
#creating model
if train == True:
model = Sequential()
model.add(Dense(3 #num of hidden units
, input_shape=(len(attributes[0]),))) #num of features in input layer
model.add(Activation('sigmoid')) #activation function from input layer to 1st hidden layer
model.add(Dense(len(labels[0]))) #num of classes in output layer
model.add(Activation('softmax')) #activation function from 1st hidden layer to output layer
model_config = model.to_json()
open("model_structure.json", "w").write(model_config)
#compile
model.compile(loss='categorical_crossentropy', optimizer='adam')
#training
model.fit(data, target, epochs=2000, verbose=0)
model.save("model.hdf5")
model.save_weights('model_weights.h5')
else:
if load_all_model == True:
model = load_model("model.hdf5") #model structure, weights
print("network structure and weights loaded")
else:
model = model_from_json(open("model_structure.json", "r").read()) #load structure
print("network structure loaded")
model.compile(loss='categorical_crossentropy', optimizer='adam')
model.load_weights('model_weights.h5') #load weights
print("weights loaded")
score = model.evaluate(data, target)
print(score)