0

我正在尝试通过处理心电图信号来诊断睡眠呼吸暂停来构建一维 CNN 模型。

我正在使用 sklearn 库并在train_test_split. 这是我的代码:

# 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)

我得到错误:

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

我不明白我做错了什么?任何帮助将非常感激。

4

2 回答 2

1

我认为n_timesteps和n_features应该是shape[1]和shape[2],第一个维度是你的样本数

于 2020-05-08T13:06:30.570 回答
0

第一的,

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

这表明您正在执行二进制分类,并且您似乎没有应用单热编码。所以,你的最后一层应该是 sigmoid。

第一个维度表示样本数。所以, trainX = tranX.reshape(trainX.shape[0], trainX.shape[1], -1) (如果还没有,请添加第三个维度)

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

最后,改变你的模型。

model.add(Dense(n_outputs, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
于 2020-05-08T14:13:03.133 回答