Thank you for your kind reply. I have changed my code to the following as instructed, but still obtained the same error.
class SDNE(nn.Module):
    def __init__(self, encode_dim, N, batch_size=128):
        super(SDNE, self).__init__()
        self.encode_dim = encode_dim
        self.vectors = {}
        self.N = N
        self.batch_size = batch_size
        self.sigmoid = nn.Sigmoid()
        self.linear1 = nn.Linear(self.N, 5000)
        self.linear2 = nn.Linear(5000, 1000)

        self.linear3 = nn.Linear(self.N, 1000)
        self.linear4 = nn.Linear(1000, 100)
        self.linear5 = nn.Linear(100, 1000)
        self.linear6 = nn.Linear(1000, self.N)

        self.linear7 = nn.Linear(1000, 5000)
        self.linear8 = nn.Linear(5000, self.N)

    def forward(self, x):
        if self.encode_dim == 3:
            emb = self.sigmoid(self.linear3(x))
            emb = self.sigmoid(self.linear4(emb))
            recon = self.sigmoid(self.linear5(emb))
            recon = self.sigmoid(self.linear6(recon))
        else:
            emb = self.sigmoid(self.linear1(x))
            emb = self.sigmoid(self.linear2(emb))
            emb = self.sigmoid(self.linear4(emb))
            recon = self.sigmoid(self.linear5(emb))
            recon = self.sigmoid(self.linear7(recon))
            recon = self.sigmoid(self.linear8(recon))

        # returning embedding embedding layer and reconstruction output
        return emb, recon