简体   繁体   English

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

[英]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.

相关问题 将 Keras 模型导出到具有 (None, 2) 输出形状的 protobuf - Exporting Keras model to protobuf with a (None, 2) ouptut shape keras,尽管有input_shape(None,…),模型仍然通过训练设置了预期的输入形状 - keras, model still setting expected input shape from training despite input_shape(None, …) Keras:计算模型输出与输入返回的导数[无] - Keras: calculating derivatives of model output wrt input returns [None] 如何在Keras中更改任何模型架构的输入和输出形状? - How to change input and output shape of any model architecture in Keras? 如何在Keras中定义DQN模型的输出层形状 - How to define output layer shape of DQN model in Keras 1DConv 输出形状 keras 模型摘要中的第二个参数是什么? - What is second Parameter in 1DConv output shape keras Model Summary? 创建 keras 张量,其形状与 model output 用于自定义损失 ZC1C425268E68385D1ABZA77 - Create keras tensor with shape as same as model output for custom loss function keras:ValueError:检查模型目标时出错:预期activation_1具有形状(无,60),但数组的形状为(10,100) - keras: ValueError: Error when checking model target: expected activation_1 to have shape (None, 60) but got array with shape (10, 100) Keras 得到错误的输出形状 - Keras getting wrong output shape 错误的 output 形状与 keras lstm - Wrong output shape with keras lstm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM