如何在TensorFlow 2.0中实现clip_gradients_by_norm?

问题描述 投票:0回答:1

我想在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://colab.research.google.com/notebooks/mlcc/first_steps_with_tensor_flow.ipynb?utm_source=mlcc&utm_campaign=colab-external&utm_medium=referral&utm_content=firststeps-colab&hl=en#scrollTo=ubhtW-NGU802

我尝试使用自定义渐变,如下所述: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
1个回答
0
投票
正在对此问题https://github.com/tensorflow/tensorflow/issues/28707#issuecomment-502336827进行评论,

我发现您可以将代码修改为如下所示:

# Use gradient descent as the optimizer for training the model. from tensorflow.keras import optimizers my_optimizer = optimizers.SGD(lr=0.0000001, clipvalue=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 )

而不是:

# 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 )

© www.soinside.com 2019 - 2024. All rights reserved.