I am trying to do a binary classification using Keras LSTM.
My input data is of shape 2340 records * 254 features.
The output is 1*2340.
Below is my code.
X_res = np.array(X_res)
X_res = np.reshape(X_res,([1,2340,254]))
y_res = np.array(y_res)
y_res = np.reshape(y_res,([1,2340]))
y_test = np.array(y_test)
y_test = np.reshape(y_test,([1,314]))
model = keras.Sequential()
model.add(keras.layers.LSTM(32 ,input_dim = 254,return_sequences=True))
model.add(keras.layers.LSTM(100))
model.add(keras.layers.Dense(100, activation='sigmoid'))
model.add(keras.layers.Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=[tf.keras.metrics.Recall()])
model.fit(X_res, y_res, epochs= 5)
However I am unable to go past the error:
ValueError: logits and labels must have the same shape ((None, 1) vs (None, 2340))
You need to change the last layer to match the desired output shape (from 1 to 2340). By running the example code below you see how it work in practice:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np
#let's simulate...
X_res=np.random.rand(2340,254)
y_res=np.random.rand(1,2340)
y_test=np.random.rand(1,314)
X_res = np.array(X_res)
X_res = np.reshape(X_res,([1,2340,254]))
y_res = np.array(y_res)
y_res = np.reshape(y_res,([1,2340]))
y_test = np.array(y_test)
y_test = np.reshape(y_test,([1,314]))
model = keras.Sequential()
model.add(keras.layers.LSTM(32 ,input_dim = 254,return_sequences=True))
model.add(keras.layers.LSTM(100))
model.add(keras.layers.Dense(100, activation='sigmoid'))
model.add(keras.layers.Dense(2340, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=[tf.keras.metrics.Recall()])
model.summary()
#test a littlebit before training...
test_input=tf.ones((1,2340,254))
print("Test result of original model: ",model(test_input).numpy())
#train...
model.fit(X_res, y_res, epochs= 5)
#test after training...just to show the shapes of inputs and outputs. Change the number of inputs you want to test to see the effect...
how_many_inputs_you_want_to_test=1
X_test=np.random.rand(how_many_inputs_you_want_to_test,2340,254)
print("Input shape: ", X_test.shape)
y_pred = (model.predict(X_test) > 0.5).astype("int32")
print("Output shape: ",y_pred.shape)