3.4. Define the loss function, optimizer, accuracy, and predicted class

After creating the network, we have to calculate the loss and optimize it. Also, to evaluate our model, we have to calculate the correct_prediction and accuracy.

In [16]:
# Define the loss function, optimizer, and accuracy
loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=prediction, labels=target))

# Create a summary to monitor the loss function
tf.summary.scalar("loss_function", loss)

# Back-propagation
optimizer = tf.train.GradientDescentOptimizer(learning_rate, name='Gradient-optimizer').minimize(loss)

# If the prediction is greater than 0.5, it should be considered as class 1, otherwise class 0
correct_prediction = tf.equal(target , (tf.to_float(tf.greater(prediction, 0.5))))

# Calculate our models performance, but first we need to convert our datatype from true and false, into 1 and 0
accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))