# Input to Neural Network

**URL:** <https://discourse.julialang.org/t/input-to-neural-network/90770>\
**Category:** Machine Learning\
**Created:** [November 24, 2022, 7:11pm UTC](https://discourse.julialang.org/t/input-to-neural-network/90770 "2022-11-24T19:11:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kuldeep](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kuldeep/32/33657_2.png) [@Kuldeep](https://discourse.julialang.org/u/Kuldeep)\
**Post date:** [November 24, 2022, 7:11pm UTC](https://discourse.julialang.org/t/input-to-neural-network/90770/1 "2022-11-24T19:11:38Z")

</div>

Hi,  
I am new to both Julia and deep learning.

Suppose we define a following network:

```julia
using Flux

nn_width = 10
m = Chain(Dense(1,nn_width,tanh),
            Dense(nn_width,nn_width,tanh),
            Dense(nn_width,1))

input1 =Vector(-2:0.01:2)'
input2 =Vector(-2:0.01:2)

m(input1) # this works
m(input2) # this doesn't work

```

Q-1 What’s the reason for this behavior? My initial prior was both won’t work.

Q-2 How can we do something like m.(input) ? map(m, input) doesn’t work.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [November 24, 2022, 9:11pm UTC](https://discourse.julialang.org/t/input-to-neural-network/90770/2 "2022-11-24T21:11:21Z")

</div>

The model `m` as defined takes a one-dimensional vector as input and outputs a one-dimensional vector:

```julia
julia> m([1.2])
1-element Vector{Float64}:
 -0.7267242113929453

```

Now,

1. why does `m(input2)` not work?  
You are passing a vector of 401 dimensions instead of one.
2. why does `m.(input2)` or `map(m, input2)` not work?  
This calls the model on each element, i.e., a scalar, of `input`. Yet, a Flux model requires a vector as input. The following will work:

```julia
map(x -> m([x]), input2)
m.(eachrow(input2))

```

Passing multiple inputs in this fashion is inconvenient though and the output is a nested vector of vectors. Thus, Flux allows passing a matrix of shape _input dimension_ \times _batch size_ to compute the outputs on a whole batch of inputs.
3. why does `m(input1)` work?  
`input1` has shape `(1, 401)` and is therefore interpreted as a batch of 401 one-dimensional inputs. Accordingly, it does work, computes the outputs on all 401 inputs and collects them into a matrix of shape _output dimension_ \times_batch size_.
