【问题标题】:how to do addition to hidden layers results?如何添加隐藏层结果?
【发布时间】:2021-05-02 08:54:36
【问题描述】:

假设我们有一个这样的神经网络

input = layers.Input(shape=(num_inputs,))
out = layers.Dense(1028, activation="relu")(input)
out = layers.Dense(512, activation="relu")(out)
out = layers.Dense(256, activation="relu")(out)
output = layers.Dense(4, activation="softmax")(out)

如果我想用随机数添加任何隐藏层结果,代码应该如何更改??

【问题讨论】:

    标签: python tensorflow machine-learning keras neural-network


    【解决方案1】:

    您可以使用随机数将结果添加到任何隐藏层,如下所示。 Reference and more details.

    from tensorflow.keras.layers import *
    from tensorflow.keras.models import Model
    from tensorflow.keras import backend as K 
    
    ins = Input((1,))
    
    out = Dense(1028, activation="relu")(ins)
    out = Lambda(lambda x: K.random_uniform((1,))*x)(out)
    
    out = Dense(512, activation="relu")(out)
    out = Lambda(lambda x: K.random_uniform((1,))*x)(out)
    
    out = Dense(256, activation="relu")(out)
    out = Lambda(lambda x: K.random_uniform((1,))*x)(out)
    
    outs = Dense(256, activation="softmax")(out)
    model = Model(ins, outs)
    

    【讨论】:

      猜你喜欢
      • 2019-01-27
      • 2020-02-02
      • 1970-01-01
      • 1970-01-01
      • 2019-05-19
      • 2011-07-16
      • 2012-10-12
      • 2019-05-12
      • 1970-01-01
      相关资源
      最近更新 更多