在计算损失之前,我正在尝试处理网络的输出。网络输出2个值,假设x,y。我想为输出生成一个直方图矩阵。基本上,我想计算特定(x,y)发生的次数并将该值存储在其中的(x,y)矩阵中。我可以使用 tf.scatter_nd 为我的地面真理成功地进行预测,但是当我计算地面真理计数矩阵和预测计数矩阵之间的l2损失时,我得到 ValueError:否为任何变量提供的渐变,请检查图形中不支持渐变的操作。下面我共享了一个代码,请让我知道错误或解决方法。
l2_loss = tf.constant([0], dtype=tf.float32)
for j in range(batch_size):
count_matrix_estimated_frame1 = tf.scatter_nd(int_estimated_frame1[j,:,:], value_estimated_frame1, shape, name='scatter_nd1')
count_matrix_frame1_gt_events = tf.scatter_nd(int_frame1_gt_events[j,:,:], value_frame1_gt_events, shape, name='scatter_nd2')
count_matrix_frame2_gt_events = tf.scatter_nd(int_frame2_gt_events[j,:,:], value_frame1_gt_events, shape, name='scatter_nd3')
l2_loss = tf.add(tf.reduce_mean(\
tf.reduce_sum((count_matrix_estimated_frame1 - count_matrix_frame1_gt_events)\
*(count_matrix_estimated_frame1 - count_matrix_frame1_gt_events), axis=1, name='reduce_sum')/2.0, name='reduce_mean'), l2_loss)
return l2_loss
上面提到的是损失函数的摘录。该函数仅包含除此以外的变量的声明。当我使用此损失函数运行代码时,出现以下错误:
File "train.py", line 300, in <module>
train()
File "train.py", line 128, in train
train_op = optimizer.minimize(loss, global_step=batch)
File "/home/<name_of_user>/.local/lib/python3.5/site-packages/tensorflow/python/training/optimizer.py", line 406, in minimize
([str(v) for _, v in grads_and_vars], loss))
ValueError: No gradients provided for any variable, check your graph for ops that do not support gradients,
between variables ["<tf.Variable 'Variable:0' shape=() dtype=int32_ref>", "<tf.Variable 'sa1/layer1/conv0/weights:0'
shape=(1, 1, 6, 32) dtype=float32_ref>" ........
错误继续出现
我首先认为散点图没有梯度,但是使用get_gradient_function()我发现散点图具有梯度。我不知道是什么问题。任何帮助都会很棒。