# Retrieve the NN from a DQNTrainer agent

**URL:** <https://discuss.ray.io/t/retrieve-the-nn-from-a-dqntrainer-agent/10200>\
**Category:** Uncategorized\
**Created:** [April 12, 2023, 11:38am UTC](https://discuss.ray.io/t/retrieve-the-nn-from-a-dqntrainer-agent/10200 "2023-04-12T11:38:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlorop](https://avatars.discourse-cdn.com/v4/letter/c/b19c9b/32.png) [@carlorop](https://discuss.ray.io/u/carlorop)\
**Post date:** [April 12, 2023, 11:38am UTC](https://discuss.ray.io/t/retrieve-the-nn-from-a-dqntrainer-agent/10200/1 "2023-04-12T11:38:31Z")

</div>

I can retrieve the weight-bias dictionary from the DQNTrainer `trainer` using the following command:

```python
weights = _service.model.get_policy().get_weights()

```

In this case, I trained a model using {“fcnet\_hiddens”:[100, 50],“fcnet\_activation”:"tanh}. The rest of the model config is default from dqn.DEFAULT\_CONFIG. When I run the previous command, I get several tensors, [‘default\_policy/fc\_1/kernel’, ‘default\_policy/fc\_1/bias’, ‘default\_policy/fc\_out/kernel’, ‘default\_policy/fc\_out/bias’, ‘default\_policy/value\_out/kernel’, ‘default\_policy/value\_out/bias’, ‘default\_policy/hidden\_0/kernel’, ‘default\_policy/hidden\_0/bias’, ‘default\_policy/dense/kernel’, ‘default\_policy/dense/bias’, ‘default\_policy/dense\_1/kernel’, ‘default\_policy/dense\_1/bias’, ‘default\_policy/dense\_2/kernel’, ‘default\_policy/dense\_2/bias’]. This is the input dimension of these tensors:

```auto
default_policy/fc_1/kernel: 51
default_policy/fc_1/bias: 100
default_policy/fc_out/kernel: 100
default_policy/fc_out/bias: 50
default_policy/value_out/kernel: 100
default_policy/value_out/bias: 1
default_policy/hidden_0/kernel: 50
default_policy/hidden_0/bias: 256
default_policy/dense/kernel: 256
default_policy/dense/bias: 3
default_policy/dense_1/kernel: 50
default_policy/dense_1/bias: 256
default_policy/dense_2/kernel: 256
default_policy/dense_2/bias: 1

```

The input and output dimension are 51 and 3 respectively, hence, I assume that the layers of the policy network are fc\_1 → fc\_out → hidden\_0 → dense. Renaming them as fc1, fc2, fc3 and fc4. I have created this NN in pytorch

```python
class MyModel(torch.nn.Module):
    def __init__ (self, ):
        super(MyModel, self). __init__ ()
        self.fc1 = torch.nn.Linear(51, 100)
        self.fc2 = torch.nn.Linear(100, 50)
        self.fc3 = torch.nn.Linear(50, 256)
        self.fc4 = torch.nn.Linear(256, 3)

    def forward(self, x):
        x = torch.tanh(self.fc1(x))
        x = torch.tanh(self.fc2(x))
        x = torch.tanh(self.fc3(x))
        x = torch.argmax(self.fc4(x))
        return x

```

I have also set the weights based on the weight-bias dictionary. However, it is not returning the same outputs as the agent. How can I retrieve the neural network from the DQNTrainer agent?
