如何使用内置的tensorflow方法对特征张量和标签张量执行sklearn样式训练测试拆分?

问题描述

重新发布我的original question,因为即使在提高清晰度后,社区也没有恢复它。

我正在寻找一种使用TensorFlow内置方法将要素和相应标签数据拆分为训练和测试的方法。我的数据已经在两个张量(即tf.Tensor对象)中,分别名为featureslabels

我知道如何使用sklearn.model_selection来处理numpy数组,如本post所示。另外,有人指出了这个method,它要求数据在单个张量中。另外,我需要训练集和测试集不相交,这与method不同(这意味着它们在拆分后不能有公共数据点)。

我正在寻找一种使用Tensorflow中内置方法进行相同操作的方法

我的要求可能有太多条件,但是基本上需要的是Tensorflow中与sklearn.model_selection.train_test_split()等效的方法,如下所示:

import tensorflow as tf

X_train,X_test,y_train,y_test = tf.train_test_split(features,labels,test_size=0.1,random_state=123)

解决方法

您可以通过以下方式使用TF来实现此目的

custom scopes

我们在这里要做的是首先创建一个随机的统一张量,其长度与数据的长度相同。 然后,根据 $this->load->library('zend'); $this->zend->load('Zend/Barcode'); $barcodeOptions = array( 'text' => $code,'barHeight' => $height,'barThinWidth' => $width ); $rendererOptions = array( 'imageType' => 'png','horizontalPosition' => 'center','verticalPosition' => 'middle' ); $imageResource = Zend_Barcode::render($bcs,'image',$barcodeOptions,$rendererOptions); return $imageResource; 给出的比率创建布尔蒙版,最后使用tf.boolean_mask

提取相关部分进行训练/测试