我正在尝试根据这个https://machinelearningmastery.com/cnn-models-for-human-activity-recognition-time-series-classification/示例创建一个模型,该模型接受3个(为了调试,将会有数千个)输入,这些输入是维度为(17,40)的数组:
[[[0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0] [0 0 0 ... 5 5 5] ... [0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0]] [[0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0] ... [0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0]] [[0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0] ... [0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0]]]
输出是一个介于0到8之间的单一整数:
[[6] [3] [1]]
我使用CNN如下:
X_train, X_test, y_train, y_test = train_test_split(Xo, Yo)print("Xtrain", X_train)print("Y_train", y_train)print("Xtest", X_test)print("Y_test", y_test)print("X_train.shape[1]", X_train.shape[1])print("X_train.shape[2]", X_train.shape[2])#print("y_train.shape[1]", y_train.shape[1])verbose, epochs, batch_size = 1, 10, 10n_timesteps, n_features, n_outputs = X_train.shape[1], X_train.shape[2], 1model = Sequential()model.add(Conv1D(filters=64, kernel_size=2, activation='relu', input_shape=(n_timesteps,n_features)))model.add(Conv1D(filters=64, kernel_size=2, activation='relu'))model.add(Dropout(0.5))model.add(MaxPooling1D(pool_size=2))model.add(Flatten())model.add(Dense(10, activation='relu'))model.add(Dense(1, activation='softmax'))model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
它给我如下错误:ValueError: 您正在传递一个形状为(6, 1)的目标数组,但实际上它应该只接受一个值作为输出。为什么当它应该只接受一个值作为输出时,我会得到这样的错误信息?
回答:
Softmax层的尺寸应该等于类别的数量。您的Softmax层只有一个输出。对于这个分类问题,首先,您应该将目标转换为独热编码格式,然后将Softmax层的尺寸调整为类别数量。