def main(unused_argv): # Load training and eval data prepper = prep_hiragana.prepper('hiragana', 'hiragana.txt') train_data = prepper.train_images() # Returns np.array train_labels = np.asarray(prepper.train_labels(), dtype=np.int32) eval_data = prepper.validate_images() # Returns np.array eval_labels = np.asarray(prepper.validate_labels(), dtype=np.int32) # Create the Estimator hiragana_classifier = learn.Estimator( model_fn=cnn_model_fn, model_dir="/tmp/hiragana_convnet_model2") # Set up logging for predictions tensors_to_log = {"probabilities": "softmax_tensor"} logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=50) # Train the model hiragana_classifier.fit(x=train_data, y=train_labels, batch_size=50, steps=1000, monitors=[logging_hook]) # Configure the accuracy metric for evaluation metrics = { "accuracy": learn.MetricSpec(metric_fn=tf.metrics.accuracy, prediction_key="classes"), } # Evaluate the model and print results eval_results = hiragana_classifier.evaluate(x=eval_data, y=eval_labels, metrics=metrics) print(eval_results)
def main(_): # Import data # mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True) prepper = prep_hiragana.prepper('hiragana', 'hiragana.txt') training = prepper.train_images() t_labels = np.asarray(prepper.train_labels(), dtype=np.int32) validation = prepper.validate_images() v_labels = np.asarray(prepper.validate_labels(), dtype=np.int32) v_labels = onehot_labels(v_labels) # Create the model x = tf.placeholder(tf.float32, [None, 900]) W = tf.Variable(tf.zeros([900, 45])) b = tf.Variable(tf.zeros([45])) y = tf.matmul(x, W) + b # Define loss and optimizer y_ = tf.placeholder(tf.float32, [None, 45]) # The raw formulation of cross-entropy, # # tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)), # reduction_indices=[1])) # # can be numerically unstable. # # So here we use tf.nn.softmax_cross_entropy_with_logits on the raw # outputs of 'y', and then average across the batch. cross_entropy = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y)) train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) sess = tf.InteractiveSession() tf.global_variables_initializer().run() # Test trained model correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # Train for i in range(1000): a = i * 50 % len(training) batchx = training[a:a + 50] batchy = t_labels[a:a + 50] batchy = onehot_labels(batchy) sess.run(train_step, feed_dict={x: batchx, y_: batchy}) if i % 100 == 0: print(sess.run(accuracy, feed_dict={x: validation, y_: v_labels})) print(sess.run(accuracy, feed_dict={x: validation, y_: v_labels}))
def main(_): # Hyper-parameters width, height = 32, 32 classes = 90 batch_size = 50 steps = 1000 save_location = "/tmp/kana_cnn" # Import data prepper = prep_hiragana.prepper('kana', 'kana.txt') training = prepper.train_images() t_labels = np.asarray(prepper.train_labels(), dtype=np.int32) validation = prepper.validate_images() v_labels = np.asarray(prepper.validate_labels(), dtype=np.int32) v_labels = onehot_labels(v_labels, classes) # Create the model x = tf.placeholder(tf.float32, [None, width * height]) W = tf.Variable(tf.zeros([width * height, classes])) b = tf.Variable(tf.zeros([classes])) y = tf.matmul(x, W) + b # Define loss and optimizer y_ = tf.placeholder(tf.float32, [None, classes]) # adding in the convolutional layer W_conv1 = weight_variable([5, 5, 1, 32], "w1") b_conv1 = bias_variable([32], "b1") x_image = tf.reshape(x, [-1, width, height, 1]) h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) h_pool1 = max_pool_2x2(h_conv1) # adding the second convolutional layer W_conv2 = weight_variable([5, 5, 32, 64], "w2") b_conv2 = bias_variable([64], "b2") h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) h_pool2 = max_pool_2x2(h_conv2) # adding the third convolutional layer W_conv3 = weight_variable([5, 5, 64, 128], "w3") b_conv3 = bias_variable([128], "b3") h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3) h_pool3 = max_pool_2x2(h_conv3) #adding the final layer W_fc1 = weight_variable([4 * 4 * 128, 1024], "w_flat") b_fc1 = bias_variable([1024], "b_flat") h_pool3_flat = tf.reshape(h_pool3, [-1, 4 * 4 * 128]) h_fc1 = tf.nn.relu(tf.matmul(h_pool3_flat, W_fc1) + b_fc1) # adding the dropout keep_prob = tf.placeholder(tf.float32) h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) # adding the readout layer W_fc2 = weight_variable([1024, classes], "w_read") b_fc2 = bias_variable([classes], "b_read") y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2 # The raw formulation of cross-entropy, # # tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)), # reduction_indices=[1])) # # can be numerically unstable. # # So here we use tf.nn.softmax_cross_entropy_with_logits on the raw # outputs of 'y', and then average across the batch. cross_entropy = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv)) train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) # Add ops to save and restore all the variables. saver = tf.train.Saver() sess = tf.InteractiveSession() sess.run(tf.global_variables_initializer()) if os.path.exists(os.path.join(save_location)): saver.restore(sess, save_location + "/model.ckpt") # Test trained model correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # Train for i in range(steps): a = i * batch_size % len(training) batchx = training[a:a + batch_size] batchy = t_labels[a:a + batch_size] batchy = onehot_labels(batchy, classes) train_step.run(feed_dict={x: batchx, y_: batchy, keep_prob: 0.5}) if i % 100 == 0: train_accuracy = accuracy.eval(feed_dict={ x: batchx, y_: batchy, keep_prob: 1.0 }) print("step %d, training accuracy %g" % (i, train_accuracy)) print("test accuracy %g" % accuracy.eval(feed_dict={ x: validation, y_: v_labels, keep_prob: 1.0 })) if i % 300 == 0: # Save the variables to disk. save_path = saver.save(sess, save_location + "/model.ckpt") # print("Model saved in file: %s" % save_path) print("test accuracy %g" % accuracy.eval(feed_dict={ x: validation, y_: v_labels, keep_prob: 1.0 })) save_path = saver.save(sess, save_location + "/model.ckpt")