简体   繁体   中英

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. 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. Now I want to "extend" the existing Model with a dataset that provides answers to questions related to shipping, what's in stock etc.

Here is my working/already trained 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.

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_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 . Now that you have defined this very specific input shape, all data fed into your model must have the same feature dimension size. That is the reason for your error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM