def main(): # 入力データのshape定義 x = nn.variable.Variable( [BATCH_SIZE, IMAGE_DEPTH * IMAGE_WIDTH * IMAGE_HEIGHT]) # ラベルのshape定義 t = nn.variable.Variable([BATCH_SIZE, LABEL_NUM]) pred = convolution(x) loss_ = loss(pred, t) solver = S.Adam() solver.set_parameters(nn.get_parameters()) data = InputData() for i in range(NUM_STEP): # 100STEP毎にテスト実施 if i % 100 == 0: l = 0 a = 0 for k, (t.d, x.d) in enumerate(data.test_data()): loss_.forward() l += loss_.d a += accuracy(pred, t) print("Step: %05d Test loss: %0.05f Test accuracy: %0.05f" % (i, l / k, a / k)) t.d, x.d = data.next_batch() loss_.forward() solver.zero_grad() loss_.backward() solver.weight_decay(DECAY_RATE) solver.update() if i % 10 == 0: print("Step: %05d Train loss: %0.05f Train accuracy: %0.05f" % (i, loss_.d, accuracy(pred, t)))
def train(): '''訓練を実施する関数 ''' with tf.Graph().as_default(): print('Loading data...') data = InputData(train_data_path=FLAGS.train_data, validation_data_path=FLAGS.validation_data, batch_size=FLAGS.batch_size) input_ph = tf.placeholder(tf.int32, [None, data.max_len]) training_ph = tf.placeholder(tf.bool, []) label_ph = tf.placeholder(tf.float32, [None, data.num_category]) # summary用のplace holder summary_ph = tf.placeholder(tf.float32, [3]) config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True)) with tf.Session(config=config) as sess: convolution_op = convolution(input_ph, training_ph, data.num_chars, data.num_category) loss_op = loss(convolution_op, label_ph) train_op = minimize(loss_op) accuracy_op = accuracy(convolution_op, label_ph) top_3_accuracy_op = top_3_accuracy(convolution_op, label_ph) summary_writer = tf.summary.FileWriter(FLAGS.log_dir, sess.graph) write_validation_summary_op = write_validation_summary(summary_ph) for vals in tf.trainable_variables(): tf.summary.histogram(vals.name, vals) summary_op = tf.summary.merge_all() saver = tf.train.Saver() load_checkpoint(sess, saver) for i in range(FLAGS.max_step): label_, text_ = data.next_batch() _ = sess.run(train_op, feed_dict={ input_ph: text_, label_ph: label_, training_ph: True }) if i % 100 == 0: loss_, accuracy_, top_3_accuracy_ = sess.run( [loss_op, accuracy_op, top_3_accuracy_op], feed_dict={ input_ph: text_, label_ph: label_, training_ph: False }) print( 'global step: %04d, train loss: %01.7f, train accuracy_top_1 %01.5f train accuracy_top_3 %01.5f' % (i, loss_, accuracy_, top_3_accuracy_)) if i % 1000 == 0: validation_loss = [] validation_top_3_accuracy = [] validation_top_1_accuracy = [] for validation_label, validation_text in data.next_batch_evaluation_data( ): loss_, top_3_accuracy_, accuracy_ = sess.run( [loss_op, top_3_accuracy_op, accuracy_op], feed_dict={ input_ph: validation_text, label_ph: validation_label, training_ph: False }) validation_loss.append(loss_) validation_top_1_accuracy.append(accuracy_) validation_top_3_accuracy.append(top_3_accuracy_) loss_ = sum(validation_loss) / len(validation_loss) accuracy_ = sum(validation_top_1_accuracy) / len( validation_top_1_accuracy) top_3_accuracy_ = sum(validation_top_3_accuracy) / len( validation_top_3_accuracy) print( 'Validation loss: %s validation accuracy_top_1: %01.5f validation accuracy_top_3: %01.5f' % (loss_, accuracy_, top_3_accuracy_)) saver.save(sess, FLAGS.checkpoint_dir, global_step=i) # サマリー出力 _, summary_str = sess.run( [write_validation_summary_op, summary_op], feed_dict={ input_ph: text_, label_ph: label_, training_ph: False, summary_ph: [loss_, accuracy_, top_3_accuracy_] }) summary_writer.add_summary(summary_str, i) ru = resource.getrusage(resource.RUSAGE_SELF) print('Max memory usage(byte): ' + str(ru.ru_maxrss))