Exemplo n.º 1
0
# Calculate accuracy
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# TODO: Set batch size
batch_size = 128
assert batch_size is not None, 'You must set the batch size'

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)

    # TODO: Train optimizer on all batches
    # for batch_features, batch_labels in ____
    for batch_features, batch_labels in batches(batch_size, train_features,
                                                train_labels):
        sess.run(optimizer,
                 feed_dict={
                     features: batch_features,
                     labels: batch_labels
                 })

    # Calculate accuracy for test dataset
    test_accuracy = sess.run(accuracy,
                             feed_dict={
                                 features: test_features,
                                 labels: test_labels
                             })

print('Test Accuracy: {}'.format(test_accuracy))
Exemplo n.º 2
0
# Define loss and optimizer
learning_rate = tf.placeholder(tf.float32)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)

# Calculate accuracy
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

init = tf.global_variables_initializer()

batch_size = 128
epochs = 10
learn_rate = 0.001

train_batches = batches(batch_size, train_features, train_labels)

with tf.Session() as sess:
    sess.run(init)

    # Training cycle
    for epoch_i in range(epochs):

        # Loop over all batches
        for batch_features, batch_labels in train_batches:
            train_feed_dict = {
                features: batch_features,
                labels: batch_labels,
                learning_rate: learn_rate}
            sess.run(optimizer, feed_dict=train_feed_dict)
Exemplo n.º 3
0
    tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
optimizer = tf.train.GradientDescentOptimizer(
    learning_rate=learning_rate).minimize(cost)

correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

batch_size = 128
assert batch_size is not None

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)

    for batch_features, batch_lables in batches(batch_size, train_features,
                                                train_features):
        sess.run(optimizer,
                 feed_dict={
                     features: batch_features,
                     labels: batch_lables
                 })

    test_accuracy = sess.run(accuracy,
                             feed_dict={
                                 features: batch_features,
                                 labels: batch_lables
                             })

print('Test accuracy: {}'.format(test_accuracy))