来自 keras 密集层的意外输出形状

Unexpected output shape from a keras dense layer

我尝试创建一个最小的 非卷积 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))

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

由于 dense_1 层只有一个神经元,我期望这一层的输出形状是 (None, 1) (即,表示预测二进制标签的单个数字)而是该模型给出 (None, 256, 256, 1).

我的模型设置有什么问题,我该如何纠正?

你的函数有错误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)

您可能希望第二行是

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

而不是

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

不幸的是,x 存在于范围内,因此它没有抛出错误。

如果要使用输出形状 (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())