Michael Yadidya
Michael Yadidya

Reputation: 1457

Error When Checking Target (Keras)

Hi I am following this code along from Deep learning with python Book. I am trying to train a model with pretrained word embeddings on raw IMDB dataset. I get this error how to fix this.

I followed this process

1. Processing the labels of the raw IMDB data

2. Tokenizing the text of the raw IMDB data

3. Parsing the GloVe word-embeddings file

4. Preparing the GloVe word-embeddings matrix

5. Defining my model

from keras.models import Sequential
from keras.layers import Embedding, Flatten, Dense


model = Sequential()

model.add(Embedding(max_words, embedding_dim, input_length=maxlen))

model.add(Flatten())

model.add(Dense(32, activation='relu'))

model.add(Dense(1, activation='sigmoid'))

model.layers[0].set_weights([embedding_matrix])
model.layers[0].trainable = False

model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics['acc'])
history = model.fit(x_train,y_train,epochs=10,batch_size=32,validation_data(x_val, y_val))

model.save_weights('pre_trained_glove_model.h5')

When I run it I get an error about the last dense layer:

ValueError: Error when checking target: expected dense_12 to have shape (1,) but got array with shape (100,)

When I change the shape of the Dehse layer to 100 it gives me this error:

ValueError: Error when checking target: expected dense_13 to have shape (100,) but got array with shape (1,)

How to fix this error?

It works perfectly without the pre-trained word embedding and it also gives me 86% accuracy

Upvotes: 1

Views: 112

Answers (0)

Related Questions