I am building a NN model using keras and tensorflow for time series binary classification. This is how my input looks like, with a shape of (124,4,591):
| Col 1 | Col 2 | Col 3 | Col 4 |
Row 1 | [x1, ..., x591] | [x1, ..., x591] | [x1, ..., x591] | [x1, ..., x591] |
Row 2 | [x1, ..., x591] | [x1, ..., x591] | [x1, ..., x591] | [x1, ..., x591] |
Row 3 | [x1, ..., x591] | [x1, ..., x591] | [x1, ..., x591] | [x1, ..., x591] |
I split my data into X_train
, X_test
, y_train
and y_test
. I also encoded my labels from ['True', 'False']
into [0, 1]
using LabelEncoder()
and OneHotEncoder()
.
x = np.stack((np.vstack(x[:,0]),np.vstack(x[:,1]),np.vstack(x[:,2]),np.vstack(x[:,3])))
x = x.reshape((124,4,591))
y = table_raw_ptpt['Binding Known']
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 0)
X_train.shape
returns (86, 4, 591).
Encoding for labels:
label_encoder = LabelEncoder()
integer_encoded_train = label_encoder.fit_transform(array(y_train))
integer_encoded_test = label_encoder.fit_transform(array(y_test))
onehot_encoded_y_train = OneHotEncoder(sparse=False)
integer_encoded_train = integer_encoded_train.reshape(len(integer_encoded_train), 1)
onehot_encoded_y_train = onehot_encoded_y_train.fit_transform(integer_encoded_train)
onehot_encoded_y_test = OneHotEncoder(sparse=False)
integer_encoded_test = integer_encoded_test.reshape(len(integer_encoded_test), 1)
onehot_encoded_y_test = onehot_encoded_y_test.fit_transform(integer_encoded_test)
onehot_encoded_y_train.shape
returns (86, 2).
Here is my NN:
model = Sequential()
model.add(Dense(86, activation='relu', input_shape=(4,591)))
model.add(Dense(43, activation='relu'))
model.add(Dense(20, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss = 'binary_crossentropy',
optimizer = 'adam',
metrics = ['accuracy'])
model.summary()
It works. But when I try to fit the X_train I get an error:
Error when checking target: expected dense_227 to have shape (1,) but got array with shape (2,)
The mentioned layer is the output layer. As far as I understood, the output shape is incorrect. I tried using Flattern()
in between the layers, and even tried a Reshape(1,)
. But since I'm a beginner at this, I don't fully understand what I have to add to control my data shape in the NN in order to obtain the output I need.
I managed to make it work with softmax
, but I need the sigmoid
to work too so I can have a final prediction afterward (True or False / 1 or 0).
Thank you.