Layer style cost calculation

I am getting an error when try to calculate the Style layer cost. I double check the Grant matrix GS and GG and even for the first loop of test (J_style_layer_GG = compute_layer_style_cost(a_G, a_G)) I cannot compute the layer style cost properly without the following error.

Any tip of what could be wrong with my code.

# Computing the loss (≈1 line)
# J_style_layer = None
J_style_layer = (1/(tf.square(2*n_C*n_W*n_H)))*(tf.reduce_sum(tf.square(tf.subtract(GS,GG))))

a_S before (1, 4, 4, 3)
a_G before (1, 4, 4, 3)
a_S (3, 16)
a_G (3, 16)
GA Shape (3, 3)
GA Shape (3, 3)
GS (3, 3)
GG (3, 3)

InvalidArgumentError Traceback (most recent call last)
in
2 a_S = tf.random.normal([1, 4, 4, 3], mean=1, stddev=4)
3 a_G = tf.random.normal([1, 4, 4, 3], mean=1, stddev=4)
----> 4 J_style_layer_GG = compute_layer_style_cost(a_G, a_G)
5 J_style_layer_SG = compute_layer_style_cost(a_S, a_G)
6

in compute_layer_style_cost(a_S, a_G)
43 # Computing the loss (≈1 line)
44 # J_style_layer = None
—> 45 J_style_layer = (1/(tf.square(2n_Cn_Wn_H)))(tf.reduce_sum(tf.square(tf.subtract(GS,GG))))
46
47 ### END CODE HERE

/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py in binary_op_wrapper(x, y)
1123 with ops.name_scope(None, op_name, [x, y]) as name:
1124 try:
→ 1125 return func(x, y, name=name)
1126 except (TypeError, ValueError) as e:
1127 # Even if dispatching the op failed, the RHS may be a tensor aware

/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py in _mul_dispatch(x, y, name)
1455 return sparse_tensor.SparseTensor(y.indices, new_vals, y.dense_shape)
1456 else:
→ 1457 return multiply(x, y, name=name)
1458
1459

/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
199 “”“Call target, and fall back on dispatchers if there is a TypeError.”""
200 try:
→ 201 return target(*args, **kwargs)
202 except (TypeError, ValueError):
203 # Note: convert_to_eager_tensor currently raises a ValueError, not a

/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py in multiply(x, y, name)
507 “”"
508
→ 509 return gen_math_ops.mul(x, y, name)
510
511

/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_math_ops.py in mul(x, y, name)
6164 return _result
6165 except _core._NotOkStatusException as e:
→ 6166 _ops.raise_from_not_ok_status(e, name)
6167 except _core._FallbackException:
6168 pass

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)
6841 message = e.message + (" name: " + name if name is not None else “”)
6842 # pylint: disable=protected-access
→ 6843 six.raise_from(core._status_to_exception(e.code, message), None)
6844 # pylint: enable=protected-access
6845

/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: cannot compute Mul as input #1(zero-based) was expected to be a double tensor but is a float tensor [Op:Mul]

The problem is that in the scalar portion of the computation you are using integer values. In TF, they have different type coercion rules than in python and numpy. The easiest thing to do is to do the scalar part of the calculation in numpy or just plain python and only use TF for the tensor portion. Here’s a thread which discusses this more.

Understood. I replace the square calculation using (**2) and works well. The cost math.

I think I learn “don’t use tensors for things that don’t really need to be tensors”.

Thanks

Well said! That’s a good “rule of thumb” to keep in mind.

Thank you for the tip