tape.gradient的输出无法应用于optimizer.apply_gradients

时间:2019-09-12 12:30:13

标签: python tensorflow gradient

tape.gradients的输出不能应用于optimizer.apply_gradients

我正在使用一个奇怪的成本函数,需要计算梯度并明确地应用它们。

函数调用的格式很奇怪。我期望能够直接应用

的输出
dw = t.gradient( cost, wl, unconnected_gradients=tf.UnconnectedGradients.ZERO)

进入

optimizer.apply_gradients(dw, wl)  # optimizer = Adam

如果wl不是向量而是变量列表,则它起作用。但是,当我想创建一个更大的系统时,我不想独立定义变量。

dw = t.gradient(cost,wl,unconnected_gradients = tf.UnconnectedGradients.ZERO) Optimizer.apply_gradients(dw,wl)#Optimizer = Adam

变量的内容为:`

print(dw)
tf.Tensor(
[[  12.000002]
 [  10.000002]
 [  98.00002 ]
 [ 250.00003 ]
 [1594.      ]], shape=(5, 1), dtype=float32)

print(wl)
<tf.Variable 'Shape/Variable:0' shape=(5, 1) dtype=float32, numpy=
array([[1.],
       [1.],
       [1.],
       [1.],
       [1.]], dtype=float32)>
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-144-a875badce41c> in <module>()
     41 #     print(d4y_dx4)
     42     print(dw)
---> 43     optimizer.apply_gradients(dw, wl)  # optimizer = Adam

/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/optimizer.py in apply_gradients(self, grads_and_vars, global_step, name)
    574       raise ValueError("No variables provided.")
    575     converted_grads_and_vars = []
--> 576     for g, v in grads_and_vars:
    577       if g is not None:
    578         try:

ValueError: not enough values to unpack (expected 2, got 1)

使用zip解决方案也不起作用

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-146-d722d545815d> in <module>()
     41 #     print(d4y_dx4)
     42     print(dw)
---> 43     optimizer.apply_gradients(zip(dw, wl))  # optimizer = Adam
     44     #tf.print(wl)
     45     #tf.print(tf.math.reduce_mean((2*wl[2] + 6 * wl[3] * x + 12*wl[4] * x**2-1)**2))

TypeError: zip argument #2 must support iteration

0 个答案:

没有答案