def __init__(self, config): super(LSRPIPELINE, self).__init__() self.config = config self.finetune_emb = config.finetune_emb self.word_emb = nn.Embedding(config.data_word_vec.shape[0], config.data_word_vec.shape[1]) self.word_emb.weight.data.copy_(torch.from_numpy(config.data_word_vec)) if not self.finetune_emb: self.word_emb.weight.requires_grad = False self.ner_emb = nn.Embedding(13, config.entity_type_size, padding_idx=0) self.coref_embed = nn.Embedding(config.max_length, config.coref_size, padding_idx=0) hidden_size = config.rnn_hidden input_size = config.data_word_vec.shape[1] + config.coref_size + config.entity_type_size #+ char_hidden self.linear_re = nn.Linear(hidden_size * 2, hidden_size) self.linear_sent = nn.Linear(hidden_size * 2, hidden_size) self.bili = torch.nn.Bilinear(hidden_size, hidden_size, hidden_size) self.self_att = SelfAttention(hidden_size) self.bili = torch.nn.Bilinear(hidden_size+config.dis_size, hidden_size+config.dis_size, hidden_size) self.dis_embed = nn.Embedding(20, config.dis_size, padding_idx=10) self.linear_output = nn.Linear(2 * hidden_size, config.relation_num) self.relu = nn.ReLU() self.dropout_rate = nn.Dropout(config.dropout_rate) self.rnn_sent = Encoder(input_size, hidden_size, config.dropout_emb, config.dropout_rate) self.hidden_size = hidden_size self.use_struct_att = config.use_struct_att if self.use_struct_att == True: self.structInduction = StructInduction(hidden_size // 2, hidden_size, True) self.dropout_gcn = nn.Dropout(config.dropout_gcn) self.reasoner_layer_first = config.reasoner_layer_first self.reasoner_layer_second = config.reasoner_layer_second self.use_reasoning_block = config.use_reasoning_block if self.use_reasoning_block: self.reasoner = nn.ModuleList() self.reasoner.append(DynamicReasoner(hidden_size, self.reasoner_layer_first, self.dropout_gcn)) self.reasoner.append(DynamicReasoner(hidden_size, self.reasoner_layer_second, self.dropout_gcn))
def __init__(self, config): super(LSR, self).__init__() self.config = config self.bert = BertModel.from_pretrained('bert-base-uncased') print("loaded bert-base-uncased") hidden_size = config.rnn_hidden bert_hidden_size = 768 self.linear_re = nn.Linear(bert_hidden_size, hidden_size) self.linear_sent = nn.Linear(hidden_size * 2, hidden_size) self.bili = torch.nn.Bilinear(hidden_size, hidden_size, hidden_size) self.self_att = SelfAttention(hidden_size) self.bili = torch.nn.Bilinear(hidden_size+config.dis_size, hidden_size+config.dis_size, hidden_size) self.dis_embed = nn.Embedding(20, config.dis_size, padding_idx=10) self.linear_output = nn.Linear(2 * hidden_size, config.relation_num) self.relu = nn.ReLU() self.dropout_rate = nn.Dropout(config.dropout_rate) #self.rnn_sent = Encoder(input_size, hidden_size, config.dropout_emb, config.dropout_rate) self.hidden_size = hidden_size self.use_struct_att = config.use_struct_att if self.use_struct_att == True: self.structInduction = StructInduction(hidden_size // 2, hidden_size, True) self.dropout_gcn = nn.Dropout(config.dropout_gcn) self.reasoner_layer_first = config.reasoner_layer_first self.reasoner_layer_second = config.reasoner_layer_second self.use_reasoning_block = config.use_reasoning_block if self.use_reasoning_block: self.reasoner = nn.ModuleList() self.reasoner.append(DynamicReasoner(hidden_size, self.reasoner_layer_first, self.dropout_gcn)) self.reasoner.append(DynamicReasoner(hidden_size, self.reasoner_layer_second, self.dropout_gcn))