我的目標是僅將來自 tf.data.Dataset 對象的(批量)特征提供給 Autoencoder 的 Keras 模型。我正在加載數據集,格式化圖像并像這樣創建批次:#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 imagesIMG_SIZE = 160def 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, labeltrain = raw_train.map(format_example)validation = raw_validation.map(format_example)test = raw_test.map(format_example)#create batchesSHUFFLE_BUFFER_SIZE = 1000BATCH_SIZE = 32train_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_batchesValueError: too many values to unpack (expected 2)`
2 回答

慕哥9229398
TA貢獻1877條經驗 獲得超6個贊
我遇到了同樣的問題,我這樣解決了:
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

ABOUTYOU
TA貢獻1812條經驗 獲得超5個贊
如果您只需要自動編碼器的功能,您可以通過以下方式對它們進行切片map
:
train_x_batches = train_batches.map(lambda x: x[0])
當然,您可以對標簽執行相同的操作:
train_y_batches = train_batches.map(lambda x: x[1])
添加回答
舉報
0/150
提交
取消