【问题标题】:How to seperate a Tensorflow dataset object in features and labels如何在特征和标签中分离 Tensorflow 数据集对象
【发布时间】:2020-02-12 11:42:07
【问题描述】:

我的目标是只为自编码器的 Keras 模型提供来自 tf.data.Dataset 对象的(批量)特征。

我正在加载数据集,格式化图像并像这样创建批次:

#load dataset
(raw_train, raw_validation, raw_test), metadata = tfds.load(
    'cats_vs_dogs',
    split=[
    tfds.Split.TRAIN.subsplit(tfds.percent[:80]),
    tfds.Split.TRAIN.subsplit(tfds.percent[80:90]),
    tfds.Split.TRAIN.subsplit(tfds.percent[90:])],
    with_info=True,
    as_supervised=True, 
    )

#normalize and resize images
IMG_SIZE = 160
def format_example(self, image, label):
    image = tf.cast(image, tf.float32)
    image = (image/255.0) 
    image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
    return image, label
train = raw_train.map(format_example)
validation = raw_validation.map(format_example)
test = raw_test.map(format_example)

#create batches
SHUFFLE_BUFFER_SIZE = 1000
BATCH_SIZE = 32
train_batches = train.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE)
validation_batches = validation.batch(BATCH_SIZE)
test_batches = test.batch(BATCH_SIZE)

此时我想在特征和标签中分开批次,如下所示:

train_x_batches, train_y_batches = train_batches

但我得到这个错误:

`ValueError                                Traceback (most recent call last)
 in 
----> 1 train_x_batches, train_y_batches = train_batches

ValueError: too many values to unpack (expected 2)`

【问题讨论】:

    标签: python numpy tensorflow keras


    【解决方案1】:

    我遇到了同样的问题,我是这样解决的:

    train_x_batches = np.concatenate([x for x, y in train_batches], axis=0)
    train_y_batches = np.concatenate([y for x, y in train_batches], axis=0)
    

    您可以使用以下命令返回您的课程标签:

    train_batches.class_names
    

    【讨论】:

      【解决方案2】:

      如果您只需要自动编码器的功能,您可以通过map 对它们进行切片:

      train_x_batches = train_batches.map(lambda x: x[0]) 
      

      当然,您可以为标签做同样的事情:

      train_y_batches = train_batches.map(lambda x: x[1]) 
      

      【讨论】:

      • 这对我不起作用。我收到错误TypeError: <lambda>() takes 1 positional argument but 2 were given
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多