0

I'm trying to make a binary Classification by combining CNN (con1D) with GRU. my dataset dataset is like that :

X_train shape : (223461, 5)

y_train shape :(223461,)

the X_train is like that and the Y_train is a labels (0,1) like that

first I convert that train dataset :

dataset = X_train.values
dataset=dataset[1:]
dataset = dataset.astype('float32')
dataset

the same for y-train:

dataset_target = y_train.values
dataset_target=dataset_target[1:]
dataset_target = dataset_target.astype('float32')
dataset_target

now the shapes are dataset.shape =(223460, 5) , dataset_target.shape = (223460,)

than my model structure is :

verbose, epochs, batch_size = 0, 100, 64
n_timesteps, n_features, n_outputs = dataset.shape[0], dataset.shape[1],          dataset_target.shape[0]
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=   (n_timesteps,n_features)))
model.add(MaxPooling1D(pool_size=2))
model.add(GRU(64))
model.add(Dropout(0.4))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(1, activation='sigmoid'))
opt = Adam(learning_rate=0.01)
model.compile(loss=tf.keras.losses.BinaryCrossentropy(), optimizer=opt , metrics=['accuracy'])
model.summary()

and when I want to fit dataset to my model:

# fit network
model.fit(dataset, dataset_target, epochs=epochs, batch_size=batch_size, verbose=1)
# evaluate model
_, accuracy = model.evaluate(X_test, y_test, batch_size=batch_size, verbose=1)

#accuracy

I get an error Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 223460, 5), found shape=(64, 5)

MexcelsiorB
  • 29
  • 1
  • 5

1 Answers1

0

Is the first axis of the dataset (233460 samples) actually time steps, and do you have 5 'channels' of data? In that case, it would help if you slice the dataset along the first axis and then assign to each 'slice' single label, for example, the last value related to the slice from the y_train. In that case, n_timesteps would be the length of the slice, and the shape of the dataset something like (n_samples, n_timesteps, 5). Basically, Conv1D expects each training sample to be 2D, but in you case it's 1D, because the first dimension is just a number of samples.

I might have interpreted the dataset the wrong way. In that case, please clarify how it works so I would fix my suggestion.

Here's the example:

import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras.layers import Conv1D, MaxPooling1D, GRU, \
  Dropout, Flatten, Dense
from tensorflow.keras import Sequential
from tensorflow.keras.optimizers import Adam
import numpy as np


X_train = np.random.normal(0, 1, (223461, 5))
y_train = np.random.randint(0, 2, 223461)

dataset = X_train[1:]
dataset_target = y_train[1:]

n_timesteps = 10

# Slice dataset and target 
dataset = np.stack(np.split(dataset, n_timesteps)[:-1])
dataset_target = np.stack([y[-1] for y in np.split(dataset_target, n_timesteps)[:-1]])

Define and train the model:

def get_model(dataset, n_timesteps):
  verbose, epochs, batch_size = 0, 100, 64
  n_timesteps, n_features = dataset.shape[1], dataset.shape[2]
  model = Sequential()
  model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape = (n_timesteps, n_features)))
  model.add(MaxPooling1D(pool_size=2))
  model.add(GRU(64))
  model.add(Dropout(0.4))
  model.add(Flatten())
  model.add(Dense(128, activation='relu'))
  model.add(Dropout(0.4))
  model.add(Dense(1, activation='sigmoid'))
  opt = Adam(learning_rate=0.01)
  model.compile(loss=tf.keras.losses.BinaryCrossentropy(), optimizer=opt , metrics=['accuracy'])
  model.summary()
  return model


verbose, epochs, batch_size = 0, 1, 64
model = get_model(dataset, n_timesteps)
model.fit(dataset, dataset_target, epochs=epochs, batch_size=batch_size, verbose=1)

Hope it helps!

Mikhail Stepanov
  • 3,680
  • 3
  • 23
  • 24
  • First I want to thank you for your response. For the dataset that I used I explain it here [here in this image](https://imgur.com/a/jCr380i) i build a model based on this [architecture](https://imgur.com/a/99P5m6S) to make a binary classification ["0" for "Delay-insensitive", "1" for "Interactive"] using 5 features. The target column is vmcategory. You can check the my colab [here](https://colab.research.google.com/drive/1BSdnj_1TtNSrZKJSjNAUmWvW5n2Cyobr?usp=sharing) please. – MexcelsiorB May 20 '22 at 14:40