【问题标题】:Keras Lambda layer, how to use multiple argumentsKeras Lambda 层,如何使用多个参数
【发布时间】:2021-04-20 07:29:25
【问题描述】:

我有这个功能:

def sampling(x):
    zeros = x*0
    samples = tf.random.categorical(tf.math.log(x), 1)
    samples = tf.squeeze(tf.one_hot(samples, depth=2), axis=1)
    return zeros+samples

我从这一层调用:

x = layers.Lambda(sampling, name="lambda")(x)

但我需要更改 sampling 函数中的 depth 变量,所以我需要这样的东西:

def sampling(x, depth):

但是,我怎样才能使它与 Lambda 层一起工作?

非常感谢

【问题讨论】:

    标签: python tensorflow machine-learning keras deep-learning


    【解决方案1】:

    在 Lambda 层内使用 lambda 函数...

    def sampling(x, depth):
        zeros = x*0
        samples = tf.random.categorical(tf.math.log(x), 1)
        samples = tf.squeeze(tf.one_hot(samples, depth=depth), axis=1)
        return zeros+samples
    

    用法:

    Lambda(lambda t: sampling(t, depth=3), name="lambda")(x)
    

    【讨论】:

      猜你喜欢
      • 2017-12-09
      • 2017-12-06
      • 2017-08-10
      • 2018-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      • 2019-09-10
      相关资源
      最近更新 更多