# Help building a PID example

**URL:** <https://discourse.julialang.org/t/help-building-a-pid-example/27194>\
**Category:** Machine Learning\
**Created:** [August 5, 2019, 11:55pm UTC](https://discourse.julialang.org/t/help-building-a-pid-example/27194 "2019-08-05T23:55:14Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [August 5, 2019, 11:55pm UTC](https://discourse.julialang.org/t/help-building-a-pid-example/27194/1 "2019-08-05T23:55:15Z")

</div>

Hi guys!

I am trying to use Flux.jl to build a PIDNN as shown here: [PID Neural Network](https://molefrog.github.io/pidnn-talk/)

I started to code using only the proportional part. This, I need an input layer with 2 inputs and 1 output, and one output layer with 1 input and 1 output. This is my code so far:

```julia
struct Input
    W
end
 
Input() = Input(param(randn(2)))
 
(m::Input)(x) = m.W[1]*x[1] + m.W[2]*x[2]
 
struct P
    W
end
 
P() = P(param(randn()))
 
function (m::P)(x)
    # P
    if (x > 1)
        return m.W[1]
    elseif (x < -1)
        return -m.W[1]
    else
        return x*m.W[1]
    end
end
 
m = Chain(Input(), P())
 
function loss(x,y)
    # Simulate the system.
    t = 0:0.1:100
 
    o = x
 
    Δ = 0.0
 
    for k in t
        Δ = Δ + (y-o)^2
        r = m([y;o])
        o = o + r*0.1
    end
 
    return Δ/length(t)
end
 
ps = Flux.params(m)
Tracker.gradient(()->loss(0,1), ps)

```

Which produces the following error:

```julia
julia> Tracker.gradient(()->loss(0,1), ps)
ERROR: MethodError: Cannot `convert` an object of type Array{Float64,1} to an object of type Float64

```

Can anyone please help me?

Btw, if the input layer only have one input, then it works fine.

---

<div class="post-metadata">

**Author:** ![phelipe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phelipe/32/14643_2.png) [@phelipe](https://discourse.julialang.org/u/phelipe)\
**Post date:** [August 8, 2019, 2:45pm UTC](https://discourse.julialang.org/t/help-building-a-pid-example/27194/2 "2019-08-08T14:45:26Z")

</div>

> [@Ronis\_BR](#):
>
> r = m([y;o])

The model only has one input, the input data, you are using the input and output.  
It should be something like this:

> r = m(o)

I think “y” is Float and “o” is a Vector:

> (y-o)^2

In the loss function it is not clear who is the system to be controlled and I think there are errors in the controller, I think it should be something like this.

```julia
for k in t
       control_output = Tracker.data(m(o)) #control output
       out = model(control_output) #model output
       Δ = Δ + (y - out)^2 # squared error (desired output - model output)
       o = [y, out] # update next input 
    
end

```

Obs: Vi que é brasileiro 🙂, tem um grupo no telegram de usuários de julia no Brasil caso interesse. ([Telegram: Contact @juliabrasil](https://t.me/juliabrasil))

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [August 9, 2019, 5:43pm UTC](https://discourse.julialang.org/t/help-building-a-pid-example/27194/3 "2019-08-09T17:43:30Z")

</div>

Hi @phelipe !

Thanks! With the help of some people on Slack, I managed to finally tune my PID Neural Network. I will write a tutorial on my website, something like a Hello World on machine learning using Julia for Control engineers 😃

P.S.: Já me adicionei!

---

<div class="post-metadata">

**Author:** ![kevin.squire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin.squire/32/62_2.png) [@kevin.squire](https://discourse.julialang.org/u/kevin.squire)\
**Post date:** [January 16, 2020, 8:56pm UTC](https://discourse.julialang.org/t/help-building-a-pid-example/27194/4 "2020-01-16T20:56:35Z")

</div>

Hi @Ronis_BR,

Thanks for posting this question. It doesn’t look like you’ve had time to update your blog yet (or at least, I don’t see anything at [https://www.ronanarraes.com/blog/](https://www.ronanarraes.com/blog/)). Beyond what @phelipe suggested above, did you have to do much to get a working system? How well did it work?

Thanks,  
Kevin

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 17, 2020, 1:14am UTC](https://discourse.julialang.org/t/help-building-a-pid-example/27194/5 "2020-01-17T01:14:59Z")

</div>

Hi,

Indeed, I did not have time to write the text yet, sorry ☹ I decided to spend some time finishing the package PrettyTables.jl and trying to release an initial version of TextUserIntefaces.jl.

But it worked well after some modifications based on @phelipe advices. I currently have two students working on it and we managed to tune a PIDNN using Flux.jl. I will try to write the text in my blog soon about it 🙂

---

<div class="post-metadata">

**Author:** ![Peter-Jolly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter-jolly/32/19371_2.png) [@Peter-Jolly](https://discourse.julialang.org/u/Peter-Jolly)\
**Post date:** [February 8, 2021, 5:14am UTC](https://discourse.julialang.org/t/help-building-a-pid-example/27194/6 "2021-02-08T05:14:07Z")

</div>

Hi, I am wondering if you have written up your package yet? I am looking for something similar to tune a temperature and humidity PID controller in a Greenhouse(GH). I have a full numerical model of my GH working in Julia. Thanks

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [February 9, 2021, 1:57pm UTC](https://discourse.julialang.org/t/help-building-a-pid-example/27194/7 "2021-02-09T13:57:13Z")

</div>

Hi @Peter-Jolly

Yes, we could apply a PIDNN to control the satellite attitude. I should write a blog post shortly about it. However, the solution is very simple, a package to implement a PIDNN seems an overkill. We you need more details, please, let me know.

---

<div class="post-metadata">

**Author:** ![Peter-Jolly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter-jolly/32/19371_2.png) [@Peter-Jolly](https://discourse.julialang.org/u/Peter-Jolly)\
**Post date:** [February 10, 2021, 4:42am UTC](https://discourse.julialang.org/t/help-building-a-pid-example/27194/8 "2021-02-10T04:42:32Z")

</div>

Can I send you something by email? My email is [peter\_jolly21@hotmail.com](mailto:peter_jolly21@hotmail.com)

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [February 11, 2021, 12:31am UTC](https://discourse.julialang.org/t/help-building-a-pid-example/27194/9 "2021-02-11T00:31:44Z")

</div>

Yes, sure, I sent you an email.

---

<div class="post-metadata">

**Author:** ![Peter-Jolly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter-jolly/32/19371_2.png) [@Peter-Jolly](https://discourse.julialang.org/u/Peter-Jolly)\
**Post date:** [February 11, 2021, 1:08am UTC](https://discourse.julialang.org/t/help-building-a-pid-example/27194/10 "2021-02-11T01:08:51Z")

</div>

Thanks. I received and sent you a reply.

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [July 31, 2021, 10:01am UTC](https://discourse.julialang.org/t/help-building-a-pid-example/27194/12 "2021-07-31T10:01:00Z")

</div>

Tuning a PID controller for a transfer function (that is, a single-input-single-output system modelled as linear) using neural network? Isn’t that an overkill?

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [July 31, 2021, 2:30pm UTC](https://discourse.julialang.org/t/help-building-a-pid-example/27194/13 "2021-07-31T14:30:59Z")

</div>

> [@skela96](#):
>
> Hi, I’m trying to build neural network that will tune PID controller. Controlled process will be a transfer function. Are you willing to share the code with me ?

Hi @skela96 ,

What I did was to replace the PID with a Neural controller. As @zdenek_hurak said, tuning a PID using a neural network seems an overkill. What you can do (although I do not know if this is possible) is using Flux.jl back propagation to create an optimization framework to find the gains as they were the neural network weights.
