# How to freeze a single weight of a layer?

**URL:** https://discourse.julialang.org/t/how-to-freeze-a-single-weight-of-a-layer/58279
**Category:** General Usage
**Tags:** flux
**Created:** [March 31, 2021, 7:37am UTC](https://discourse.julialang.org/t/how-to-freeze-a-single-weight-of-a-layer/58279 "2021-03-31T07:37:55Z")
**Posts on this page:** 1
**Showing post:** 4

<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: [April 2, 2021, 4:51pm UTC](https://discourse.julialang.org/t/how-to-freeze-a-single-weight-of-a-layer/58279/4 "2021-04-02T16:51:46Z")

</div>

The code above still removes an entire parameter. The easiest way to freeze a single connection/weight is to zero or mask out the corresponding index in the gradient array:

```julia
...

for epoch in 1:epochs
    for (x, y) in data
        gr = gradient(p) do 
            loss(x, y)
        end

        # remove offending gradient
        gr[model[1].W][2] = 0
        Flux.update!(opt, p, gr)
    end
end

```

---

_[View the full topic](https://discourse.julialang.org/t/how-to-freeze-a-single-weight-of-a-layer/58279)._
