ご質問にお答えするには、optimizer.minimize(loss)
に電話する際にオプティマイザが通常行うことを確認する必要があります。
実際には、次の2つの操作:compute_gradients()
とapply_gradients
を実行しています。
Tensorflow documentation of tf.train.Optimizerから、私たちは読み:minimize()
を呼び出す
は、勾配を計算し、変数に適用することの両方の世話をします。
だから、
:
あなたがあなたの代わりに三の段階でオプティマイザを使用することができ、それらを適用する前に勾配 を処理したい場合:
- は
compute_gradients()
との勾配を計算します。
- グラデーションを任意の方法で処理します。
apply_gradients()
で処理された勾配を適用します。 # Create an optimizer.
opt = GradientDescentOptimizer(learning_rate=0.1)
# Compute the gradients for a list of variables.
grads_and_vars = opt.compute_gradients(loss, <list of variables>)
# grads_and_vars is a list of tuples (gradient, variable). Do whatever you
# need to the 'gradient' part, for example cap them, etc.
capped_grads_and_vars = [(MyCapper(gv[0]), gv[1]) for gv in grads_and_vars]
# Ask the optimizer to apply the capped gradients.
opt.apply_gradients(capped_grads_and_vars)
:私はグラデーションにいくつかの変更を適用した例を取ることができますドキュメントから直接