Python TF梯度胶带有交叉产品问题吗?

Python TF梯度胶带有交叉产品问题吗?,python,tensorflow,Python,Tensorflow,我试着用TF梯度带作为一个自动标记工具,通过牛顿法寻找根。但当我试图计算雅可比矩阵时,tf.GradientTape.Jacobian似乎无法处理叉积: x = tf.convert_to_tensor(np.array([1., 2., 3.])) Wx = np.ones((3)) with tf.GradientTape() as tape: tape.watch(x) y = tf.linalg.cross(x, Wx) print(tape.jacobian(y, x)

我试着用TF梯度带作为一个自动标记工具,通过牛顿法寻找根。但当我试图计算雅可比矩阵时,tf.GradientTape.Jacobian似乎无法处理叉积:

x = tf.convert_to_tensor(np.array([1., 2., 3.]))
Wx = np.ones((3))
with tf.GradientTape() as tape:
    tape.watch(x)
    y = tf.linalg.cross(x, Wx)
print(tape.jacobian(y, x))
给出以下错误:

StagingError:在转换的代码中: 相对于/Users/xinzhang/anaconda3/lib/python3.7/site-packages:

然而,如果我把雅可比变换为一个简单的梯度:

x = tf.convert_to_tensor(np.array([1., 2., 3.]))
Wx = np.ones((3))
with tf.GradientTape() as tape:
    tape.watch(x)
    y = tf.linalg.cross(x, Wx)
print(tape.gradient(y, x))
给出了预期的结果:

tf.Tensor([0. 0. 0.], shape=(3,), dtype=float64)
这是一只虫子吗??还是我用磁带做了什么错事,雅可比方法


p、 美国python版本3.7.4;tf版本2.0.0随conda一起安装的所有设备。

这可能是Tensorflow版本2.0中的一个错误,但在Tensorflow版本2.1中已修复

因此,请将Tensorflow版本升级到
2.1
2.2
,问题将得到解决

工作代码如下所述:

!pip install tensorflow==2.2

import tensorflow as tf
import numpy as np

print(tf.__version__)

x = tf.convert_to_tensor(np.array([1., 2., 3.]))
Wx = np.ones((3))
with tf.GradientTape() as tape:
    tape.watch(x)
    y = tf.linalg.cross(x, Wx)
print(tape.jacobian(y, x))
输出如下所示:

2.2.0

tf.Tensor(
[[ 0.  1. -1.]
 [-1.  0.  1.]
 [ 1. -1.  0.]], shape=(3, 3), dtype=float64)

非常感谢。结果表明,无法使用conda安装TF2.0以上的任何内容。我用pip安装了2.2,它运行正常。
2.2.0

tf.Tensor(
[[ 0.  1. -1.]
 [-1.  0.  1.]
 [ 1. -1.  0.]], shape=(3, 3), dtype=float64)