# Art\_Generation\_with\_Neural\_Style\_Transfer exercise 3

**URL:** <https://community.deeplearning.ai/t/art-generation-with-neural-style-transfer-exercise-3/574177>\
**Category:** Convolutional Neural Networks\
**Tags:** week-module-4, coursera-platform\
**Created:** [February 19, 2024, 8:33am UTC](https://community.deeplearning.ai/t/art-generation-with-neural-style-transfer-exercise-3/574177 "2024-02-19T08:33:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fo2sh](https://avatars.discourse-cdn.com/v4/letter/f/ac91a4/32.png) [@Fo2sh](https://community.deeplearning.ai/u/Fo2sh)\
**Post date:** [February 19, 2024, 8:33am UTC](https://community.deeplearning.ai/t/art-generation-with-neural-style-transfer-exercise-3/574177/1 "2024-02-19T08:33:30Z")

</div>

I’ve retrieved dimensions successfully, converted the shape of a\_S and a\_G from(1,n\_H,n\_W,n\_C) to (n\_C,n\_H\*n\_W) , calculated the gram matrices using the predefined function gram\_matrix(A) and computed J\_style\_layer as follow:  
J\_style\_layer = (1 / (4 \* n\_C\*\*2 \* (n\_H \* n\_W)\*\*2)) \* tf.reduce\_sum(tf.square(tf.subtract(GS, GG)))

this error appears and I can’t understand  
"AssertionError Traceback (most recent call last)  
Input In [34], in \<cell line: 3\>()  
1 ### you cannot edit this cell  
----\> 3 compute\_layer\_style\_cost\_test(compute\_layer\_style\_cost)

File /tf/W4A2/public\_tests.py:57, in compute\_layer\_style\_cost\_test(target)  
55 assert np.isclose(J\_style\_layer\_GG, 0.0), “Wrong value. compute\_layer\_style\_cost(A, A) must be 0”  
56 assert J\_style\_layer\_SG \> 0, “Wrong value. compute\_layer\_style\_cost(A, B) must be greater than 0 if A != B”  
—\> 57 assert np.isclose(J\_style\_layer\_SG, 14.01649), “Wrong value.”  
59 print(“J\_style\_layer = " + str(J\_style\_layer\_SG))  
60 print(”\033[92mAll tests passed")

AssertionError: Wrong value."

---

<div class="post-metadata">

**Author:** ![saifkhanengr](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/saifkhanengr/32/152858_2.png) [@saifkhanengr](https://community.deeplearning.ai/u/saifkhanengr)\
**Post date:** [February 19, 2024, 1:06pm UTC](https://community.deeplearning.ai/t/art-generation-with-neural-style-transfer-exercise-3/574177/2 "2024-02-19T13:06:17Z")

</div>

Your `J_style_layer ` code seems correct. How you are reshaping the tensors from (1, n\_H, n\_W, n\_C) to (n\_C, n\_H \* n\_W)? You have to use both, `tf.reshape` and `tf.transpose`. First, use `tf.reshape` and then `tf.transpose`. Maybe [this](https://community.deeplearning.ai/t/flattening-images-in-the-logistic-regression-assignment-in-course-1-week-2/149) thread seems relevant to your query.

---

<div class="post-metadata">

**Author:** ![Fo2sh](https://avatars.discourse-cdn.com/v4/letter/f/ac91a4/32.png) [@Fo2sh](https://community.deeplearning.ai/u/Fo2sh)\
**Post date:** [February 19, 2024, 2:48pm UTC](https://community.deeplearning.ai/t/art-generation-with-neural-style-transfer-exercise-3/574177/3 "2024-02-19T14:48:07Z")

</div>

I’m using this method “a\_S = tf.transpose(tf.reshape(a\_S, shape=[n\_C, n\_H \* n\_W]))  
a\_G = tf.transpose(tf.reshape(a\_G, shape=[n\_C, n\_H \* n\_W]))” … am I right ?

---

<div class="post-metadata">

**Author:** ![saifkhanengr](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/saifkhanengr/32/152858_2.png) [@saifkhanengr](https://community.deeplearning.ai/u/saifkhanengr)\
**Post date:** [February 19, 2024, 3:57pm UTC](https://community.deeplearning.ai/t/art-generation-with-neural-style-transfer-exercise-3/574177/4 "2024-02-19T15:57:07Z")

</div>

> [@Fo2sh](#):
>
> am I right ?

No. Please read this again:

```auto
 # Reshape the tensors from (1, n_H, n_W, n_C) to (n_C, n_H * n_W) (≈2 lines)

```

If you are using transpose, then what should be the shape for `tf.reshape` to have `(n_C, n_H * n_W)`?

---

<div class="post-metadata">

**Author:** ![Fo2sh](https://avatars.discourse-cdn.com/v4/letter/f/ac91a4/32.png) [@Fo2sh](https://community.deeplearning.ai/u/Fo2sh)\
**Post date:** [February 19, 2024, 4:07pm UTC](https://community.deeplearning.ai/t/art-generation-with-neural-style-transfer-exercise-3/574177/5 "2024-02-19T16:07:06Z")

</div>

Got it ! thank you for your helpful hint ❤
