ValueError:没有为Tensorflow中的任何变量提供梯度
我试图在Tensorflow中创建一个骰子损失函数。 我正面临着tensorlfow的麻烦。执行以下代码ValueError:没有为Tensorflow中的任何变量提供梯度,tensorflow,Tensorflow,我试图在Tensorflow中创建一个骰子损失函数。 我正面临着tensorlfow的麻烦。执行以下代码 import tensorflow as tf import tensorlayer as tl def conv3d(x, inChans, outChans, kernel_size, stride, padding): weights = weight_variable([kernel_size, kernel_size, kernel_size, inChans, out
import tensorflow as tf
import tensorlayer as tl
def conv3d(x, inChans, outChans, kernel_size, stride, padding):
weights = weight_variable([kernel_size, kernel_size, kernel_size, inChans, outChans])
biases = bias_variable([outChans])
conv = tf.nn.conv3d(x, weights, strides=[1, stride, stride, stride, 1], padding=padding)
return tf.nn.bias_add(conv, biases)
def train(loss_val, var_list):
optimizer = tf.train.AdamOptimizer(FLAGS.learning_rate)
grads = optimizer.compute_gradients(loss_val, var_list=var_list)
return optimizer.apply_gradients(grads)
def main(argv=None):
image = tf.placeholder(tf.float32, shape=[None, SLICE_SIZE, IMAGE_SIZE, IMAGE_SIZE, 1], name="input_image")
annotation = tf.placeholder(tf.float32, shape=[None, SLICE_SIZE, IMAGE_SIZE, IMAGE_SIZE, 1], name="annotation")
logits, pred_annotation = vnet.VNet(image)
loss = 1 - tl.cost.dice_coe(output=pred_annotation, target=annotation, axis=[1,2,3,4])
trainable_var = tf.trainable_variables()
train_op = train(loss, trainable_var)
sess = tf.Session()
...
...
def VNet(x):
...
out = tf.nn.elu(BatchNorm3d(conv3d(x, inChans, 2, kernel_size=5, stride=1, padding="SAME")))
out = conv3d(out, 2, 2, kernel_size=1, stride=1, padding="SAME")
annotation_pred = tf.to_float(tf.argmax(out, dimension=4, name='prediction'))
return out, tf.expand_dims(annotation_pred, dim=4)
我得到以下错误:
ValueError:没有为任何变量提供渐变:
有人能帮我吗?当你做
注释时,你会得到张量中最大值的索引。此索引无法推导,因此梯度无法通过此操作
因此,由于你的损失仅由这个值定义,梯度不能通过它,因此无法计算网络的梯度
我不知道骰子损失是如何具体工作的,但也许你想使用tf.max
而不是tf.argmax
,或者你必须找到一种方法来使用一种可以让梯度流动的操作。没有足够的代码来解决这个问题。。。你怎么称呼你的grads操作,vnet中有什么,等等谢谢@Arnaud De Broissia,我修改了代码,你有什么想法吗?我认为你的分析很合理!我会试试你的建议。非常感谢你!