forked from wallarm/nascell-automl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_manager.py
66 lines (58 loc) · 3.16 KB
/
net_manager.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
import tensorflow as tf
from cnn import CNN
class NetManager():
def __init__(self, num_input, num_classes, learning_rate, dataset, train_size, test_size,
max_step_per_action=5500*3,
bathc_size=100,
dropout_rate=0.85):
self.num_input = num_input
self.num_classes = num_classes
self.learning_rate = learning_rate
self.dataset = dataset
self.train_size = train_size
self.test_size = test_size
self.max_step_per_action = max_step_per_action
self.bathc_size = bathc_size
self.dropout_rate = dropout_rate
def get_reward(self, action, step, pre_acc):
action = [action[0][0][x:x+4] for x in range(0, len(action[0][0]), 4)]
cnn_drop_rate = [c[3] for c in action]
with tf.Graph().as_default() as g:
with g.container('experiment'+str(step)):
model = CNN(self.num_input, self.num_classes, action)
loss_op = tf.reduce_mean(model.loss)
optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate)
train_op = optimizer.minimize(loss_op)
with tf.Session() as train_sess:
init = tf.global_variables_initializer()
train_sess.run(init)
for step in range(self.max_step_per_action):
batch_x, batch_y = self.dataset.train.next_batch(self.bathc_size)
feed = {model.X: batch_x,
model.Y: batch_y,
model.dropout_keep_prob: self.dropout_rate,
model.cnn_dropout_rates: cnn_drop_rate}
_ = train_sess.run(train_op, feed_dict=feed)
if step % 100 == 0:
# Calculate batch loss and accuracy
loss, acc = train_sess.run(
[loss_op, model.accuracy],
feed_dict={model.X: batch_x,
model.Y: batch_y,
model.dropout_keep_prob: 1.0,
model.cnn_dropout_rates: [1.0]*len(cnn_drop_rate)})
print("Step " + str(step) +
", Minibatch Loss= " + "{:.4f}".format(loss) +
", Current accuracy= " + "{:.3f}".format(acc))
batch_x, batch_y = self.dataset.test.next_batch(self.test_size)
loss, acc = train_sess.run(
[loss_op, model.accuracy],
feed_dict={model.X: batch_x,
model.Y: batch_y,
model.dropout_keep_prob: 1.0,
model.cnn_dropout_rates: [1.0]*len(cnn_drop_rate)})
print("!!!!!!acc:", acc, pre_acc)
if acc - pre_acc <= 0.01:
return acc, acc
else:
return 0.01, acc