简体   繁体   English

来自 keras 密集层的意外 output 形状

[英]Unexpected output shape from a keras dense layer

I try to create a minimal non-convolutional NN image binary classifier with one hidden layer only (as a practice before more complicated models):我尝试创建一个仅具有一个隐藏层的最小非卷积NN 图像二进制分类器(作为更复杂模型之前的实践):

def make_model(input_shape):
    inputs = keras.Input(shape=input_shape)
    x = layers.Dense(128, activation="ReLU")(inputs)
    outputs = layers.Dense(1, activation="sigmoid")(x)
    return keras.Model(inputs, outputs)
model = make_model(input_shape=(256, 256, 3))

Its model.summary() shows它的model.summary()显示

Model: "model"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_1 (InputLayer)        [(None, 256, 256, 3)]     0                                                                       
 dense (Dense)               (None, 256, 256, 128)     512                                                                    
 dense_1 (Dense)             (None, 256, 256, 1)       129                                                      
=================================================================
Total params: 641
Trainable params: 641
Non-trainable params: 0

Since the dense_1 layer has one neuron only, what I expect from this layer is an output shape of (None, 1) (i,e, a single number indicating the predicted binary label) but instead the model gives (None, 256, 256, 1) .由于dense_1层只有一个神经元,我对这一层的期望是(None, 1)的output形状(即,表示预测的二进制标签的单个数字),而是model给出(None, 256, 256, 1) 256,256 (None, 256, 256, 1) .

What's wrong with my model setting and how can I get it right?我的 model 设置有什么问题,如何才能正确设置?

You have to flatten your preposterously large tensor if you want to use the output shape (None, 1) :如果你想使用 output 形状(None, 1)你必须压平你荒谬的大张量:

import tensorflow as tf

def make_model(input_shape):
    inputs = tf.keras.layers.Input(shape=input_shape)
    x = tf.keras.layers.Dense(128, activation="relu")(inputs)
    x = tf.keras.layers.Flatten()(x)
    outputs = tf.keras.layers.Dense(1, activation="sigmoid")(x)
    return tf.keras.Model(inputs, outputs)

model = make_model(input_shape=(256, 256, 3))
print(model.summary())

A mistake is in your function make_model .您的 function make_model中有一个错误。

def make_model(input_shape):
    inputs = keras.Input(shape=input_shape)
    x = layers.Dense(128, activation="ReLU")(x)
    outputs = layers.Dense(1, activation="sigmoid")(x)
    return keras.Model(inputs, outputs)

You probably wanted the second line to be您可能希望第二行是

x = layers.Dense(128, activation="ReLU")(inputs)

and not并不是

x = layers.Dense(128, activation="ReLU")(x)

and unfortunately, x exists in scope, so it didn't throw an error.不幸的是, x存在于 scope 中,所以它没有抛出错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM