我仍在学习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分类报告和/或混淆矩阵的迭代器:
> 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 的基本直觉)。任何建议都将不胜感激。
y = np.concatenate([y for x, y in ds], axis=0)
- prashanth