Python transformers.BertConfig() Examples
The following are 13
code examples of transformers.BertConfig().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
transformers
, or try the search function
.
Example #1
Source File: bert_tf_to_pytorch.py From inference with Apache License 2.0 | 6 votes |
def main(): with open("build/data/bert_tf_v1_1_large_fp32_384_v2/bert_config.json") as f: config_json = json.load(f) config = BertConfig( attention_probs_dropout_prob=config_json["attention_probs_dropout_prob"], hidden_act=config_json["hidden_act"], hidden_dropout_prob=config_json["hidden_dropout_prob"], hidden_size=config_json["hidden_size"], initializer_range=config_json["initializer_range"], intermediate_size=config_json["intermediate_size"], max_position_embeddings=config_json["max_position_embeddings"], num_attention_heads=config_json["num_attention_heads"], num_hidden_layers=config_json["num_hidden_layers"], type_vocab_size=config_json["type_vocab_size"], vocab_size=config_json["vocab_size"]) model = load_from_tf(config, "build/data/bert_tf_v1_1_large_fp32_384_v2/model.ckpt-5474") torch.save(model.state_dict(), "build/data/bert_tf_v1_1_large_fp32_384_v2/model.pytorch") save_to_onnx(model)
Example #2
Source File: pytorch_SUT.py From inference with Apache License 2.0 | 5 votes |
def __init__(self): print("Loading BERT configs...") with open("bert_config.json") as f: config_json = json.load(f) config = BertConfig( attention_probs_dropout_prob=config_json["attention_probs_dropout_prob"], hidden_act=config_json["hidden_act"], hidden_dropout_prob=config_json["hidden_dropout_prob"], hidden_size=config_json["hidden_size"], initializer_range=config_json["initializer_range"], intermediate_size=config_json["intermediate_size"], max_position_embeddings=config_json["max_position_embeddings"], num_attention_heads=config_json["num_attention_heads"], num_hidden_layers=config_json["num_hidden_layers"], type_vocab_size=config_json["type_vocab_size"], vocab_size=config_json["vocab_size"]) print("Loading PyTorch model...") self.model = BertForQuestionAnswering(config) self.model.eval() self.model.cuda() self.model.load_state_dict(torch.load("build/data/bert_tf_v1_1_large_fp32_384_v2/model.pytorch")) print("Constructing SUT...") self.sut = lg.ConstructSUT(self.issue_queries, self.flush_queries, self.process_latencies) print("Finished constructing SUT.") self.qsl = get_squad_QSL()
Example #3
Source File: test_modeling_tf_bert.py From exbert with Apache License 2.0 | 5 votes |
def prepare_config_and_inputs(self): input_ids = ids_tensor([self.batch_size, self.seq_length], self.vocab_size) input_mask = None if self.use_input_mask: input_mask = ids_tensor([self.batch_size, self.seq_length], vocab_size=2) token_type_ids = None if self.use_token_type_ids: token_type_ids = ids_tensor([self.batch_size, self.seq_length], self.type_vocab_size) sequence_labels = None token_labels = None choice_labels = None if self.use_labels: sequence_labels = ids_tensor([self.batch_size], self.type_sequence_label_size) token_labels = ids_tensor([self.batch_size, self.seq_length], self.num_labels) choice_labels = ids_tensor([self.batch_size], self.num_choices) config = BertConfig( vocab_size=self.vocab_size, hidden_size=self.hidden_size, num_hidden_layers=self.num_hidden_layers, num_attention_heads=self.num_attention_heads, intermediate_size=self.intermediate_size, hidden_act=self.hidden_act, hidden_dropout_prob=self.hidden_dropout_prob, attention_probs_dropout_prob=self.attention_probs_dropout_prob, max_position_embeddings=self.max_position_embeddings, type_vocab_size=self.type_vocab_size, initializer_range=self.initializer_range, ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
Example #4
Source File: test_modeling_tf_bert.py From exbert with Apache License 2.0 | 5 votes |
def setUp(self): self.model_tester = TFBertModelTest.TFBertModelTester(self) self.config_tester = ConfigTester(self, config_class=BertConfig, hidden_size=37)
Example #5
Source File: test_transformers.py From keras-onnx with MIT License | 5 votes |
def test_TFBertModel(self): from transformers import BertConfig, TFBertModel keras.backend.clear_session() # pretrained_weights = 'bert-base-uncased' tokenizer_file = 'bert_bert-base-uncased.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = BertConfig() model = TFBertModel(config) predictions = model.predict(inputs) onnx_model = keras2onnx.convert_keras(model, model.name) self.assertTrue( run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files, rtol=1.e-2, atol=1.e-4))
Example #6
Source File: test_transformers.py From keras-onnx with MIT License | 5 votes |
def test_TFBertForPreTraining(self): from transformers import BertConfig, TFBertForPreTraining keras.backend.clear_session() # pretrained_weights = 'bert-base-uncased' tokenizer_file = 'bert_bert-base-uncased.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = BertConfig() model = TFBertForPreTraining(config) predictions = model.predict(inputs) onnx_model = keras2onnx.convert_keras(model, model.name) self.assertTrue( run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files, rtol=1.e-2, atol=1.e-4))
Example #7
Source File: test_transformers.py From keras-onnx with MIT License | 5 votes |
def test_TFBertForMaskedLM(self): from transformers import BertConfig, TFBertForMaskedLM keras.backend.clear_session() # pretrained_weights = 'bert-base-uncased' tokenizer_file = 'bert_bert-base-uncased.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = BertConfig() model = TFBertForMaskedLM(config) predictions = model.predict(inputs) onnx_model = keras2onnx.convert_keras(model, model.name) self.assertTrue( run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files, rtol=1.e-2, atol=1.e-4))
Example #8
Source File: test_transformers.py From keras-onnx with MIT License | 5 votes |
def test_TFBertForNextSentencePrediction(self): from transformers import BertConfig, TFBertForNextSentencePrediction keras.backend.clear_session() # pretrained_weights = 'bert-base-uncased' tokenizer_file = 'bert_bert-base-uncased.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = BertConfig() model = TFBertForNextSentencePrediction(config) predictions = model.predict(inputs) onnx_model = keras2onnx.convert_keras(model, model.name) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files))
Example #9
Source File: test_transformers.py From keras-onnx with MIT License | 5 votes |
def test_TFBertForSequenceClassification(self): from transformers import BertConfig, TFBertForSequenceClassification keras.backend.clear_session() # pretrained_weights = 'bert-base-uncased' tokenizer_file = 'bert_bert-base-uncased.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = BertConfig() model = TFBertForSequenceClassification(config) predictions = model.predict(inputs) onnx_model = keras2onnx.convert_keras(model, model.name) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files))
Example #10
Source File: test_transformers.py From keras-onnx with MIT License | 5 votes |
def test_TFBertForQuestionAnswering(self): from transformers import BertConfig, TFBertForQuestionAnswering keras.backend.clear_session() # pretrained_weights = 'bert-base-uncased' tokenizer_file = 'bert_bert-base-uncased.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = BertConfig() model = TFBertForQuestionAnswering(config) predictions = model.predict(inputs) onnx_model = keras2onnx.convert_keras(model, model.name) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files))
Example #11
Source File: BertCapModel.py From self-critical.pytorch with MIT License | 5 votes |
def make_model(self, src_vocab, tgt_vocab, N_enc=6, N_dec=6, d_model=512, d_ff=2048, h=8, dropout=0.1): "Helper: Construct a model from hyperparameters." enc_config = BertConfig(vocab_size=1, hidden_size=d_model, num_hidden_layers=N_enc, num_attention_heads=h, intermediate_size=d_ff, hidden_dropout_prob=dropout, attention_probs_dropout_prob=dropout, max_position_embeddings=1, type_vocab_size=1) dec_config = BertConfig(vocab_size=tgt_vocab, hidden_size=d_model, num_hidden_layers=N_dec, num_attention_heads=h, intermediate_size=d_ff, hidden_dropout_prob=dropout, attention_probs_dropout_prob=dropout, max_position_embeddings=17, type_vocab_size=1, is_decoder=True) encoder = BertModel(enc_config) def return_embeds(*args, **kwargs): return kwargs['inputs_embeds'] del encoder.embeddings; encoder.embeddings = return_embeds decoder = BertModel(dec_config) model = EncoderDecoder( encoder, decoder, Generator(d_model, tgt_vocab)) return model
Example #12
Source File: BertCapModel.py From ImageCaptioning.pytorch with MIT License | 5 votes |
def make_model(self, src_vocab, tgt_vocab, N_enc=6, N_dec=6, d_model=512, d_ff=2048, h=8, dropout=0.1): "Helper: Construct a model from hyperparameters." enc_config = BertConfig(vocab_size=1, hidden_size=d_model, num_hidden_layers=N_enc, num_attention_heads=h, intermediate_size=d_ff, hidden_dropout_prob=dropout, attention_probs_dropout_prob=dropout, max_position_embeddings=1, type_vocab_size=1) dec_config = BertConfig(vocab_size=tgt_vocab, hidden_size=d_model, num_hidden_layers=N_dec, num_attention_heads=h, intermediate_size=d_ff, hidden_dropout_prob=dropout, attention_probs_dropout_prob=dropout, max_position_embeddings=17, type_vocab_size=1, is_decoder=True) encoder = BertModel(enc_config) def return_embeds(*args, **kwargs): return kwargs['inputs_embeds'] del encoder.embeddings; encoder.embeddings = return_embeds decoder = BertModel(dec_config) model = EncoderDecoder( encoder, decoder, Generator(d_model, tgt_vocab)) return model
Example #13
Source File: model_builder.py From nlp-recipes with MIT License | 5 votes |
def __init__(self, encoder, args, model_class, pretrained_model_name, max_pos=512, pretrained_config = None, temp_dir="./"): super(BertSumExt, self).__init__() self.loss = torch.nn.BCELoss(reduction='none') #self.device = device self.transformer = Transformer(temp_dir, model_class, pretrained_model_name, pretrained_config) if (encoder == 'classifier'): self.encoder = Classifier(self.transformer.model.config.hidden_size) elif(encoder=='transformer'): self.encoder = ExtTransformerEncoder(self.transformer.model.config.hidden_size, args.ff_size, args.heads, args.dropout, args.inter_layers) elif(encoder=='rnn'): self.encoder = RNNEncoder(bidirectional=True, num_layers=1, input_size=self.transformer.model.config.hidden_size, hidden_size=args.rnn_size, dropout=args.dropout) elif (encoder == 'baseline'): bert_config = BertConfig(self.transformer.model.config.vocab_size, hidden_size=args.hidden_size, num_hidden_layers=6, num_attention_heads=8, intermediate_size=args.ff_size) self.transformer.model = BertModel(bert_config) self.encoder = Classifier(self.transformer.model.config.hidden_size) self.max_pos = max_pos if(max_pos > 512): my_pos_embeddings = nn.Embedding(self.max_pos, self.transformer.model.config.hidden_size) my_pos_embeddings.weight.data[:512] = self.transformer.model.embeddings.position_embeddings.weight.data my_pos_embeddings.weight.data[512:] = self.transformer.model.embeddings.position_embeddings.weight.data[-1][None,:].repeat(self.max_pos-512,1) self.transformer.model.embeddings.position_embeddings = my_pos_embeddings if args.param_init != 0.0: for p in self.encoder.parameters(): p.data.uniform_(-args.param_init, args.param_init) if args.param_init_glorot: for p in self.encoder.parameters(): if p.dim() > 1: xavier_uniform_(p) #self.to(device)