簡體   English   中英

在 Keras model.fit 中將訓練數據指定為元組 (x, y) 的正確方法,具有多個輸入和輸出

[英]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])

當我通過分別指定xy數據來安裝這個 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).

僅供參考,但如果您的數據是數組或張量( 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM