def test_mnist_mlp(FLAGS): # Import data mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True) # Create the model x = tf.placeholder(tf.float32, [None, 784]) # Define loss and optimizer y_ = tf.placeholder(tf.float32, [None, 10]) y_conv = common.mlp_model(x, 'test') with tf.Session() as sess: start_time = time.time() x_test = mnist.test.images[:FLAGS.batch_size] y_test = mnist.test.labels[:FLAGS.batch_size] # Run model y_conv_val = y_conv.eval(feed_dict={x: x_test, y_: y_test}) elasped_time = time.time() - start_time print("total time(s)", np.round(elasped_time, 2)) print('result') print(np.round(y_conv_val, 2)) x_test_batch = mnist.test.images[:FLAGS.batch_size] y_test_batch = mnist.test.labels[:FLAGS.batch_size] x_test = mnist.test.images y_test = mnist.test.labels y_label_batch = np.argmax(y_test_batch, 1) if FLAGS.save_batch: x_test_batch.tofile("x_test_" + str(FLAGS.batch_size) + ".bin") y_label_batch.astype('float32').tofile("y_label_" + str(FLAGS.batch_size) + ".bin") y_pred = np.argmax(y_conv_val, 1) print('y_pred', y_pred) correct_prediction = np.equal(y_pred, y_label_batch) error_count = np.size(correct_prediction) - np.sum(correct_prediction) test_accuracy = np.mean(correct_prediction) print('Error count', error_count, 'of', FLAGS.batch_size, 'elements.') print('Accuracy: %g ' % test_accuracy) # Rename serialized graph try: serialized_graphs = glob.glob("tf_function_ngraph*.json") if os.environ.get('NGRAPH_ENABLE_SERIALIZE', '') == "1" and len(serialized_graphs) == 1: src_path = serialized_graphs[0] dst_path = "mnist_mlp_batch_%s.json" % (FLAGS.batch_size, ) print("Moving", src_path, "to", dst_path) os.rename(src_path, dst_path) except: print("Renaming serialized graph not successful")
def main(_): # Disable mnist dataset deprecation warning tf.logging.set_verbosity(tf.logging.ERROR) # Import data mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True) # Create the model x = tf.placeholder(tf.float32, [None, 784]) # Define loss and optimizer y_ = tf.placeholder(tf.float32, [None, 10]) # Build the graph for the deep net y_conv = common.mlp_model(x, 'train') with tf.name_scope('loss'): cross_entropy = tf.nn.softmax_cross_entropy_with_logits( labels=y_, logits=y_conv) cross_entropy = tf.reduce_mean(cross_entropy) with tf.name_scope('adam_optimizer'): train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) with tf.name_scope('accuracy'): correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) correct_prediction = tf.cast(correct_prediction, tf.float32) accuracy = tf.reduce_mean(correct_prediction) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) loss_values = [] for i in range(FLAGS.train_loop_count): batch = mnist.train.next_batch(FLAGS.batch_size) if i % 100 == 0: t = time.time() train_accuracy = accuracy.eval(feed_dict={ x: batch[0], y_: batch[1] }) print('step %d, training accuracy %g, %g msec to evaluate' % (i, train_accuracy, 1000 * (time.time() - t))) t = time.time() _, loss = sess.run([train_step, cross_entropy], feed_dict={ x: batch[0], y_: batch[1] }) loss_values.append(loss) if i % 1000 == 999 or i == FLAGS.train_loop_count - 1: x_test = mnist.test.images[:FLAGS.test_image_count] y_test = mnist.test.labels[:FLAGS.test_image_count] test_accuracy = accuracy.eval(feed_dict={ x: x_test, y_: y_test }) print('test accuracy %g' % test_accuracy) print("Training finished. Saving variables.") for var in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES): weight = (sess.run([var]))[0].flatten().tolist() filename = (str(var).split())[1].replace('/', '_') filename = filename.replace("'", "").replace(':0', '') + '.txt' print("saving", filename) np.savetxt(str(filename), weight)