Skip to content

Instantly share code, notes, and snippets.

@SubhadityaMukherjee
Created January 21, 2020 16:35
Show Gist options
  • Save SubhadityaMukherjee/512368f3cd2d8c606d718a8a55afc472 to your computer and use it in GitHub Desktop.
Save SubhadityaMukherjee/512368f3cd2d8c606d718a8a55afc472 to your computer and use it in GitHub Desktop.
loss

calc_loss

  • We take the image and the model as inputs
  • expand dims basically adds an extra dimension to our input along the x axis to make it work with inception
  • for every activation in our layers, we calculate the loss and append it to a list
  • reduce_mean() and reduce_sum() are approximately the mean and sum equivalent for tensors instead of just plain arrays
  • Thus the sum is the total loss we get
def calc_loss(img, model):
    img_batch = tf.expand_dims(img, axis=0)
    layer_activations = model(img_batch)

    losses = []
    for act in layer_activations:
        loss = tf.math.reduce_mean(act)
        losses.append(loss)

    return tf.reduce_sum(losses)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment