用于非图像数据的 CNN
CNN for non-image data
我正在尝试从这个 https://machinelearningmastery.com/cnn-models-for-human-activity-recognition-time-series-classification/ 示例创建一个模型,该模型将输入 3(为了消除错误,将有 1000 个)输入,这些输入是维度 (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, 10
n_timesteps, n_features, n_outputs = X_train.shape[1], X_train.shape[2], 1
model = 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) 的目标数组
但实际上它应该只需要 1 个值作为输出。
为什么我应该只取 1 个值作为输出时出现这样的错误消息?
Softmax层的大小应该等于类的个数。您的 Softmax 层只有 1 个输出。
对于这个分类问题,首先,你应该把你的目标变成one-hot encoded格式,然后编辑Softmax层的大小为类.
的数量。
我正在尝试从这个 https://machinelearningmastery.com/cnn-models-for-human-activity-recognition-time-series-classification/ 示例创建一个模型,该模型将输入 3(为了消除错误,将有 1000 个)输入,这些输入是维度 (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, 10
n_timesteps, n_features, n_outputs = X_train.shape[1], X_train.shape[2], 1
model = 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) 的目标数组 但实际上它应该只需要 1 个值作为输出。 为什么我应该只取 1 个值作为输出时出现这样的错误消息?
Softmax层的大小应该等于类的个数。您的 Softmax 层只有 1 个输出。 对于这个分类问题,首先,你应该把你的目标变成one-hot encoded格式,然后编辑Softmax层的大小为类.
的数量。