尝试将 Python 字典格式化为表时,BatchDataset 不可下标

2023-12-02

我正在努力通过TensorFlow 加载 pandas.DataFrame 教程,我正在尝试修改创建字典切片的代码片段的输出:

dict_slices = tf.data.Dataset.from_tensor_slices((df.to_dict('list'), target.values)).batch(16)

for dict_slice in dict_slices.take(1):
     print (dict_slice)

我发现以下输出很草率,我想将其放入更易读的表格格式。

Dictionary output from TensorFlow Tutorial

I tried to format the for loop, based on this recommendation Trying a basic for loop

这给了我 BatchDataset 不可下标的错误

Error messages from for loop modification

Then I tried to use the range and leng function on the dict_slices, so that i would be an integer index and not a slice Modifying dict_slices with range and len

这给了我以下错误(据我所知,因为 dict_slices 仍然是一个数组,并且每次迭代都是数组的一个向量,而不是向量的一个索引):

enter image description here


Refer here以获得解决方案。总而言之,我们需要使用as_numpy_iterator

example = list(dict_slices.as_numpy_iterator())
example[0]['age']
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试将 Python 字典格式化为表时,BatchDataset 不可下标 的相关文章

随机推荐