【问题标题】:ValueError: The `batch_size` argument must not be specified for the given input typeValueError:不能为给定的输入类型指定“batch_size”参数
【发布时间】:2021-04-15 13:00:50
【问题描述】:

我使用 tf.data.dataset 为自动编码器模型创建了虚拟数据。我正在使用 tensorflow-1.15.x、keras 2.3.1 和 numpy 1.19.5 在我的 GPU 上运行。

N = 5000
H = 128
W = 128
C = 2

train_data = np.random.randn(N,H,W,C)

train_data = tf.convert_to_tensor(train_data)

test_data = np.random.randn(500,H,W,C)

test_data = tf.convert_to_tensor(test_data)

batch_size = 1

train_dataset = tf.data.Dataset.from_tensor_slices((train_data, train_data))
train_dataset = train_dataset.batch(batch_size, drop_remainder=True)

test_dataset = tf.data.Dataset.from_tensor_slices((test_data, test_data))
test_dataset = test_dataset.batch(batch_size, drop_remainder=True)

但是在模型上拟合这些数据时,我得到了以下错误 -

epochs = 5

rms = RMSprop(learning_rate=0.00002,
              rho=0.9,
              epsilon=1e-07)

model.compile(loss='mean_squared_error', optimizer=rms, metrics=['mean_squared_error'])

callback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=3)

logs = "logs/"

tboard_callback = tf.keras.callbacks.TensorBoard(log_dir = logs,
                                                 histogram_freq = 1)

history = model.fit(train_dataset, epochs=epochs, batch_size=batch_size, callbacks=[callback, tboard_callback],
                    validation_data=test_dataset)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-25-47134a802115> in <module>()
     15 
     16 history = model.fit(train_dataset, epochs=epochs, batch_size=batch_size, callbacks=[callback, tboard_callback],
---> 17                     validation_data=test_dataset)

2 frames
/tensorflow-1.15.2/python3.7/tensorflow_core/python/keras/engine/training.py in _validate_or_infer_batch_size(self, batch_size, steps, x)
   1813             'The `batch_size` argument must not be specified for the given '
   1814             'input type. Received input: {}, batch_size: {}'.format(
-> 1815                 x, batch_size))
   1816       return
   1817 

ValueError: The `batch_size` argument must not be specified for the given input type. Received input: <DatasetV1Adapter shapes: ((1, 128, 128, 2), (1, 128, 128, 2)), types: (tf.float64, tf.float64)>, batch_size: 1

对有关此错误的其他问题的回答建议对训练数据集和验证数据集进行批处理,但我已经对这两个数据集进行了批处理。什么可能导致此错误?谢谢。

【问题讨论】:

  • 您可以尝试使用 pip3 install --upgrade tensorflow 将 tensorflow 升级到最新版本吗?
  • 实际上问题是我在具有这些版本的远程服务器上运行它,我无法更改库版本。使用最新的 keras/tf 版本,它可以在 Google Colab 上正常运行。
  • 如果在model.fit() 中不设置batch_size 会怎样?另外你为什么使用 1 的批量大小?
  • 您的 tf.dataset 正在生成批次,因此无需在 model.fit 中再次设置批次大小
  • 好的@Frightera,我认为你是对的。当我从 model.fit() 中删除 batch_size 时,错误被删除。但它允许 model.fit() 中的 batch_size 以及 tf2 中的 tf.dataset。

标签: tensorflow keras tensorflow-datasets


【解决方案1】:

删除model.fit()中的batch_size

history = model.fit(train_dataset, epochs=epochs, callbacks=[callback, tboard_callback],validation_data=test_dataset)

【讨论】:

    猜你喜欢
    • 2023-04-07
    • 2022-01-18
    • 1970-01-01
    • 2021-09-04
    • 2021-04-15
    • 2020-01-25
    • 2015-06-19
    • 1970-01-01
    • 2019-07-21
    相关资源
    最近更新 更多