# Knet and JLD question

**URL:** <https://discourse.julialang.org/t/knet-and-jld-question/22383>\
**Category:** General Usage\
**Created:** [March 26, 2019, 11:32pm UTC](https://discourse.julialang.org/t/knet-and-jld-question/22383 "2019-03-26T23:32:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![omalled](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/omalled/32/12532_2.png) [@omalled](https://discourse.julialang.org/u/omalled)\
**Post date:** [March 26, 2019, 11:32pm UTC](https://discourse.julialang.org/t/knet-and-jld-question/22383/1 "2019-03-26T23:32:47Z")

</div>

I have been playing around with [Knet](https://github.com/denizyuret/Knet.jl), and it is great! However, I have run into a bit of trouble trying to move results from a machine with a GPU to another machine without an (NVIDIA) GPU.

Essentially, what I have done is run the example [here](https://github.com/denizyuret/Knet.jl/blob/master/examples/variational-autoencoder/vae_mnist_demo.ipynb) on the machine with the GPU. Then I saved the results to a JLD file via `JLD.save("myfile.jld", "theta", θ, "phi", ϕ)`. Then I moved the JLD file to the computer which does not have a GPU. On this other computer, it seems that doing things with theta or phi (other than loading them) produces an error, e.g.,:

```julia
julia> using Knet; import JLD; theta, phi = JLD.load("myfile.jld", "theta", "phi");

julia> theta
4-element Array{KnetArray{Float32,N} where N,1}:
Error showing value of type Array{KnetArray{Float32,N} where N,1}:
ERROR: UndefVarError: lib not defined
Stacktrace:
...

julia> VAE.decode(theta, zeros(Float32, 100))
ERROR: MethodError: no method matching *(::KnetArray{Float32,2}, ::Array{Float32,1})
...

julia> theta[1]
500×100 KnetArray{Float32,2}:
Error showing value of type KnetArray{Float32,2}:
ERROR: UndefVarError: lib not defined
Stacktrace:
...

julia> theta[1][1, 1]
ERROR: UndefVarError: lib not defined
Stacktrace:
...

```

Is there any way for me to use what I saved in the JLD file to encode and decode on the machine without the GPU? Did I really just save a bunch of pointers to GPU memory without actually saving the data? What is the best way to save these results so that I can then do things like encode/decode on another machine?

BTW, If I do the training on the machine without the GPU, then the above code works fine, but the types of things are different. E.g., `typeof(theta) == Array{Array{Float32,N} where N,1}` when trained on the machine without the GPU, whereas `typeof(theta) == Array{KnetArray{Float32,N} where N,1}` when trained on the machine with the GPU.

---

<div class="post-metadata">

**Author:** ![omalled](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/omalled/32/12532_2.png) [@omalled](https://discourse.julialang.org/u/omalled)\
**Post date:** [March 27, 2019, 4:06pm UTC](https://discourse.julialang.org/t/knet-and-jld-question/22383/2 "2019-03-27T16:06:22Z")

</div>

I thought I’d answer my own question in case anyone finds it helpful in the future. Knet has its own mechanism for [File IO](http://denizyuret.github.io/Knet.jl/latest/reference/#File-I/O-1). Basically, I replaced my calls to `JLD.save` and `JLD.load` with identical calls to `Knet.save` and `Knet.load`. After that, everything worked smoothly.

---

<div class="post-metadata">

**Author:** ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)\
**Post date:** [March 27, 2019, 4:52pm UTC](https://discourse.julialang.org/t/knet-and-jld-question/22383/3 "2019-03-27T16:52:22Z")

</div>

I always move everything back to CPU after training on a GPU, before saving the networks which I guess is what knet does behind the scenes.
