# Week 4, assignment 2, train\_step, error in function compute\_style\_cost

**URL:** <https://community.deeplearning.ai/t/week-4-assignment-2-train-step-error-in-function-compute-style-cost/491405>\
**Category:** Convolutional Neural Networks\
**Tags:** coursera-platform\
**Created:** [November 10, 2023, 5:59pm UTC](https://community.deeplearning.ai/t/week-4-assignment-2-train-step-error-in-function-compute-style-cost/491405 "2023-11-10T17:59:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gopakumar\_Sivanpilla](https://avatars.discourse-cdn.com/v4/letter/g/8e8cbc/32.png) [@Gopakumar\_Sivanpilla](https://community.deeplearning.ai/u/Gopakumar_Sivanpilla)\
**Post date:** [November 10, 2023, 5:59pm UTC](https://community.deeplearning.ai/t/week-4-assignment-2-train-step-error-in-function-compute-style-cost/491405/1 "2023-11-10T17:59:45Z")

</div>

Hi, To resolve the error “cannot compute Mul as input #1(zero-based) was expected to be a double tensor but is a float tensor [Op:Mul]” in compute\_layer\_style\_cost, I added numpy() in reduce\_sum and it resolved the above error. Alternatively, tried to cast reduce\_sum to float64 and this also resolved the above error and made the test pass at compute\_layer\_style\_cost. But when it comes to train\_step, the error is thrown in compute\_layer\_style\_cost fn call that ‘Tensor’ object has no attribute ‘numpy’. When tried type-casting in compute\_layer\_style\_cost, the error in train\_step becomes “Unexpected cost for epoch 0: -318755264.80295044 != 25629.055”. I have gone through all the discussion topics, unfortunately could not find a proper solution. Please help.

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [November 10, 2023, 6:07pm UTC](https://community.deeplearning.ai/t/week-4-assignment-2-train-step-error-in-function-compute-style-cost/491405/2 "2023-11-10T18:07:26Z")

</div>

It is not a good idea to use numpy functions when you’re working in TF. The reason is that gradients are computed in TF automatically: you don’t have to worry about writing the back propagation code. But if you include numpy functions in the compute graph, then the gradient computations will fail because only TF functions have the “autograd” logic.

You can use numpy in places that are not involved in the gradients, though. E.g. computing the constant values involving fractions and the dimensions. But there you need to be careful to do everything in floating point. Use `1.` instead of just `1` or `4.` instead of `4` … Had to be careful with the punctuation there not to confuse the issue further! 😆

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [November 10, 2023, 6:16pm UTC](https://community.deeplearning.ai/t/week-4-assignment-2-train-step-error-in-function-compute-style-cost/491405/3 "2023-11-10T18:16:24Z")

</div>

There should be no need to coerce types anywhere in your code.

On the point about the floating point constants, did you find [this thread](https://community.deeplearning.ai/t/be-careful-with-division-in-c4w4-compute-layer-style-cost/11519/4)? Anytime someone ends up with a negative cost value, this is the first thing to check. There’s no way costs should ever be negative, right?

---

<div class="post-metadata">

**Author:** ![Gopakumar\_Sivanpilla](https://avatars.discourse-cdn.com/v4/letter/g/8e8cbc/32.png) [@Gopakumar\_Sivanpilla](https://community.deeplearning.ai/u/Gopakumar_Sivanpilla)\
**Post date:** [November 11, 2023, 7:36am UTC](https://community.deeplearning.ai/t/week-4-assignment-2-train-step-error-in-function-compute-style-cost/491405/4 "2023-11-11T07:36:53Z")

</div>

Indeed I had tried the solution in the thread, but partially! I tried tf.math.divide instead of “/”, but didn’t change 'tf.square into \*\*2. Now tried as per the solution and worked like a charm :). Thank you very much Paul.
