ValueError: Can't convert non-rectangular Python sequence to Tensor when using tf.data.Dataset.from_tensor_slices
ValueError: Can't convert non-rectangular Python sequence to Tensor when using tf.data.Dataset.from_tensor_slices
这个问题已经在 SO 中发布了几次,但我仍然无法弄清楚我的代码有什么问题,特别是因为它来自 medium and the author makes the code available on google colab[=23= 中的教程]
我看到其他用户遇到变量类型错误的问题 (which is not my case, as my model input is the output of tokenizer
) and even seen the function I am trying to use (tf.data.Dataset.from_tensor_slices
) being suggested as a solution #56304986。
行产生错误是:
# train dataset
ds_train_encoded = encode_examples(ds_train).shuffle(10000).batch(batch_size)
方法 encode_examples
定义为(我在 encode_examples
方法中插入了 assert
行以确保我的问题不是长度不匹配):
def encode_examples(ds, limit=-1):
# prepare list, so that we can build up final TensorFlow dataset from slices.
input_ids_list = []
token_type_ids_list = []
attention_mask_list = []
label_list = []
if (limit > 0):
ds = ds.take(limit)
for review, label in tfds.as_numpy(ds):
bert_input = convert_example_to_feature(review.decode())
ii = bert_input['input_ids']
tti = bert_input['token_type_ids']
am = bert_input['attention_mask']
assert len(ii) == len(tti) == len(am), "unmatching lengths!"
input_ids_list.append(ii)
token_type_ids_list.append(tti)
attention_mask_list.append(am)
label_list.append([label])
return tf.data.Dataset.from_tensor_slices((input_ids_list, attention_mask_list, token_type_ids_list, label_list)).map(map_example_to_dict)
数据是这样加载的(这里我改变了数据集,只得到10%的训练数据,这样我可以加快调试速度)
(ds_train, ds_test), ds_info = tfds.load('imdb_reviews', split = ['train[:10%]','test[10%:15%]'], as_supervised=True, with_info=True)
另外两个调用(convert_example_to_feature
和map_example_to_dict
)和tokenizer如下:
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', do_lower_case=True)
def convert_example_to_feature(text):
# combine step for tokenization, WordPiece vector mapping, adding special tokens as well as truncating reviews longer than the max length
return tokenizer.encode_plus(text,
add_special_tokens = True, # add [CLS], [SEP]
#max_length = max_length, # max length of the text that can go to BERT
pad_to_max_length = True, # add [PAD] tokens
return_attention_mask = True,)# add attention mask to not focus on pad tokens
def map_example_to_dict(input_ids, attention_masks, token_type_ids, label):
return ({"input_ids": input_ids,
"token_type_ids": token_type_ids,
"attention_mask": attention_masks,
}, label)
我怀疑该错误可能与不同版本的 TensorFlow(我使用的是 2.3)有关,但不幸的是,由于内存原因,我无法 运行 google.colab 笔记本中的片段.
有谁知道我的代码哪里出了问题?感谢您的时间和关注。
原来是我注释了这行造成了麻烦
#max_length = max_length, # max length of the text that can go to BERT
我假设它会截断模型的最大尺寸,或者它会将最长的输入作为最大尺寸。它会处理 none,然后即使我有相同数量的条目,这些条目的大小也会有所不同,从而生成 non-rectangular 张量。
我删除了 #
并使用 512 作为 max_lenght。无论如何,这是 BERT 所采用的最大值。 (参考transformer's tokenizer class)
另一个可能的原因是应该在分词器中明确启用截断。参数为truncation = True
这个问题已经在 SO 中发布了几次,但我仍然无法弄清楚我的代码有什么问题,特别是因为它来自 medium and the author makes the code available on google colab[=23= 中的教程]
我看到其他用户遇到变量类型错误的问题 tokenizer
) and even seen the function I am trying to use (tf.data.Dataset.from_tensor_slices
) being suggested as a solution #56304986。
行产生错误是:
# train dataset
ds_train_encoded = encode_examples(ds_train).shuffle(10000).batch(batch_size)
方法 encode_examples
定义为(我在 encode_examples
方法中插入了 assert
行以确保我的问题不是长度不匹配):
def encode_examples(ds, limit=-1):
# prepare list, so that we can build up final TensorFlow dataset from slices.
input_ids_list = []
token_type_ids_list = []
attention_mask_list = []
label_list = []
if (limit > 0):
ds = ds.take(limit)
for review, label in tfds.as_numpy(ds):
bert_input = convert_example_to_feature(review.decode())
ii = bert_input['input_ids']
tti = bert_input['token_type_ids']
am = bert_input['attention_mask']
assert len(ii) == len(tti) == len(am), "unmatching lengths!"
input_ids_list.append(ii)
token_type_ids_list.append(tti)
attention_mask_list.append(am)
label_list.append([label])
return tf.data.Dataset.from_tensor_slices((input_ids_list, attention_mask_list, token_type_ids_list, label_list)).map(map_example_to_dict)
数据是这样加载的(这里我改变了数据集,只得到10%的训练数据,这样我可以加快调试速度)
(ds_train, ds_test), ds_info = tfds.load('imdb_reviews', split = ['train[:10%]','test[10%:15%]'], as_supervised=True, with_info=True)
另外两个调用(convert_example_to_feature
和map_example_to_dict
)和tokenizer如下:
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', do_lower_case=True)
def convert_example_to_feature(text):
# combine step for tokenization, WordPiece vector mapping, adding special tokens as well as truncating reviews longer than the max length
return tokenizer.encode_plus(text,
add_special_tokens = True, # add [CLS], [SEP]
#max_length = max_length, # max length of the text that can go to BERT
pad_to_max_length = True, # add [PAD] tokens
return_attention_mask = True,)# add attention mask to not focus on pad tokens
def map_example_to_dict(input_ids, attention_masks, token_type_ids, label):
return ({"input_ids": input_ids,
"token_type_ids": token_type_ids,
"attention_mask": attention_masks,
}, label)
我怀疑该错误可能与不同版本的 TensorFlow(我使用的是 2.3)有关,但不幸的是,由于内存原因,我无法 运行 google.colab 笔记本中的片段.
有谁知道我的代码哪里出了问题?感谢您的时间和关注。
原来是我注释了这行造成了麻烦
#max_length = max_length, # max length of the text that can go to BERT
我假设它会截断模型的最大尺寸,或者它会将最长的输入作为最大尺寸。它会处理 none,然后即使我有相同数量的条目,这些条目的大小也会有所不同,从而生成 non-rectangular 张量。
我删除了 #
并使用 512 作为 max_lenght。无论如何,这是 BERT 所采用的最大值。 (参考transformer's tokenizer class)
另一个可能的原因是应该在分词器中明确启用截断。参数为truncation = True