keras tf.gradienttape()可以计算其他库函数的梯度吗

q35jwt9p  于 2023-02-04  发布在  其他
关注(0)|答案(1)|浏览(149)

如果我在tf.GradientTape()中包含一些来自其他Python库的函数,比如'sklearn.decomposition.PCA.inverse_transform()',TensorFlow是否可以从该函数计算梯度?
具体来说,tf是否可以自动区分pca_inverse_tranform = pca.inverse_transform(h2)

...
from sklearn.decomposition import PCA
pca = PCA(n_components=10)
pca.fit(x)
...
with tf.GradientTape() as tape:
    h1 = x@w1 + tf.broadcast_to(b1, [x.shape[0], 256])
    h1 = tf.nn.relu(h1)
    h2 = h1@w2 + tf.broadcast_to(b2, [x.shape[0], 10])
    h2 = tf.nn.relu(h2)

    pca_inverse_tranform = pca.inverse_transform(h2)

    loss = tf.square(pca_inverse_tranform - target)
    loss = tf.reduce_mean(loss)
[dl_dw1, dl_db1, dl_dw2, dl_db2] = tape.gradient(loss, [w1,b1,w2,b2])
xmd2e60i

xmd2e60i1#

我从tf文档中找到了答案,它显示The tape can't record the gradient path if the calculation exits TensorFlow. For example:

x = tf.Variable([[1.0, 2.0],
                 [3.0, 4.0]], dtype=tf.float32)

with tf.GradientTape() as tape:
  x2 = x**2

  # This step is calculated with NumPy
  y = np.mean(x2, axis=0)

  # Like most ops, reduce_mean will cast the NumPy array to a constant tensor
  # using `tf.convert_to_tensor`.
  y = tf.reduce_mean(y, axis=0)

print(tape.gradient(y, x))

因此,问题的答案是“不,tf不能计算其他库函数的梯度”。

相关问题