2 回答
TA贡献1828条经验 获得超3个赞
我报告了我的示例,其中我尝试预测 3 个文本样本并获得 (3, 42) 作为输出形状
### define model
config = BertConfig.from_pretrained(
'bert-base-multilingual-cased',
num_labels=42,
output_hidden_states=False,
output_attentions=False
)
model = TFBertForSequenceClassification.from_pretrained('bert-base-multilingual-cased', config=config)
optimizer = tf.keras.optimizers.Adam(learning_rate=3e-05, epsilon=1e-08)
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
metric = tf.keras.metrics.SparseCategoricalAccuracy(name='accuracy')
model.compile(optimizer=optimizer,
loss=loss,
metrics=[metric])
### import tokenizer
tokenizer = AutoTokenizer.from_pretrained("bert-base-multilingual-cased")
### utility functions for text encoding
def return_id(str1, str2, length):
inputs = tokenizer.encode_plus(str1, str2,
add_special_tokens=True,
max_length=length)
input_ids = inputs["input_ids"]
input_masks = [1] * len(input_ids)
input_segments = inputs["token_type_ids"]
padding_length = length - len(input_ids)
padding_id = tokenizer.pad_token_id
input_ids = input_ids + ([padding_id] * padding_length)
input_masks = input_masks + ([0] * padding_length)
input_segments = input_segments + ([0] * padding_length)
return [input_ids, input_masks, input_segments]
### encode 3 sentences
input_ids, input_masks, input_segments = [], [], []
for instance in ['hello hello', 'ciao ciao', 'marco marco']:
ids, masks, segments = \
return_id(instance, None, 100)
input_ids.append(ids)
input_masks.append(masks)
input_segments.append(segments)
input_ = [np.asarray(input_ids, dtype=np.int32),
np.asarray(input_masks, dtype=np.int32),
np.asarray(input_segments, dtype=np.int32)]
### make prediction
model.predict(input_).shape # ===> (3,42)
添加回答
举报