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