1
$\begingroup$

I am working on a Reinforcement Learning problem in StableBaselines3, but I don't think that really matters for this question. SB3 is based on PyTorch.

So, below is a screenshot of my model architecture (after I wrote in code that I want 2 layers, each with 64 units/nodes): enter image description here

This is a PPO algorithm in Reinforcement Learning, which is why there are two (identical) networks. Again, I don't think that's really relevant to the question.

You can see that the first layer has an input of 101 features (because I have 101 variables), but an output of 64 features. I'm not sure I understand how this works.

What does it mean when it says # of "in/out features"? If these are fully-connected dense layers (which I think they are), there are way more than 101 or 64 connections, right? Since each node is connect to every single node in the next layer?

So then, what does the 101 and 64 refer to?

$\endgroup$

1 Answer 1

1
$\begingroup$

A linear layer has a weight matrix $W$ and a bias vector $b$. (If you do not estimate a bias vector, then $b=0$.) The weight matrix has shape $n\times m$, i.e. $n$ rows and $m$ columns. The bias vector has shape $m \times 1$. The total number of parameters is $nm + m$ (if you don't use a bias, then $m$ of these parameters are fixed at 0, i.e. they are not trained).

Given a vector input $x$ with shape $m \times 1$, the output of a linear layer is given by $f(x) = Wx + b$, which is a vector of shape $n \times 1$.

So we know that there are $m$ inputs ("in features") to the linear layer $f$ and $n$ outputs ("out features").

$\endgroup$
2
  • $\begingroup$ Thank you for your response! So, to be clear then - the number of outputs of layer 1 is 64 (because there are 64 nodes and each gives 1 output), but each of those outputs goes to every node in the next layer (also 64 nodes), so the number of trainable weights between layer 1 and 2 is 64x64? And if biases are included, then 64x64x2 trainable parameters? $\endgroup$ Jun 28, 2022 at 19:06
  • 1
    $\begingroup$ We know the number of output features is 64 in the first 2 layers of your example because the printout says out_features=64. Yes, linear layers connect all inputs to all outputs; to verify, write out each element of the result as sums and products of the inputs. Layer 1 has 101 inputs and 64 outputs, so it has $101 \times 64 + 64$ parameters with biases. Layer 2 has $64^2 + 64$ parameters with biases. $\endgroup$
    – Sycorax
    Jun 28, 2022 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.