根据模型预测过滤 Tensorflow Dataset [英] Filter Tensorflow Dataset according to model prediction

查看:41
本文介绍了根据模型预测过滤 Tensorflow Dataset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过仅选择经过训练的模型正确预测的样本来过滤 TensorFlow 数据集.数据集由图像、标签对组成.我试过这个:

I would like to filter a TensorFlow dataset by selecting only samples that are correctly predicted by a trained model. Dataset is made of image,label pairs. I've tried this:

predicted_labels = np.argmax(model.predict(dataset), axis=1)
predicted_labels_dataset = tf.data.Dataset.from_tensor_slices(predicted_labels)
zipped_dataset = tf.data.Dataset.zip((dataset, predicted_labels_dataset))
correct_dataset = zipped_dataset.filter(lambda sample, predicted_label: tf.math.equal(x = predicted_label, y = sample[1]))

但它没有按预期工作.我仍然在 correct_dataset 中得到错误分类的样本.预先感谢您的帮助

But it does not work as expected. I still get wrongly classified samples in the correct_dataset. Thanks in advance for your help

推荐答案

我认为这不起作用的唯一原因是,您的 sample[1] 不代表标签(或不代表格式正确).我运行了以下玩具示例并且工作正常.很高兴根据您的跟进来完善答案,直到我们确定确切的问题.

THe only reason I can think of this not working is that, your sample[1] does not represent the label (or not in the correct format). I ran the following toy example and worked fine. Happy to refine the answer depending on your follow ups until we identify the exact problem.

import numpy as np
import tensorflow as tf


predicted_labels_dataset = tf.data.Dataset.from_tensor_slices([0,1,2,1,2,3])

inp_ds = tf.data.Dataset.from_tensor_slices(tf.random.normal(shape=[6,5]))
lbl_ds = tf.data.Dataset.from_tensor_slices([1,1,0,0,2,3])
dataset = tf.data.Dataset.zip((inp_ds, lbl_ds))
zipped_dataset = tf.data.Dataset.zip((dataset, predicted_labels_dataset))

correct_dataset = zipped_dataset.filter(lambda sample, predicted_label: tf.math.equal(x = predicted_label, y = sample[1]))

这篇关于根据模型预测过滤 Tensorflow Dataset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆