ash
ash

Reputation: 21

Converting tf.gradients to tensor type

I'm using tensorflow 1.14.0. I would like to know how I can type cast list into tensor. I get this error when trying to use tf.convert_to_tensor(). Appreciate any help

Failed to convert object of type to Tensor. Contents: [None]. Consider casting elements to a supported type.

Here is my code

def testtf4():
    x = tf.placeholder(tf.float32, shape=[None])
    y = tf.placeholder(tf.float32, shape=[None])
    op = tf.placeholder(tf.float32, shape=[None,3])

    print("\nshape of x,y", x.shape, y.shape)
    arr = np.genfromtxt("C:\\Data\\Training_and_codes\\ML\\TF Samples\\Data.csv", delimiter=",");
    gradmulx_op = tf.gradients(op[:,0],x)
    gradmuly_op = tf.gradients(op[:,0],y)
    tgradmulx_op = tf.convert_to_tensor(gradmulx_op)
    tgradmuly_op = tf.convert_to_tensor(gradmuly_op)
    print("\nshape of gradmul tensors", tgradmulx_op.shape, tgradmuly_op.shape)

    with tf.Session() as sess:
        print("started session......\n")
        input_feed={}
        input_feed[x]=arr[:,0]
        input_feed[y]=arr[:,1]
        input_feed[op]=arr[:,2:4]
        [gradx, grady] = sess.run([tgradmulx_op, tgradmuly_op],input_feed)
        print("x gradient",gradx) 
        print("y gradient",grady) 

Upvotes: 2

Views: 352

Answers (1)

javidcf
javidcf

Reputation: 59681

Your problem does not have to do with tf.convert_to_tensor, but with the fact that your are trying to compute some gradients that do not exist. You have these two placeholders:

x = tf.placeholder(tf.float32, shape=[None])
op = tf.placeholder(tf.float32, shape=[None, 3])

And then you try to get the following gradients:

gradmulx_op = tf.gradients(op[:, 0], x)
gradmuly_op = tf.gradients(op[:, 0], y)

For these gradients to exist (that is, not be None), the value of op[:, 0] would have to be the result of one or more differentiable operations using x and y. For example, if op were defined as:

op = tf.stack([2 * x + 3 * y, x - 1, 2 * y + 2], axis=1)

Then it would work, because op[:, 0] would be computed from x and y (and possibly other values), so there is a gradient between the tensors. Or, put it a different way, changing x or y changes the value of op[:, 0]. TensorFlow keeps track of the operations used to compute each value and uses that information to automatically compute the gradients.

But op is not calculated from x and y, in fact it is not calculated from anything, since it is a placeholder, it is just a given value. A change in x or y does not entail a change in op. So there is no gradients between those tensors. I am not sure what you are trying to achieve with your code, but you probably need to rethink what exactly is the result that you want to compute.

Upvotes: 1

Related Questions