Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional assignment of tensor values in TensorFlow

I want to replicate the following numpy code in tensorflow. For example, I want to assign a 0 to all tensor indices that previously had a value of 1.

a = np.array([1, 2, 3, 1])
a[a==1] = 0

# a should be [0, 2, 3, 0]

If I write similar code in tensorflow I get the following error.

TypeError: 'Tensor' object does not support item assignment

The condition in the square brackets should be arbitrary as in a[a<1] = 0.

Is there a way to realize this "conditional assignment" (for lack of a better name) in tensorflow?

like image 966
chillinger Avatar asked Aug 19 '16 18:08

chillinger


People also ask

How do you access values in a tensor?

We can access the value of a tensor by using indexing and slicing. Indexing is used to access a single value in the tensor. slicing is used to access the sequence of values in a tensor. we can modify a tensor by using the assignment operator.

How do you find the value of tensor in TensorFlow?

The easiest[A] way to evaluate the actual value of a Tensor object is to pass it to the Session. run() method, or call Tensor. eval() when you have a default session (i.e. in a with tf. Session(): block, or see below).

What are three parameters that define tensors in TensorFlow?

Each tensor object is defined with tensor attributes like a unique label (name), a dimension (shape) and TensorFlow data types (dtype). You can define a tensor with decimal values or with a string by changing the type of data.

What is tf Cond?

cond stitches together the graph fragments created during the true_fn and false_fn calls with some additional graph nodes to ensure that the right branch gets executed depending on the value of pred . tf. cond supports nested structures as implemented in tensorflow. python.


2 Answers

Comparison operators such as greater than are available within TensorFlow API.

However, there is nothing equivalent to the concise NumPy syntax when it comes to manipulating the tensors directly. You have to make use of individual comparison, where and assign operators to perform the same action.

Equivalent code to your NumPy example is this:

import tensorflow as tf

a = tf.Variable( [1,2,3,1] )    
start_op = tf.global_variables_initializer()    
comparison = tf.equal( a, tf.constant( 1 ) )    
conditional_assignment_op = a.assign( tf.where (comparison, tf.zeros_like(a), a) )

with tf.Session() as session:
    # Equivalent to: a = np.array( [1, 2, 3, 1] )
    session.run( start_op )
    print( a.eval() )    
    # Equivalent to: a[a==1] = 0
    session.run( conditional_assignment_op )
    print( a.eval() )

# Output is:
# [1 2 3 1]
# [0 2 3 0]

The print statements are of course optional, they are just there to demonstrate the code is performing correctly.

like image 99
Neil Slater Avatar answered Oct 22 '22 08:10

Neil Slater


I'm also just starting to use tensorflow Maybe some one will fill my approach more intuitive

import tensorflow as tf

conditionVal = 1
init_a = tf.constant([1, 2, 3, 1], dtype=tf.int32, name='init_a')
a = tf.Variable(init_a, dtype=tf.int32, name='a')
target = tf.fill(a.get_shape(), conditionVal, name='target')

init = tf.initialize_all_variables()
condition = tf.not_equal(a, target)
defaultValues = tf.zeros(a.get_shape(), dtype=a.dtype)
calculate = tf.select(condition, a, defaultValues)

with tf.Session() as session:
    session.run(init)
    session.run(calculate)
    print(calculate.eval())

main trouble is that it is difficult to implement "custom logic". if you could not explain your logic within linear math terms you need to write "custom op" library for tensorflow (more details here)

like image 31
Frut Dzentready Avatar answered Oct 22 '22 06:10

Frut Dzentready