fchollet / deep-learning-with-python-notebooks

Jupyter notebooks for the code samples of the book "Deep Learning with Python"

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Data cardinality is ambiguous: x sizes: 55008 y sizes: 19646 Make sure all arrays contain the same number of samples.

nihayahzahra opened this issue · comments

Hello, can you help me please. When I try to check my result on a test samples. I have an error.

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=0)
y_train = np.asarray(y_train).astype('float32').reshape((-1,1))
y_test = np.asarray(y_test).astype('float32').reshape((-1,1))
X_train = np.asarray(X_train).astype('float32').reshape((-1,1))
X_test = np.asarray(X_test).astype('float32').reshape((-1,1))
X_val = np.asarray(X_val).astype('float32').reshape((-1,1))
y_val = np.asarray(y_val).astype('float32').reshape((-1,1))
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)

LSTMmodel = keras.Sequential()
LSTMmodel.add(Embedding(14301, 32, input_length=X_train.shape[1], name="embedding"))
LSTMmodel.add(Bidirectional(LSTM(64, return_sequences=True), backward_layer=LSTM(64, return_sequences=True, go_backwards=True)))
LSTMmodel.add(Dropout(0.2))
LSTMmodel.add(Bidirectional(LSTM(64)))
LSTMmodel.add(Dropout(0.2))
LSTMmodel.add(Dense(1, activity_regularizer=l2(0.002)))
LSTMmodel.add(Activation('sigmoid'))
lr_schedule = keras.optimizers.schedules.ExponentialDecay(
    initial_learning_rate=1e-2,
    decay_steps=10000,
    decay_rate=0.9)
opt = keras.optimizers.Adam(learning_rate=lr_schedule)
LSTMmodel.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
LSTMmodel.summary()
LSTMmodel.save('modelbilstm.h5')

es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=8)
mc = ModelCheckpoint('modelbilstm.h5', monitor='val_accuracy', mode='max', verbose=1, save_best_only=True)
LSTMhistory=LSTMmodel.fit(np.asarray(X_train), np.asarray(y_train) , epochs=20, batch_size=128, validation_split=0.2, verbose=0, callbacks=[es,mc])
LSTMhistory.history

What could be going wrong?