Keras模型的输出形状为“(无)”

时间:2020-04-02 08:51:44

标签: python tensorflow keras

我的模型包括一个先前加载的模型,并给出了“(None,)”的输出形状:

from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Activation, Dense, Input, Subtract, Multiply, Lambda

x = Input((158,))
y = model(x)
c = Subtract()([x,y])
c = Multiply()([c,c])
d = Lambda(lambda arg: tf.keras.backend.mean(arg,axis=1), output_shape = (None,1))
e = d(c)

new_model = Model(inputs = x, outputs = e)
new_model.summary()

Model: "model"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            [(None, 158)]        0                                            
__________________________________________________________________________________________________
model_1 (Model)                 (None, 158)          57310       input_1[0][0]                    
__________________________________________________________________________________________________
subtract (Subtract)             (None, 158)          0           input_1[0][0]                    
                                                                 model_1[1][0]                    
__________________________________________________________________________________________________
multiply (Multiply)             (None, 158)          0           subtract[0][0]                   
                                                                 subtract[0][0]                   
__________________________________________________________________________________________________
lambda (Lambda)                 (None,)              0           multiply[0][0]                   
==================================================================================================
Total params: 57,310
Trainable params: 57,310
Non-trainable params: 0
__________________________________________________________________________________________________

此模型输出正确的值,但可能会在下一步工作中产生问题,因此我想知道此输出形状的含义以及是否必须对其进行纠正(如我所见)此案例的示例在线)。

编辑

要指定,我不是在调查None的值,而是事实并没有说(None,1),是同一回事吗?

例如,此摘要:

Layer (type)                 Output Shape              Param #
=================================================================
dense_1 (Dense)              (None, 2)                 4
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 3
=================================================================
Total params: 7
Trainable params: 7
Non-trainable params: 0
_________________________________________________________________

来源:https://machinelearningmastery.com/visualize-deep-learning-neural-network-model-keras/

2 个答案:

答案 0 :(得分:1)

这里没有一个代表您的batch size。批次大小值是动态的,您可以在.fit()以后进行定义,因此在定义之前,它不知道大小,并且保持None的意思是任何正整数。

您可以阅读here来更好地了解参数和值。

答案 1 :(得分:0)

我设法将最后一层重塑为(None,1),并且确实解决了代码中的问题,我通过在模型中添加Reshape层来做到这一点:

x = Input(158,)
y = model(x)
c = Subtract()([x,y])
c = Multiply()([c,c])
d = Lambda(lambda arg: tf.keras.backend.mean(arg,axis=1), output_shape = (None,1))
e = d(c)
f = Reshape([1])(e)

new_model = Model(inputs = x, outputs = f)

哪个给:

new_model.summary()

Model: "model_4"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_5 (InputLayer)            [(None, 158)]        0                                            
__________________________________________________________________________________________________
model_1 (Model)                 (None, 158)          57310       input_5[0][0]                    
__________________________________________________________________________________________________
subtract_4 (Subtract)           (None, 158)          0           input_5[0][0]                    
                                                                 model_1[5][0]                    
__________________________________________________________________________________________________
multiply_4 (Multiply)           (None, 158)          0           subtract_4[0][0]                 
                                                                 subtract_4[0][0]                 
__________________________________________________________________________________________________
lambda_4 (Lambda)               (None,)              0           multiply_4[0][0]                 
__________________________________________________________________________________________________
reshape_3 (Reshape)             (None, 1)            0           lambda_4[0][0]                   
==================================================================================================
Total params: 57,310
Trainable params: 57,310
Non-trainable params: 0
__________________________________________________________________________________________________