张量流仅区分向量的元素 [英] tensorflow differentiate only element of vector
问题描述
我将了解 tensorflow 微分在应用于向量元素时的行为方式.这是我的代码
w=tf.Variable([[1.0,2.3],[4.5,6.7]],dtype=tf.float32)x=tf.constant([[1.2],[2.3]],dtype=tf.float32)使用 tf.GradientTape() 作为磁带:y=tf.matmul(w,x)d=tape.gradient(y[0],w)
y 是 2x1 张量.当区分 y[0] w.r.t w 即 y 的单个元素时,我得到 None.d 在这里没有.我无法理解这种行为.y[0] 依赖于 w ,为什么我仍然得到 None?
你没有得到,因为访问张量的子元素实际上是一个需要在 tf.GradientTape
上下文.否则,磁带不知道导致该变量的历史记录.
您的示例类似于这样做:
w=tf.Variable([[1.0,2.3],[4.5,6.7]],dtype=tf.float32)x=tf.constant([[1.2],[2.3]],dtype=tf.float32)使用 tf.GradientTape() 作为磁带:y=tf.matmul(w,x)d=tape.gradient(y+2,w)
磁带没有观察操作y+2
,所以无法计算梯度.
要仅获取一个元素的梯度,您需要在磁带的上下文中显式获取该元素:
w=tf.Variable([[1.0,2.3],[4.5,6.7]],dtype=tf.float32)x=tf.constant([[1.2],[2.3]],dtype=tf.float32)使用 tf.GradientTape() 作为磁带:y=tf.matmul(w,x)y0 = y[0]d=tape.gradient(y0,w)
然后,你得到 y[0]
w.r.t w
的梯度:
I am to understand how tensorflow differentiation behaves when applied on elements of vector. Here is my code
w=tf.Variable([[1.0,2.3],[4.5,6.7]],dtype=tf.float32)
x=tf.constant([[1.2],[2.3]],dtype=tf.float32)
with tf.GradientTape() as tape:
y=tf.matmul(w,x)
d=tape.gradient(y[0],w)
y is 2x1 tensor. When differentiate y[0] w.r.t w i.e a single element of y I get None. d is None here. I can't understand this behavior. y[0] is depended on w ,Why still I'm getting None?
You're getting none because accessing a sub element of a tensor is actually an operation that needs to be done inside the tf.GradientTape
context. Otherwise, the tape does not know the history that lead to that variable.
Your example is akin to doing this:
w=tf.Variable([[1.0,2.3],[4.5,6.7]],dtype=tf.float32)
x=tf.constant([[1.2],[2.3]],dtype=tf.float32)
with tf.GradientTape() as tape:
y=tf.matmul(w,x)
d=tape.gradient(y+2,w)
The tape did not watch the operation y+2
, so the gradient cannot be calculated.
To get the gradient of only one element, you need to explicitly get that element in the tape's context:
w=tf.Variable([[1.0,2.3],[4.5,6.7]],dtype=tf.float32)
x=tf.constant([[1.2],[2.3]],dtype=tf.float32)
with tf.GradientTape() as tape:
y=tf.matmul(w,x)
y0 = y[0]
d=tape.gradient(y0,w)
And then, you get the gradient of y[0]
w.r.t w
:
>>> d
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1.2, 2.3],
[0. , 0. ]], dtype=float32)>
这篇关于张量流仅区分向量的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!