简体   繁体   English

Python 1D CNN model - model.fit() 中的错误

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

I'm trying to build a 1D CNN model by processing ECG signals to diagnose sleep apnea.我正在尝试通过处理心电图信号来诊断睡眠呼吸暂停来构建一维 CNN model。

I am using the sklearn library and encountered an error in train_test_split .我正在使用 sklearn 库并在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.任何帮助将非常感激。

I think that n_timesteps and n_features should be shape[1] and shape[2], the first dimension is your number of samples我认为n_timesteps和n_features应该是shape[1]和shape[2],第一个维度是你的样本数

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.所以,你的最后一层应该是 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)因此, trainX = tranX.reshape(trainX.shape[0], trainX.shape[1], -1) (如果还没有,请添加第三维)

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

Finally, change your model.最后,更改您的 model。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM