Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: No gradients provided for any variable in Tensorflow

Tags:

tensorflow

I'm trying to create a dice_loss function in Tensorflow. I'm facing a trouble with tensorlfow. Executing the following code

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)

I get the following error:

ValueError: No gradients provided for any variable: ...

Someone can help me?

like image 662
Tramac Avatar asked Jan 28 '26 01:01

Tramac


1 Answers

When you do annotation_pred = tf.to_float(tf.argmax(out, dimension=4, name='prediction')), you get an index of the max value in your tensor. This index can't be derivated, thus the gradient can't flow throught this operation.

So as your loss is only defined by this value, and the gradient can't flow throught it, no gradient can be calculated for your network.

I don't know specificately how the dice loss work, but maybe you wanted to use tf.max instead of tf.argmax, or you have to find a way to use an operation that can let the gradient flow.

like image 90
Arnaud De Broissia Avatar answered Feb 03 '26 07:02

Arnaud De Broissia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!