Question about compute_style_cost in Week 4 Programming Assignment 2

In the compute_style_cost function that was provided in Exercise 4, is style_image_output[:-1] a list of activation tensors that corresponds to the layers indicated in STYLE_LAYERS, hence len(STYLE_LAYERS) = len(style_image_output) - 1?

The [:-1] notation removes the last element from a list.
The comments in the function explain why we do this:

# Set a_S to be the hidden layer activation from the layer we have selected.
# The last element of the array contains the content layer image, which must not to be used.
2 Likes

When I first worked on this assignment, I was also confused. Why? Because you first see the concatenation of layers in Section 5.4 - Load Pre-trained VGG19 Model, quite far away from that comment :slight_smile:

content_layer = [('block5_conv4', 1)]

vgg_model_outputs = get_layer_outputs(vgg, STYLE_LAYERS + content_layer)

The content layer is last, which is why

   def compute_content_cost(content_output, generated_output):
    """
    Computes the content cost
    
    Arguments:
    a_C -- tensor of dimension (1, n_H, n_W, n_C), hidden layer activations representing content of the image C 
    a_G -- tensor of dimension (1, n_H, n_W, n_C), hidden layer activations representing content of the image G
    
    Returns: 
    J_content -- scalar that you compute using equation 1 above.
    """
    a_C = content_output[-1]
    a_G = generated_output[-1]

you have the above reference to the last index for content cost.

1 Like