检查目标时出错:预期 dense_3 具有形状 (256,) 但得到形状为 (1,) 的数组
Error when checking target: expected dense_3 to have shape (256,) but got array with shape (1,)
我正在 Keras 中训练类似 VGG16 的模型,试图预测具有输入图像的连续/事件发生时间值(回归),但遇到以下错误:
Error when checking target: expected dense_3 to have shape (256,) but
got array with shape (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 损失,因为它是一个回归问题。
我正在 Keras 中训练类似 VGG16 的模型,试图预测具有输入图像的连续/事件发生时间值(回归),但遇到以下错误:
Error when checking target: expected dense_3 to have shape (256,) but got array with shape (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 损失,因为它是一个回归问题。