Skip to content

Commit

Permalink
shuffle feng_train
Browse files Browse the repository at this point in the history
  • Loading branch information
qolina committed Aug 29, 2017
1 parent f644f2c commit 1d13ae6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
24 changes: 14 additions & 10 deletions src/lstm_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(self, pretrain_embedding, pretrain_embed_dim, lstm_hidden_dim, voca

# conv layer
self.cnn_flag = False
if self.cnn_flag: print "##------ Use CNN: true"
self.position_size = 300
self.position_dim = 5
self.position_embeddings = nn.Embedding(self.position_size, self.position_dim)
Expand Down Expand Up @@ -115,17 +116,19 @@ def forward(self, sentence, gpu, debug=False):
if debug:
print "## word embedding:", type(self.word_embeddings.weight.data), self.word_embeddings.weight.data.size()
print self.word_embeddings.weight.data[:5, :5]
print type(self.word_embeddings.weight)
#print type(self.word_embeddings.weight)
print "## position embedding:", self.position_embeddings.weight.requires_grad, type(self.position_embeddings.weight), type(self.position_embeddings.weight.data), self.position_embeddings.weight.data.size()
print self.position_embeddings.weight.data[:5]
print "## embeds", embeds.requires_grad, embeds.data[:10]
#print "## embeds", embeds.requires_grad, embeds.data[:10]

# output grad
#if self.word_embeddings.weight.grad is not None:
# print "## word embedding grad:", self.word_embeddings.weight.grad#[:5, :5]
#if self.position_embeddings.weight.grad is not None:
# print "## position embedding grad:", self.position_embeddings.weight.grad[:5]
#if embeds.grad is not None:
# print "## sent word embedding grad:", embeds.grad[:5, :5]

if self.random_embed:
pretrain_embeds = self.pretrain_word_embeddings(sentence)
embeds = torch.cat((pretrain_embeds, embeds), 1)
Expand All @@ -141,7 +144,7 @@ def forward(self, sentence, gpu, debug=False):
for word_id, position in enumerate(positions):
if debug and word_id == 0:
print "## -------------- word_id", word_id
print position.data.view(1, -1)
#print position.data.view(1, -1)
if gpu: position = position.cuda()
pos_embeds = self.position_embeddings(position)
comb_embeds = torch.cat((embeds, pos_embeds), 1)
Expand All @@ -151,7 +154,7 @@ def forward(self, sentence, gpu, debug=False):
print "## maxp2:", type(self.maxp2)
print "## input:", type(inputs.data), inputs.data.size()
print "## pos_embeds:", type(pos_embeds.data), pos_embeds.data.size()
print pos_embeds.data[:5]
#print pos_embeds.data[:5]

c1 = self.conv1(inputs) # batch_size*out_channels*(sent_length-conv_width+1)
if debug and word_id == 0:
Expand All @@ -169,9 +172,9 @@ def forward(self, sentence, gpu, debug=False):

c1_embed_temp = self.cnnformat2lstm(p1)
c2_embed_temp = self.cnnformat2lstm(p2)
if debug and word_id == 0:
print "## c1_embed_temp:", type(c1_embed_temp.data), c1_embed_temp.data.size()
print "## c2_embed_temp:", type(c2_embed_temp.data), c2_embed_temp.data.size()
#if debug and word_id == 0:
# print "## c1_embed_temp:", type(c1_embed_temp.data), c1_embed_temp.data.size()
# print "## c2_embed_temp:", type(c2_embed_temp.data), c2_embed_temp.data.size()
if word_id == 0:
c1_embed = c1_embed_temp
c2_embed = c2_embed_temp
Expand All @@ -180,16 +183,17 @@ def forward(self, sentence, gpu, debug=False):
c2_embed = torch.cat((c2_embed, c2_embed_temp), 0)
if debug:
print "## c1_embed:", type(c1_embed.data), c1_embed.data.size()
print c1_embed.data[:5, :5]
#print c1_embed.data[:5, :5]
print "## c2_embed:", type(c2_embed.data), c2_embed.data.size()
print c2_embed.data[:5, :5]
#print c2_embed.data[:5, :5]

embeds = self.drop(embeds)
lstm_out, self.hidden = self.lstm(
embeds.view(len(sentence), 1, -1), self.hidden)
lstm_out = lstm_out.view(len(sentence), -1)
if debug:
print "## lstm out:", lstm_out.data[:10, :10]
print "## lstm out:", type(lstm_out.data)
#print lstm_out.data[:10, :10]
hidden_in = lstm_out
if self.cnn_flag:
#c1_embed= c1_embed.expand(len(sentence), c1_embed.size()[1])
Expand Down
3 changes: 2 additions & 1 deletion src/trigger_lstm.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def main():
training_data, test_data, vocab, tags_data, pretrain_embedding, model_path = load_data()
model_path = model_path + "_" + time.strftime("%Y%m%d%H%M%S", time.gmtime()) + "_"
if True:
random.shuffle(training_data, lambda: 0.3) # shuffle data before get dev
training_data = training_data[:-500]
dev_data = training_data[-500:]
else:
Expand Down Expand Up @@ -221,7 +222,7 @@ def main():
num_layers = 1
iteration_num = 200
Hidden_dim = 300
learning_rate = 0.05
learning_rate = 0.03
Embedding_dim = pretrain_embed_dim

conv_width1 = 2
Expand Down

0 comments on commit 1d13ae6

Please sign in to comment.