我正在Keras中训练类似于VGG16的模型,试图预测具有作为输入图像的继续/事件发生时间的值(回归),并遇到以下错误:
检查目标时出错:预期density_3的形状为(256,),但
得到形状为(1,)的数组
这是模型的结构:
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 256, 256, 64) 1792
_________________________________________________________________
batch_normalization_1 (Batch (None, 256, 256, 64) 256
_________________________________________________________________
.
.
.
_________________________________________________________________
conv2d_16 (Conv2D) (None, 16, 16, 512) 2359808
_________________________________________________________________
batch_normalization_16 (Batc (None, 16, 16, 512) 2048
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 8, 8, 512) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 32768) 0
_________________________________________________________________
dense_1 (Dense) (None, 1000) 32769000
_________________________________________________________________
dense_2 (Dense) (None, 1000) 1001000
_________________________________________________________________
dense_3 (Dense) (None, 256) 256256
Total params: 54,072,656
Trainable params: 54,061,648
Non-trainable params: 11,008
我试图在最后只用一个神经元添加另一层,它似乎可以这样工作,但我认为这不是正确的方法。
我已经读过类似的问题,但是我没有找到解决方法。
下面的代码构建的模型的最后一层
#Convolution Layer
#input: 64x64x128, image with 128 channels, appy 256 convolution filters
model.add(Conv2D(512, kernel_size=3, activation='relu',padding='same' ))
#the output of the layer above is 64x64x256
#Normalization layer
model.add(BatchNormalization())
#Convolution Layer
#input: 64x64x128, image with 128 channels, appy 256 convolution filters
model.add(Conv2D(512, kernel_size=3, activation='relu',padding='same' ))
#the output of the layer above is 64x64x256
#Normalization layer
model.add(BatchNormalization())
#Max-Pooling
#poolsize:(2,2), factors by which to downscale (vertical, horizontal)
model.add(MaxPooling2D(pool_size=(2,2), dim_ordering="tf"))
#Flatten layer
model.add(Flatten())
#Fully connected layer
#number of neurons is chosen randomly
model.add(Dense(1000, activation='relu'))
#Fully connected layer
model.add(Dense(1000, activation='relu'))
#Fully connected layer
model.add(Dense(256, activation='softmax'))
model.summary()
#Compile model
model.compile(loss='categorical_crossentropy', optimizer='adagrad')
我也不确定在预测事件发生时间值时应该使用哪个损失函数。
最佳答案
model.add(Dense(256, activation='relu'))
model.add(Dense(1, activation='linear'))
model.compile(loss='mean_squared_error', optimizer='adagrad')
将最后一层更改为relu激活,最后添加一个线性激活层。使用MSE损失,因为这是一个回归问题。
关于python - 检查目标时出错:预期density_3的形状为(256,),但形状为(1,),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56163701/