# Flux networks as arguments in functions

**URL:** https://discourse.julialang.org/t/flux-networks-as-arguments-in-functions/23988
**Category:** Machine Learning
**Tags:** flux
**Created:** [May 8, 2019, 7:05am UTC](https://discourse.julialang.org/t/flux-networks-as-arguments-in-functions/23988 "2019-05-08T07:05:46Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![charlesll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charlesll/32/3255_2.png) [@charlesll](https://discourse.julialang.org/u/charlesll)
#### Post date: [May 8, 2019, 7:05am UTC](https://discourse.julialang.org/t/flux-networks-as-arguments-in-functions/23988/1 "2019-05-08T07:05:46Z")

</div>

Hey,

I recently opened an [issue](https://github.com/FluxML/Flux.jl/issues/765) on Flux as there is something that needs clarification I think about using Flux with Julia’s functions.

To summarize my issue: I have a project where I have various equations embedded in functions. Here is an [example notebook with a toy problem looking like my research problem](https://github.com/charlesll/Examples/blob/master/Toy_problem.ipynb).

I saw that most functions used with Flux code are functions that call global variables/Flux structures. However, to work well in my case (particularly when using different scripts or looping between different networks and hence avoiding ambiguity), I directly provide the Flux models as an argument to my functions. This is nice but a potential downside is that this forces me to also provide the network in the dataset list during training… See the notebook for a (quickly written and probably messy) example.

My question is related to the later point: Is this a legit way of doing things, or is there a better way to provide models to functions?

Also, I wonder if this may have any influence on training speed?

Thanks in advance for any insights!

Charles.

---

<div class="post-metadata">

### Author: ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)
#### Post date: [May 8, 2019, 7:30am UTC](https://discourse.julialang.org/t/flux-networks-as-arguments-in-functions/23988/2 "2019-05-08T07:30:56Z")

</div>

Just to clarify, is your concern specifically about this line?

```julia
evalcb = () -> (push!(record_loss_train, loss(X1_training, X2_training, y_training,core).data))

```

which calls the functions in cell 11

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [May 8, 2019, 7:50am UTC](https://discourse.julialang.org/t/flux-networks-as-arguments-in-functions/23988/3 "2019-05-08T07:50:03Z")

</div>

> [@charlesll](#):
>
> I directly provide the Flux models as an argument to my functions. This is nice but a potential downside is that this forces me to also provide the network in the dataset list during training

I don’t understand this. Could you make a smaller example than your notebook?

---

<div class="post-metadata">

### Author: ![charlesll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charlesll/32/3255_2.png) [@charlesll](https://discourse.julialang.org/u/charlesll)
#### Post date: [May 8, 2019, 7:55am UTC](https://discourse.julialang.org/t/flux-networks-as-arguments-in-functions/23988/4 "2019-05-08T07:55:21Z")

</div>

My concern is that, as my functions take the network as an argument, I need to pass the network during training as a dataset when Flux.train is called:

`Flux.train!(loss, params(core), [(X1_training, X2_training, y_training,core)], ADAM(0.001)`

The ` [(X1_training, X2_training, y_training,core)]` is called a dataset in Flux docs but there it actually also contains the core network as in my case it needs to be passed to the loss() and all other functions.

Is that a problem or should we not care? I mean, it works and training is going fine for now, so maybe it is not important, but I’m concern that this could be a potential problem later.

(@kristoffer.carlsson I think this should clarify things?)

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [May 8, 2019, 7:57am UTC](https://discourse.julialang.org/t/flux-networks-as-arguments-in-functions/23988/5 "2019-05-08T07:57:49Z")

</div>

You can just pass

```julia
(x1, x2, y) -> loss(x1, x2, y, core)

```

as the loss function to `train!`.

---

<div class="post-metadata">

### Author: ![charlesll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charlesll/32/3255_2.png) [@charlesll](https://discourse.julialang.org/u/charlesll)
#### Post date: [May 8, 2019, 9:37pm UTC](https://discourse.julialang.org/t/flux-networks-as-arguments-in-functions/23988/6 "2019-05-08T21:37:35Z")

</div>

OK thanks. I guess it ends up doing the same thing, and this actually is not really a problem.

I could also write my own train() function that is a bit different and takes the network as an input. I will try and see if I see any difference.

---

<div class="post-metadata">

### Author: ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)
#### Post date: [May 8, 2019, 10:22pm UTC](https://discourse.julialang.org/t/flux-networks-as-arguments-in-functions/23988/7 "2019-05-08T22:22:25Z")

</div>

I’ve just started doing this (writing my own training function). You have to dig into internals a bit, but the most challenging part is if you’re not familiar with AD. However, if you’re just starting out I’d just stick to the built in `train!` method while you get a better intuition.
