词向量上的 CNN 抛出输入维度错误
CNN on word vectors throws input dimension error
我有一个数据框,其中包含大约 14560 个维度为 400 的词向量。我在 20*20 中重塑了每个向量,并使用 1 个通道来应用 CNN,因此维度变为 (14560,20,20,1)
。当我尝试拟合 CNN 模型时它抛出错误。
代码:
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers import BatchNormalization
from keras.utils import np_utils
from keras import backend as K
model_cnn=Sequential()
model_cnn.add(Convolution2D(filters = 16, kernel_size = (3, 3),
activation='relu',input_shape = (20, 20,1)))
model_cnn.compile(loss='categorical_crossentropy', optimizer = 'adadelta',
metrics=["accuracy"])
model_cnn.fit(x_tr_,y_tr_,validation_data=(x_te_,y_te))
错误:
Error when checking target: expected conv2d_6 to have 4 dimensions,
but got array with shape (14560, 1). When I reshape train data to
(14560,1,20,20) still it gives error as model receives input
=(1,20,20) and required is (20,20,1).
我该如何解决?
问题
问题不仅在于 x_tr
形状,正如另一个答案中正确指出的那样,它应该是 (-1,20,20,1)
。它也是网络架构本身。如果您这样做 model_cnn.summary()
,您将看到以下内容:
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 18, 18, 16) 160
=================================================================
Total params: 160
Trainable params: 160
Non-trainable params: 0
模型的输出是等级 4:(batch_size, 18, 18, 16)
。当标签为 (batch_size, 1)
.
时无法计算损失
解决方案
正确的架构必须将卷积输出张量 (batch_size, 18, 18, 16)
重塑为 (batch_size, 1)
。可以有多种方法,这里是一个:
model_cnn = Sequential()
model_cnn.add(Convolution2D(filters=16, kernel_size=(3, 3), activation='relu', input_shape=(20, 20, 1)))
model_cnn.add(MaxPool2D(pool_size=18))
model_cnn.add(Flatten())
model_cnn.add(Dense(units=1))
model_cnn.compile(loss='sparse_categorical_crossentropy', optimizer='adadelta', metrics=["accuracy"])
总结:
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 18, 18, 16) 160
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 1, 1, 16) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 16) 0
_________________________________________________________________
dense_1 (Dense) (None, 1) 17
=================================================================
Total params: 177
Trainable params: 177
Non-trainable params: 0
请注意,我添加了最大池化以将 18x18
特征图减少到 1x1
,然后展平层以将张量压缩到 (None, 16)
,最后是密集层以输出单个价值。还要注意损失函数:它是 sparse_categorical_crossentropy
。如果你想做 categorical_crossentropy
,你必须做 one-hot 编码并且输出不是一个数字,而是 类 上的概率分布:(None, classes)
.
顺便说一下,还要检查您的验证数组是否具有有效的形状。
我有一个数据框,其中包含大约 14560 个维度为 400 的词向量。我在 20*20 中重塑了每个向量,并使用 1 个通道来应用 CNN,因此维度变为 (14560,20,20,1)
。当我尝试拟合 CNN 模型时它抛出错误。
代码:
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers import BatchNormalization
from keras.utils import np_utils
from keras import backend as K
model_cnn=Sequential()
model_cnn.add(Convolution2D(filters = 16, kernel_size = (3, 3),
activation='relu',input_shape = (20, 20,1)))
model_cnn.compile(loss='categorical_crossentropy', optimizer = 'adadelta',
metrics=["accuracy"])
model_cnn.fit(x_tr_,y_tr_,validation_data=(x_te_,y_te))
错误:
Error when checking target: expected conv2d_6 to have 4 dimensions, but got array with shape (14560, 1). When I reshape train data to (14560,1,20,20) still it gives error as model receives input =(1,20,20) and required is (20,20,1).
我该如何解决?
问题
问题不仅在于 x_tr
形状,正如另一个答案中正确指出的那样,它应该是 (-1,20,20,1)
。它也是网络架构本身。如果您这样做 model_cnn.summary()
,您将看到以下内容:
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 18, 18, 16) 160
=================================================================
Total params: 160
Trainable params: 160
Non-trainable params: 0
模型的输出是等级 4:(batch_size, 18, 18, 16)
。当标签为 (batch_size, 1)
.
解决方案
正确的架构必须将卷积输出张量 (batch_size, 18, 18, 16)
重塑为 (batch_size, 1)
。可以有多种方法,这里是一个:
model_cnn = Sequential()
model_cnn.add(Convolution2D(filters=16, kernel_size=(3, 3), activation='relu', input_shape=(20, 20, 1)))
model_cnn.add(MaxPool2D(pool_size=18))
model_cnn.add(Flatten())
model_cnn.add(Dense(units=1))
model_cnn.compile(loss='sparse_categorical_crossentropy', optimizer='adadelta', metrics=["accuracy"])
总结:
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 18, 18, 16) 160
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 1, 1, 16) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 16) 0
_________________________________________________________________
dense_1 (Dense) (None, 1) 17
=================================================================
Total params: 177
Trainable params: 177
Non-trainable params: 0
请注意,我添加了最大池化以将 18x18
特征图减少到 1x1
,然后展平层以将张量压缩到 (None, 16)
,最后是密集层以输出单个价值。还要注意损失函数:它是 sparse_categorical_crossentropy
。如果你想做 categorical_crossentropy
,你必须做 one-hot 编码并且输出不是一个数字,而是 类 上的概率分布:(None, classes)
.
顺便说一下,还要检查您的验证数组是否具有有效的形状。