# How to use Lux's \`apply\` function?

**URL:** https://discourse.julialang.org/t/how-to-use-luxs-apply-function/130339
**Category:** Machine Learning
**Tags:** neural-network, lux
**Created:** [June 30, 2025, 4:31pm UTC](https://discourse.julialang.org/t/how-to-use-luxs-apply-function/130339 "2025-06-30T16:31:22Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [June 30, 2025, 4:31pm UTC](https://discourse.julialang.org/t/how-to-use-luxs-apply-function/130339/1 "2025-06-30T16:31:22Z")

</div>

I am trying to get Lux to work, but am stumbling at a relatively early stage of the tutorials. I am trying to use a combination of the quickstart and getting started pages ([Getting Started | Lux.jl Docs](https://lux.csail.mit.edu/stable/introduction/index#Quickstart), [Getting Started | Lux.jl Docs](https://lux.csail.mit.edu/stable/introduction/)).

Here, I am trying to simply fit a neural network to match data from a simple R1 → R1 function:

```julia
# Fetch packages.
using Lux, Random, Optimisers, Zygote, StableRNGs, Plots

# Create training data and true function.
f(x) = 0.5 * (x^3)/(x^3 + 0.7^3)
xs = collect(0.0:0.1:1.0)
ys = [(0.9 + 0.2rand())*f(x) for x in xs]

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/2/12cbe4bad4a3e16129990d9b358feca1e1aa0946.png)

Following the tutorial I get to

```julia
# Set RNG.
rng = StableRNG(1234)

# Create Neural network.
model = Lux.Chain(
    Lux.Dense(1 => 3, Lux.softplus, use_bias = false),
    Lux.Dense(3 => 3, Lux.softplus, use_bias = false),
    Lux.Dense(3 => 1, Lux.softplus, use_bias = false)
)

# Prepares the model for training.
ps, st = Lux.setup(rng, model)

```

However, next things go wrong, I try:

```julia
x = reshape(xs, 1, length(xs))
y, st = Lux.apply(model, xs, ps, st)

```

but get an error

```julia
ERROR: DimensionMismatch: matrix A has axes (Base.OneTo(3),Base.OneTo(1)), matrix B has axes (Base.OneTo(11),Base.OneTo(1))

```

What should I do here? My impression is that the first dimension of `x` should be the number of inputs to the network (in my case 1). The second dimension it is never really said what it is. My best guess here was, since I am training on 11 data points, I should have it be 11. This is obviously wrong.

In addition to reading the tutorial, I tried

```julia
?Lux.apply

```

but it just calls `x` the `input`. I also tried looking a bit at the manual, but didn’t really find anything.

Exactly what should `x` be here?

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [June 30, 2025, 4:51pm UTC](https://discourse.julialang.org/t/how-to-use-luxs-apply-function/130339/2 "2025-06-30T16:51:29Z")

</div>

> [@Torkel](#):
>
> ```julia
> x = reshape(xs, 1, length(xs))
> y, st = Lux.apply(model, xs, ps, st)
> 
> ```

I think you meant `x` instead of `xs` in the second argument on the second line.

---

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [June 30, 2025, 5:01pm UTC](https://discourse.julialang.org/t/how-to-use-luxs-apply-function/130339/3 "2025-06-30T17:01:08Z")

</div>

Brilliant!  
(and very embarassing)

Thansk a lot, works well 🙂
