【问题标题】:Tensorflow model reading parquet files using IODatasetTensorflow 模型使用 IODataset 读取镶木地板文件
【发布时间】:2021-12-28 16:45:05
【问题描述】:

我一直在尝试使用tfio.IODataset.from_parquet 来训练模型大型镶木地板文件。下面是我正在使用的镶木地板加载过程的一个最小示例:

pd.DataFrame({'a':[.1,.2], 'b':[.01,.02]}).to_parquet('file.parquet')
ds = tfio.IODataset.from_parquet('file.parquet', columns = ['a','b'])

for batch in ds.batch(5):
    print(batch)

OrderedDict([('a', ), ('b', )])

批处理数据集类型为OrderedDict,键为ab。为了训练我的模型,我想要更类似于“密集”特征向量的东西,而不是有序字典中的两个单独的键。 如何将 OrderedDict 转换为密集元组?

试试 1

根据this example,我尝试了以下方法将数据集转换为“密集”特征:

def make_dense(features):
    features = tf.stack(list(features), axis=1)
    return features
ds = ds.map(make_dense)

不幸的是,这会引发错误。我尝试了这个主题的几种变体,包括

  1. axis=1 更改为axis=0
  2. 使用ds = ds.map(lambda *items: tf.stack(items)) 代替我的make_dense 函数。

我想这是IODataset 的一个非常基本的操作;我只是不知道如何完成它。

【问题讨论】:

    标签: python tensorflow tensorflow-datasets


    【解决方案1】:

    不是最漂亮的解决方案,但您可以尝试以下方法:

    import pandas as pd
    import tensorflow_io as tfio
    
    pd.DataFrame({'a':[.1,.2], 'b':[.01,.02]}).to_parquet('file.parquet')
    ds = tfio.IODataset.from_parquet('file.parquet', columns = ['a','b'])
    
    def option1(features):
        keys, values = tf.TensorArray(dtype=tf.string, size=0, dynamic_size=True), tf.TensorArray(dtype=tf.float64, size=0, dynamic_size=True)
        for k, v in features.items():
           keys = keys.write(keys.size(), k)
           values = values.write(values.size(), v)
        return (keys.stack(), values.stack())
    
    def option2(features):
        ta = tf.TensorArray(dtype=tf.float64, size=0, dynamic_size=True)
        for _, v in features.items():
           ta = ta.write(ta.size(), v)
        return ta.stack()  
    
    option1_ds = ds.map(option1)
    for batch in option1_ds.batch(5):
        print(batch)
    
    print()
    
    option2_ds = ds.map(option2)
    for batch in option2_ds.batch(5):
        print(batch)
    
    (<tf.Tensor: shape=(2, 2), dtype=string, numpy=
    array([[b'a', b'b'],
           [b'a', b'b']], dtype=object)>, <tf.Tensor: shape=(2, 2), dtype=float64, numpy=
    array([[0.1 , 0.01],
           [0.2 , 0.02]])>)
    
    tf.Tensor(
    [[0.1  0.01]
     [0.2  0.02]], shape=(2, 2), dtype=float64)
    

    【讨论】:

      猜你喜欢
      • 2018-08-13
      • 2019-08-04
      • 2022-06-16
      • 2019-09-23
      • 2018-05-06
      • 2017-12-27
      • 2017-05-06
      • 2021-01-12
      • 1970-01-01
      相关资源
      最近更新 更多