Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TF2.0数据API从每个类标签获取n_i个样本_Python_Tensorflow_Keras_Tensorflow Datasets_Tensorflow2.0 - Fatal编程技术网

Python TF2.0数据API从每个类标签获取n_i个样本

Python TF2.0数据API从每个类标签获取n_i个样本,python,tensorflow,keras,tensorflow-datasets,tensorflow2.0,Python,Tensorflow,Keras,Tensorflow Datasets,Tensorflow2.0,我必须使用TF2 Keras模型将32x32形状的输入分为3类。我的训练集有7000个例子 >>> X_train.shape # (7000, 32, 32) >>> Y_train.shape # (7000, 3) 每个类别的示例数量各不相同(例如,类别0有约2500个示例,类别1有约800个示例,等等) 我想使用tf.data API创建一个dataset对象,该对象返回成批的训练数据,其中包含[n_0,n_1,n_2]指定的每个类的示例数 我想从每

我必须使用TF2 Keras模型将32x32形状的输入分为3类。我的训练集有7000个例子

>>> X_train.shape # (7000, 32, 32)
>>> Y_train.shape # (7000, 3)
每个类别的示例数量各不相同(例如,类别0有约2500个示例,类别1有约800个示例,等等)

我想使用tf.data API创建一个dataset对象,该对象返回成批的训练数据,其中包含
[n_0,n_1,n_2]
指定的每个类的示例数

我想从每个班级中随机抽取这些样本,并从
X\u序列、Y\u序列中进行替换

例如,如果我调用
get\u batch([100150125])
它应该从类0的
X\u batch
返回100个随机样本,从类1返回150个,从类2返回125个

如何使用TF2.0数据API实现这一点,以便我可以使用它来训练Keras模型?

Keras'实际上有一个参数。虽然它不允许您选择精确数量的样本,但它会从类中均匀地选择它们

X_train_stratified, X_test_stratified, y_train_strat, y_test_strat = train_test_split(X_train, y_train, test_size=0.2, stratify=y)
如果要进行交叉验证,还可以使用


我希望我正确地理解了您的问题

一种可能的方法是如下进行:

  • X\u列
    Y\u列
    中的数据加载到单个
    tf.data
    数据集中,以确保每个
    X
    与正确的
    Y
    匹配
  • .shuffle()
    然后使用
    过滤器()将数据集拆分为每个
    n_i
  • 编写我们的
    get_batch
    函数,从每个数据集中返回正确数量的样本,
    shuffle()
    样本,然后将其拆分回
    X
    Y
  • 大概是这样的:

    #1:将数据加载到数据集中
    原始数据=tf.data.Dataset.zip(
    (
    tf.data.Dataset.from_tensor_切片(X_序列),
    tf.data.Dataset.from_tensor_切片(Y_序列)
    )
    ).shuffle(7000)
    #2:对每个类别进行拆分
    def get_过滤器(n):
    def过滤器(x,y):
    返回tf.equal(1.0,y[n])
    返回过滤器
    n_0s=原始数据。过滤器(获取过滤器(0))
    n_1s=原始数据过滤器(获取过滤器(1))
    n_2s=原始数据过滤器(获取过滤器(2))
    # 3:
    def get_批次(n_0、n_1、n_2):
    sample=n_0s.take(n_0).连接(n_1s.take(n_1)).连接(n_2s.take(n_2))
    shuffled=sample.shuffle(n_0+n_1+n_2)
    返回洗牌.map(λx,y:x),洗牌.map(λx,y:y)
    
    现在我们可以做:

    x_批,y_批=获取_批(100150125)
    
    请注意,我在这里使用了一些可能会浪费的操作,采用了一种我觉得直观而直接的方法(特别是在过滤操作中读取
    raw_数据
    dataset 3次)因此,我不认为这是实现所需功能的最有效方法,但对于像您描述的那样适合内存的数据集,我相信这种低效率可以忽略不计