Python 在Keras模型中将训练数据指定为元组(x,y)的正确方法。适合多个输入和输出
我正在培训一个Keras Tensorflow模型,该模型有三个输入和两个输出:Python 在Keras模型中将训练数据指定为元组(x,y)的正确方法。适合多个输入和输出,python,tensorflow,keras,training-data,numpy,Python,Tensorflow,Keras,Training Data,Numpy,我正在培训一个Keras Tensorflow模型,该模型有三个输入和两个输出: mymodel = tf.keras.Model([X1, X2, X3], [y1, y2]) 当我通过分别指定x和y数据来拟合此模型时,它工作正常,没有任何故障: history = mymodel.fit([X1, X2, X3], [y1, y2], batch_size=128, epochs=5) 但是,为了保持与自定义数据生成器的兼容性,我希望将培训数据作为单个元组(x,y)提供。当我这样做时,它
mymodel = tf.keras.Model([X1, X2, X3], [y1, y2])
当我通过分别指定x
和y
数据来拟合此模型时,它工作正常,没有任何故障:
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)
我想我的数据
元组格式是错误的
如何正确指定培训数据?您需要的是使用生成器或
tf.data
API构建数据管道。根据培训API的文件:
参数
- 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).
如果你不明白,请告诉我。谢谢!在这种情况下,如何使用具有numpy数组(和多个输入/输出)的数据集生成器。是一篇关于keras生成器的好文章。这是tf.dataset API的教程。这些是针对dataloader的,在模型定义的情况下,请参阅本讨论。更多参考资料可供参考。
- 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:
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).