# Calculating crossentropy with varying sized vectors on GPU

**URL:** <https://discourse.julialang.org/t/calculating-crossentropy-with-varying-sized-vectors-on-gpu/79700>\
**Category:** GPU\
**Tags:** cuda, machine-learning, cuarrays\
**Created:** [April 19, 2022, 4:15pm UTC](https://discourse.julialang.org/t/calculating-crossentropy-with-varying-sized-vectors-on-gpu/79700 "2022-04-19T16:15:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Casper](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/casper/32/32150_2.png) [@Casper](https://discourse.julialang.org/u/Casper)\
**Post date:** [April 19, 2022, 4:15pm UTC](https://discourse.julialang.org/t/calculating-crossentropy-with-varying-sized-vectors-on-gpu/79700/1 "2022-04-19T16:15:08Z")

</div>

Hello,  
I’m attempting to calculate the cross entropy of a bunch of samples of different length.

```julia
using CUDA

entropy(π̂, π) =-sum(π .* log.(π̂ )) 

m = 3
n = 3
w = ones(m) |> gpu

a1 = rand(Float32, m, n)
a1 = softmax(a1) |> gpu

a2 = [rand(Float32, m) for i in 1:n]|>gpu

@time entropy(a1, a1)
@time entropy(a2, a2)

```

Calculating using equal sized vectors is no problem as they can be batched to a matrix, however for unequal sized vectors I’m left with a vector of CuArrays. How do I efficiently calculate the crossentropy on my GPU?
