如何消除在张量流的tape.gradient方法中将虚数转换为实值的警告?

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

我正在使用tape.gradient方法来优化一些神经网络。它按预期工作,但当我在单次迭代中多次使用 Tape.gradients 计算梯度时,不断发出此警告。这意味着在单个循环内,在执行 back prop 时,它会在某个地方摆弄复数。

WARNING:tensorflow:You are casting an input of type complex64 to an incompatible dtype float64.  This will discard the imaginary part and may not be what you intended.
cost_progress=[]
trace_progress=[]
for i in range(reps):

  with tf.GradientTape() as tape:
    tape.watch(params)
    loss,trace = cost(params,ratio)
    trace_progress.append(trace)
    cost_progress.append(loss)

  gradients = tape.gradient(loss, params)
  opt.apply_gradients(zip([gradients], [params]))

现在,所有参数和损失都是 tf.float64,但仍在 Tape.gradient() 中给出了一些复杂类型,我想手动将它们转换为真实值,以便此警告停止显示在我的屏幕上。但我无法找到如何投射,以免弄乱。

蛮力

gradients = tf.cast(tape.gradient(loss, params),tf.float64)
不起作用。我已经验证
gradients = tape.gradient(loss, params)
发出警告,并且损失和参数都是 tf.float64 类型。

machine-learning optimization tensorflow2.0 gradienttape
1个回答
0
投票

我怀疑问题出在你的

cost
函数上,但如果没有 最小的、可重现的例子,我无法明确回答。就我而言,我通过将
tf.cast(·, tf.complex64)
替换为
tf.dtypes.complex(·)
解决了这个问题。

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