如何在Tensorflow中对数据集的10%进行采样?

2024-04-19 17:37:21 发布

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

我有一个mnist数据集,我想用它的10%作为验证集。如何在Tensorflow中执行此操作?你知道吗


Tags: 数据tensorflowmnist
2条回答

您应该考虑提前对数据进行下采样,因为验证数据应该与训练数据分开。你知道吗

如果必须在tensorflow中对数据进行采样,请考虑使用tensorflow dataset object中的shuffle。 从文件中

shuffle shuffle( buffer_size, seed=None, reshuffle_each_iteration=None ) Randomly shuffles the elements of this dataset.

Args: buffer_size: A tf.int64 scalar tf.Tensor, representing the number of elements from this dataset from which the new dataset will sample.

可以使用Dataset方法,如数据集.take()和数据集.skip()提取数据的一部分并按您的意愿使用,用于培训、测试或验证。你知道吗

或者,您可以使用scikit learn将数据拆分为测试和(培训+验证)数据,然后将(培训+验证)数据再次拆分为培训和验证数据。你知道吗

import sklearn.model_selection as sk

X_train_val, X_test, y_train_val, y_test = sk.train_test_split(features,labels,test_size=0.5, random_state = 5)

X_train, X_val, y_train, y_val = sk.train_test_split(X_train_val,y_train_val,test_size=0.2, random_state = 5)

请记住调整第二个分割,以便测试大小是完整数据集的期望值(总数据集的50%的20%是10%),而不是train\val数据集

相关问题 更多 >