Commit 7f3baaa1 authored by ppwwyyxx's avatar ppwwyyxx

monitor cost

parent 4267ea4d
...@@ -34,6 +34,7 @@ def get_model(input, label): ...@@ -34,6 +34,7 @@ def get_model(input, label):
output: variable output: variable
cost: scalar variable cost: scalar variable
""" """
# use this dropout variable! it will be set to 1 at test time
keep_prob = tf.placeholder(tf.float32, name='dropout_prob') keep_prob = tf.placeholder(tf.float32, name='dropout_prob')
input = tf.reshape(input, [-1, IMAGE_SIZE, IMAGE_SIZE, 1]) input = tf.reshape(input, [-1, IMAGE_SIZE, IMAGE_SIZE, 1])
...@@ -57,10 +58,11 @@ def get_model(input, label): ...@@ -57,10 +58,11 @@ def get_model(input, label):
fc1 = FullyConnected('lr', fc0, out_dim=10) fc1 = FullyConnected('lr', fc0, out_dim=10)
prob = tf.nn.softmax(fc1, name='output') prob = tf.nn.softmax(fc1, name='output')
logprob = logSoftmax(fc1)
y = one_hot(label, NUM_CLASS) y = one_hot(label, NUM_CLASS)
cost = tf.reduce_sum(-y * logprob, 1) cost = tf.nn.softmax_cross_entropy_with_logits(fc1, y)
cost = tf.reduce_mean(cost, name='cost') #logprob = logSoftmax(fc1)
#cost = tf.reduce_sum(-y * logprob, 1)
cost = tf.reduce_sum(cost, name='cost')
tf.scalar_summary(cost.op.name, cost) tf.scalar_summary(cost.op.name, cost)
return prob, cost return prob, cost
...@@ -74,28 +76,27 @@ def main(): ...@@ -74,28 +76,27 @@ def main():
prefix='test', period=2), prefix='test', period=2),
PeriodicSaver(LOG_DIR, period=2) PeriodicSaver(LOG_DIR, period=2)
] ]
optimizer = tf.train.AdamOptimizer(1e-4)
sess_config = tf.ConfigProto()
sess_config.device_count['GPU'] = 1
with tf.Graph().as_default(): with tf.Graph().as_default():
G = tf.get_default_graph()
input_var = tf.placeholder(tf.float32, shape=(None, IMAGE_SIZE, IMAGE_SIZE), name='input') input_var = tf.placeholder(tf.float32, shape=(None, IMAGE_SIZE, IMAGE_SIZE), name='input')
label_var = tf.placeholder(tf.int32, shape=(None,), name='label') label_var = tf.placeholder(tf.int32, shape=(None,), name='label')
prob, cost = get_model(input_var, label_var) prob, cost = get_model(input_var, label_var)
optimizer = tf.train.AdamOptimizer(1e-4)
train_op = optimizer.minimize(cost) train_op = optimizer.minimize(cost)
for ext in extensions: for ext in extensions:
ext.init() ext.init()
summary_op = tf.merge_all_summaries() summary_op = tf.merge_all_summaries()
config = tf.ConfigProto() sess = tf.Session(config=sess_config)
config.device_count['GPU'] = 1
sess = tf.Session(config=config)
sess.run(tf.initialize_all_variables()) sess.run(tf.initialize_all_variables())
summary_writer = tf.train.SummaryWriter(LOG_DIR, graph_def=sess.graph_def) summary_writer = tf.train.SummaryWriter(LOG_DIR, graph_def=sess.graph_def)
g = tf.get_default_graph() keep_prob = G.get_tensor_by_name('dropout_prob:0')
keep_prob = g.get_tensor_by_name('dropout_prob:0')
with sess.as_default(): with sess.as_default():
for epoch in count(1): for epoch in count(1):
for (img, label) in BatchData(dataset_train, batch_size).get_data(): for (img, label) in BatchData(dataset_train, batch_size).get_data():
......
...@@ -60,21 +60,27 @@ class OnehotClassificationValidation(PeriodicExtension): ...@@ -60,21 +60,27 @@ class OnehotClassificationValidation(PeriodicExtension):
correct = tf.equal(tf.cast(tf.argmax(self.output_var, 1), tf.int32), correct = tf.equal(tf.cast(tf.argmax(self.output_var, 1), tf.int32),
self.label_var) self.label_var)
# TODO: add cost
self.nr_correct_var = tf.reduce_sum(tf.cast(correct, tf.int32)) self.nr_correct_var = tf.reduce_sum(tf.cast(correct, tf.int32))
self.cost_var = self.graph.get_tensor_by_name('cost:0')
def _trigger(self): def _trigger(self):
cnt = 0 cnt = 0
cnt_correct = 0 cnt_correct = 0
sess = tf.get_default_session()
cost_sum = 0
for (img, label) in self.ds.get_data(): for (img, label) in self.ds.get_data():
feed = {self.input_var: img, feed = {self.input_var: img,
self.label_var: label, self.label_var: label,
self.dropout_var: 1.0} self.dropout_var: 1.0}
cnt += img.shape[0] cnt += img.shape[0]
cnt_correct += self.nr_correct_var.eval(feed_dict=feed) correct, cost = sess.run([self.nr_correct_var, self.cost_var],
feed_dict=feed)
cnt_correct += correct
cost_sum += cost
cost_sum /= cnt
# TODO write to summary? # TODO write to summary?
print "Accuracy at epoch {}: {}".format( print "After epoch {}: acc={}, cost={}".format(
self.epoch_num, cnt_correct * 1.0 / cnt) self.epoch_num, cnt_correct * 1.0 / cnt, cost_sum)
class PeriodicSaver(PeriodicExtension): class PeriodicSaver(PeriodicExtension):
......
...@@ -8,22 +8,24 @@ import numpy as np ...@@ -8,22 +8,24 @@ import numpy as np
__all__ = ['one_hot', 'batch_flatten', 'logSoftmax'] __all__ = ['one_hot', 'batch_flatten', 'logSoftmax']
def one_hot(y, num_labels): def one_hot(y, num_labels):
batch_size = tf.size(y) with tf.variable_scope('one_hot'):
y = tf.expand_dims(y, 1) batch_size = tf.size(y)
indices = tf.expand_dims(tf.range(0, batch_size), 1) y = tf.expand_dims(y, 1)
concated = tf.concat(1, [indices, y]) indices = tf.expand_dims(tf.range(0, batch_size), 1)
onehot_labels = tf.sparse_to_dense( concated = tf.concat(1, [indices, y])
concated, tf.pack([batch_size, num_labels]), 1.0, 0.0) onehot_labels = tf.sparse_to_dense(
onehot_labels.set_shape([None, num_labels]) concated, tf.pack([batch_size, num_labels]), 1.0, 0.0)
return tf.cast(onehot_labels, tf.float32) onehot_labels.set_shape([None, num_labels])
return tf.cast(onehot_labels, tf.float32)
def batch_flatten(x): def batch_flatten(x):
total_dim = np.prod(x.get_shape()[1:].as_list()) total_dim = np.prod(x.get_shape()[1:].as_list())
return tf.reshape(x, [-1, total_dim]) return tf.reshape(x, [-1, total_dim])
def logSoftmax(x): def logSoftmax(x):
z = x - tf.reduce_max(x, 1, keep_dims=True) with tf.variable_scope('logSoftmax'):
logprob = z - tf.log(tf.reduce_sum(tf.exp(z), 1, keep_dims=True)) z = x - tf.reduce_max(x, 1, keep_dims=True)
return logprob logprob = z - tf.log(tf.reduce_sum(tf.exp(z), 1, keep_dims=True))
return logprob
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment