首页 文章

使用tf.gather选择顶部值样本以获得线性模型结果'No gradients provided for any variable'错误

提问于
浏览
0

我想按照以下步骤进行训练:

  • 为线性创建张量:target = Weight * X.

  • 选择最高目标值并删除所有其余样本 .

  • 获取相应的标签,即Y.

  • 使用GradientDescentOptimizer,最小化sum(Y)并得到拟合变量(W)

from tensorflow.python.framework import ops
import numpy as np
import tensorflow as tf

sess = tf.Session()
X=tf.placeholder(shape=[None,2], dtype=tf.float32)
Y=tf.placeholder(shape=[None,1], dtype=tf.float32)
W = tf.Variable(tf.random_normal(shape=[2, 1]), dtype=tf.float32)

target=tf.matmul(X, W)

flattened=tf.reshape(target,[-1])
selected_targets, keys=tf.nn.top_k(flattened, k=100)

#get corresponding Y
selected_y = tf.gather(Y, keys)

#now we have top 100 selected_targets, and selected_y, train and evaluate W, and fit minimal sum(Y)
train_target = tf.reduce_sum(selected_y) #But if use selected_targets instead of selected_y, it would run successfully, why?
optimizer = tf.train.GradientDescentOptimizer(1)
train = optimizer.minimize(train_target)

# training
x_vals = np.random.rand(1000,2)
y_vals = np.random.rand(1000,1)

sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
sess.run(train, {X: x_vals, Y:y_vals})
print(sess.run([W]))

我收到了这个错误:

ValueError:没有为任何变量提供渐变,检查图表中不支持渐变的ops,变量[“”]和损失Tensor(“Sum:0”,shape =(),dtype = float32) .

有人可以帮忙解决这个问题吗?我发现在张量上应用 tf.nn.top_k 时会发生这种情况 . 但为什么?

1 回答

  • 0

    它说这是因为没有渐变 . 您创建的图形是不可区分的,因为键是唯一将损失连接到W变量的键,而键是不可微分的整数 .

相关问题