Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluate a condition on each element of a vector y in tensorflow

Tags:

tensorflow

I am trying to evaluate a condition on each element of a vector y so that I get a vector whose i’th element tells me whether y[i]satisfies the condition. Is there any way to do this without using loops? So far, I have tried the following:

dim = 3

x = tf.placeholder(tf.float32, shape = [dim])
y = tf.log(x)

tf1 = tf.constant(1)
tf0 = tf.constant(0)


x_0 = tf.tile([x[0]], [dim])

delta = tf.cond(tf.equal(y,x_0),  tf1, tf0))
sess = tf.Session()
a = np.ones((1,3))

print(sess.run(delta, feed_dict={x:a}))

For a given input x, I want delta[i] to be 1 if y[i] = x[0] and 0 otherwise.

I get error

shape must be of equal rank but are 0 and 1 for 'Select_2' (op: 'select') with input shapes [3], [],[]

I am new to TensorFlow, any help would be appreciated!

like image 367
mund_ak Avatar asked Dec 04 '25 18:12

mund_ak


1 Answers

Seems like that you have error because you are trying to compare tensors with different shape.

That's working code:

import tensorflow as tf
import numpy as np

dim = 3

x = tf.placeholder(tf.float32, shape=(1, dim), name='ktf')
y = tf.log(x)

delta = tf.cast(tf.equal(y, x[0]), dtype=tf.int32)

sess = tf.Session()
a = np.ones((1, 3))

print(sess.run(delta, feed_dict={x: a}))
like image 118
V. Smirnov Avatar answered Dec 09 '25 06:12

V. Smirnov