簡體   English   中英

梯度錯誤地不返回

[英]Gradient erroneously returns none

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.Variable x正在影響損失函數,因此梯度不應為None / 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與熱切的執行配合使用。

返回值None表示函數的輸出和變量之間沒有依賴關系。

查看鏈接到的compute_cost函數, 我在其中看到一個numpy操作( np.log ,它將破壞可區分操作的“痕跡”。 請注意,TensorFlow只能通過TensorFlow操作來計算梯度。

您的其他問題類似,將numpy操作替換為相應的TensorFlow一個( tf.log )應該可以解決問題。

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM