Can not squeeze dim[1], expected a dimension of 1, got 499

半腔热情 提交于 2021-02-08 17:23:12

问题


I am trying to make an AutoEncoder and am stuck at the above error. Looking at other posts with this on Stack Exchange didn't help.

Here is the error in full:

InvalidArgumentError: Can not squeeze dim[1], expected a dimension of 1, got 499
 [[{{node metrics_12/acc/Squeeze}}]]
 [[{{node ConstantFoldingCtrl/loss_12/time_distributed_6_loss/broadcast_weights/assert_broadcastable/AssertGuard/Switch_0}}]]

I can compile my model. Here it is:

Layer (type) Output Shape Param #
================================================================= lambda_7 (Lambda) (None, 499, 22) 0
_________________________________________________________________ cu_dnnlstm_14 (CuDNNLSTM) (None, 300) 388800
_________________________________________________________________ repeat_vector_12 (RepeatVect (None, 499, 300) 0
_________________________________________________________________ cu_dnnlstm_15 (CuDNNLSTM) (None, 499, 50) 70400
_________________________________________________________________ time_distributed_6 (TimeDist (None, 499, 22) 1122
================================================================= Total params: 460,322 Trainable params: 460,322 Non-trainable params: 0

The lambda layer takes a padded sequence of shape (1,499) and converts it to a onehot with 22 possible values. I then pass this through an encoding CuDNNLSTM, a repeat vector, another CuDNNLSTM and then a time distributed dense layer with a softmax activation.

I use:

model.compile('rmsprop', 'sparse_categorical_crossentropy', metrics=['acc'])

And:

model.fit(s_min_one, s_min_one,
   batch_size=batchS,
   epochs=epochS,
   verbose = 1,  
   shuffle=True)

Which gives me my error.

I use the sparse categorical so that my data can be in integer format taking on values between 0 and 21 (for the 22 features). And I don't understand why the error expects a dimension of 1 for anything. It is almost as if it wants me to flip my columns and rows?


回答1:


Your issue is related to the shape of the output. sparse_categorical_crossentropy expects integer targets (see the documentation: "When using the sparse_categorical_crossentropy loss, your targets should be integer targets.")

You are passing data which is of shape (batch_size, 1, 499) both as the input as well as the labels:

model.fit(s_min_one, s_min_one, ...)

That's not going to work, the labels need to be of shape (batch_size, 1) or simply (batch_size,).

I'm not entirely sure I understand what you are trying to accomplish, but it looks like you need to adapt the loss function accordingly.



来源:https://stackoverflow.com/questions/55203072/can-not-squeeze-dim1-expected-a-dimension-of-1-got-499

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!