1

I can unpack my RNN model onto my website, but I am having trouble getting it to predict a numpy array of predictions using a list as input (contains only one string called text but needs to be a list for preprocessing from what I've gathered) and I am coming across the problem:

ValueError: Error when checking : expected embedding_1_input to have shape (None, 72) 
but got array with shape (1, 690)

Here is how I am currently preprocessing and predicting with the model:

tokenizer = Tokenizer(num_words = 5000, split=' ')
tokenizer.fit_on_texts([text])
X = tokenizer.texts_to_sequences([text])
X = pad_sequences(X)

prediction = loadedModel.predict(X)
print(prediction)

And this is how I trained my model:

HIDDEN_LAYER_SIZE = 195         # Details the amount of nodes in a hidden layer.
TOP_WORDS = 5000                # Most-used words in the dataset.
MAX_REVIEW_LENGTH = 500         # Char length of each text being sent in (necessary).
EMBEDDING_VECTOR_LENGTH = 128   # The specific Embedded later will have 128-length vectors to
                                # represent each word.
BATCH_SIZE = 32                 # Takes 64 sentences at a time and continually retrains RNN.
NUMBER_OF_EPOCHS = 10           # Fits RNN to more accurately guess the data's political bias.
DROPOUT = 0.2                   # Helps slow down overfitting of data (slower convergence rate)

# Define the model
model = Sequential()
model.add(Embedding(TOP_WORDS, EMBEDDING_VECTOR_LENGTH, \
            input_length=X.shape[1]))
model.add(SpatialDropout1D(DROPOUT))
model.add(LSTM(HIDDEN_LAYER_SIZE))
model.add(Dropout(DROPOUT))
model.add(Dense(2, activation='softmax'))

# Compile the model
model.compile(loss='categorical_crossentropy', optimizer='adam', \
                metrics=['accuracy'])

#printModelSummary(model)

# Fit the model
model.fit(X_train, Y_train, validation_data=(X_test, Y_test), \
            epochs=NUMBER_OF_EPOCHS, batch_size=BATCH_SIZE)

How can I fix my preprocessing code in the codebox starting with "tokenizer" to stop getting the ValueError? Thank you, and I can definitely provide more code or expand upon the purpose of the project.

1 Answer 1

1

So there are two problems here:

  1. Set max_len in pad_sequences: it seems that all of your training sequences were padded to have length 72 so - you need to change the following line:

    X = pad_sequences(X, max_len=72)
    
  2. Use training Tokenizer: this is a subtle problem - you are creating and fitting a totally new Tokenizer so it could be different than one which you used for training. This could cause problems - because different words could have different indexes - and this will make your model to work awful. Try to pickle your training Tokenizer and load it during deployment in order to transform sentences into data points fed to your model properly.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.