简体   繁体   English

用于训练多输入功能的几个 x 输入的格式 keras model

[英]Format of several x inputs for training multi input functional keras model

So I am currently trying to understand what formats a multi input keras model expect and don´t understand how to feed in several ones.因此,我目前正在尝试了解多输入 keras model 期望并且不了解如何输入多个输入的格式。

from tensorflow.keras.models import Model
import tensorflow.keras
import tensorflow as tf

first_input = Input(2)
second_input = Input(2)
concat_layer= Concatenate()([first_input, second_input ])
hidden= Dense(2, activation="relu")(concat_layer)
output = Dense(1, activation="sigmoid")(hidden)
model = Model(inputs=[first_input, second_input], outputs=output)
model.summary()
model.compile(loss='mean_squared_error', metrics=['mean_squared_error'], optimizer='adam')

# I managed to get the format for prediction and single training data correct
# this works
inp = [np.array([[0,2]]), np.array([[0,2]])]
model.predict(inp)
model.fit(inp,np.array([42]), epochs=3, )

# I don´t get why this isn´t working
# this doesn´t work
model.fit(np.array([inp,inp]),np.array([42, 43]), epochs=3, )´

Having read the keras doc of the fit function I really don´t understand why my version isn´t working:阅读了适合 function 的 keras 文档后,我真的不明白为什么我的版本不起作用:

x: Vector, matrix, or array of training data (or list if the model has multiple inputs). x:向量、矩阵或训练数据数组(如果 model 有多个输入,则为列表)。 If all inputs in the model are named, you can also pass a list mapping input names to data.如果 model 中的所有输入都已命名,您还可以传递将输入名称映射到数据的列表。 x can be NULL (default) if feeding from framework-native tensors (eg TensorFlow data tensors).如果从框架原生张量(例如 TensorFlow 数据张量)馈送,x 可以是 NULL(默认)。

Because I am literally giving it an array of lists.因为我实际上是在给它一个列表数组。

The last code line results in following error:最后一行代码导致以下错误:

ValueError: Layer model expects 2 input(s), but it received 1 input tensors. ValueError:model 层需要 2 个输入,但它接收到 1 个输入张量。 Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, 2, 1, 2) dtype=int64>]收到的输入:[<tf.Tensor 'IteratorGetNext:0' shape=(None, 2, 1, 2) dtype=int64>]

Any help appreciated.任何帮助表示赞赏。

When you create the model当您创建 model

model = Model(inputs=[first_input, second_input], outputs=output)

This means that the inputs are expected to be a list of 2 tensors with shapes (2,) and one output with shape (1,) (as defined by the last Dense layer).这意味着输入应该是一个包含 2 个形状 (2,) 的张量和一个具有形状 (1,) 的 output 的列表(由最后一个 Dense 层定义)。

So, when you use as arguments:因此,当您用作 arguments 时:

inp = [np.array([[0,2]]), np.array([[0,2]])]

This is a list with 2 arrays of shape (1, 2)这是一个包含 2 个形状为 (1, 2) 的 arrays 的列表

And

np.array([42])

which is an array with shape (1)这是一个形状为 (1) 的数组

This matches your model definition.这与您的 model 定义相匹配。 [Actually the output should have a shape of (1, 1)] [其实output应该是(1, 1)的形状]

The line线

model.fit(np.array([inp,inp]),np.array([42, 43]))

Is trying to feed an a list of lists of arrays with an overall shape of [2, 2, 1, 2] and a target with shape [2].正在尝试提供一个整体形状为 [2, 2, 1, 2] 的 arrays 列表和形状为 [2] 的目标列表。 This doesn't match the model definition.这与 model 定义不匹配。

Since it is easy to get this wrong, i tend to like to build the arguments to the.fit or predict call first and then explicitly print their shapes...由于很容易出错,我倾向于先将 arguments 构建到 .fit 或 predict 调用,然后显式打印它们的形状......

eg例如

x_train = [a, b] # where a and b are np.arrays
print([x.shape for x in x_train])

For instance try:例如尝试:

x_first = np.random.rand(8, 2) # batch_size 8, feature_size 2
x_second = np.random.rand(8, 2)
x_train = [a, b]

y_true = np.random.rand(8)

model.fit(x_train, y_true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为功能性keras模型格式化具有多个类别的多个输入,并将其输入模型 - Format mutiple inputs with mutiple categories for a functional keras model and feed it to the model 多输入多输出 Model 与 Keras 功能 API - Multi-input Multi-output Model with Keras Functional API Keras 功能 api 多输入:传递给 model 的输入列表是冗余的 - Keras functional api multiple input: The list of inputs passed to the model is redundant Keras model 在训练期间有 2 个输入,但在推理期间只有 1 个输入 - Keras model with 2 inputs during training, but only 1 input during inference 使用多个输入训练 Keras 模型 - Training Keras model with multiple inputs Keras中的输入整形和模型训练 - Input shaping in Keras and model training 训练集包含“标签”作为keras模型的输入 - Training set contains “labels” as inputs to keras model Keras 自定义数据生成器提供多输入和多输出的维度错误(功能 api 模型) - Keras custom data generator giving dimension errors with multi input and multi output( functional api model) 在 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模型 - training a multi-output keras model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM