【发布时间】:2019-06-03 13:44:34
【问题描述】:
我想像在 TF 1.3 下一样在 TF 2.0 中使用 tf.contrib.estimator.clip_gradients_by_norm,但是现在 contrib 消失了,我需要一个解决方法,甚至只是一些关于它如何工作的基本直觉。
我知道此问题已在 Github (https://github.com/tensorflow/tensorflow/issues/28707) 上作为问题提出,但如果可能的话希望尽快找到解决方案。
# Use gradient descent as the optimizer for training the model.
my_optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.0000001)
my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer, 5.0)
# Configure the linear regression model with our feature columns and optimizer.
# Set a learning rate of 0.0000001 for Gradient Descent.
linear_regressor = tf.estimator.LinearRegressor(
feature_columns=feature_columns,
optimizer=my_optimizer
)
更多:
我已尝试使用此处描述的自定义渐变: https://www.tensorflow.org/guide/eager
@tf.custom_gradient
def clip_gradient_by_norm(x, norm):
y = tf.identity(x)
def grad_fn(dresult):
return [tf.clip_by_norm(dresult, norm), None]
return y, grad_fn
没有成功。
【问题讨论】:
标签: python tensorflow tensorflow2.0