Created
March 22, 2019 23:40
-
-
Save alinazhanguwo/04d1297c737a5ef7501bf223736ae8c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Computes softmax cross entropy between logits and labels | |
# Measures the probability error in discrete classification tasks | |
# For example, each font image is labeled with one and only one label: an image can be font SansSerif or Serif, but not both. | |
cross_entropy = tf.reduce_mean( | |
tf.nn.softmax_cross_entropy_with_logits_v2(logits = y + 1e-50, labels = y_)) | |
# GradientDescentOptimizer is used to minimize loss | |
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) | |
# Define accuracy | |
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) | |
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) | |
# Train for 6000 times | |
epochs = 6000 | |
train_acc = np.zeros(epochs//10) | |
test_acc = np.zeros(epochs//10) | |
for i in tqdm(range(epochs), ascii=True): | |
if i % 10 == 0: # Record summary data, and the accuracy | |
# Check accuracy on train set | |
A = accuracy.eval(feed_dict={x: train_dataset, y_: train_labels}) | |
train_acc[i//10] = A | |
# And now the test set | |
A = accuracy.eval(feed_dict={x: test_dataset, y_: test_labels}) | |
test_acc[i//10] = A | |
train_step.run(feed_dict={x: train_dataset, y_: train_labels}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment