You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

显式创建独立输出神经元的多输出回归模型结果异常排查

多输出神经网络:独立单神经元输出层与多神经元输出层的性能差异问题

我需要构建一个多输出回归模型,为了后续能给每个输出神经元单独设置损失函数,最初设计了如下模型:

class FDC_Model(Model):
    def __init__(self, out_names):
        super(FDC_Model, self).__init__()
        self.out_names = out_names
        self.n_outputs = len(self.out_names)

        self.fc1 = Dense(units=35, activation="relu", kernel_initializer="he_uniform", name="FC1")
        self.dropout1 = Dropout(rate=0.22, name="dropout1")
        self.fc2 = Dense(units=31, activation="relu", kernel_initializer="he_uniform", name="FC2")
        self.dropout2 = Dropout(rate=0.32, name="dropout2")
        
        self.fc3 = []
        for name in self.out_names:
            fc3 = Dense(units=1, activation="linear", name=name)
            self.fc3.append(fc3)

    def call(self, inputs, training=None, mask=None):
        base = self.fc1(inputs)
        base = self.dropout1(base)
        base = self.fc2(base)
        base = self.dropout2(base)
        outputs = []
        for i in range(len(self.out_names)):
            output_neuron = self.fc3[i](base)
            outputs.append(output_neuron)
        return outputs

模型包含两个隐藏Dense层(35和31单元),通过循环创建15个独立的单神经元Dense层作为输出,在call方法中分别连接到隐藏层输出,返回输出列表。

对应的模型构建与训练代码:

fdc_model = FDC_Model(list(y_data.columns))  # 共15个输出列
inputs = keras.Input(shape=(len(x_data.columns)), name='input layer')
outputs = fdc_model.call(inputs)

model = keras.Model(inputs=inputs, outputs=outputs)
model.compile(loss='mse',
              optimizer=keras.optimizers.RMSprop(learning_rate=0.002),
              metrics=['mae'])

history = model.fit(
        x=x_train_np,  # 形状为(1600, 170),1600行170个特征
        y=y_train_np,  # 形状为(400, 15),400行15个输出
        batch_size=300,
        epochs=400,
        validation_data=(x_val_np, y_val_np),
        verbose=0
    )

训练后发现:

  • 训练和验证MSE损失收敛至约0.9
  • 预测结果所有输出值极为接近,不符合预期的单调递增序列

之后我将输出层替换为一个15单元的Dense层,模型代码修改为:

class FDC_Model(Model):
    def __init__(self, out_names):
        super(FDC_Model, self).__init__()
        self.out_names = out_names
        self.n_outputs = len(self.out_names)

        self.fc1 = Dense(units=35, activation="relu", kernel_initializer="he_uniform", name="FC1")
        self.dropout1 = Dropout(rate=0.22, name="dropout1")
        self.fc2 = Dense(units=31, activation="relu", kernel_initializer="he_uniform", name="FC2")
        self.dropout2 = Dropout(rate=0.32, name="dropout2")

        self.output_layer = Dense(units=self.n_outputs, activation="linear")
        
    def call(self, inputs, training=None, mask=None):
        base = self.fc1(inputs)
        base = self.dropout1(base)
        base = self.fc2(base)
        base = self.dropout2(base)
        base = self.output_layer(base)
        return base

保持模型构建与训练代码不变,训练后结果:

  • 训练和验证MSE损失约为0.1
  • 预测结果符合预期的单调递增序列

我尝试手动定义15个独立Dense层连接到基础模型,结果仍与第一种情况一致。想知道是否误解了Dense层创建逻辑,或是连接方式存在问题。


内容的提问来源于stack exchange,提问作者b-dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 11:59:54