【问题标题】:Load numpy array from pandas dataframe into tensorflow dataset将 pandas 数据帧中的 numpy 数组加载到 tensorflow 数据集中
【发布时间】:2020-08-18 17:54:45
【问题描述】:

我正在尝试使用以下命令将我的 pandas 数据帧 (df) 加载到 Tensorflow 数据集中:

target = df['label']
features = df['encoded_sentence']

dataset = tf.data.Dataset.from_tensor_slices((features.values, target.values))

这是我的 pandas 数据框的摘录:

+-------+-----------------------+------------------+
| label | sentence              | encoded_sentence |
+-------+-----------------------+------------------+
| 0     | Hello world           | [5, 7]           |
+-------+-----------------------+------------------+
| 1     | my name is john smith | [1, 9, 10, 2, 6] |
+-------+-----------------------+------------------+
| 1     | Hello! My name is     | [5, 3, 9, 10]    |
+-------+-----------------------+------------------+
| 0     | foo baar              | [8, 4]           |
+-------+-----------------------+------------------+

# df.dtypes gives me:
label                int8
sentence             object
encoded_sentencee    object

但它总是给我一个值错误:

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list).

谁能告诉我如何在我的 Tensorflow 数据集中使用编码句子?非常感谢您的帮助!

【问题讨论】:

    标签: pandas numpy tensorflow


    【解决方案1】:

    你可以先把你的 Pandas 值变成一个参差不齐的张量,然后用它制作数据集:

    import tensorflow as tf
    import pandas as pd
    
    df = pd.DataFrame({'label': [0, 1, 1, 0],
                       'sentence': ['Hello world', 'my name is john smith',
                                    'Hello! My name is', 'foo baar'],
                       'encoded_sentence': [[5, 7], [1, 9, 10, 2, 6],
                                            [5, 3, 9, 10], [8, 4]]})
    features = tf.ragged.stack(list(df['encoded_sentence']))
    target = tf.convert_to_tensor(df['label'].values)
    dataset = tf.data.Dataset.from_tensor_slices((features, target))
    for f, t in dataset:
        print(f.numpy(), t.numpy())
    

    输出:

    [5 7] 0
    [ 1  9 10  2  6] 1
    [ 5  3  9 10] 1
    [8 4] 0
    

    请注意,您可能希望使用 padded_batch 从数据集中获取批量示例。

    编辑:由于填充批处理目前似乎不适用于由不规则张量制成的数据集,因此您也可以先将不规则张量转换为常规张量:

    import tensorflow as tf
    import pandas as pd
    
    df = pd.DataFrame({'label': [0, 1, 1, 0],
                       'sentence': ['Hello world', 'my name is john smith',
                                    'Hello! My name is', 'foo baar'],
                       'encoded_sentence': [[5, 7], [1, 9, 10, 2, 6],
                                            [5, 3, 9, 10], [8, 4]]})
    features_ragged = tf.ragged.stack(list(df['encoded_sentence']))
    features = features_ragged.to_tensor(default_value=-1)
    target = tf.convert_to_tensor(df['label'].values)
    dataset = tf.data.Dataset.from_tensor_slices((features, target))
    batches = dataset.batch(2)
    for f, t in batches:
        print(f.numpy(), t.numpy())
    

    输出:

    [[ 5  7 -1 -1 -1]
     [ 1  9 10  2  6]] [0 1]
    [[ 5  3  9 10 -1]
     [ 8  4 -1 -1 -1]] [1 0]
    

    【讨论】:

    • 非常感谢您的帮助!当我尝试创建一个批次时,它给了我一个类型错误...TypeError: ('Padded batching of components of type ', <class 'tensorflow.python.ops.ragged.ragged_tensor.RaggedTensorSpec'>, ' is not supported.')你能告诉我创建训练集和测试集的正确方法吗?
    • @StudentAsker 我明白了,我会说这是一个错误,我提交了issue #39163
    • @StudentAsker 我添加了一种替代方法,只需将参差不齐的张量转换为常规张量。
    猜你喜欢
    • 1970-01-01
    • 2019-09-23
    • 2017-07-07
    • 2021-03-19
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多