简体   繁体   English

ValueError:“顺序”层的输入 0 与该层不兼容

[英]ValueError: Input 0 of layer "sequential" is incompatible with the layer

I am currently implementing my first project using keras/tensorflow, and so far it works pretty good, but now I ran into an error I don't know how to solve.我目前正在使用 keras/tensorflow 实现我的第一个项目,到目前为止它运行良好,但现在我遇到了一个我不知道如何解决的错误。 I googled a lot and found lots of relating questions, but none solved my issue.我在谷歌上搜索了很多,发现了很多相关问题,但没有一个能解决我的问题。

Here is what I want to achieve:这是我想要实现的目标:

I have a model for a chatbot that I trained with a dataset to provide "standard" conversations like hello, how are u and stuff.我有一个聊天机器人 model,我用数据集对其进行了训练,以提供“标准”对话,例如你好,你好吗等等。 Now I want to "extend" the existing Model with a dataset that provides answers to questions related to shipping, what's in stock etc.现在我想用一个数据集“扩展”现有的 Model,该数据集提供与运输、库存等相关问题的答案。

Here is my working/already trained model:这是我的工作/已经训练过的 model:

# create Sequential model
    model = Sequential()

    # add first layer with input shape dependent on size of input and "relu" activation function
    model.add(Dense(256, input_shape=(len(training_data_x[0]),), activation=activations.relu))

    # add Dropout to prevent overfitting
    model.add(Dropout(0.6))
    # additional layer with 64 neurons
    model.add(Dense(128, activation=activations.relu))
    model.add(Dropout(0.2))
    # Additional dense layer with num of neurons of classes & softmax activation function
    # -> adds results in output layer to "1" to get %
    model.add(Dense(len(training_data_y[0]), activation=activations.softmax))
    # print(len(training_data_y[0])) = 71
    sgd = SGD(learning_rate=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    # compile the model
    model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])

    output = model.fit(np.array(training_data_x), np.array(training_data_y), epochs=200, batch_size=5, verbose=1)
    plot_model_output(output)
    model.summary()
    model.save('./MyModel_tf', save_format='tf')

The training data is prepared in a separate class and takes a certain json file as input.训练数据在单独的class中准备,并以某个json文件作为输入。

Now I just swapped the JSON file for the one with data related to the stuff I want to add to the model and tried fitting it like this:现在我只是将 JSON 文件交换为包含与我要添加到 model 的内容相关的数据的文件,并尝试像这样安装它:

json_data = json.loads(open('data.json').read())

model = load_model('MyModel_tf')

model.fit(np.array(training_data_x), np.array(training_data_y), epochs=200, batch_size=5, verbose=1)

However when I run it I get this error:但是,当我运行它时出现此错误:

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 652), found shape=(None, 71)

I am assuming the data is the problem.. however it is structured exactly the same just way shorter.我假设数据是问题所在。但是它的结构完全相同,只是更短。

Now my question(s):现在我的问题:

  1. does it make sense the way I try to implement it?我尝试实施它的方式有意义吗?
  2. should I try adding the additional data in a different way?我应该尝试以不同的方式添加额外的数据吗?
  3. Does the second dataset have to be the same length as the first one?第二个数据集是否必须与第一个数据集的长度相同?

Any help appreciated!任何帮助表示赞赏!

The problem is probably coming from this line问题可能出在这一行

model.add(Dense(256, input_shape=(len(training_data_x[0]),), activation=activations.relu))

where you define the input shape of your model based on the size of the feature dimension of training_data_x .您根据training_data_x的特征维度的大小定义 model 的输入形状。 Now that you have defined this very specific input shape, all data fed into your model must have the same feature dimension size.现在您已经定义了这个非常具体的输入形状,输入 model 的所有数据必须具有相同的特征维度大小。 That is the reason for your error.这就是你错误的原因。

暂无
暂无

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

相关问题 "ValueError: Input 0 of layer "sequential" is in compatible with the layer" 在预测中 - "ValueError: Input 0 of layer "sequential" is incompatible with the layer" In prediction ValueError: 层序列 1 的输入 0 与层不兼容 - ValueError: Input 0 of layer sequential_1 is incompatible with the layer TensorFlow ValueError:层顺序的输入0与层不兼容 - TensorFlow ValueError: Input 0 of layer sequential is incompatible with the layer ValueError: 层序号_2 的输入 0 与层不兼容 - ValueError: Input 0 of layer sequential_2 is incompatible with the layer ValueError: 层序号_3 的输入 0 与层不兼容: - ValueError: Input 0 of layer sequential_3 is incompatible with the layer: 构建简单的神经网络:ValueError: Input 0 of layer sequence is in compatible with the layer - Building a simple Neural Network: ValueError: Input 0 of layer sequential is incompatible with the layer 无法解决 ValueError: 层序贯_1 的输入 0 与层不兼容 - Cannot solve ValueError: Input 0 of layer sequential_1 is incompatible with the layer ValueError: 层序号_40 的输入 0 与层不兼容 - ValueError: Input 0 of layer sequential_40 is incompatible with the layer ValueError:顺序层的输入 0 与层不兼容(神经网络) - ValueError: Input 0 of layer sequential is incompatible with the layer (Neural Networks) ValueError: Input 0 of layer "sequential_8" is in compatible with the layer - 深度学习 model - ValueError: Input 0 of layer "sequential_8" is incompatible with the layer - deep learning model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM