How to define the loss function using the output of intermediate layers?
class Model(nn.Module):
def __init__(self):
    super(Model, self).__init__()
    self.encoder = nn.Linear(300, 100)
    self.dense1 = nn.Sequential(nn.Linear(100, 10),nn.ReLU())
    self.dense2 = nn.Sequential(nn.Linear(10, 5),nn.ReLU())
    self.dense3 = nn.Sequential(nn.Linear(5, 1))
def forward(self, x):
    x = self.encoder(x)
    x = self.dense1(x)
    x = self.dense2(x)
    x = self.dense3(x)
    return x

I am working on a regression problem, and I need to use the output of the dense2 layer to calculate the loss.
output of dense2 layer is 5 dimensional (5x1).
I am using PyTorch.
Dataset: Suppose i am using 300 features and i need to predict some score(a floating value).
Input: 300 Features
Output: Some Floating Value

In general, your nn.Module can return as many elements as you like. Moreover, you don't have to use them anywhere - there is no mechanism that checks that. Pytorch philosophy is to compute computational graph on-the-run.
class Model(nn.Module):
def __init__(self):
    super(Model, self).__init__()
    self.encoder = nn.Linear(300, 100)
    self.dense1 = nn.Sequential(nn.Linear(100, 10),nn.ReLU())
    self.dense2 = nn.Sequential(nn.Linear(10, 5),nn.ReLU())
    self.dense3 = nn.Sequential(nn.Linear(5, 1))

def forward(self, x):
    enc_output = self.encoder(x)
    dense1_output = self.dense1(enc_output)
    dense2_output = self.dense2(dense1_output)
    dense3_output = self.dense3(dense2_output)
    return dense3_output, dense2_output

