-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleveland_bnn.py
112 lines (95 loc) · 3.64 KB
/
cleveland_bnn.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# cleveland_bnn.py
# CNTK 2.3 with Anaconda 4.1.1 (Python 3.5, NumPy 1.11.1)
import numpy as np
import cntk as C
import os
def create_reader(path, input_dim, output_dim, rnd_order, sweeps):
x_strm = C.io.StreamDef(field='symptoms', shape=input_dim,
is_sparse=False)
y_strm = C.io.StreamDef(field='disease', shape=output_dim,
is_sparse=False)
streams = C.io.StreamDefs(x_src=x_strm, y_src=y_strm)
deserial = C.io.CTFDeserializer(path, streams)
mb_src = C.io.MinibatchSource(deserial, randomize=rnd_order, \
max_sweeps=sweeps)
return mb_src
# ===================================================================
def main():
print("\nBegin binary classification (two-node technique) \n")
print("Using CNTK version = " + str(C.__version__) + "\n")
input_dim = 18
hidden_dim = 20
output_dim = 2
# ******************* Updates from Original
# Linux Version
app_root = os.environ['APP_ROOT']
model_run = os.environ['MODEL_RUN']
train_file = app_root + "/Data/cleveland_cntk_twonode.txt"
model_file = app_root + "/Model/cleveland_bnn_" + model_run + ".model"
# *******************
# Windows Version
# TBD
# 1. create network
X = C.ops.input_variable(input_dim, np.float32)
Y = C.ops.input_variable(output_dim, np.float32)
print("Creating a 18-20-2 tanh-softmax NN ")
with C.layers.default_options(init=C.initializer.uniform(scale=0.01,\
seed=1)):
hLayer = C.layers.Dense(hidden_dim, activation=C.ops.tanh,
name='hidLayer')(X)
oLayer = C.layers.Dense(output_dim, activation=None,
name='outLayer')(hLayer)
nnet = oLayer
model = C.ops.softmax(nnet)
# 2. create learner and trainer
print("Creating a cross entropy batch=10 SGD LR=0.005 Trainer ")
tr_loss = C.cross_entropy_with_softmax(nnet, Y)
tr_clas = C.classification_error(nnet, Y)
max_iter = 5000
batch_size = 10
learn_rate = 0.005
learner = C.sgd(nnet.parameters, learn_rate)
trainer = C.Trainer(nnet, (tr_loss, tr_clas), [learner])
# 3. create reader for train data
rdr = create_reader(train_file, input_dim, output_dim,
rnd_order=True, sweeps=C.io.INFINITELY_REPEAT)
heart_input_map = {
X : rdr.streams.x_src,
Y : rdr.streams.y_src
}
# 4. train
print("\nStarting training \n")
for i in range(0, max_iter):
curr_batch = rdr.next_minibatch(batch_size, \
input_map=heart_input_map)
trainer.train_minibatch(curr_batch)
if i % int(max_iter/10) == 0:
mcee = trainer.previous_minibatch_loss_average
macc = (1.0 - trainer.previous_minibatch_evaluation_average) \
* 100
print("batch %4d: mean loss = %0.4f, accuracy = %0.2f%% " \
% (i, mcee, macc))
print("\nTraining complete")
# 5. evaluate model using all data
print("\nEvaluating accuracy using built-in test_minibatch() \n")
rdr = create_reader(train_file, input_dim, output_dim,
rnd_order=False, sweeps=1)
heart_input_map = {
X : rdr.streams.x_src,
Y : rdr.streams.y_src
}
num_test = 297
all_test = rdr.next_minibatch(num_test, input_map=heart_input_map)
acc = (1.0 - trainer.test_minibatch(all_test)) * 100
print("Classification accuracy on the %d data items = %0.2f%%" \
% (num_test,acc))
# ******************* Updates from Original
# Save Model
model.save(model_file)
print("\nSaved Cleveland Heart Disease Model to: " + model_file)
# *******************
# (use trained model to make prediction)
print("\nEnd Cleveland Heart Disease classification ")
# ===================================================================
if __name__ == "__main__":
main()