# How to calculate Cross Entropy loss between y\_predict and y\_target on GPU

**URL:** <https://discourse.julialang.org/t/how-to-calculate-cross-entropy-loss-between-y-predict-and-y-target-on-gpu/83429>\
**Category:** Performance\
**Tags:** gpu, flux\
**Created:** [June 27, 2022, 9:05pm UTC](https://discourse.julialang.org/t/how-to-calculate-cross-entropy-loss-between-y-predict-and-y-target-on-gpu/83429 "2022-06-27T21:05:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jack\_N](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jack_n/32/37265_2.png) [@Jack\_N](https://discourse.julialang.org/u/Jack_N)\
**Post date:** [June 27, 2022, 9:05pm UTC](https://discourse.julialang.org/t/how-to-calculate-cross-entropy-loss-between-y-predict-and-y-target-on-gpu/83429/1 "2022-06-27T21:05:54Z")

</div>

I have not been able to find out how to calculate the loss between each column of a y\_predict and y\_target matrix as a GPU operation. Please let me know how I can do this.

Thank you,

Jack

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [June 28, 2022, 10:45pm UTC](https://discourse.julialang.org/t/how-to-calculate-cross-entropy-loss-between-y-predict-and-y-target-on-gpu/83429/2 "2022-06-28T22:45:47Z")

</div>

Calculating cross-entropy on 2D (and more) arrays is covered in [the docs](https://fluxml.ai/Flux.jl/stable/models/losses/#Flux.Losses.crossentropy). There is no difference between using GPU and non-GPU arrays for this particular operation.

In future, I’d also recommend using the #domain:ml category so that you get more domain expert eyes on questions. Most people are not subscribed to #usage because of the high volume of messages.

---

<div class="post-metadata">

**Author:** ![Jack\_N](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jack_n/32/37265_2.png) [@Jack\_N](https://discourse.julialang.org/u/Jack_N)\
**Post date:** [June 29, 2022, 12:51am UTC](https://discourse.julialang.org/t/how-to-calculate-cross-entropy-loss-between-y-predict-and-y-target-on-gpu/83429/3 "2022-06-29T00:51:17Z")

</div>

Thank you for letting me know! I did not see anything in the docs which explains why this operation cannot be done in parallel (loss on each column at the same time) with the GPU. Is there a reason why this hasn’t been implemented on the GPU that I am not aware of?

Thank you,

Jack

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [June 29, 2022, 1:05am UTC](https://discourse.julialang.org/t/how-to-calculate-cross-entropy-loss-between-y-predict-and-y-target-on-gpu/83429/4 "2022-06-29T01:05:53Z")

</div>

I’m not sure what exactly you mean by “parallel on each column at the same time”, but it absolutely does make use of GPU parallelism (just like the cross entropy loss function in any other ML framework). The easiest way to find out if it works for your particular use case is just to run it with some GPU arrays—I think you’ll be pleasantly surprised with the results 😉

---

<div class="post-metadata">

**Author:** ![Jack\_N](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jack_n/32/37265_2.png) [@Jack\_N](https://discourse.julialang.org/u/Jack_N)\
**Post date:** [June 29, 2022, 3:08am UTC](https://discourse.julialang.org/t/how-to-calculate-cross-entropy-loss-between-y-predict-and-y-target-on-gpu/83429/5 "2022-06-29T03:08:12Z")

</div>

Thank you for clarifying, from your previous answer I had thought that it was all processed on the CPU but I understand now.
