梯度错误地返回非

2024-03-29 05:29:46 发布

您现在位置:Python中文网/ 问答频道 /正文

print("\ncomputed 1")
x=tfe.Variable(np.random.uniform(size=[166,]), name='x', dtype=tf.float64)
print(compute_cost(tf.constant(normed_data[:10], dtype=tf.float64), x))

print("\ncomputed 2")
x=tfe.Variable(np.random.uniform(size=[166,]), name='x', dtype=tf.float64)
print(compute_cost(tf.constant(normed_data[:10], dtype=tf.float64), x))

print("\ncomputed 0")
x=tfe.Variable(np.zeros((166,)), name='x', dtype=tf.float64)
print(compute_cost(tf.constant(normed_data[:10], dtype=tf.float64), x))

print("\ncomputed 0")
x=tfe.Variable(np.zeros((166,)), name='x', dtype=tf.float64)
print(compute_cost(tf.constant(normed_data[:10], dtype=tf.float64), x))

输出:

computed 1
tf.Tensor(0.00627350861776064, shape=(), dtype=float64)

computed 2
tf.Tensor(0.0032756996843633264, shape=(), dtype=float64)

computed 0
tf.Tensor(19.318829784931626, shape=(), dtype=float64)

computed 0
tf.Tensor(19.318829784931626, shape=(), dtype=float64)

显然tfe.变量x影响损失函数,所以梯度不应该是无/0。然而它是。。。你知道吗

x=tfe.Variable(np.random.uniform(size=[166,]), name='x', dtype=tf.float64)
f_grad = tfe.gradients_function(lambda s: compute_cost(tf.constant(normed_data[:10], dtype=tf.float64), s), params=['s'])
print("\nGradient " + str(f_grad(x)))

输出:

Gradient [None]

有人能解释怎么了吗?使用Tensorflow 1.10.1和急切执行。你知道吗


Tags: namedatatfnpvariableprintcomputedtype
1条回答
网友
1楼 · 发布于 2024-03-29 05:29:46

返回值None表示函数的输出和变量之间没有依赖关系。你知道吗

查看链接到的compute_cost函数I see a numpy operation (^{}) in there,它将打破可微操作的“跟踪”。请注意,TensorFlow只能通过TensorFlow操作计算梯度。你知道吗

your other question类似,将numpy操作替换为相应的TensorFlow操作(^{})应该可以做到这一点。你知道吗

希望有帮助。你知道吗

相关问题 更多 >