Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flux networks as arguments in functions #765

Closed
charlesll opened this issue May 7, 2019 · 3 comments
Closed

Flux networks as arguments in functions #765

charlesll opened this issue May 7, 2019 · 3 comments
Labels

Comments

@charlesll
Copy link

Hi,

I am using Flux 0.8 on Julia 1.1.0 for a project where I have various equations embedded in functions. Here is an example notebook with a toy problem looking like my research problem.

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?

Thanks in advance for any insights!

Charles.

@darsnack
Copy link
Member

I am not sure about the question, but the short answer is that any model can be directly passed to another function as an argument. There are no requirements to use globals (in fact, you probably shouldn't for most cases). Please let us know if there is more to the question. If not, I'll close this in a few days.

I see that you are trying to fit an equation. Maybe try SymbolicRegression.jl?

@charlesll
Copy link
Author

Thanks for the reply ! I think this actually answers the question: it is thus legit to pass any model to another function.

I think at the time I was asking this question, I was worried that this could slow-down the training if the model was copied or anything like that in the memory when passed to the function. But now I understand that the model is just provided to the function and anything happening inside the function should update the model. So passing the model to functions should not slow down things I guess.

Thanks for the SR package, I will try it !

@darsnack
Copy link
Member

Exactly, models (or any structs) are passed by reference to other functions. The model weight arrays are mutable, so as long as you modify them in-place, there should be no copying happening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants