首页 > 解决方案 > 不兼容的形状:[32] 与 [4536] [[{{nodelogistic_loss/mul}}]]

问题描述

我正在训练具有以下结构和参数的分类器:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
encoder_input (InputLayer)      (None, 567, 1)       0                                            
__________________________________________________________________________________________________
conv1d_1 (Conv1D)               (None, 189, 32)      128         encoder_input[0][0]              
__________________________________________________________________________________________________
conv1d_2 (Conv1D)               (None, 63, 64)       6208        conv1d_1[0][0]                   
__________________________________________________________________________________________________
conv1d_3 (Conv1D)               (None, 21, 100)      19300       conv1d_2[0][0]                   
__________________________________________________________________________________________________
flatten_1 (Flatten)             (None, 2100)         0           conv1d_3[0][0]                   
__________________________________________________________________________________________________
z_mean (Dense)                  (None, 20)           42020       flatten_1[0][0]                  
__________________________________________________________________________________________________
z_log_var (Dense)               (None, 20)           42020       flatten_1[0][0]                  
__________________________________________________________________________________________________
z (Lambda)                      (None, 20)           0           z_mean[0][0]                     
                                                                 z_log_var[0][0]                  
__________________________________________________________________________________________________
dense_1 (Dense)                 (None, 64)           1344        z[0][0]                          
__________________________________________________________________________________________________
dense_2 (Dense)                 (None, 128)          8320        dense_1[0][0]                    
__________________________________________________________________________________________________
dense_3 (Dense)                 (None, 200)          25800       dense_2[0][0]                    
__________________________________________________________________________________________________
dense_10 (Dense)                (None, 4)            804         dense_3[0][0]                    
==================================================================================================
Total params: 145,944
Trainable params: 145,944
Non-trainable params: 0

除了层之外的所有dense层都取自不同的模型。这里只有dense层参数是可训练的。这是训练的代码片段:

for layer in classifier.layers[:-4]:
  layer.trainable = False
classifier.fit(x_train, y_train,
        epochs=50,
        batch_size=8,
        validation_data=(x_val, y_val)
              )
classifier.save_weights('cnn_hhar.h5')

但我得到了这个InvalidArgumentError: Incompatible shapes: [32] vs. [4536] [[{{node logistic_loss/mul}}]]

这段代码有什么问题?

编辑: 代码可在此处获得。它基本上改编自变分自动编码器的代码。我采用了编码器部分并向其中添加了 FC 层以执行分类任务。

标签: python-3.xmachine-learningkeras

解决方案


推荐阅读