Course 4 Week 4 Assignment 2 Compute Cost error

Hello !

I tried to read through all the others showing similar problem but I just cannot figure out why my reshaping goes wrong when called from the optimiser loop while it seems to work correctly with the first tests.

I’ve tried reshaping to [-1, n_W*n_H, n_C], [1, -1, n_C], [m, -1, n_C] but the same problem comes up.

Is there a specific reason why the m value was not assigned from the shape on the current template ? i.e. the ready template had:
_, n_H, n_W, n_C = …

Here’s the error message:
ValueError: in user code:

<ipython-input-26-cd29da83566d>:26 train_step  *
    J_content = compute_content_cost(a_C, a_G)
<ipython-input-4-0fa8231dc89d>:26 compute_content_cost  *
    a_C_unrolled = tf.reshape(content_output, shape=[-1, n_H*n_W, n_C])
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/ wrapper  **
    return target(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/ reshape
    result = gen_array_ops.reshape(tensor, shape, name)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/ reshape
    "Reshape", tensor=tensor, shape=shape, name=name)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ _apply_op_helper
    (input_name, err))

ValueError: Tried to convert ‘tensor’ to a tensor and failed. Error: Dimension 1 in both shapes must be equal, but are 50 and 25. Shapes are [1,50,50,512] and [1,25,25,512].
From merging shape 3 with other shapes. for ‘{{node Reshape_10/packed}} = Pack[N=6, T=DT_FLOAT, axis=0](Reshape_10/tensor/values_0, Reshape_10/tensor/values_1, Reshape_10/tensor/values_2, Reshape_10/tensor/values_3, Reshape_10/tensor/values_4, Reshape_10/tensor/values_5)’ with input shapes: [1,400,400,64], [1,200,200,128], [1,100,100,256], [1,50,50,512], [1,25,25,512], [1,25,25,512].

Any ideas how to move forward ?

Trying to isolate the problem I started from fresh template just in case I had changed something somewhere on the way.

Just completing the first graded part:


GRADED FUNCTION: compute_content_cost

The local tests pass OK as previously but when submitting the part I get 0/100 and an error from the grader:
Cell #8. Can’t compile the student’s code. Error: AssertionError(‘Use the tensorflow function’)

I’ve used the tricks I saw elswhere, i.e. using float constants and only using tf.* functions from subtracting, squaring etc.

The output of the local test function is:
J_content = tf.Tensor(7.056877, shape=(), dtype=float32)
All tests passed

Which is different from the Expected output given on the assingment:
Expected Output:

J_content 7.0568767

So instead of the actual value I get a higher level object… But I guess this is OK ?

Seems I’m able to get the same error on Jupyter by adding @tf.function() in front of the function. Not sure what this actually does but I see that directive being used later in the code, maybe the grader uses that and thus the error ?

Also I’m a bit confused by the difference between the Tensor types EagerTensor and Tensor. The local tests seem to use datatype EagerTensor and even assert against that but the network model returns a Tensor. Also the input shapes are a bit different, local tests give [1,1,4,4,3] while the network Tensor is [None, 1,4,4,8]. The first dimension is collapsed before my unrolling code so shouldn’t matter.