def forward(self, input, index, src_enc): """ # input: relBatch: packed_gold_amr_lengths x n_feature, AMR_CAT, AMR_LE, AMR_NER, AMR_SENSE, index of nodes, # mypacked_seq[packed_batch_gold_amr_len x tgt_feature_dim] # index: rel_index_batch: list(batch, real_gold_amr_len), but content is the index of recatogrized amr index, is a mapping # src_enc: DoublePackedSequence(packed: packed_g_amr_len x re_amr_len x dim), length= re_lens) """ assert isinstance(input, MyPackedSequence), input # lengths: real_gold_amr_lens # after unpack, input is packed_gold_amr_lengths x n_features input, lengths = input if self.alpha_dropout and self.training: input = data_dropout(input, self.alpha_dropout) psd_target_pos_embed = self.psd_target_pos_lut(input[:, PSD_POS]) #psd_sense_embed = self.psd_sense_lut(input[:,PSD_SENSE]) #psd_lemma_embed = self.lemma_lut(input[:,PSD_LE]) #psd_emb = torch.cat([psd_target_pos_embed, psd_sense_embed,psd_lemma_embed],1) #psd_emb = torch.cat([psd_target_pos_embed, psd_sense_embed],1) psd_emb = torch.cat([psd_target_pos_embed], 1) # head_emb_t : MyPackedSequence(data: packed_real_g_amr_l x dim), g_amr_l # dep_emb_t : MyDoublePackedSequence(PackedSequenceLength(packed_real_g_amr_l x real_g_amr_l x dim), length_pairs) # length_pairs :(g_amr_l, g_amr_l) head_emb_t, dep_emb_t, length_pairs = self.getEmb( index, src_enc) #packed, mydoublepacked head_emb = torch.cat([psd_emb, head_emb_t.data], 1) dep_psd_emb_t = myunpack(*MyPackedSequence(psd_emb, lengths)) dep_psd_emb = [ emb.unsqueeze(0).expand(emb.size(0), emb.size(0), emb.size(-1)) for emb in dep_psd_emb_t ] mydouble_psd_emb = mydoublepack(dep_psd_emb, length_pairs) dep_emb = torch.cat([mydouble_psd_emb.data, dep_emb_t.data], -1) # emb_unpacked = myunpack(emb,lengths) assert head_emb.size( -1) == self.inputSize, "wrong head size {}".format( head_emb.size()) # head_packed : MyPackedSequence(data: packed_real_g_amr_l x rel_dim), g_amr_l head_packed = MyPackedSequence(self.head(head_emb), lengths) # total,rel_dim head_psd_packed = MyPackedSequence(psd_emb, lengths) # total,rel_dim size = dep_emb.size() assert dep_emb.size(-1) == self.inputSize, "wrong dep size {}".format( dep_emb.size()) dep = self.dep(dep_emb.view(-1, size[-1])).view(size[0], size[1], -1) # dep_emb_t : MyDoublePackedSequence(PackedSequenceLength(packed_real_g_amr_l x real_g_amr_l x rel_dim), length_pairs) dep_packed = MyDoublePackedSequence( MyPackedSequence(dep, mydouble_psd_emb[0][1]), mydouble_psd_emb[1], dep) return head_psd_packed, head_packed, dep_packed #,MyPackedSequence(emb,lengths)
def forward(self, input, index, src_enc): """ # input: relBatch, packed_gold_amr_lengths x n_feature, AMR_CAT, AMR_LE, AMR_NER, AMR_SENSE, index of nodes, # index: rel_index_batch: list(batch, real_gold_amr_len), but content is the index of recatogrized amr index, is a mapping # src_enc: is weighted root_src_enc, MyPackedSequence(data: packed_re_amr_len x txt_enc_size, lengtgs: re_amr_lens) """ assert isinstance(input, MyPackedSequence), input # lengths: real_gold_amr_length_ input, lengths = input if self.alpha_dropout and self.training: input = data_dropout(input, self.alpha_dropout) dm_target_pos_embed = self.dm_target_pos_lut(input[:, DM_POS]) dm_cat_embed = self.dm_cat_lut(input[:, DM_CAT]) dm_sense_embed = self.dm_sense_lut(input[:, DM_SENSE]) # dm_emb = torch.cat([dm_target_pos_embed,dm_cat_embed],1) dm_emb = torch.cat([dm_target_pos_embed, dm_cat_embed, dm_sense_embed], 1) # head_emb: MyPackedSequence(data: packed_gold_amr_len x dim, lengths: readl_gold_amr_len) head_emb = self.getEmb(index, src_enc) #packed, mydoublepacked # head_emb.data, [packed_head_amr_len, src_enc_size] root_emb = torch.cat([dm_emb, head_emb.data], 1) root_emb = self.root(root_emb) return MyPackedSequence(root_emb, lengths)
def forward(self, _, heads, deps): '''heads.data: mypacked amr_l x rel_dim deps.data: mydoublepacked amr_l x amr_l x rel_dim ''' heads_data = heads.data deps_data = deps.data head_bilinear_transformed = self.bilinear( heads_data) #all_data x ( n_rel x inputsize) head_bias_unpacked = myunpack(self.head_bias(heads_data), heads.lengths) #[len x n_rel] size = deps_data.size() dep_bias = self.dep_bias(deps_data.view(-1, size[-1])).view( size[0], size[1], -1) dep_bias_unpacked, length_pairs = mydoubleunpack( MyDoublePackedSequence(MyPackedSequence(dep_bias, deps[0][1]), deps[1], dep_bias)) #[len x n_rel] bilinear_unpacked = myunpack(head_bilinear_transformed, heads.lengths) deps_unpacked, length_pairs = mydoubleunpack(deps) output, l = self.bilinearForParallel( zip(bilinear_unpacked, deps_unpacked, head_bias_unpacked, dep_bias_unpacked), length_pairs) myscore_packed = mypack(output, l) # prob_packed = MyPackedSequence(myscore_packed.data,l) return myscore_packed
def rel_to_batch(rel_batch_p,rel_index_batch_p,data_iterator,dicts): lemma_dict,category_dict = dicts["lemma_dict"], dicts["category_dict"] data = [torch.LongTensor([[category_dict[uni.cat],lemma_dict[uni.le],0] for uni in uni_seq]) for uni_seq in rel_batch_p ] rel_index = [torch.LongTensor(index) for index in rel_index_batch_p] rel_batch,rel_index_batch,rel_lengths = data_iterator._batchify_rel_concept(data,rel_index) return MyPackedSequence(rel_batch,rel_lengths),rel_index_batch
def forward(self, input, index,src_enc): assert isinstance(input, MyPackedSequence),input input,lengths = input if self.alpha and self.training: input = data_dropout(input,self.alpha) cat_embed = self.cat_lut(input[:,AMR_CAT]) lemma_embed = self.lemma_lut(input[:,AMR_LE]) amr_emb = torch.cat([cat_embed,lemma_embed],1) # print (input,lengths) head_emb_t,dep_emb_t,length_pairs = self.getEmb(index,src_enc) #packed, mydoublepacked head_emb = torch.cat([amr_emb,head_emb_t.data],1) dep_amr_emb_t = myunpack(*MyPackedSequence(amr_emb,lengths)) dep_amr_emb = [ emb.unsqueeze(0).expand(emb.size(0),emb.size(0),emb.size(-1)) for emb in dep_amr_emb_t] mydouble_amr_emb = mydoublepack(dep_amr_emb,length_pairs) # print ("rel_encoder",mydouble_amr_emb.data.size(),dep_emb_t.data.size()) dep_emb = torch.cat([mydouble_amr_emb.data,dep_emb_t.data],-1) # emb_unpacked = myunpack(emb,lengths) head_packed = MyPackedSequence(self.head(head_emb),lengths) # total,rel_dim head_amr_packed = MyPackedSequence(amr_emb,lengths) # total,rel_dim # print ("dep_emb",dep_emb.size()) size = dep_emb.size() dep = self.dep(dep_emb.view(-1,size[-1])).view(size[0],size[1],-1) dep_packed = MyDoublePackedSequence(MyPackedSequence(dep,mydouble_amr_emb[0][1]),mydouble_amr_emb[1],dep) return head_amr_packed,head_packed,dep_packed #,MyPackedSequence(emb,lengths)
def forward(self, input, index,src_enc): assert isinstance(input, MyPackedSequence),input input,lengths = input if self.alpha and self.training: input = data_dropout(input,self.alpha) cat_embed = self.cat_lut(input[:,AMR_CAT]) lemma_embed = self.lemma_lut(input[:,AMR_LE]) amr_emb = torch.cat([cat_embed,lemma_embed],1) # print (input,lengths) head_emb = self.getEmb(index,src_enc) #packed, mydoublepacked root_emb = torch.cat([amr_emb,head_emb.data],1) root_emb = self.root(root_emb) return MyPackedSequence(root_emb,lengths)
def rel_to_batch(rel_batch_p,rel_index_batch_p,data_iterator,dicts,frame): """ build rel batch with concept batches. rel_batch_p: concept batches rel_index_batch_p : alignment batches. """ if frame =="amr": lemma_dict,amr_category_dict = dicts["lemma_dict"], dicts["amr_category_dict"] data = [torch.LongTensor([[amr_category_dict[uni.cat],lemma_dict[uni.le],0] for uni in uni_seq]) for uni_seq in rel_batch_p ] elif frame=="dm": target_pos_dict,cat_dict = dicts["dm_target_pos_dict"], dicts["dm_cat_dict"] data = [torch.LongTensor([[target_pos_dict[uni.pos],cat_dict[uni.cat],0] for uni in uni_seq]) for uni_seq in rel_batch_p ] elif frame =="psd": psd_target_pos_dict,psd_sense_dict = dicts["psd_target_pos_dict"], dicts["psd_sense_dict"] data = [torch.LongTensor([[psd_target_pos_dict[uni.pos],psd_sense_dict[uni.sense],0] for uni in uni_seq]) for uni_seq in rel_batch_p ] else: raise NotImplementedError("{} is not supported".format(frame)) rel_index = [torch.LongTensor(index) for index in rel_index_batch_p] rel_batch,rel_index_batch,rel_lengths = data_iterator._batchify_rel_concept(data,rel_index) return MyPackedSequence(rel_batch,rel_lengths),rel_index_batch
def __getitem__(self, index): assert index < self.numBatches, "%d > %d" % (index, self.numBatches) lengths,max_len = self.getLengths(index ) def wrap(b,l ): #batch, len, feature if b is None: return b b = torch.stack(b, 0).transpose(0,1).contiguous() if self.cuda: b = b.cuda() packed = pack(b,list(l)) return PackedSequence(Variable(packed[0], volatile=self.volatile,requires_grad = False),packed[1]) def wrap_align(b,l ): #batch, len_tgt, len_src if b is None: return b b = torch.stack(b, 0).transpose(0,1).contiguous().float() if self.cuda: b = b.cuda() packed = pack(b,list(l)) return PackedSequence(Variable(packed[0], volatile=self.volatile,requires_grad = False),packed[1]) srcBatch = self._batchify_src( self.src[index*self.batchSize:(index+1)*self.batchSize],max_len) if self.source_only: src_sourceBatch = self.src_source[index*self.batchSize:(index+1)*self.batchSize] batch = zip( srcBatch,src_sourceBatch) lengths,max_len = self.getLengths(index ) order_data = sorted(list(enumerate(list(zip(batch, lengths)))),key = lambda x:-x[1][1]) order,data = zip(*order_data) batch, lengths = zip(*data) srcBatch,src_sourceBatch = zip(*batch) return order,wrap(srcBatch,lengths),src_sourceBatch else: tgtBatch = self._batchify_tgt( self.tgt[index*self.batchSize:(index+1)*self.batchSize],max_len) alignBatch = self._batchify_align( self.align_index[index*self.batchSize:(index+1)*self.batchSize],max_len) rel_seq_pre = self.rel_seq[index*self.batchSize:(index+1)*self.batchSize] rel_index_pre = self.rel_index[index*self.batchSize:(index+1)*self.batchSize] rel_role_pre = self.rel_mat[index*self.batchSize:(index+1)*self.batchSize] # roots = Variable(torch.IntTensor(self.root[index*self.batchSize:(index+1)*self.batchSize]),volatile = True) roots =self.root[index*self.batchSize:(index+1)*self.batchSize] src_sourceBatch = self.src_source[index*self.batchSize:(index+1)*self.batchSize] tgt_sourceBatch = self.tgt_source[index*self.batchSize:(index+1)*self.batchSize] sourceBatch = [ src_s +tgt_s for src_s,tgt_s in zip(src_sourceBatch,tgt_sourceBatch)] # within batch sorting by decreasing length for variable length rnns indices = range(len(srcBatch)) batch = zip(indices, srcBatch ,tgtBatch,alignBatch,rel_seq_pre,rel_index_pre,rel_role_pre,sourceBatch,roots) order_data = sorted(list(enumerate(list(zip(batch, lengths)))),key = lambda x:-x[1][1]) order,data = zip(*order_data) batch, lengths = zip(*data) indices, srcBatch,tgtBatch,alignBatch ,rel_seq_pre,rel_index_pre,rel_role_pre,sourceBatch,roots= zip(*batch) rel_batch,rel_index_batch,rel_lengths = self._batchify_rel_concept(rel_seq_pre,rel_index_pre) rel_roles,length_squares = self._batchify_rel_roles(rel_role_pre) #,wrap(charBatch)) return order,wrap(srcBatch,lengths), wrap(tgtBatch,lengths), wrap_align(alignBatch,lengths),\ MyPackedSequence(rel_batch,rel_lengths),rel_index_batch,MyPackedSequence(rel_roles,length_squares),roots,sourceBatch
def __getitem__(self, index): """ retrieve every batch from the preprocessed dataset """ assert index < self.numBatches, "%d > %d" % (index, self.numBatches) lengths, max_len = self.getLengths(index) def tuple2tensor(t): # generate a not batch first b = torch.stack(t, 0).contiguous() if self.cuda: b = b.cuda() return b def wrap(b, l): #batch, len, feature if b is None: return b b = torch.stack(b, 0).transpose(0, 1).contiguous() if self.cuda: b = b.cuda() # src_len, batch, feature packed = pack(b, list(l)) return packed def wrap_align(b, l): #batch, len_tgt, len_src if b is None: return b b = torch.stack(b, 0).transpose(0, 1).contiguous().float() if self.cuda: b = b.cuda() packed = pack(b, list(l)) #len_tag, batch, len_src return packed idsBatch = self.example_ids[index * self.batchSize:(index + 1) * self.batchSize] # prep a tensor with fixed max_len of this batch # batch_size, max_len, n_feature srcBatch = self._batchify_src( self.src[index * self.batchSize:(index + 1) * self.batchSize], max_len) # batch_size x word_len x char_len src_charBatch = self._batchify_src_char( self.src_char[index * self.batchSize:(index + 1) * self.batchSize], max_len) # batch_size, max_bert_len, no need to get length for bert, all non-zero token are padding srcBertBatch = self._batchify_srcBert( self.srcBert[index * self.batchSize:(index + 1) * self.batchSize], self.opt.max_bert_seq_length) srcBertIndexBatch = self._batchify_srcBertIndex( self.srcBertIndex[index * self.batchSize:(index + 1) * self.batchSize], max_len) if self.source_only: src_sourceBatch = self.src_source[index * self.batchSize:(index + 1) * self.batchSize] # zip with batch id batch = zip(idsBatch, srcBatch, src_charBatch, src_sourceBatch, srcBertBatch, srcBertIndexBatch) # sort by length, ascending order_data = sorted(list(enumerate(list(zip(batch, lengths)))), key=lambda x: -x[1][1]) # order is the sorted index for batch order, data = zip(*order_data) batch, lengths = zip(*data) #keep consistent idsBatch, srcBatch, src_charBatch, src_sourceBatch, srcBertBatch, srcBertBatchIndex = zip( *batch) return order, idsBatch, wrap(srcBatch, lengths), wrap( src_charBatch, lengths), src_sourceBatch, tuple2tensor(srcBertBatch), wrap( srcBertBatchIndex, lengths) else: # batch input data for amr tgtBatch = self._batchify_tgt( self.tgt[index * self.batchSize:(index + 1) * self.batchSize], max_len) # batch input for alignment from align_index alignBatch = self._batchify_align( self.align_index[index * self.batchSize:(index + 1) * self.batchSize], max_len) rel_seq_pre = self.rel_seq[index * self.batchSize:(index + 1) * self.batchSize] rel_index_pre = self.rel_index[index * self.batchSize:(index + 1) * self.batchSize] rel_role_pre = self.rel_mat[index * self.batchSize:(index + 1) * self.batchSize] roots = self.root[index * self.batchSize:(index + 1) * self.batchSize] src_sourceBatch = self.src_source[index * self.batchSize:(index + 1) * self.batchSize] tgt_sourceBatch = self.tgt_source[index * self.batchSize:(index + 1) * self.batchSize] sourceBatch = [ src_s + tgt_s for src_s, tgt_s in zip(src_sourceBatch, tgt_sourceBatch) ] # within batch sorting by decreasing length for variable length rnns indices = range(len(srcBatch)) # align with each data in a batch batch = zip(indices, idsBatch, srcBatch, src_charBatch, tgtBatch, alignBatch, rel_seq_pre, rel_index_pre, rel_role_pre, sourceBatch, roots, srcBertBatch, srcBertIndexBatch) order_data = sorted(list(enumerate(list(zip(batch, lengths)))), key=lambda x: -x[1][1]) order, data = zip(*order_data) batch, lengths = zip(*data) indices, idsBatch, srcBatch, src_charBatch, tgtBatch, alignBatch, rel_seq_pre, rel_index_pre, rel_role_pre, sourceBatch, roots, srcBertBatch, srcBertIndexBatch = zip( *batch) rel_batch, rel_index_batch, rel_lengths = self._batchify_rel_concept( rel_seq_pre, rel_index_pre) rel_roles, length_squares = self._batchify_rel_roles(rel_role_pre) #,wrap(charBatch)) return order, idsBatch, wrap(srcBatch,lengths), wrap(src_charBatch, lengths), wrap(tgtBatch,lengths), wrap_align(alignBatch,lengths),\ MyPackedSequence(rel_batch,rel_lengths),rel_index_batch,MyPackedSequence(rel_roles,length_squares),roots,sourceBatch,tuple2tensor(srcBertBatch),wrap(srcBertIndexBatch, lengths)
def __getitem__(self, index): """ retrieve every batch from the preprocessed dataset """ assert index < self.numBatches, "%d > %d" % (index, self.numBatches) lengths,max_len = self.getLengths(index ) def wrap(b,l ): #batch, len, feature if b is None: return b b = torch.stack(b, 0).transpose(0,1).contiguous() if self.cuda: b = b.cuda() packed = pack(b,list(l)) return packed def wrap_align(b,l ): #batch, len_tgt, len_src if b is None: return b b = torch.stack(b, 0).transpose(0,1).contiguous().float() if self.cuda: b = b.cuda() packed = pack(b,list(l)) return packed idsBatch = self.example_ids[index*self.batchSize:(index+1)*self.batchSize] # prep a tensor with fixed max_len of this batch srcBatch = self._batchify_src( self.src[index*self.batchSize:(index+1)*self.batchSize],max_len) # batch_size x word_len x char_len src_charBatch= self._batchify_src_char( self.src_char[index*self.batchSize: (index+1)*self.batchSize], max_len) if self.source_only: src_sourceBatch = self.src_source[index*self.batchSize:(index+1)*self.batchSize] batch = zip(idsBatch, srcBatch, src_charBatch, src_sourceBatch) order_data = sorted(list(enumerate(list(zip(batch, lengths)))),key = lambda x:-x[1][1]) order,data = zip(*order_data) batch, lengths = zip(*data) idsBatch, srcBatch, src_charBatch, src_sourceBatch = zip(*batch) return order,idsBatch, wrap(srcBatch,lengths), wrap(src_charBatch, lengths), src_sourceBatch else: # batch input data for amr tgtBatch = self._batchify_tgt( self.tgt[index*self.batchSize:(index+1)*self.batchSize],max_len) # batch input for alignment from align_index alignBatch = self._batchify_align( self.align_index[index*self.batchSize:(index+1)*self.batchSize],max_len) rel_seq_pre = self.rel_seq[index*self.batchSize:(index+1)*self.batchSize] rel_index_pre = self.rel_index[index*self.batchSize:(index+1)*self.batchSize] rel_role_pre = self.rel_mat[index*self.batchSize:(index+1)*self.batchSize] roots =self.root[index*self.batchSize:(index+1)*self.batchSize] src_sourceBatch = self.src_source[index*self.batchSize:(index+1)*self.batchSize] tgt_sourceBatch = self.tgt_source[index*self.batchSize:(index+1)*self.batchSize] sourceBatch = [ src_s +tgt_s for src_s,tgt_s in zip(src_sourceBatch,tgt_sourceBatch)] # within batch sorting by decreasing length for variable length rnns indices = range(len(srcBatch)) batch = zip(indices, idsBatch, srcBatch, src_charBatch, tgtBatch,alignBatch,rel_seq_pre,rel_index_pre,rel_role_pre,sourceBatch,roots) order_data = sorted(list(enumerate(list(zip(batch, lengths)))),key = lambda x:-x[1][1]) order,data = zip(*order_data) batch, lengths = zip(*data) indices, idsBatch, srcBatch,src_charBatch, tgtBatch,alignBatch ,rel_seq_pre,rel_index_pre,rel_role_pre,sourceBatch,roots= zip(*batch) rel_batch,rel_index_batch,rel_lengths = self._batchify_rel_concept(rel_seq_pre,rel_index_pre) rel_roles,length_squares = self._batchify_rel_roles(rel_role_pre) return order,idsBatch, wrap(srcBatch,lengths), wrap(src_charBatch, lengths), wrap(tgtBatch,lengths), wrap_align(alignBatch,lengths),\ MyPackedSequence(rel_batch,rel_lengths),rel_index_batch,MyPackedSequence(rel_roles,length_squares),roots,sourceBatch