# ERROR: UndefVarError: \`GPU\` not defined Stacktrace: \[1\] top-level scope

**URL:** <https://discourse.julialang.org/t/error-undefvarerror-gpu-not-defined-stacktrace-1-top-level-scope/104097>\
**Category:** VS Code\
**Tags:** error, error-message\
**Created:** [September 21, 2023, 7:37am UTC](https://discourse.julialang.org/t/error-undefvarerror-gpu-not-defined-stacktrace-1-top-level-scope/104097 "2023-09-21T07:37:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abraham\_Lincoln](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraham_lincoln/32/53067_2.png) [@Abraham\_Lincoln](https://discourse.julialang.org/u/Abraham_Lincoln)\
**Post date:** [September 21, 2023, 7:37am UTC](https://discourse.julialang.org/t/error-undefvarerror-gpu-not-defined-stacktrace-1-top-level-scope/104097/1 "2023-09-21T07:37:27Z")

</div>

When i run this code appears this error:ERROR: UndefVarError: `GPU` not defined  
Stacktrace:  
[1] top-level scope  
CODE:

using CUDA  
using Flux  
using Flux.Data: DataLoader  
using Flux: onehotbatch, onecold, crossentropy, throttle  
using Statistics  
using Base.Iterators: partition  
using MLDatasets: CIFAR10

devices = GPU.devices()

if length(devices) \> 0  
device = devices[1]  
CUDA.allowscalar(false)  
else  
device = CUDA.CPU  
end

CUDA.device!(device)

train\_x, train\_y = CIFAR10.traindata(Float32)  
test\_x, test\_y = CIFAR10.testdata(Float32)

train\_y = onehotbatch(train\_y, 0:9)  
test\_y = onehotbatch(test\_y, 0:9)

batch\_size = 64  
train\_data = DataLoader((train\_x, train\_y), batchsize=batch\_size, shuffle=true)  
test\_data = DataLoader((test\_x, test\_y), batchsize=batch\_size)

model = Chain(  
Conv((3, 3), 3=\>16, relu),  
MaxPool((2, 2)),  
Conv((3, 3), 16=\>32, relu),  
MaxPool((2, 2)),  
x → reshape(x, :, size(x, 4)),  
Dense(32_6_6, 256, relu),  
Dense(256, 10),  
softmax  
) |\> gpu

loss(x, y) = crossentropy(model(x), y)  
optimizer = ADAM(0.001)

n\_epochs = 10  
for epoch in 1:n\_epochs  
println(“Epoch: $epoch”)  
Flux.train!(loss, params(model), train\_data, optimizer)  
end

function test\_accuracy(data\_loader)  
acc = 0.0  
for (x, y) in data\_loader  
acc += sum(onecold(model(x)) .== onecold(y))  
end  
return acc / length(data\_loader)  
end

accuracy = test\_accuracy(test\_data)  
println(“Test Accuracy: $accuracy”)

---

<div class="post-metadata">

**Author:** ![April-Hannah-Lena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/april-hannah-lena/32/38975_2.png) [@April-Hannah-Lena](https://discourse.julialang.org/u/April-Hannah-Lena)\
**Post date:** [September 21, 2023, 4:01pm UTC](https://discourse.julialang.org/t/error-undefvarerror-gpu-not-defined-stacktrace-1-top-level-scope/104097/2 "2023-09-21T16:01:44Z")

</div>

You called `GPU.devices()`, but you never imported any module called GPU. Maybe you meant `CUDA.devices()`?

The stacktrace tells you the line where the error was. This is usually a good way to deduce what the error might be, whenever an error occurs.
