![](/img/trans.png)
[英]what is a “batch” in Keras model.fit() when training data are images
[英]Correct way to specify training data as tuple (x, y) in Keras model.fit with multiple inputs and outputs
我正在訓練具有三個輸入和兩個輸出的 Keras Tensorflow model:
mymodel = tf.keras.Model([X1, X2, X3], [y1, y2])
當我通過分別指定x
和y
數據來安裝這個 model 時,它可以正常工作,沒有任何障礙:
history = mymodel.fit([X1, X2, X3], [y1, y2], batch_size=128, epochs=5)
但是,我想將訓練數據作為單個元組 (x, y) 提供,以保持與自定義數據生成器的兼容性。 當我這樣做時,它會拋出一個錯誤:
data = ([X1, X2, X3], [y1, y2])
history = mymodel.fit(data, batch_size=128, epochs=5)
No gradients provided for any variable: ['dense/kernel:0', 'dense/bias:0',...
我猜我的data
元組格式是錯誤的。
如何正確指定我的訓練數據?
您需要的是使用生成器或tf.data
API 構建數據管道。 根據培訓 API 的文檔,來源:
Model.fit(
x=None,
y=None,
batch_size=None,
epochs=1,
...
Arguments
- x: Input data. It could be:
A tf.data dataset. Should return a tuple of either (inputs, targets)
or (inputs, targets, sample_weights).
A generator or keras.utils.Sequence returning (inputs, targets) or
(inputs, targets, sample_weights).
- y: If x is a dataset, generator, or keras.utils.Sequence instance,
y should not be specified (since targets will be obtained from x).
僅供參考,但如果您的數據是numpy數組或tensorflow張量( x
),那么您需要提供相應的y
。 根據文檔
- x:
A Numpy array (or array-like), or a list of arrays (in case the model has
multiple inputs).
A TensorFlow tensor, or a list of tensors (in case the model has multiple inputs).
- y:
Like the input data x, it could be either Numpy array(s) or TensorFlow tensor(s).
It should be consistent with x (you cannot have Numpy inputs
and tensor targets, or inversely).
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.