I'm trying to classify IMDB movie reviews with binary classification using Keras. The following is the code I used.

from keras import models
from keras import layers

model = models.Sequential()
model.add(layers.Dense(16,activation="relu",input_shape=(10000,)))
model.add(layers.Dense(16,activation="relu"))
model.add(layers.Dense(1,activation="sigmoid"))

model.compile(optimizer="rmsprop",loss="binary_crossentropy", metrics=["acc"])

history = model.fit(partial_x_train,partial_y_train, epochs=20, batch_size=512, validation_data = (x_val, y_val))

shapes of each input tensors are as follows.

print(partial_x_train.shape) --> (15000, 10000)
print(partial_y_train.shape)--> (15000, 10000)
print(x_val.shape) --> (10000, 10000)
print(y_val.shape) --> (10000, 10000)

But on executing the above program, I get the following error.

ValueError: in user code:
ValueError: logits and labels must have the same shape ((None, 1) vs (None, 10000))

I searched through a lot of SO questions, but couldn't understand what I have done wrong. Can someone help me to avoid this error and make the model compiled?

1

There are 1 best solutions below

0
On

As stated ValueError, you're trying to compute the loss of between shape ((None, 1) vs (None, 10000)). It would be clear if you posted or refer the training set of IMDB. Try with in-built IMDB data set from keras.

import numpy as np
from tensorflow import keras
from tensorflow.keras import layers

max_features = 20000  # Only consider the top 20k words
maxlen = 200  # Only consider the first 200 words of each movie review

(x_train, y_train), (x_val, y_val) = keras.datasets.imdb.load_data(
    num_words=max_features
)

print(len(x_train), "Training sequences")
print(len(x_val), "Validation sequences")

x_train = keras.preprocessing.sequence.pad_sequences(x_train, maxlen=maxlen)
x_val = keras.preprocessing.sequence.pad_sequences(x_val, maxlen=maxlen)

x_train.shape, y_train.shape
# ((25000, 200), (25000,))

According to your model

model = models.Sequential()
model.add(layers.Dense(16,activation="relu",input_shape=(maxlen,)))
model.add(layers.Dense(16,activation="relu"))
model.add(layers.Dense(1,activation="sigmoid"))

model.compile(optimizer="rmsprop",loss="binary_crossentropy", metrics=["acc"])
model.fit(x_train, y_train, batch_size=32, epochs=2, validation_data=(x_val, y_val))
Epoch 1/2
782/782 [==============================] - 5s 4ms/step - loss: 164.2350 - acc: 0.5018 - val_loss: 1.0527 - val_acc: 0.5000
Epoch 2/2
782/782 [==============================] - 3s 4ms/step - loss: 1.0677 - acc: 0.4978 - val_loss: 0.7446 - val_acc: 0.5000