单词向量上的CNN会抛出输入维度错误

时间:2018-04-14 19:57:28

标签: machine-learning neural-network keras word2vec convolutional-neural-network

我有一个数据帧,大约有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))

错误:

  

检查目标时出错:预期conv2d_6有4个维度,   但得到了阵形(14560,1)。当我重新训练火车数据时   (14560,1,20,20)当模型接收输入时仍会出错   =(1,20,20),要求是(20,20,1)。

我该如何解决?

1 个答案:

答案 0 :(得分:1)

问题

问题不仅在于public IActionResult ImageDownload() { HttpContext.JsReportFeature().Recipe(Recipe.PhantomPdf) .Configure((r) => r.Template.Phantom = new Phantom { Format = PhantomFormat.A4, Orientation = PhantomOrientation.Portrait }).OnAfterRender( (r) => { var streamIo = r.Content; // streamIo is of type System.IO using(var fs = System.IO.File.OpenWrite("C:GeneratedReports\\myReport.pdf")) { streamIo.CopyTo(fs); } streamIo.Seek(0, SeekOrigin.Begin); } ); var dp = new Classes.DataProvider(); var lstnames = dp.GetRegisteredNames(); var lst = lstnames.ToArray<string>(); return View("Users", lst); } 形状,而且应该是x_tr,如另一个答案中正确指出的那样。它也是网络架构本身。如果您执行(-1,20,20,1),则会看到以下内容:

model_cnn.summary()

模型的输出是等级4:Layer (type) Output Shape Param # ================================================================= conv2d_1 (Conv2D) (None, 18, 18, 16) 160 ================================================================= Total params: 160 Trainable params: 160 Non-trainable params: 0 。当标签为(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"])

请注意,我添加了max-pooling以将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

顺便说一下,还要检查你的验证数组是否有效。