when I define a model as follows:
class TextNet(nn.Module):
def init(self, input_dim=100, hidden_dim=100):
super(TextNet, self).__init__()
self.input_dim = input_dim
self.hidden_dim = hidden_dim
###lstm层生成句子的encoding表示
self.encoder = nn.LSTM(input_size=100, hidden_size=100, batch_first=True)
self.fc1 = nn.Sequential(nn.Linear(200, 100), nn.Tanh()
)
self.fc2 = nn.Sequential(nn.Linear(100, 50), nn.Tanh())
self.fc3 = nn.Sequential(nn.Linear(50, 25), nn.Tanh())
self.fc4 = nn.Sequential(nn.Linear(25, 1), nn.Sigmoid())
def forward(self, input_p, input_h, lens_p, lens_h):
self.encoder.flatten_parameters()
# encode sentence p
_, idx_sort_p = torch.sort(lens_p, dim=0, descending=True)
_, idx_unsort_p = torch.sort(idx_sort_p, dim=0)
lens_p = list(lens_p[idx_sort_p])
enc_pack_input_p = input_p.index_select(0, idx_sort_p)
total_length_p = enc_pack_input_p.size(1)
enc_pack_input_p = pack_padded_sequence(enc_pack_input_p, lens_p, True)
enc_pack_output_p, (_, _) = self.encoder(enc_pack_input_p)
output_p, _ = pad_packed_sequence(enc_pack_output_p, batch_first=True,
total_length=total_length_p)
enc_p = output_p[0].index_select(0, idx_unsort_p)
# encode sentence h
_, idx_sort_h = torch.sort(lens_h, dim=0, descending=True)
_, idx_unsort_h = torch.sort(idx_sort_h, dim=0)
lens_h = list(lens_h[idx_sort_h])
enc_pack_input_h = input_h.index_select(0, idx_sort_h)
total_length_h = enc_pack_input_h.size(1)
enc_pack_input_h = pack_padded_sequence(enc_pack_input_h, lens_h, True)
enc_pack_output_h, (_, _) = self.encoder(enc_pack_input_h)
output_h, _ = pad_packed_sequence(enc_pack_output_h, batch_first=True,
total_length=total_length_h)
enc_h = output_h[0].index_select(0, idx_unsort_h)
x = torch.cat(enc_p, enc_h)
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
x = self.fc4(x)
return x
after that I do some print operation。
model = TextNet()
print(model)
params = list(model.parameters())
print(params)
print(len(params))
I got the results like that:
TextNet()
[]
0
which means the parameter of this model is empty! I really don’t know what’s tha problems, can anyone help me ? thanks a lot!
Preformatted text