def visualize_cam_with_losses(input_tensor, losses, seed_input, penultimate_layer, grad_modifier=None):
penultimate_output = penultimate_layer.output<EOL>opt = Optimizer(input_tensor, losses, wrt_tensor=penultimate_output, norm_grads=False)<EOL>_, grads, penultimate_output_value = opt.minimize(seed_input, max_iter=<NUM_LIT:1>, grad_modifier=grad_modifier, verbose=False)<EOL>grads = grads / (np.max(grads) + K.epsilon())<EOL>channel_idx = <NUM_LIT:1> if K.image_data_format() == '<STR_LIT>' else -<NUM_LIT:1><EOL>other_axis = np.delete(np.arange(len(grads.shape)), channel_idx)<EOL>weights = np.mean(grads, axis=tuple(other_axis))<EOL>output_dims = utils.get_img_shape(penultimate_output)[<NUM_LIT:2>:]<EOL>heatmap = np.zeros(shape=output_dims, dtype=K.floatx())<EOL>for i, w in enumerate(weights):<EOL><INDENT>if channel_idx == -<NUM_LIT:1>:<EOL><INDENT>heatmap += w * penultimate_output_value[<NUM_LIT:0>, ..., i]<EOL><DEDENT>else:<EOL><INDENT>heatmap += w * penultimate_output_value[<NUM_LIT:0>, i, ...]<EOL><DEDENT><DEDENT>heatmap = np.maximum(heatmap, <NUM_LIT:0>)<EOL>input_dims = utils.get_img_shape(input_tensor)[<NUM_LIT:2>:]<EOL>zoom_factor = [i / (j * <NUM_LIT:1.0>) for i, j in iter(zip(input_dims, output_dims))]<EOL>heatmap = zoom(heatmap, zoom_factor)<EOL>return utils.normalize(heatmap)<EOL>
Generates a gradient based class activation map (CAM) by using positive gradients of `input_tensor`
    with respect to weighted `losses`.

    For details on grad-CAM, see the paper:
    [Grad-CAM: Why did you say that? Visual Explanations from Deep Networks via Gradient-based Localization]
    (https://arxiv.org/pdf/1610.02391v1.pdf).

    Unlike [class activation mapping](https://arxiv.org/pdf/1512.04150v1.pdf), which requires minor changes to
    network architecture in some instances, grad-CAM has a more general applicability.

    Compared to saliency maps, grad-CAM is class discriminative; i.e., the 'cat' explanation exclusively highlights
    cat regions and not the 'dog' region and vice-versa.

    Args:
        input_tensor: An input tensor of shape: `(samples, channels, image_dims...)` if `image_data_format=
            channels_first` or `(samples, image_dims..., channels)` if `image_data_format=channels_last`.
        losses: List of ([Loss](vis.losses#Loss), weight) tuples.
        seed_input: The model input for which activation map needs to be visualized.
        penultimate_layer: The pre-layer to `layer_idx` whose feature maps should be used to compute gradients
            with respect to filter output.
        grad_modifier: gradient modifier to use. See [grad_modifiers](vis.grad_modifiers.md). If you don't
            specify anything, gradients are unchanged (Default value = None)

    Returns:
        The normalized gradients of `seed_input` with respect to weighted `losses`.