# How to define a data-dependent model without using global variables

**URL:** <https://discourse.julialang.org/t/how-to-define-a-data-dependent-model-without-using-global-variables/73584>\
**Category:** Machine Learning\
**Tags:** question, diffeq, flux, sciml\
**Created:** [December 24, 2021, 11:43am UTC](https://discourse.julialang.org/t/how-to-define-a-data-dependent-model-without-using-global-variables/73584 "2021-12-24T11:43:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chen2021](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chen2021/32/32214_2.png) [@chen2021](https://discourse.julialang.org/u/chen2021)\
**Post date:** [December 24, 2021, 11:43am UTC](https://discourse.julialang.org/t/how-to-define-a-data-dependent-model-without-using-global-variables/73584/1 "2021-12-24T11:43:01Z")

</div>

Hi, I am facing a problem that the network model depends on data. In fact, I am trying to train an iterator to solve $Au=f$，the model performs a single-step iteration `u_next = model(u)`, hence the `model` depends on A and f. For example, Richardson iteration reads

```julia
model = Chain(
    x -> x + ω*(f-A(x)),
) 

```

At this time, the A and f in the model are taken from the data

```julia
for (x, y) in train_loader
    global A = x |> device
    global f = y |> device
    u = zeros(size(f)) |> device
	for i = 1:max_iter
		u = model(u)
	loss = norm(f - Au) / norm(f)
	# backward
end

```

But doing so will give me warnings or even errors

```julia
Warning: Performing scalar indexing on task Task (runnable) @0x00007f477458c2f0.
│Invocation of getindex resulted in scalar indexing of a GPU array.
│This is typically caused by calling an iterating implementation of a method.
Such implementations *do not* execute on the GPU, but very slowly on the CPU,
and therefore are only permitted from the REPL for prototyping purposes.
If you did intend to index this array, annotate the caller with @allowscalar.

```

How do I correctly define this type of model using `Flux.jl`? 😃  
Thanks

---

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [December 24, 2021, 12:26pm UTC](https://discourse.julialang.org/t/how-to-define-a-data-dependent-model-without-using-global-variables/73584/2 "2021-12-24T12:26:05Z")

</div>

Would not defining your own model solve the problem, for that, see docs here  
[https://fluxml.ai/Flux.jl/stable/models/overview/](https://fluxml.ai/Flux.jl/stable/models/overview/)

Tomas

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 24, 2021, 4:38pm UTC](https://discourse.julialang.org/t/how-to-define-a-data-dependent-model-without-using-global-variables/73584/3 "2021-12-24T16:38:57Z")

</div>

> [@chen2021](#):
>
> ```julia
> model = Chain(
> x -> x + ω*(f-A(x)),
> ) 
> 
> ```

(Note that this has nothing to do with globals vs. locals. Thanks to lexical scoping, an expression like `x -> x + ω*(f-A(x))` works fine to define an anonymous function even if `A`, `f`, and `ω` are local variables.)

---

<div class="post-metadata">

**Author:** ![chen2021](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chen2021/32/32214_2.png) [@chen2021](https://discourse.julialang.org/u/chen2021)\
**Post date:** [December 25, 2021, 4:37am UTC](https://discourse.julialang.org/t/how-to-define-a-data-dependent-model-without-using-global-variables/73584/4 "2021-12-25T04:37:16Z")

</div>

> [@stevengj](#):
>
> lexical scoping,

Okay, but what I mean is that A in the `model` corresponds to A in the for loop. The A in the for loop is a local variable. As a result, the `model` defined outside the for loop cannot see A , so I need to set A to a global variable, otherwise an error will occur

> ERROR: LoadError: UndefVarError: A not defined

---

<div class="post-metadata">

**Author:** ![chen2021](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chen2021/32/32214_2.png) [@chen2021](https://discourse.julialang.org/u/chen2021)\
**Post date:** [December 25, 2021, 4:40am UTC](https://discourse.julialang.org/t/how-to-define-a-data-dependent-model-without-using-global-variables/73584/5 "2021-12-25T04:40:20Z")

</div>

Thanks for your reply 😃 let me take a look.
