简体   繁体   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

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:当我通过分别指定xy数据来安装这个 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 array or tensor ( x ), then you need to provide the corresponding y .仅供参考,但如果您的数据是数组或张量( 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.

相关问题 当训练数据是图像时,Keras model.fit() 中的“批次”是什么 - what is a “batch” in Keras model.fit() when training data are images Keras:model.fit 中的详细(值 1)显示较少的训练数据 - Keras: verbose (value 1) in model.fit shows less training data 如何将 Keras 中的多个输入的标签赋予 model.fit() function? - How to give labels of multiple inputs in Keras to model.fit() function? Tensorflow Keras:在 model.fit() 上进行训练 - Tensorflow Keras: Training Holting on model.fit() 如何打印在 Tensorflow 训练 model.fit(X, Y) 期间使用的每个 x, y 对? - How to print every x, y pair used during a Tensorflow training model.fit(X, Y)? 如何将自定义数据生成器输入到 model.fit 中,它会生成 X,y 和一个额外的数组,到 tensorflow.keras Z20F4ZF011622Z Z20F4ZF35E630DAF39466 - How to input custom data generator into model.fit, which generates X,y and one additional array, into tensorflow.keras model? Keras:model.fit()在siamese_model中出现多个输入错误 - Keras: model.fit() getting error for multiple inputs in siamese_model 在Keras中调用model.fit输入不同形状的输入吗? - Call model.fit in Keras for inputs of different shapes? 使用多个输入训练 Keras 模型 - Training Keras model with multiple inputs In Tensorflow.keras 2.0, when a model has multiple outputs, how to define a flexible loss function for model.fit()? - In Tensorflow.keras 2.0, when a model has multiple outputs, how to define a flexible loss function for model.fit()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM