[英]Correct way to specify training data as tuple (x, y) in Keras model.fit with multiple inputs and outputs
I am training a Keras Tensorflow model with three inputs and two outputs:我正在训练具有三个输入和两个输出的 Keras Tensorflow model:
mymodel = tf.keras.Model([X1, X2, X3], [y1, y2])
When I fit this model by separately specifying x
and y
data, it works fine without any hitches:当我通过分别指定
x
和y
数据来安装这个 model 时,它可以正常工作,没有任何障碍:
history = mymodel.fit([X1, X2, X3], [y1, y2], batch_size=128, epochs=5)
However, I would like to provide the training data as a single tuple (x, y) in order to maintain compatibility with a custom data generator.但是,我想将训练数据作为单个元组 (x, y) 提供,以保持与自定义数据生成器的兼容性。 When I do this, it throws an error:
当我这样做时,它会抛出一个错误:
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',...
I guess my format for the data
tuple is wrong.我猜我的
data
元组格式是错误的。
How can I correctly specify my training data?如何正确指定我的训练数据?
What you need is to build your data pipeline with a generator or tf.data
API.您需要的是使用生成器或
tf.data
API 构建数据管道。 According to the documentation of the training API, source :根据培训 API 的文档,来源:
Model.fit(
x=None,
y=None,
batch_size=None,
epochs=1,
...
Arguments 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).
FYI, but if your data is numpy array or tensorflow tensor ( x
), then you need to provide the corresponding y
.仅供参考,但如果您的数据是numpy数组或tensorflow张量(
x
),那么您需要提供相应的y
。 According to the doc根据文档
- 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.