def _build_model(self, pretrain_model): input_ids = Input(shape=(self.seq_length, )) input_mask = Input(shape=(self.seq_length, )) inputs = [input_ids, input_mask] if self.use_token_type: input_token_type_ids = Input(shape=(self.seq_length, )) inputs.append(input_token_type_ids) self.bert = BertModel( self.config, batch_size=self.batch_size, seq_length=self.seq_length, max_predictions_per_seq=self.max_predictions_per_seq, use_token_type=self.use_token_type, mask=self.mask) self.bert_encoder = self.bert.get_bert_encoder() self.bert_encoder.load_weights(pretrain_model) pooled_output = self.bert_encoder(inputs) pooled_output = Dropout(self.config.hidden_dropout_prob)(pooled_output) pred = Dense(units=self.num_classes, activation='softmax', kernel_initializer=initializers.truncated_normal( stddev=self.config.initializer_range))(pooled_output) model = Model(inputs=inputs, outputs=pred) return model
def convert(): # Initialise PyTorch model config = BertConfig.from_json_file(args.bert_config_file) model = BertModel(config) # Load weights from TF model path = args.tf_checkpoint_path print("Converting TensorFlow checkpoint from {}".format(path)) init_vars = tf.train.list_variables(path) names = [] arrays = [] for name, shape in init_vars: print("Loading {} with shape {}".format(name, shape)) array = tf.train.load_variable(path, name) print("Numpy array shape {}".format(array.shape)) names.append(name) arrays.append(array) for name, array in zip(names, arrays): if not name.startswith("bert"): print("Skipping {}".format(name)) continue else: name = name.replace("bert/", "") # skip "bert/" print("Loading {}".format(name)) name = name.split('/') # adam_v and adam_m are variables used in AdamWeightDecayOptimizer to calculated m and v # which are not required for using pretrained model if name[0] in ['redictions', 'eq_relationship' ] or name[-1] == "adam_v" or name[-1] == "adam_m": print("Skipping {}".format("/".join(name))) continue pointer = model for m_name in name: if re.fullmatch(r'[A-Za-z]+_\d+', m_name): l = re.split(r'_(\d+)', m_name) else: l = [m_name] if l[0] == 'kernel': pointer = getattr(pointer, 'weight') else: pointer = getattr(pointer, l[0]) if len(l) >= 2: num = int(l[1]) pointer = pointer[num] if m_name[-11:] == '_embeddings': pointer = getattr(pointer, 'weight') elif m_name == 'kernel': array = np.transpose(array) try: assert pointer.shape == array.shape except AssertionError as e: e.args += (pointer.shape, array.shape) raise pointer.data = torch.from_numpy(array) # Save pytorch-model torch.save(model.state_dict(), args.pytorch_dump_path)
def convert(): args = parser.parse_args() args.tf_checkpoint_path = "chinese_L-12_H-768_A-12\\bert_model.ckpt" args.bert_config_file = "chinese_L-12_H-768_A-12\\bert_config.json" args.pytorch_dump_path = "chinese_L-12_H-768_A-12\pytorch_model.bin" # Initialise PyTorch model config = BertConfig.from_json_file(args.bert_config_file) model = BertModel(config) # Load weights from TF model path = args.tf_checkpoint_path print("Converting TensorFlow checkpoint from {}".format(path)) init_vars = tf.train.list_variables(path) names = [] arrays = [] for name, shape in init_vars: print("Loading {} with shape {}".format(name, shape)) array = tf.train.load_variable(path, name) print("Numpy array shape {}".format(array.shape)) names.append(name) arrays.append(array) for name, array in zip(names, arrays): name = name[5:] # skip "bert/" print("Loading {}".format(name)) name = name.split('/') if name[0] in ['redictions', 'eq_relationship']: print("Skipping") continue pointer = model for m_name in name: if re.fullmatch(r'[A-Za-z]+_\d+', m_name): l = re.split(r'_(\d+)', m_name) else: l = [m_name] if l[0] == 'kernel': pointer = getattr(pointer, 'weight') else: pointer = getattr(pointer, l[0]) if len(l) >= 2: num = int(l[1]) pointer = pointer[num] if m_name[-11:] == '_embeddings': pointer = getattr(pointer, 'weight') elif m_name == 'kernel': array = np.transpose(array) try: assert pointer.shape == array.shape except AssertionError as e: e.args += (pointer.shape, array.shape) raise pointer.data = torch.from_numpy(array) # Save pytorch-model torch.save(model.state_dict(), args.pytorch_dump_path)
def create_model(bert_config, is_training, input_ids, input_mask, segment_ids, labels, num_labels, use_one_hot_embeddings, dropout_rate=1.0, lstm_size=1, cell='lstm', num_layers=1): """ 创建X模型 :param bert_config: bert 配置 :param is_training: :param input_ids: 数据的idx 表示 :param input_mask: :param segment_ids: :param labels: 标签的idx 表示 :param num_labels: 类别数量 :param use_one_hot_embeddings: :return: """ # 使用数据加载BertModel,获取对应的字embedding model = BertModel(config=bert_config, is_training=is_training, input_ids=input_ids, input_mask=input_mask, token_type_ids=segment_ids, use_one_hot_embeddings=use_one_hot_embeddings) # 获取对应的embedding 输入数据[batch_size, seq_length, embedding_size] embedding = model.get_sequence_output() max_seq_length = embedding.shape[1].value # 算序列真实长度 used = tf.sign(tf.abs(input_ids)) lengths = tf.reduce_sum( used, reduction_indices=1) # [batch_size] 大小的向量,包含了当前batch中的序列长度 # 添加CRF output layer blstm_crf = BLSTM_CRF(embedded_chars=embedding, hidden_unit=lstm_size, cell_type=cell, num_layers=num_layers, dropout_rate=dropout_rate, initializers=initializers, num_labels=num_labels, seq_length=max_seq_length, labels=labels, lengths=lengths, is_training=is_training) # 默认仅仅crf层 rst = blstm_crf.add_blstm_crf_layer(crf_only=True) return rst
def bert_module_fn(is_training): """Spec function for a token embedding module.""" input_ids = tf.placeholder(shape=[None, None], dtype=tf.int32, name="input_ids") input_mask = tf.placeholder(shape=[None, None], dtype=tf.int32, name="input_mask") token_type = tf.placeholder(shape=[None, None], dtype=tf.int32, name="segment_ids") config = BertConfig.from_json_file(config_path) model = BertModel(config=config, is_training=is_training, input_ids=input_ids, input_mask=input_mask, token_type_ids=token_type) model.input_to_output() seq_output = model.get_all_encoder_layers()[-1] config_file = tf.constant(value=config_path, dtype=tf.string, name="config_file") vocab_file = tf.constant(value=vocab_path, dtype=tf.string, name="vocab_file") lower_case = tf.constant(do_lower_case) tf.add_to_collection(tf.GraphKeys.ASSET_FILEPATHS, config_file) tf.add_to_collection(tf.GraphKeys.ASSET_FILEPATHS, vocab_file) input_map = { "input_ids": input_ids, "input_mask": input_mask, "segment_ids": token_type } output_map = {"sequence_output": seq_output} output_info_map = { "vocab_file": vocab_file, "do_lower_case": lower_case } hub.add_signature(name="tokens", inputs=input_map, outputs=output_map) hub.add_signature(name="tokenization_info", inputs={}, outputs=output_info_map)
def __init__(self, config, opt): super(BBFC, self).__init__() embedding_dim = opt.embed_dim # embedding维度 output_dim = opt.output_dim # 输出维度,此处为3,分别代表负面,中性,正面 self.bert = BertModel(config) self.dropout = nn.Dropout(config.hidden_dropout_prob) self.fc = nn.Linear(embedding_dim * 2, output_dim) # 全连接层
def __init__( self, vocab_size, seq_length, hidden_size, hidden_layers, atten_heads, intermediate_size, hidden_act, hidden_dropout_prob, attention_probs_dropout_prob, max_position_embeddings, type_vocab_size, initializer_range=0.02, ): super().__init__() self.bert = BertModel( vocab_size, seq_length, hidden_size, hidden_layers, atten_heads, intermediate_size, hidden_act, hidden_dropout_prob, attention_probs_dropout_prob, max_position_embeddings, type_vocab_size, ) self.seq_length = seq_length self.hidden_size = hidden_size self.cls_squad = nn.Linear(hidden_size, 2) self.cls_squad.weight.data.normal_(mean=0.0, std=initializer_range) self.cls_squad.bias.data.fill_(0)
def load_stock_model(model_dir, max_seq_len): from modeling import BertModel, BertConfig, get_assignment_map_from_checkpoint tf.compat.v1.reset_default_graph( ) # to scope naming for checkpoint loading (if executed more than once) bert_config_file = os.path.join(model_dir, "bert_config.json") bert_ckpt_file = os.path.join(model_dir, "bert_model.ckpt") pl_input_ids = tf.compat.v1.placeholder(tf.int32, shape=(1, max_seq_len)) pl_mask = tf.compat.v1.placeholder(tf.int32, shape=(1, max_seq_len)) pl_token_type_ids = tf.compat.v1.placeholder(tf.int32, shape=(1, max_seq_len)) bert_config = BertConfig.from_json_file(bert_config_file) s_model = BertModel(config=bert_config, is_training=False, input_ids=pl_input_ids, input_mask=pl_mask, token_type_ids=pl_token_type_ids, use_one_hot_embeddings=False) tvars = tf.compat.v1.trainable_variables() (assignment_map, initialized_var_names) = get_assignment_map_from_checkpoint( tvars, bert_ckpt_file) tf.compat.v1.train.init_from_checkpoint(bert_ckpt_file, assignment_map) return s_model, pl_input_ids, pl_token_type_ids, pl_mask
def __init__(self, config, opt): super(MemNet, self).__init__() self.opt = opt self.bert = BertModel(config) self.squeeze_embedding = SqueezeEmbedding(batch_first=True) self.attention = Attention(opt.embed_dim, score_function='mlp') self.x_linear = nn.Linear(opt.embed_dim, opt.embed_dim) self.dense = nn.Linear(opt.embed_dim, opt.output_dim)
def __init__(self, config, opt): super(BERT_IAN, self).__init__() self.opt = opt self.bert = BertModel(config) self.attention_aspect = Attention(opt.hidden_dim, score_function='bi_linear') self.attention_context = Attention(opt.hidden_dim, score_function='bi_linear') self.dense = nn.Linear(opt.hidden_dim * 2, opt.output_dim)
def __init__(self, config, opt): super(IAN, self).__init__() self.opt = opt self.bert = BertModel(config) self.lstm_context = DynamicLSTM(opt.embed_dim, opt.hidden_dim, num_layers=1, batch_first=True) self.lstm_aspect = DynamicLSTM(opt.embed_dim, opt.hidden_dim, num_layers=1, batch_first=True) self.attention_aspect = Attention(opt.hidden_dim, score_function='bi_linear') self.attention_context = Attention(opt.hidden_dim, score_function='bi_linear') self.dense = nn.Linear(opt.hidden_dim*2, opt.output_dim)
def __init__(self, config, num_tag): super(Net, self).__init__(config) self.bert = BertModel(config) # for p in self.bert.parameters(): # p.requires_grad = False self.dropout = nn.Dropout(config.hidden_dropout_prob) self.classifier = nn.Linear(config.hidden_size, num_tag) self.apply(self.init_bert_weights) self.crf = CRF(num_tag)
def __init__(self, config, opt): super(TD_BERT, self).__init__() self.opt = opt n_filters = opt.n_filters # 卷积核个数 filter_sizes = opt.filter_sizes # 卷积核尺寸,多个尺寸则传递过来的是一个列表 embedding_dim = opt.embed_dim # embedding维度 output_dim = opt.output_dim # 输出维度,此处为3,分别代表负面,中性,正面 self.bert = BertModel(config) self.dropout = nn.Dropout(opt.keep_dropout) self.fc = nn.Linear(embedding_dim, output_dim) # 全连接层 bbfc
def push_test(pickle_name, tok_model_file, tok_vocab_file, bert_model_file, config_file): dataset = _load_data_using_dataset_api(pickle_name) converter = Converter() data = [dataset.get_example(0)] data_conv = converter(data, 0) bert_config = modeling.BertConfig.from_json_file(config_file) bert = BertModel(config=bert_config) bert_pre = BertPretrainer(bert) bert_pre.to_gpu() serializers.load_npz(bert_model_file, bert_pre)
def __init__(self, config, opt): super(TC_CNN, self).__init__() n_filters = opt.n_filters # 卷积核个数 filter_sizes = opt.filter_sizes # 卷积核尺寸,多个尺寸则传递过来的是一个列表 embedding_dim = opt.embed_dim # embedding维度 output_dim = opt.output_dim # 输出维度,此处为3,分别代表负面,中性,正面 self.bert = BertModel(config) self.convs = nn.ModuleList( [nn.Conv2d(in_channels=1, out_channels=n_filters, kernel_size=(fs, embedding_dim*2)) for fs in filter_sizes]) self.dropout = nn.Dropout(opt.keep_dropout) self.fc = nn.Linear(len(filter_sizes) * n_filters, output_dim) # 全连接层
def __init__(self, config, opt): super(ATAE_LSTM, self).__init__() self.opt = opt # self.embed = nn.Embedding.from_pretrained(torch.tensor(embedding_matrix, dtype=torch.float)) self.bert = BertModel(config) self.squeeze_embedding = SqueezeEmbedding() self.lstm = DynamicLSTM(opt.embed_dim * 2, opt.hidden_dim, num_layers=1, batch_first=True) self.attention = NoQueryAttention(opt.hidden_dim + opt.embed_dim, score_function='bi_linear') self.dense = nn.Linear(opt.hidden_dim, opt.output_dim)
def __init__(self, config, opt): super(MLP, self).__init__() self.opt = opt n_filters = opt.n_filters # 卷积核个数 filter_sizes = opt.filter_sizes # 卷积核尺寸,多个尺寸则传递过来的是一个列表 embedding_dim = opt.embed_dim # embedding维度 output_dim = opt.output_dim # 输出维度,此处为3,分别代表负面,中性,正面 self.bert = BertModel(config) self.dropout = nn.Dropout(opt.keep_dropout) self.fc1 = nn.Linear(embedding_dim*2, embedding_dim) self.fc2 = nn.Linear(embedding_dim, embedding_dim) self.fc3 = nn.Linear(embedding_dim, int(embedding_dim/2)) self.fc4 = nn.Linear(int(embedding_dim/2), output_dim) self.bn1 = nn.BatchNorm1d(embedding_dim)
def main(tok_model_file, tok_vocab_file, bert_model_file, config_file, text): # push_test(tok_model_file) bert_config = modeling.BertConfig.from_json_file(config_file) tokenizer = FullTokenizer(model_file=tok_model_file, vocab_file=tok_vocab_file, do_lower_case=False) bert = BertModel(config=bert_config) bert_pre = BertPretrainer(bert) interp = BertInterpolater(bert_pre, tokenizer) interp.to_gpu() serializers.load_npz(bert_model_file, bert_pre) interp.push_text(text)
def __init__(self, config, opt): super(RAM, self).__init__() self.opt = opt # self.embed = nn.Embedding.from_pretrained(torch.tensor(embedding_matrix, dtype=torch.float)) self.bert = BertModel(config) self.bi_lstm_context = DynamicLSTM(opt.embed_dim, opt.hidden_dim, num_layers=1, batch_first=True, bidirectional=True) self.att_linear = nn.Linear(opt.hidden_dim * 2 + 1 + opt.embed_dim * 2, 1) self.gru_cell = nn.GRUCell(opt.hidden_dim * 2 + 1, opt.embed_dim) self.dense = nn.Linear(opt.embed_dim, opt.output_dim)
def __init__(self, config, opt): super(PF_CNN, self).__init__() self.n_filters = opt.n_filters # 卷积核个数 filter_sizes = opt.filter_sizes # 卷积核尺寸,多个尺寸则传递过来的是一个列表 embedding_dim = opt.embed_dim # embedding维度 output_dim = opt.output_dim # 输出维度,此处为3,分别代表负面,中性,正面 dropout = 0 # self.embedding = nn.Embedding.from_pretrained(torch.tensor(embedding_matrix, dtype=torch.float)) # self.embedding.weight.requires_grad = True self.bert = BertModel(config) self.convs = nn.ModuleList([ nn.Conv2d(in_channels=1, out_channels=self.n_filters, kernel_size=(fs, embedding_dim)) for fs in filter_sizes ]) self.convs_target_1 = nn.ModuleList([ nn.Conv2d(in_channels=1, out_channels=int(filter_sizes[0] * self.n_filters * embedding_dim / len(filter_sizes)), kernel_size=(fs, embedding_dim)) for fs in filter_sizes ]) self.convs_target_2 = nn.ModuleList([ nn.Conv2d(in_channels=1, out_channels=int(filter_sizes[1] * self.n_filters * embedding_dim / len(filter_sizes)), kernel_size=(fs, embedding_dim)) for fs in filter_sizes ]) self.convs_target_3 = nn.ModuleList([ nn.Conv2d(in_channels=1, out_channels=int(filter_sizes[2] * self.n_filters * embedding_dim / len(filter_sizes)), kernel_size=(fs, embedding_dim)) for fs in filter_sizes ]) self.convs_target_4 = nn.ModuleList([ nn.Conv2d(in_channels=1, out_channels=int(filter_sizes[3] * self.n_filters * embedding_dim / len(filter_sizes)), kernel_size=(fs, embedding_dim)) for fs in filter_sizes ]) self.convs_t = nn.ModuleList([ nn.Conv2d(in_channels=1, out_channels=self.n_filters, kernel_size=(fs, embedding_dim)) for fs in filter_sizes ]) for i in range(len(filter_sizes)): self.convs_t._modules['0'].weight.requires_grad = False self.fc = nn.Linear( len(filter_sizes) * self.n_filters * 2, output_dim) # 全连接层 self.dropout = nn.Dropout(dropout)
def __init__(self): self.input_ids = tf.placeholder(tf.int32, [None, None]) self.input_mask = tf.placeholder(tf.int32, [None, None]) self.model = BertModel(config=BertConfig.from_json_file(bert_config), is_training=True, input_ids=self.input_ids, input_mask=self.input_mask) self.is_training = tf.placeholder(tf.bool, []) self.predictions = self.construct_model(self.model) self.id_predictions = tf.argmax(self.predictions, axis=2) self.Y = tf.placeholder(tf.float32, [None, None, len(entity_types)]) self.tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case=False) self.model_path = "/scratch/sanjay/bert/bert_ner_model.ckpt" self.saver = tf.train.Saver()
def __init__(self, config, opt): super(TCN, self).__init__() self.opt = opt self.num_input = opt.embed_dim self.num_channels = [256, 128, 64, 32, 32, 32] # self.kernel_size = [2, 3, 4] self.bert = BertModel(config) # linear self.hidden2label1 = nn.Linear(opt.max_seq_length, opt.max_seq_length // 2) # 全连接,200 -> 100, 两个斜杠表示去尾整除 self.hidden2label2 = nn.Linear(opt.max_seq_length // 2, opt.label_size) # 全连接, 100 -> len(label) self.net = TemporalConvNet(self.num_input, self.num_channels) # 输入维度,输出维度 self.dropout = nn.Dropout(opt.keep_dropout) self.bn1 = nn.BatchNorm1d(opt.max_seq_length//2) # 64 self.bn2 = nn.BatchNorm1d(opt.label_size)
def __init__(self, model_dir, top_rnns=False, vocab_size=None, entity_num=0, device='cpu', finetuning=False): super(Net, self).__init__() # print(entity_num) self.bert = BertModel.from_pretrained(model_dir, entity_num=entity_num) self.hidden_dim = self.bert.config.hidden_size # print(self.hidden_dim) self.top_rnns=top_rnns if top_rnns: self.rnn = nn.LSTM(bidirectional=True, num_layers=2, input_size=self.hidden_dim, hidden_size=self.hidden_dim//2, batch_first=True) self.fc = nn.Linear(self.hidden_dim, vocab_size) self.device = device self.finetuning = finetuning self._param_init(self.fc)
def __init__(self, config, opt): super(CNN, self).__init__() n_filters = opt.n_filters # 卷积核个数 filter_sizes = opt.filter_sizes # 卷积核尺寸,多个尺寸则传递过来的是一个列表 embedding_dim = opt.embed_dim # embedding维度 output_dim = opt.output_dim # 输出维度,此处为3,分别代表负面,中性,正面 dropout = opt.dropout # self.embedding = nn.Embedding.from_pretrained(torch.tensor(embedding_matrix, dtype=torch.float)) self.bert = BertModel(config) self.convs = nn.ModuleList([ nn.Conv2d(in_channels=1, out_channels=n_filters, kernel_size=(fs, embedding_dim)) for fs in filter_sizes ]) self.fc = nn.Linear(len(filter_sizes) * n_filters, output_dim) # 全连接层 self.dropout = nn.Dropout(dropout)
def __init__(self, config, opt): super(AEN_BERT, self).__init__() self.opt = opt self.squeeze_embedding = SqueezeEmbedding() self.dropout = nn.Dropout(opt.dropout) self.bert = BertModel(config) self.attn_k = Attention(opt.embed_dim, out_dim=opt.hidden_dim, n_head=8, score_function='mlp', dropout=opt.dropout) self.attn_q = Attention(opt.embed_dim, out_dim=opt.hidden_dim, n_head=8, score_function='mlp', dropout=opt.dropout) self.ffn_c = PositionwiseFeedForward(opt.hidden_dim, dropout=opt.dropout) self.ffn_t = PositionwiseFeedForward(opt.hidden_dim, dropout=opt.dropout) self.attn_s1 = Attention(opt.hidden_dim, n_head=8, score_function='mlp', dropout=opt.dropout) self.dense = nn.Linear(opt.hidden_dim * 3, opt.output_dim)
def __init__(self, config, opt): super(AOA, self).__init__() self.opt = opt # self.embed = nn.Embedding.from_pretrained(torch.tensor(embedding_matrix, dtype=torch.float)) self.bert = BertModel(config) self.ctx_lstm = DynamicLSTM(opt.embed_dim, opt.hidden_dim, num_layers=1, batch_first=True, bidirectional=True) self.asp_lstm = DynamicLSTM(opt.embed_dim, opt.hidden_dim, num_layers=1, batch_first=True, bidirectional=True) self.dense = nn.Linear(2 * opt.hidden_dim, opt.output_dim)
def __init__(self, config, opt): super(Bert_PF, self).__init__() self.n_filters = opt.n_filters # 卷积核个数 filter_sizes = opt.filter_sizes # 卷积核尺寸,多个尺寸则传递过来的是一个列表 embedding_dim = opt.embed_dim # embedding维度 output_dim = opt.output_dim # 输出维度,此处为3,分别代表负面,中性,正面 dropout = 0.5 self.bert = BertModel(config) self.convs_target_1 = nn.ModuleList([ nn.Conv2d(in_channels=1, out_channels=int(filter_sizes[0] * self.n_filters * embedding_dim / len(filter_sizes)), kernel_size=(fs, embedding_dim)) for fs in filter_sizes ]) self.convs_target_2 = nn.ModuleList([ nn.Conv2d(in_channels=1, out_channels=int(filter_sizes[1] * self.n_filters * embedding_dim / len(filter_sizes)), kernel_size=(fs, embedding_dim)) for fs in filter_sizes ]) self.convs_target_3 = nn.ModuleList([ nn.Conv2d(in_channels=1, out_channels=int(filter_sizes[2] * self.n_filters * embedding_dim / len(filter_sizes)), kernel_size=(fs, embedding_dim)) for fs in filter_sizes ]) self.convs_target_4 = nn.ModuleList([ nn.Conv2d(in_channels=1, out_channels=int(filter_sizes[3] * self.n_filters * embedding_dim / len(filter_sizes)), kernel_size=(fs, embedding_dim)) for fs in filter_sizes ]) self.convs_t = nn.ModuleList([ nn.Conv2d(in_channels=1, out_channels=self.n_filters, kernel_size=(fs, embedding_dim)) for fs in filter_sizes ]) for i in range(len(filter_sizes)): self.convs_t._modules['0'].weight.requires_grad = False # self.fc = nn.Linear(len(filter_sizes) * self.n_filters * 2, output_dim) # 全连接层 self.fc = nn.Linear( len(filter_sizes) * self.n_filters + embedding_dim, output_dim) # 全连接层 self.dropout = nn.Dropout(dropout)
def create_stock_bert_graph(bert_config_file, max_seq_len): from modeling import BertModel, BertConfig tf_placeholder = tf.compat.v1.placeholder pl_input_ids = tf_placeholder(tf.int32, shape=(1, max_seq_len)) pl_mask = tf_placeholder(tf.int32, shape=(1, max_seq_len)) pl_token_type_ids = tf_placeholder(tf.int32, shape=(1, max_seq_len)) bert_config = BertConfig.from_json_file(bert_config_file) s_model = BertModel(config=bert_config, is_training=False, input_ids=pl_input_ids, input_mask=pl_mask, token_type_ids=pl_token_type_ids, use_one_hot_embeddings=False) return s_model, pl_input_ids, pl_mask, pl_token_type_ids
def __init__(self, config, opt): super(MGAN, self).__init__() self.opt = opt self.bert = BertModel(config) self.ctx_lstm = DynamicLSTM(opt.embed_dim, opt.hidden_dim, num_layers=1, batch_first=True, bidirectional=True) self.asp_lstm = DynamicLSTM(opt.embed_dim, opt.hidden_dim, num_layers=1, batch_first=True, bidirectional=True) self.location = LocationEncoding(opt) self.w_a2c = nn.Parameter( torch.Tensor(2 * opt.hidden_dim, 2 * opt.hidden_dim)) self.w_c2a = nn.Parameter( torch.Tensor(2 * opt.hidden_dim, 2 * opt.hidden_dim)) self.alignment = AlignmentMatrix(opt) self.dense = nn.Linear(8 * opt.hidden_dim, opt.output_dim)
def __init__(self, config, opt): super(TNet_LF, self).__init__() print("this is TNet_LF model") self.bert = BertModel(config) self.position = Absolute_Position_Embedding(opt) self.opt = opt D = opt.embed_dim # 模型词向量维度 C = opt.output_dim # 分类数目 L = opt.max_seq_length HD = opt.hidden_dim self.lstm1 = DynamicLSTM(opt.embed_dim, opt.hidden_dim, num_layers=1, batch_first=True, bidirectional=True) self.lstm2 = DynamicLSTM(opt.embed_dim, opt.hidden_dim, num_layers=1, batch_first=True, bidirectional=True) self.convs3 = nn.Conv1d(2 * HD, 50, 3, padding=1) self.fc1 = nn.Linear(4 * HD, 2 * HD) self.fc = nn.Linear(50, C)