简体   繁体   English

Keras 模型输入:3 个数组与 3 个张量的元组不一样?

[英]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.不要认为输出不重要,如果您没有为此准备具有特殊损失的模型,则没有输出就无法拟合。


Data process数据处理

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.

相关问题 Keras - TypeError:模型的输出张量必须是Keras张量 - 同时建模多输入,多输出网络 - Keras - TypeError: Output tensors to a Model must be Keras tensors - while modelling multiple input , multiple output network 在带有 Tensorflow 张量的 Keras 模型中使用 InputLayer(或 Input)有什么好处? - What is the advantage of using an InputLayer (or an Input) in a Keras model with Tensorflow tensors? 为模型创建 Keras 模型输入张量的问题必须来自`keras.layers.Input`? - Issue in creating Keras Model Input tensors to a Model must come from `keras.layers.Input`? TypeError:模型的输出张量必须是Keras张量 - TypeError: Output tensors to a Model must be Keras tensors 模型的输出张量必须是Keras张量 - Output tensors to a Model must be Keras tensors Keras model.fit ValueError:输入数组的样本数应与目标数组相同 - Keras model.fit ValueError: Input arrays should have the same number of samples as target arrays 在处理 keras 功能 model - Keras、ZCB20B802A3F21255E054E488 的输入张量时发现意外实例 - Found unexpected instance while processing input tensors for keras functional model - Keras, Tensorflow 将数组元组转换为张量,然后将它们堆叠在 pytorch 中 - Convert tuple of arrays into tensors to then stack them in pytorch keras - 层模型_131 需要 7 个输入,但它收到 1 个输入张量 - keras - Layer model_131 expects 7 input(s), but it received 1 input tensors Python Keras Model -- ValueError: Layer sequential expects 1 input(s), but it received 16 input tensors - Python Keras Model -- ValueError: Layer sequential expects 1 input(s), but it received 16 input tensors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM