在TensorFlow Distributions(现为Probability )的参考论文中,提到了 TensorFlow可用于构造和对象,即:Variable
Bijector
TransformedDistribution
import tensorflow as tf
import tensorflow_probability as tfp
tfd = tfp.distributions
tf.enable_eager_execution()
shift = tf.Variable(1., dtype=tf.float32)
myBij = tfp.bijectors.Affine(shift=shift)
# Normal distribution centered in zero, then shifted to 1 using the bijection
myDistr = tfd.TransformedDistribution(
distribution=tfd.Normal(loc=0., scale=1.),
bijector=myBij,
name="test")
# 2 samples of a normal centered at 1:
y = myDistr.sample(2)
# 2 samples of a normal centered at 0, obtained using inverse transform of myBij:
x = myBij.inverse(y)
我现在想修改 shift 变量(比如说,我可能会计算一些似然函数的梯度作为 shift 的函数并更新它的值)所以我这样做
shift.assign(2.)
gx = myBij.forward(x)
我希望如此gx=y+1
,但我看到了gx=y
……确实,myBij.shift
仍然评估为1
.
如果我尝试直接修改双射器,即:
myBij.shift.assign(2.)
我明白了
AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'assign'
计算梯度也不能按预期工作:
with tf.GradientTape() as tape:
gx = myBij.forward(x)
grad = tape.gradient(gx, shift)
Yields None
,以及脚本结束时的此异常:
Exception ignored in: <bound method GradientTape.__del__ of <tensorflow.python.eager.backprop.GradientTape object at 0x7f529c4702e8>>
Traceback (most recent call last):
File "~/.local/lib/python3.6/site-packages/tensorflow/python/eager/backprop.py", line 765, in __del__
AttributeError: 'NoneType' object has no attribute 'context'
我在这里想念什么?
编辑:我让它与图形/会话一起工作,所以似乎急切执行存在问题......
注意:我有 tensorflow 版本 1.12.0 和 tensorflow_probability 版本 0.5.0