November 2017
Intermediate to advanced
304 pages
6h 58m
English
We will define all the operations in a new Python file named models.py. First, let's create some operations to compute loss and accuracy:
def compute_loss(logits, labels): labels = tf.squeeze(tf.cast(labels, tf.int32)) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels) cross_entropy_mean = tf.reduce_mean(cross_entropy) tf.add_to_collection('losses', cross_entropy_mean) return tf.add_n(tf.get_collection('losses'), name='total_loss') def compute_accuracy(logits, labels): labels = tf.squeeze(tf.cast(labels, tf.int32)) batch_predictions = tf.cast(tf.argmax(logits, 1), tf.int32) predicted_correctly = tf.equal(batch_predictions, labels) accuracy = tf.reduce_mean(tf.cast(predicted_correctly, ...Read now
Unlock full access