[英]Keras model input: 3 arrays is not the same as a tuple of 3 tensors?
I am trying to fit my inputs to the keras model I have prepared.我正在尝试将我的输入适合我准备的 keras 模型。 The input layers of my network are:
我的网络的输入层是:
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)
And I specify the input this way:我以这种方式指定输入:
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
Then I load my data from a csv file, do the appropriate preprocessing and create a dataset object which looks like this in debug:然后我从一个 csv 文件加载我的数据,进行适当的预处理并创建一个在调试中看起来像这样的数据集对象:
Now my model compiles, all is good and then I try to fit it to the data:现在我的模型编译好了,一切都很好,然后我尝试将它拟合到数据中:
model_x.compile(loss='mse', optimizer='adam', metrics=['accuracy'])
history = model_x.fit(context_paths, epochs=20, verbose=2)
But it throws this error:但它抛出这个错误:
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected.
ValueError:检查模型输入时出错:您传递给模型的 Numpy 数组列表不是模型预期的大小。 Expected to see 3 array(s), for inputs ['input_1', 'input_2', 'input_3'] but instead got the following list of 1 arrays: []...
期望看到 3 个数组,对于输入 ['input_1', 'input_2', 'input_3'] 但得到了以下 1 个数组的列表:[]...
At this point I am not sure what is wrong, because in debug it seems that my dataset is a tuple of length 3 (the way I want it and specify as 'input') but something goes wrong.在这一点上,我不确定出了什么问题,因为在调试中,我的数据集似乎是一个长度为 3 的元组(我想要它并指定为“输入”的方式)但出了点问题。 I would appreciate any help, thank you.
我将不胜感激任何帮助,谢谢。
You built the model to get three different inputs: (path_source_token_input, path_input, path_target_token_input)
.您构建了模型以获得三个不同的输入:
(path_source_token_input, path_input, path_target_token_input)
。
You need data that is a list of 3 arrays.您需要一个包含 3 个数组的列表的数据。 One array for
path_source_token_input
, another array for path_input
, and a third array for path_target_token_input
.一个阵列用于
path_source_token_input
,另一个阵列path_input
,以及用于第三阵列path_target_token_input
。
context_paths = [array1, array2, array3]
. context_paths = [array1, array2, array3]
。
Where:在哪里:
array1.shape == (anything, MAX_CONTEXTS)
array2.shape == (anything, MAX_CONTEXTS)
array3.shape == (anything, MAX_CONTEXTS)
Don't think that the outputs are not important, you cannot fit without outputs if you didn't prepare a model with a special loss for this.不要认为输出不重要,如果您没有为此准备具有特殊损失的模型,则没有输出就无法拟合。
Just create the arrays, don't use a dataset:只需创建数组,不要使用数据集:
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.