1

在非急切模式下,我可以毫无问题地运行它:

s = tf.complex(tf.Variable(1.0), tf.Variable(1.0))
train_op = tf.train.AdamOptimizer(0.01).minimize(tf.abs(s))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(5):
        _, s_ = sess.run([train_op, s])
        print(s_)

>(1+1j)
(0.99+0.99j)
(0.98+0.98j)
(0.9700001+0.9700001j)
(0.9600001+0.9600001j)

但我似乎无法在渴望模式下找到等效的表达式。我尝试了以下方法,但 TF 抱怨:

tfe = tf.contrib.eager
s = tf.complex(tfe.Variable(1.0), tfe.Variable(1.0))
def obj(s):
    return tf.abs(s)
with tf.GradientTape() as tape:
    loss = obj(s)
    grads = tape.gradient(loss, [s])
    optimizer.apply_gradients(zip(grads, [s]))

tf.float32调用 GradientTape.gradient 时,源张量的 dtype 必须是浮动的(例如),得到tf.complex64

没有为任何变量提供梯度:['tf.Tensor((1+1j), shape=(), dtype=complex64)']

如何在 Eager 模式下训练复杂变量?

4

1 回答 1

0

在 Tensorflow 2 中使用 eager 模式,您可以将实部和虚部作为实变量:

r, i = tf.Variable(1.0), tf.Variable(1.0)
def obj(s):
    return tf.abs(s)
with tf.GradientTape() as tape:
    s = tf.complex(r, i)
    loss = obj(s)
    grads = tape.gradient(loss, [r, i])
    optimizer.apply_gradients(zip(grads, [r, i]))
于 2019-11-28T16:05:46.007 回答