Python 1D CNN model - Error in model.fit()

391 Views Asked by At

I'm trying to build a 1D CNN model by processing ECG signals to diagnose sleep apnea.

I am using the sklearn library and encountered an error in train_test_split. Here is my code:

# loading the file
with open("ApneaData.csv") as csvDataFile:
    csvReader = csv.reader(csvDataFile)
    for line in csvReader:
        lis.append(line[0].split())  # create a list of lists

# making a list of all x-variables
for i in range(1, len(lis)):
    data.append(list(map(int, lis[i])))

# a list of all y-variables (either 0 or 1)
target = Extract(data)  # sleep apn or not

# converting to numpy arrays
data = np.array(data)
target = np.array(target)

# stacking data into 3D
loaded = dstack(data)
change = dstack(target)


trainX, testX, trainy, testy = train_test_split(loaded, change, test_size=0.3)

# the model
verbose, epochs, batch_size = 0, 10, 32
n_timesteps, n_features, n_outputs = trainX.shape[0], trainX.shape[1], trainy.shape[0]
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_timesteps,n_features)))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

# fitting the model
model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size, verbose=verbose)

# evaluate model
_, accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=0)

I get the error:

ValueError: Error when checking input: expected conv1d_15_input to have shape (11627, 6001) but got array with shape (6001, 1)

I don't understand what I'm doing wrong? Any help would be much appreciated.

2

There are 2 best solutions below

0
On BEST ANSWER

First,

# a list of all y-variables (either 0 or 1)
target = Extract(data)  # sleep apn or not

This suggets you're doing a binary classification, and it seems you haven't applied one-hot-encoding. So, you last layer should be sigmoid.

the first dimension denotes number of samples. So, trainX = tranX.reshape(trainX.shape[0], trainX.shape[1], -1) (add a third dimension if not there already)

n_timesteps, n_features, n_outputs = trainX.shape[1], trainX.shape[2], 1

Finally, change your model.

model.add(Dense(n_outputs, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
0
On

I think that n_timesteps and n_features should be shape[1] and shape[2], the first dimension is your number of samples