【问题标题】:How to calculate gradients on tensorflow_probability layers?如何计算 tensorflow_probability 层的梯度?
【发布时间】:2021-03-12 01:26:08
【问题描述】:

我想使用tf.GradientTape() 计算tensorflow_probability 层上的梯度。这很简单,使用普通的,例如 Dense 层

inp = tf.random.normal((2,5))
layer = tf.keras.layers.Dense(10)

with tf.GradientTape() as tape:
    out = layer(inp)
    loss = tf.reduce_mean(1-out)
grads = tape.gradient(loss, layer.trainable_variables)
print(grads)
[<tf.Tensor: shape=(5, 10), dtype=float32, numpy=
 array([[ 0.04086879,  0.04086879, -0.02974391,  0.04086879,  0.04086879,
          0.04086879, -0.02974391,  0.04086879, -0.02974391, -0.07061271],
        [ 0.01167339,  0.01167339, -0.02681615,  0.01167339,  0.01167339,
          0.01167339, -0.02681615,  0.01167339, -0.02681615, -0.03848954],
        [ 0.00476769,  0.00476769, -0.00492069,  0.00476769,  0.00476769,
          0.00476769, -0.00492069,  0.00476769, -0.00492069, -0.00968838],
        [-0.00462376, -0.00462376,  0.05914849, -0.00462376, -0.00462376,
         -0.00462376,  0.05914849, -0.00462376,  0.05914849,  0.06377225],
        [-0.11682947, -0.11682947, -0.06357963, -0.11682947, -0.11682947,
         -0.11682947, -0.06357963, -0.11682947, -0.06357963,  0.05324984]],
       dtype=float32)>,
 <tf.Tensor: shape=(10,), dtype=float32, numpy=
 array([-0.05, -0.05, -0.1 , -0.05, -0.05, -0.05, -0.1 , -0.05, -0.1 ,
        -0.05], dtype=float32)>]

但如果我使用 DenseReparameterization 来执行此操作,则毕业生注册无。

inp = tf.random.normal((2,5))
layer = tfp.layers.DenseReparameterization(10)

with tf.GradientTape() as tape:
    out = layer(inp)
    loss = tf.reduce_mean(1-out)
grads = tape.gradient(loss, layer.trainable_variables)
print(grads)
[None, None, None]

谁能告诉我如何解决这个问题,让渐变被录音和注册?

【问题讨论】:

  • 我运行了您发布的代码并计算了梯度。我正在使用 tf v2.4.0、tfp v0.12.1 和 python 3.8.8

标签: tensorflow tensorflow-probability bayesian-deep-learning


【解决方案1】:

啊哈,就是这样!我正在使用 tf v2.1.0。显然,这不适用于 tensorflow_probability。我会尽快升级。谢谢 gobrewers14。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-24
    • 2017-12-06
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 2015-11-27
    • 2019-11-01
    • 1970-01-01
    相关资源
    最近更新 更多