我想对振动测量结果进行二进制分类。在一篇论文中,我找到了以下网络,我想对其进行简化复制。 (CNN-LSTM structure,LINK to the paper - download)
我现在对TimeDistributed(Conv1D())的输入有问题,找不到错误。错误消息将复制到代码下方。它说我缺少一个输入变维。
我的数据具有以下结构:
2951次测量(标签=良好)+ 3043次测量(标签=不良)= 5994个样本。
每次测量16000个测量点
一维测量
希望您能帮助我,非常感谢您的宝贵时间!
我的代码:
xGood = dfGood.values
yGood = pd.DataFrame(index=range(len(dfGood)), columns=range(1))
yGood.iloc[:len(yGood)] = 0
yGood = yGood.values
xBad = dfBad.values
yBad = pd.DataFrame(index=range(len(dfBad)), columns=range(1))
yBad.iloc[:len(yBad)] = 1
yBad = yBad.values
x = np.concatenate((xBad, xGood), axis=0)
y = np.concatenate((yBad, yGood), axis=0)
trainX, testX, trainy, testy = train_test_split(x, y, test_size=0.33, random_state=42)
trainy = to_categorical(trainy)
testy = to_categorical(testy)
n_timesteps, n_outputs = trainX.shape[1], trainy.shape[1]
trainX = np.expand_dims(trainX, axis=2)
testX = np.expand_dims(testX, axis=2)
#Model
def evaluate_model(trainX, trainy, testX, testy):
verbose, epochs, batch_size = 1, 50, 400
n_timesteps, n_outputs = trainX.shape[1], trainy.shape[1]
model = Sequential()
#CNN Model
model.add(TimeDistributed(Conv1D(filters=16, kernel_size=12001, activation='relu'), input_shape=(5994,n_timesteps, 1)))
model.add(TimeDistributed(Conv1D(filters=250, kernel_size=3985, activation='relu')))
model.add(TimeDistributed(MaxPooling1D()))
model.add(TimeDistributed(Flatten()))
#LSTM Model
model.add(LSTM(16))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit network
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)
print(model.summary())
print(accuracy)
model.save(pathModel)
return accuracy
evaluate_model(trainX, trainy, testX, testy)
我得到的错误:
ValueError:检查输入时出错:预期time_distributed_28_input具有4维,但数组的形状为(4017,16000,1)