首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查目标时出错:期望dense_3具有形状(256个),但得到形状为(1 )的数组

检查目标时出错:期望dense_3具有形状(256个),但得到形状为(1 )的数组
EN

Stack Overflow用户
提问于 2019-05-16 08:02:03
回答 1查看 70关注 0票数 1

我正在Keras中训练一个类似VGG16 16的模型,试图预测具有输入映像的连续/时间到事件值(回归),并遇到以下错误:

检查目标时出错:期望dense_3具有形状(256个),但得到形状为(1 )的数组

这是模型的结构:

代码语言:javascript
复制
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

我试着在最后添加另一个层,只有一个神经元,它似乎是这样工作的,但我认为这不是正确的方法。我曾读到过类似的问题,但我未能找到解决办法。

下面代码构建的模型的最后几层

代码语言:javascript
复制
#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')

我也不确定在预测时间对事件值的情况下,应该使用哪种损失函数。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-16 08:18:12

代码语言:javascript
复制
model.add(Dense(256, activation='relu'))
model.add(Dense(1, activation='linear'))
model.compile(loss='mean_squared_error', optimizer='adagrad')

将最后一层更改为重新激活,最后添加一个线性激活层。使用MSE损失,因为这是一个回归问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56163701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档