# How to create a loss function with 3 inputs in Flux?

**URL:** <https://discourse.julialang.org/t/how-to-create-a-loss-function-with-3-inputs-in-flux/45755>\
**Category:** Machine Learning\
**Created:** [August 29, 2020, 4:58pm UTC](https://discourse.julialang.org/t/how-to-create-a-loss-function-with-3-inputs-in-flux/45755 "2020-08-29T16:58:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DeepQ](https://avatars.discourse-cdn.com/v4/letter/d/2bfe46/32.png) [@DeepQ](https://discourse.julialang.org/u/DeepQ)\
**Post date:** [August 29, 2020, 4:58pm UTC](https://discourse.julialang.org/t/how-to-create-a-loss-function-with-3-inputs-in-flux/45755/1 "2020-08-29T16:58:39Z")

</div>

How could I create a loss function with 3 inputs like loss(w,x,y)?  
Could someone show me an example?

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [August 29, 2020, 6:28pm UTC](https://discourse.julialang.org/t/how-to-create-a-loss-function-with-3-inputs-in-flux/45755/2 "2020-08-29T18:28:56Z")

</div>

Have you run into an issue using a 3 input loss function? `loss(w, x, y)` should work just fine. For example:

```julia
loss(m, x, y) = Flux.mse(m(x), y)

```

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [August 30, 2020, 3:51am UTC](https://discourse.julialang.org/t/how-to-create-a-loss-function-with-3-inputs-in-flux/45755/3 "2020-08-30T03:51:26Z")

</div>

Since you mentioned Flux and the Flux doc for `Flux.train!` says the below

**If d is a tuple of arguments to loss call loss(d…), else call loss(d).**

I imagine, you can have as many inputs as you want as long as you pass in the `data = [(w, x, y), (w1, x1, y1) etc etc]`

Full Doc as below

```julia
  train!(loss, params, data, opt; cb)

  For each datapoint d in data, compute the gradient of loss with respect to params through backpropagation and call
  the optimizer opt.

  **If d is a tuple of arguments to loss call loss(d...), else call loss(d).**

  A callback is given with the keyword argument cb. For example, this will print "training" every 10 seconds (using
  Flux.throttle):

  train!(loss, params, data, opt, cb = throttle(() -> println("training"), 10))

  The callback can call Flux.stop to interrupt the training loop.

  Multiple optimisers and callbacks can be passed to opt and cb as arrays.

```
