从Tensorflow预取数据集中提取目标

2024-04-25 07:02:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我还在学习tensorflow和keras,我怀疑这个问题有一个非常简单的答案,我只是因为不熟悉而错过了

我有一个PrefetchDataset对象:

> print(tf_test)
$ <PrefetchDataset shapes: ((None, 99), (None,)), types: (tf.float32, tf.int64)>

…由特征和目标组成。我可以使用for循环对其进行迭代:

> for example in tf_test:
>     print(example[0].numpy())
>     print(example[1].numpy())
>     exit()
$ [[-0.31 -0.94 -1.12 ... 0.18 -0.27]
   [-0.22 -0.54 -0.14 ... 0.33 -0.55]
   [-0.60 -0.02 -1.41 ... 0.21 -0.63]
   ...
   [-0.03 -0.91 -0.12 ... 0.77 -0.23]
   [-0.76 -1.48 -0.15 ... 0.38 -0.35]
   [-0.55 -0.08 -0.69 ... 0.44 -0.36]]
  [0 0 1 0 1 0 0 0 1 0 1 1 0 1 0 0 0
   ...
   0 1 1 0]

然而,这是非常缓慢的。我想做的是访问对应于类标签的张量,并将其转换为一个numpy数组、一个列表或任何类型的可输入scikit learn分类报告和/或混淆矩阵的iterable:

> y_pred = model.predict(tf_test)
> print(y_pred)
$ [[0.01]
   [0.14]
   [0.00]
   ...
   [0.32]
   [0.03]
   [0.00]]
> y_pred_list = [int(x[0]) for x in y_pred]             # assumes value >= 0.5 is positive prediction
> y_true = []                                           # what I need help with
> print(sklearn.metrics.confusion_matrix(y_true, y_pred_list)

…或访问数据,以便在tensorflow的混淆矩阵中使用:

> labels = []                                           # what I need help with
> predictions = y_pred_list                             # could we just use a tensor?
> print(tf.math.confusion_matrix(labels, predictions)

在这两种情况下,以一种计算成本不高的方式从原始对象获取目标数据的一般能力将非常有用(并且可能有助于我的基本直觉:tensorflow和keras)

如有任何建议,将不胜感激


Tags: 对象intestnumpynone目标forexample
3条回答

您可以使用list(ds)将其转换为列表,然后使用tf.data.Dataset.from_tensor_slices(list(ds))将其重新编译为普通数据集。从那以后,你的噩梦又开始了,但至少这是一场别人以前经历过的噩梦

请注意,对于更复杂的数据集(例如嵌套字典),在调用list(ds)后需要进行更多的预处理,但这对于您所询问的示例应该有效

这远不是一个令人满意的答案,但不幸的是,这个类完全没有文档记录,标准的数据集技巧都不起作用

如果要保留批次或将所有标签提取为单个张量,可以使用以下函数:


def get_labels_from_tfdataset(tfdataset, batched=False):

    labels = list(map(lambda x: x[1], tfdataset)) # Get labels 

    if not batched:
        return tf.concat(labels, axis=0) # concat the list of batched labels

    return labels

您可以使用map从每个(input, label)对中选择输入或标签,并将其转换为列表:

import tensorflow as tf
import numpy as np

inputs = np.random.rand(100, 99)
targets = np.random.rand(100)

ds = tf.data.Dataset.from_tensor_slices((inputs, targets))

X_train = list(map(lambda x: x[0], ds))
y_train = list(map(lambda x: x[1], ds))

相关问题 更多 >