![](/img/trans.png)
[英]Keras - TypeError: Output tensors to a Model must be Keras tensors - while modelling multiple input , multiple output network
[英]Keras model input: 3 arrays is not the same as a tuple of 3 tensors?
我正在尝试将我的输入适合我准备的 keras 模型。 我的网络的输入层是:
path_source_token_input = Input(shape=(MAX_CONTEXTS,), dtype=tf.int32)
path_input = Input(shape=(MAX_CONTEXTS,), dtype=tf.int32)
path_target_token_input = Input(shape=(MAX_CONTEXTS,), dtype=tf.int32)
我以这种方式指定输入:
inputs = (path_source_token_input, path_input, path_target_token_input)
model = tf.keras.Model(inputs=inputs, outputs=learned) # outputs not important at this point
然后我从一个 csv 文件加载我的数据,进行适当的预处理并创建一个在调试中看起来像这样的数据集对象:
现在我的模型编译好了,一切都很好,然后我尝试将它拟合到数据中:
model_x.compile(loss='mse', optimizer='adam', metrics=['accuracy'])
history = model_x.fit(context_paths, epochs=20, verbose=2)
但它抛出这个错误:
ValueError:检查模型输入时出错:您传递给模型的 Numpy 数组列表不是模型预期的大小。 期望看到 3 个数组,对于输入 ['input_1', 'input_2', 'input_3'] 但得到了以下 1 个数组的列表:[]...
在这一点上,我不确定出了什么问题,因为在调试中,我的数据集似乎是一个长度为 3 的元组(我想要它并指定为“输入”的方式)但出了点问题。 我将不胜感激任何帮助,谢谢。
您构建了模型以获得三个不同的输入: (path_source_token_input, path_input, path_target_token_input)
。
您需要一个包含 3 个数组的列表的数据。 一个阵列用于path_source_token_input
,另一个阵列path_input
,以及用于第三阵列path_target_token_input
。
context_paths = [array1, array2, array3]
。
在哪里:
array1.shape == (anything, MAX_CONTEXTS)
array2.shape == (anything, MAX_CONTEXTS)
array3.shape == (anything, MAX_CONTEXTS)
不要认为输出不重要,如果您没有为此准备具有特殊损失的模型,则没有输出就无法拟合。
只需创建数组,不要使用数据集:
def loadFile(filename):
with open(filename, 'r') as file:
lines = file.readlines()
triplets = [l.split(" ") for l in lines] #(16000, 430)
singles = [[t.split(',') for t in line] for line in triplets] #(16000, 430, 3)
data = np.array(singles).astype(np.int32)
data_source = data[:,:,0]
data_path = data[:,:,1]
data_target = data[:,:,2]
return [data_source, data_path, data_target]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.