Skip to content

Instantly share code, notes, and snippets.

@Crazz-Zaac
Created February 26, 2021 02:51
Show Gist options
  • Save Crazz-Zaac/5915b549ca160fe01c346889afe8f70f to your computer and use it in GitHub Desktop.
Save Crazz-Zaac/5915b549ca160fe01c346889afe8f70f to your computer and use it in GitHub Desktop.
# GRADED FUNCTION: compute_layer_style_cost
def compute_layer_style_cost(a_S, a_G):
"""
Arguments:
a_S -- tensor of dimension (1, n_H, n_W, n_C), hidden layer activations representing style of the image S
a_G -- tensor of dimension (1, n_H, n_W, n_C), hidden layer activations representing style of the image G
Returns:
J_style_layer -- tensor representing a scalar value, style cost defined above by equation (2)
"""
### START CODE HERE ###
# Retrieve dimensions from a_G (≈1 line)
m, n_H, n_W, n_C = a_G.get_shape().as_list()
# Reshape the images to have them of shape (n_C, n_H*n_W) (≈2 lines)
a_S = tf.transpose(tf.reshape(a_S, shape=[n_H*n_W, n_C]))
a_G = tf.transpose(tf.reshape(a_G, shape=[n_H*n_W, n_C]))
# Computing gram_matrices for both images S and G (≈2 lines)
GS = gram_matrix(a_S)
GG = gram_matrix(a_G)
# Computing the loss (≈1 line)
J_style_layer = 1/(4* n_C**2 *(n_H*n_W)**2)*tf.reduce_sum(tf.square(tf.subtract(GS, GG)))
### END CODE HERE ###
return J_style_layer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment