如何解决tf.data WindowDataset flat_map给出'dict'对象没有属性'batch'错误
我正在尝试处理(batch_size,time_steps,my_data)
类型的批次
为什么在flat_map
步骤上我得到AttributeError: 'dict' object has no attribute 'batch'
x_train = np.random.normal(size=(60000,768))
token_type_ids = np.ones(shape=(len(x_train)))
position_ids = np.random.normal(size=(x_train.shape[0],5))
features_ds = tf.data.Dataset.from_tensor_slices({'inputs_embeds': x_train,'token_type_ids': token_type_ids,'position_ids': position_ids})
y_ds = tf.data.Dataset.from_tensor_slices(y_train)
ds = tf.data.Dataset.zip((features_ds,y_ds))
# result = list(ds.as_numpy_iterator())
result_ds = ds.window(size=time_steps,shift=time_steps,stride=1,drop_remainder=True). \
flat_map(lambda x,y: tf.data.Dataset.zip((x.batch(time_steps),y.batch(time_steps))))
任何主意是什么问题?以及如何解决?
解决方法
您可以将批次添加为单独的步骤:
x_train = np.random.normal(size=(60000,768))
token_type_ids = np.ones(shape=(len(x_train)))
position_ids = np.random.normal(size=(x_train.shape[0],5))
features_ds = tf.data.Dataset.from_tensor_slices({'inputs_embeds': x_train,'token_type_ids': token_type_ids,'position_ids': position_ids})
y_train = np.random.normal(size=(60000,1))
y_ds = tf.data.Dataset.from_tensor_slices(y_train)
ds = tf.data.Dataset.zip((features_ds,y_ds))
result_ds = ds.window(size=time_steps,shift=time_steps,stride=1,drop_remainder=True).\
flat_map(lambda x,y: tf.data.Dataset.zip((x,y)))
time_steps=3
result_ds=result_ds.batch(time_steps)
for i in result_ds.take(1):
print(i)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。