【问题标题】:Custom layer with different activation function for each output每个输出具有不同激活函数的自定义层
【发布时间】:2021-05-15 05:31:28
【问题描述】:

我有一个简单的神经网络,有两个输出,我需要为每个输出使用不同的激活函数。我基本上做了这篇文章中写的 - here,但看起来我的具有不同激活函数的层不起作用:

请参阅下面的代码:

X = filled_df.loc[:, "SOUTEZ_MEAN_HOME":"TOTAL_POINTS_AWAY"].values
y = filled_df.loc[:, "HOME_YELLOW_CARDS"].values

X= X.astype("float32")
y= y.astype("float32")

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size= 0.3)

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train= scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

def negative_binomial_layer(x):

    # Get the number of dimensions of the input
    num_dims = len(x.get_shape())
    
    # Separate the parameters
    n, p = tf.unstack(x, num=2, axis=-1)

    
    # Add one dimension to make the right shape
    n = tf.expand_dims(n, -1)
    p = tf.expand_dims(p, -1)
        
    # Apply a softplus to make positive
    n = tf.cast(n, tf.float32)
    p = tf.cast(p, tf.float32)
    n = tf.keras.activations.softplus(n)
    
    # Apply a sigmoid activation to bound between 0 and 1
    p = tf.keras.activations.sigmoid(p)

    # Join back together again
    out_tensor = tf.concat((n, p), axis=num_dims-1)

    return out_tensor 


input_shape = (212, )

# Define inputs with predefined shape
inputs = Input(shape=input_shape)

# Build network with some predefined architecture
Layer1 = Dense(16)
Layer2 = Dense(8)

output1 = Layer1(inputs)
output2 = Layer2(output1)

# Predict the parameters of a negative binomial distribution
outputs = Dense(2)(output2)
#outputs = tf.cast(outputs, tf.float32)
distribution_outputs = Lambda(negative_binomial_layer)(outputs)


# Construct model
model = Model(inputs=inputs, outputs=outputs)

num_epochs = 10
opt = Adam()
model.compile(loss = negative_binomial_loss, optimizer = opt)

history = model.fit(X_train, y_train, epochs = num_epochs,
                    validation_data = (X_test, y_test))

如果我在自定义损失函数中打印 y_pred,这些是我的预测值:

Epoch 1/10
y_pred =  [[2.19472528 3.14479065]
 [-1.16056371 1.69369149]
 [-1.12327099 2.06830978]
 ...
 [-1.23587477 4.82307]
 [0.235431105 3.86740351]
 [-2.75554061 1.10352468]] [[[2.19472528 3.14479065]
 [-1.16056371 1.69369149]
 [-1.12327099 2.06830978]
 ...
 [-1.23587477 4.82307]
 [0.235431105 3.86740351]
 [-2.75554061 1.10352468]]]

第二个预测值 p 应该在 0 和 1 之间,因为它超出了这个范围,我在计数损失时得到了 nan。

有什么建议吗?谢谢

【问题讨论】:

    标签: python neural-network layer loss-function multipleoutputs


    【解决方案1】:

    我无法给出确切的编程解释,但我可以对这个问题给出一个理论上的答案,您应该能够使用它来构建它。

    根据我的假设,您是在询问如何为输出层中的每个输出节点使用不同的激活函数。我对您正在使用的任何库或扩展了解不多,但通常这些库包含某种创建自定义网络的方法。从您发布的代码中,我可以看到您正在为网络使用预定义的结构,这意味着您可能无法自己自定义输出层,而您将不得不create a custom network。由于您发布的代码中的某些方法,我假设您正在使用 Tensorflow。

    还有其他需要考虑的事情。通常你也会在神经元(隐藏层)上激活功能,这也是你可能需要考虑的。

    很抱歉,我无法给出切实可行的答案,但我希望这可以帮助您了解如何才能使其正常工作 - 祝您有美好的一天!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-15
      • 2018-09-30
      • 2019-09-05
      • 2019-05-09
      相关资源
      最近更新 更多