Trying to get the shape of conv layer in the __init__() method

I am trying to get the shape of the conv2 layer so that i can build a linear layer with that shape.

class PostConvNet(nn.Module):
“”"
Post Convolutional Network (mel → mel)
“”"
def init(self, num_hidden, num_input_layers):
“”"

    :param num_hidden: dimension of hidden 
    """
    super(PostConvNet, self).__init__()
    self.conv1 = nn.Conv1d(in_channels=hp.num_mels * hp.outputs_per_step,
                      out_channels=num_hidden,
                      kernel_size=16,
                      padding=4,
                      )

    self.conv_list = nn.Sequential(*[nn.Conv1d(in_channels=768,
                                 out_channels=768,
                                 kernel_size=16,
                                 padding=4,
                                 ) for _ in range(3)])
    
    self.conv2 = nn.Conv1d(in_channels=num_hidden,
                      out_channels=hp.num_mels * hp.outputs_per_step,
                      kernel_size=16,
                      padding=4)
    self.shape = self.conv2.shape[1]
    self.convLinear = nn.Linear(in_features=num_input_layers, out_features=self.shape)
    self.pre_batchnorm = nn.BatchNorm1d(num_hidden)
    self.batch_norm_list = clones(nn.BatchNorm1d(num_hidden), 3)
    self.dropout1 = nn.Dropout(p=0.1)
    self.dropout_list = nn.ModuleList([nn.Dropout(p=0.1) for _ in range(3)])

def forward(self, input_, mask=None):
    input_ = self.conv1(input_)
    input_1 = self.conv_list(input_)
    input_2 = self.conv2(input_1)
    input_ = self.convLinear(in_features=input_.shape(1), out_features=input_2.shape(1))(input_)
    
    return input_

My problem is i want to build a linear layer whose out_features are the shape of the conv2 layer

The output of a conv layer depends on the input shape and is thus not static. You could use an example input and check the shape manually or you could also use the nn.LazyLinear layer to allow PyTorch to set the in_features based on the input tensor.

Can i set the output_features in nn.LazyLinear to a value from the forward method?

You can set the out_features to any arbitrary value you wish and setting it to a computed value based on other operations would also work.
However, I’m unsure why you want to set this value in the forward method as it does not depend on any shapes of incoming activation tensors.