【问题标题】:Variable tf.Variable has 'None' for gradient in TensorFlow Probability变量 tf.Variable 在 TensorFlow Probability 中的梯度为“无”
【发布时间】:2019-08-14 12:24:37
【问题描述】:

我在 TFP 中构建基本 BNN 时遇到问题。总的来说,我是 TFP 和 BNN 的新手,所以如果我错过了一些简单的事情,我深表歉意。

我可以通过执行以下操作在 Tensorflow 中训练一个基本的 NN:

model = keras.Sequential([
    keras.layers.Dense(units=100, activation='relu'),
    keras.layers.Dense(units=50, activation='relu'),
    keras.layers.Dense(units=5, activation='softmax')
])

model.compile(optimizer=optimizer, 
              loss=tf.losses.CategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

history = model.fit(
    training_data.repeat(), 
    epochs=100, 
    steps_per_epoch=(X_train.shape[0]//1024),
    validation_data=test_data.repeat(), 
    validation_steps=2
)

但是,我在尝试使用 tfp DenseFlipout 层实现类似架构时遇到了麻烦:

model = keras.Sequential([
    tfp.layers.DenseFlipout(units=100, activation='relu'),
    tfp.layers.DenseFlipout(units=10, activation='relu'),
    tfp.layers.DenseFlipout(units=5, activation='softmax')
])

model.compile(optimizer=optimizer, 
              loss=tf.losses.CategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

history = model.fit(
    training_data.repeat(), 
    epochs=100, 
    steps_per_epoch=(X_train.shape[0]//1024),
    validation_data=test_data.repeat(), 
    validation_steps=2
)

我收到以下值错误:

ValueError: 
Variable <tf.Variable 'sequential_11/dense_flipout_15/kernel_posterior_loc:0' 
shape=(175, 100) dtype=float32> has `None` for gradient. 
Please make sure that all of your ops have a gradient defined (i.e. are differentiable). 
Common ops without gradient: K.argmax, K.round, K.eval.

我进行了一些谷歌搜索,并查看了 TFP 文档,但不知所措,所以我想我会分享这个问题。我错过了什么明显的东西吗?

提前致谢。

【问题讨论】:

  • DenseFlipout 层是否与包含精确零的数据兼容? (尝试使用与“relu”不同的东西来检查)。提示:“softmax”意在与from_logits=False 一起使用。
  • 感谢from_logits 的帮助!我相信DenseFlipout 是,但我的问题看起来很可能是因为我使用的是 TF2。

标签: python tensorflow keras tensorflow-probability


【解决方案1】:

我想这是因为您使用的是 TensorFlow 2,是吗?它还没有完全支持。如果是这样,降级到 1.14 应该可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    相关资源
    最近更新 更多