【问题标题】:Keras convolution along single feature row沿单个特征行的 Keras 卷积
【发布时间】:2017-06-17 04:49:23
【问题描述】:

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

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),但并没有真正帮助。

谢谢。

更新1: 根据 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)

【问题讨论】:

    标签: neural-network theano keras conv-neural-network keras-layer


    【解决方案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])
    

    【讨论】:

    • 正如您在我的问题中看到的那样,我尝试不重塑 y_train。它也会抛出异常。
    • @shda 您问题中的形状与重塑后的预期形状不匹配,您确定它们是正确的吗?
    • 抱歉,这些是整形前的形状。我会更新我的问题。
    • @shda 确保他输入的 dims 正确,应该是 (1, 44)。
    • 我应该在哪里检查? CreateModel 中的 input_dim 变量在运行时等于 44。所以 Convolution1D 的 input_shape 是 (1, 44)。
    猜你喜欢
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 2011-07-10
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    • 2020-12-02
    相关资源
    最近更新 更多