# Accessing a specific layer's weights in a Flux Chain

**URL:** https://discourse.julialang.org/t/accessing-a-specific-layers-weights-in-a-flux-chain/64180
**Category:** Machine Learning
**Tags:** question, flux
**Created:** [July 6, 2021, 9:47pm UTC](https://discourse.julialang.org/t/accessing-a-specific-layers-weights-in-a-flux-chain/64180 "2021-07-06T21:47:02Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![bad\_at\_math](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@bad\_at\_math](https://discourse.julialang.org/u/bad_at_math)
#### Post date: [July 6, 2021, 9:47pm UTC](https://discourse.julialang.org/t/accessing-a-specific-layers-weights-in-a-flux-chain/64180/1 "2021-07-06T21:47:02Z")

</div>

In PyTorch, I can define a network like this:

```julia
from torch import nn

class Network(nn.Module):
    def __init__ (self, ...):
        ...
        self.ln_1 = nn.Linear(64, 32)
        self.ln_2 = nn.Linear(32, 16)
        ...

```

The `named_parameters` method ([Module — PyTorch 1.12 documentation](https://pytorch.org/docs/stable/generated/torch.nn.Module.html)) lets you iterate through the modules in a network and access them (and their gradients) by name (e.g., `ln_1`).

Does Flux have similar functionality? MWE:

```julia
using Flux 
network = Chain(Dense(64, 32, tanh), Dense(32, 16, tanh))
ps = Flux.params(network)
point = ... # data point
criterion = ... # loss function
gs = Flux.gradient(ps) do 
    loss = criterion(point...)
    return loss_val
end

```

then I can access the gradient of the first layer’s gradients with `gs[network[1].weights]`, but this is maybe a little less interpretable than in PyTorch, since `gs`’s keys are arrays, not strings.

---

<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: [July 7, 2021, 1:15am UTC](https://discourse.julialang.org/t/accessing-a-specific-layers-weights-in-a-flux-chain/64180/2 "2021-07-07T01:15:44Z")

</div>

This is exactly what explicit parameters were designed for:

```julia
...
criterion(m, ...) = ... # loss function
gs = Flux.gradient(network) do m
    loss = criterion(m, point...)
    return loss
end

size(network[1].weights) == size(gs[1].weights) # true

```
