1

我有一个多类分类问题。假设我有一个特征矩阵:

A   B C D
1  -1 1 -6
2 0.5 0 11
7 3.7 1 1
4 -50 1 0

和标签:

LABEL
0
1
2
0
2

我想尝试使用 Keras 沿每个单个特征行应用卷积核。说 nb_filter=2 和 batch_size=3。所以我希望卷积层的输入形状为 (3, 4),输出形状为 (3, 3)(因为它适用于 AB、BC、CD)。

这是我用 Keras(v1.2.1,Theano 后端)尝试过的:

def CreateModel(input_dim, num_hidden_layers):
    from keras.models import Sequential
    from keras.layers import Dense, Dropout, Convolution1D, Flatten

    model = Sequential()
    model.add(Convolution1D(nb_filter=10, filter_length=1, input_shape=(1, input_dim), activation='relu'))
    model.add(Dense(3, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam')
    model.summary()
    return model

def OneHotTransformation(y):
    from keras.utils import np_utils
    return np_utils.to_categorical(y)

X_train = X_train.values.reshape(X_train.shape[0], 1, X_train.shape[1])
X_test = X_test.values.reshape(X_test.shape[0], 1, X_test.shape[1]),
y_train = OneHotTransformation(y_train)

clf = KerasClassifier(build_fn=CreateModel, input_dim=X_train.shape[1], num_hidden_layers=1, nb_epoch=10, batch_size=500)

clf.fit(X_train, y_train)

形状:

print X_train.shape
print X_test.shape
print y_train.shape

输出:

(45561, 44)
(11391, 44)
(45561L,)

当我尝试运行此代码时,我得到了异常:

ValueError: Error when checking model target: expected dense_1 to have 3 dimensions, but got array with shape (45561L, 3L)

我试图重塑 y_train:

y_train = y_train.reshape(y_train.shape[0], 1, y_train.shape[1])

这给了我例外:

ValueError: Error when checking model target: expected dense_1 to have 3 dimensions, but got array with shape (136683L, 2L)
  1. 这种使用 Convolution1D 的方法是否正确地实现了我的目标?
  2. 如果 #1 是,我该如何修复我的代码?

我已经在这里阅读了许多 github 问题和一些问题(12),但这并没有真正帮助。

谢谢。

UPDATE1: 根据 Matias Valdenegro 的评论。以下是重塑“X”和对“y”进行 onehot 编码后的形状:

print X_train.shape
print X_test.shape
print y_train.shape

输出:

(45561L, 1L, 44L)
(11391L, 1L, 44L)
(45561L, 3L)

UPDATE2:再次感谢 Matias Valdenegro。X 重塑是在创建模型后完成的,确定这是一个复制粘贴问题。代码应如下所示:

def CreateModel(input_dim, num_hidden_layers):
    from keras.models import Sequential
    from keras.layers import Dense, Dropout, Convolution1D, Flatten

    model = Sequential()
    model.add(Convolution1D(nb_filter=10, filter_length=1, input_shape=(1, input_dim), activation='relu'))
    model.add(Dense(3, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam')
    model.summary()
    return model

def OneHotTransformation(y):
    from keras.utils import np_utils
    return np_utils.to_categorical(y)

clf = KerasClassifier(build_fn=CreateModel, input_dim=X_train.shape[1], num_hidden_layers=1, nb_epoch=10, batch_size=500)

X_train = X_train.values.reshape(X_train.shape[0], 1, X_train.shape[1])
X_test = X_test.values.reshape(X_test.shape[0], 1, X_test.shape[1]),
y_train = OneHotTransformation(y_train)

clf.fit(X_train, y_train)
4

1 回答 1

1

一维卷积的输入应该有维度(num_samples、channels、width)。所以这意味着你需要重塑 X_train 和 X_test,而不是 y_train:

X_train = X_train.reshape(X_train.shape[0], 1, X_train.shape[1])
X_test = X_test.reshape(X_test.shape[0], 1, X_test.shape[1])
于 2017-02-01T01:01:58.377 回答