如何解决保存 Small_Bert 预处理的输出
我对 python 相当陌生,我正在尝试弄清楚如何在 small_bert
[https://hub.tensorflow.google.cn/tensorflow/bert_en_uncased_preprocess/3] 中提取映射的标记。我读过的示例与 BERT
相关,如下所示,映射的标记被提取并可以保存。
import numpy as np
SEQ_LEN = 128
ids = np.zeros((len(df),SEQ_LEN))
mask = np.zeros((len(df),SEQ_LEN))
tok = np.zeros((len(df),SEQ_LEN))
for i,sequence in enumerate(df_data['text']):
tokens = tokenizer.encode_plus(
sequence,# sentence to be encoded
max_length=SEQ_LEN,# maximum length of sentence
truncation=True,# truncate longer sequences
padding='max_length',# pad shorter sequences
add_special_tokens=True,# add [CLS] and [SEP]
return_token_type_ids=True,# return token ids
return_attention_mask=True,# return attention mask
return_tensors='tf' # return tflow tensors
)
ids[i,:],mask[i,tok[i,:] = tokens['input_ids'],tokens['attention_mask'],tokens['token_type_ids']
我想在 small_bert
做的事情是类似的
path_to_preprocessor = 'https://hub.tensorflow.google.cn/tensorflow/bert_en_uncased_preprocess/3'
preprocessor = hub.load(path_to_preprocessor)
words = np.zeros((len(df),SEQ_LEN))
typ = np.zeros((len(df),SEQ_LEN))
seq_length=128
for i,sequence in enumerate(df['text']):
toks = preprocess.tokenize(df['text']) # tokenize text to ragged tensors
encode = preprocess.bert_pack_inputs([toks],seq_length=seq_length) # encode to input_ids,mask,and type
# extraction
words[i,typ[i,:] = encode['input_word_ids'],encode['input_mask'],encode['input_type_ids']
当我尝试上面的代码时,我得到 "ValueError: could not broadcast input array from shape (500,128) into shape (128)"
,它告诉我我的数据的形状与我想要做的不匹配
我需要的是一些帮助来使我的代码保持一致。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。