[英]Keras Model output shape is "(None,)"
My model includes a previously loaded model, and gives an output shape of "(None,)":我的模型包含一个先前加载的模型,并给出“(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
__________________________________________________________________________________________________
This model outputs correct values, but it might be creating issues down the line with the next step of my work, so I would like to know what this output shape means, and if I have to correct it (as I saw no exemples of this case online).这个模型输出正确的值,但它可能会在我的下一步工作中产生问题,所以我想知道这个输出形状意味着什么,如果我必须纠正它(因为我没有看到这样的例子案例在线)。
Edit编辑
To specify, I'm not investigating about the None
value, but the fact that it doesn't say (None,1)
, is it the same thing ?具体来说,我不是在调查
None
值,但事实上它没有说(None,1)
,这是同一件事吗?
As an exemple, this summary:例如,这个总结:
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
_________________________________________________________________
source: https://machinelearningmastery.com/visualize-deep-learning-neural-network-model-keras/来源: https : //machinelearningmastery.com/visualize-deep-learning-neural-network-model-keras/
None here represents your batch size
.这里没有代表您的
batch size
。 Batch size value is dynamic, you define it later during .fit()
so before the definition it doesn't know the size and it stays None
meaning any positive integer value.批量大小值是动态的,您稍后在
.fit()
期间定义它,因此在定义之前它不知道大小并且它保持None
表示任何正整数值。
You can read here to understand parameters and values a bit better.您可以阅读此处以更好地理解参数和值。
I managed to reshape the last layer into a (None,1)
, and it did solve an issue in my code, I did it by adding a Reshape
layer to my model:我设法将最后一层重新整形为
(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)
Which gives:这使:
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
__________________________________________________________________________________________________
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.