Skip to content

Instantly share code, notes, and snippets.

@khanhnamle1994
Last active April 23, 2018 05:08
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 khanhnamle1994/905a1645e4956eca877ecd9d558dd121 to your computer and use it in GitHub Desktop.
Save khanhnamle1994/905a1645e4956eca877ecd9d558dd121 to your computer and use it in GitHub Desktop.
FCN - Build the TensorFLow loss and optimizer operations.
def optimize(nn_last_layer, correct_label, learning_rate, num_classes):
# Reshape 4D tensors to 2D, each row represents a pixel, each column a class
logits = tf.reshape(nn_last_layer, (-1, num_classes), name="fcn_logits")
correct_label_reshaped = tf.reshape(correct_label, (-1, num_classes))
# Calculate distance from actual labels using cross entropy
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=correct_label_reshaped[:])
# Take mean for total loss
loss_op = tf.reduce_mean(cross_entropy, name="fcn_loss")
# The model implements this operation to find the weights/parameters that would yield correct pixel labels
train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss_op, name="fcn_train_op")
return logits, train_op, loss_op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment