【发布时间】:2020-07-23 01:16:19
【问题描述】:
我正在使用 HuggingFace 的转换器库、Keras 和 BERT 构建多类文本分类模型。
为了将我的输入转换为所需的 bert 格式,我使用了 BertTokenizer 类 found here 中的 encode_plus 方法
数据是每个特征的一段句子,并且有一个标签(总共 45 个标签)
转换输入的代码是:
def create_input_array(df, tokenizer):
sentences = df.text.values
labels = df.label.values
input_ids = []
attention_masks = []
token_type_ids = []
# For every sentence...
for sent in sentences:
# `encode_plus` will:
# (1) Tokenize the sentence.
# (2) Prepend the `[CLS]` token to the start.
# (3) Append the `[SEP]` token to the end.
# (4) Map tokens to their IDs.
# (5) Pad or truncate the sentence to `max_length`
# (6) Create attention masks for [PAD] tokens.
encoded_dict = tokenizer.encode_plus(
sent, # Sentence to encode.
add_special_tokens=True, # Add '[CLS]' and '[SEP]'
max_length=128, # Pad & truncate all sentences.
pad_to_max_length=True,
return_attention_mask=True, # Construct attn. masks.
return_tensors='tf', # Return tf tensors.
)
# Add the encoded sentence to the list.
input_ids.append(encoded_dict['input_ids'])
# And its attention mask (simply differentiates padding from non-padding).
attention_masks.append(encoded_dict['attention_mask'])
token_type_ids.append(encoded_dict['token_type_ids'])
return [np.asarray(input_ids, dtype=np.int32),
np.asarray(attention_masks, dtype=np.int32),
np.asarray(token_type_ids, dtype=np.int32)]
仍然重现错误的最基本形式的模型:
model = TFBertForSequenceClassification.from_pretrained(
"bert-base-uncased",
num_labels = labellen,
output_attentions = False,
output_hidden_states = False
)
编译和适配:
optimizer = tf.keras.optimizers.Adam(learning_rate=1e-3, epsilon=1e-08, clipnorm=1.0)
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
metric = tf.keras.metrics.SparseCategoricalAccuracy('accuracy')
model.compile(optimizer=optimizer, loss=loss, metrics=[metric])
model.fit(x_train, y[:100], epochs=1, batch_size=3)
运行时的错误:
ValueError: 无法重塑具有 768 个元素的张量以进行整形 [1,1,128,1] (128 个元素) for '{{node tf_bert_for_sequence_classification_3/bert/embeddings/LayerNorm/Reshape}} = 重塑[T=DT_FLOAT, Tshape=DT_INT32](tf_bert_for_sequence_classification_3/bert/embeddings/LayerNorm/Reshape/ReadVariableOp, tf_bert_for_sequence_classification_3/bert/embeddings/LayerNorm/Reshape/shape)' 输入形状:[768],[4],输入张量计算为 部分形状:输入1 = [1,1,128,1]。
我知道 BERT 将每个标记转换为 768 值数组,但这是我对该特定数字的唯一了解,所以我不知道如何继续。
如果有人对 HuggingFace 库有经验,我也会感谢您对 TFBertForSequenceClassification 是否适合段落分类的想法。
非常感谢。
【问题讨论】:
标签: python machine-learning keras tensorflow2.0