Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alinazhanguwo/4f80f5ccd7fb5ff3c9d0a124d200cd4d to your computer and use it in GitHub Desktop.
Save alinazhanguwo/4f80f5ccd7fb5ff3c9d0a124d200cd4d to your computer and use it in GitHub Desktop.
# 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 50000 times
epochs = 50000
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 validation 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