Course 4 Week 4 Assignment 2- Exercise 6

When running the train step function, I am running into a dimension mistmatch issue. This happens when I call the compute_style_cost function. Needless to say that the unit tests up to this exercise have passed. The error is thrown when the gram matrix function is being called in the compute_layer_style_cost function.

I have a feeling that this has to do with how I reshaped a_S and a_G in the compute_layer_style_cost and specifically how I handle (or rather dont handle) the ‘m’ dimension. I further think it has to do with how I use tf.reshape.

I have tried to implement what is in the instructions and read numerous other posts but cannot really identify what is different in my implementation.

The error I am at is: ValueError: Dimensions must be equal, but are 64 and 400 for ‘{{node MatMul}} = BatchMatMulV2[T=DT_FLOAT, adj_x=false, adj_y=false](transpose_1/x, transpose_2)’ with input shapes: [1,400,400,64], [64,400,400,1].

Is that error actually being thrown in the gram_matrix function? If so, it is definitely a problem in your compute_layer_style_cost function. I added a bunch of print statements to my version of that function to show the shapes of things. Here’s the output I get when I run the test cell for compute_layer_style_cost:

shape(a_S) [1 4 4 3]
shape(a_G) [1 4 4 3]
after reshape+T: shape(a_S) [ 3 16]
a_S [[ 2.6123514   6.3462267  -2.0568852   1.0848972  -0.34144378  5.9450154
  -0.68249106  6.638078    4.405425    3.2136106  -0.88850987  8.216282
   1.1940846  -3.8442307   4.486284   -3.2315984 ]
 [-3.3520832   3.8470404  -3.1489944  -1.2055032  -3.17067    -1.7347562
  -3.1652112  -0.90944517  0.31713337  0.23800504 -0.10706711  0.6901974
   1.7071393   6.2297974  -2.2124012  -5.5964684 ]
 [ 0.74761856 -0.9571458  -4.0077353  -5.972679    5.036553    3.6944358
  -1.7189786   9.18924     2.566379    1.4399388  -1.7099016   3.6196625
  -0.9568796   2.8206615  -4.4811783   3.4338741 ]]
shape(GS) [3 3]
GS [[277.30215     1.8970766  84.06457  ]
 [  1.8970766 139.89871    -1.1890388]
 [ 84.06457    -1.1890388 245.05229  ]]

What shapes to you see in that test cell?

Thank you for the response.

When running the test cell I see the following:

a_S before reshape and T [1, 4, 4, 3]
a_G before reshape and T [1, 4, 4, 3]
a_S shape after reshape and T [3, 16]
a_G shape after reshape and T [3, 16]
GS shape [3, 3]
tf.Tensor(
[[277.30215     1.8970766  84.06457  ]
 [  1.8970766 139.89871    -1.1890388]
 [ 84.06457    -1.1890388 245.05229  ]], shape=(3, 3), dtype=float32)
GG shape [3, 3]

When running the train_step cell, the error I see:

<ipython-input-39-e371f10c3a52>:24 train_step  *
        J_style = compute_style_cost(style_image_output=a_S, generated_image_output=a_G)
    <ipython-input-27-708ec14558be>:33 compute_style_cost  *
        J_style_layer = compute_layer_style_cost(a_S[i], a_G[i])
    <ipython-input-22-9d5d15997450>:26 compute_layer_style_cost  *
        GS = gram_matrix(a_S)
    <ipython-input-16-a8642a1eb2b3>:16 gram_matrix  *
        GA = tf.linalg.matmul(A, tf.transpose(A))

Further down I see:
ValueError: Dimensions must be equal, but are 64 and 400 for '{{node MatMul}} = BatchMatMulV2[T=DT_FLOAT, adj_x=false, adj_y=false](transpose_1/x, transpose_1)' with input shapes: [1,400,400,64], [64,400,400,1].

This is why I thought I am not reshaping properly, especially the ‘m’ dimension.

Hi @Akash_Maheshwari,

Could you check the dimensions for A? The issue seems to be due to that!

Hope this helps!

Interesting. My reading is that the output of your test on compute_layer_style_cost looks correct. So I guess the theory needs to be that the bug is higher up the call stack. E.g. somehow the a_S[i] and a_G[i] values are the wrong shape.

I tried running the test cell for train_step with my print statements still in compute_layer_style_cost and here’s what I get:

shape(a_S) Tensor("Shape:0", shape=(4,), dtype=int32)
shape(a_G) Tensor("Shape_1:0", shape=(4,), dtype=int32)
after reshape+T: shape(a_S) Tensor("Shape_2:0", shape=(2,), dtype=int32)
shape(GS) Tensor("Shape_3:0", shape=(2,), dtype=int32)
GS Tensor("MatMul:0", shape=(64, 64), dtype=float32)

The thing to notice there is that we don’t actually see the values because we are not running in “eager” mode, but GS does end up being 64 x 64. Maybe there is something wrong with how you wrote the reshape and transpose logic such that it doesn’t work in non-eager mode. I’m not sure what that would be, but did you perhaps use numpy instead of TF functions for the reshape and transpose? If that theory doesn’t help, then I probably need to see your notebook by DM.

Hello @paulinpaloalto ,

When I keep the print statements, I see the following:

a_S before reshape and T [1, 400, 400, 64]
a_G before reshape and T [1, 400, 400, 64]
a_S shape after reshape and T [1, 400, 400, 64]
a_G shape after reshape and T [64, 160000]

So is the reshape and transpose of a_S incorrect? I am doing exactly the same thing for both a_S and a_G.

Sorry, I don’t believe you. If the shape of a_S is different than the shape of a_G after the transformations and they were the same before, how can it possibly be that you did the same thing in both cases? This is science: evidence matters, right?

I have sent you the snippet of my code to you via DM.