I would like to slice a tensor and store it in a variable. Slicing with fixed numbers works fine eg: t[0:2]
. But slicing a variable with another tensor doesnt work. eg t[t1:t2]
Also storing the slice in a tensor works fine but when I try to store it in a tf.Variable i get errors.
import tensorflow as tf
import numpy
i=tf.zeros([2,1],tf.int32)
i2=tf.get_variable('i2_variable',initializer=i) #putting a multidimensional tensor in a variable
i4=tf.ones([10,1],tf.int32)
sess=tf.Session()
sess.run(tf.global_variables_initializer()) #initializing variables
itr=tf.constant(0,tf.int32)
def w_c(i2,itr):
return tf.less(itr,2)
def w_b(i2,itr):
i2=i4[(itr*0):((itr*0)+2)] #doesnt work
#i2=i4[0:2] #works
#i=i4[(itr*0):((itr*0)+2)] #works with tensor i
itr=tf.add(itr,1)
return[i2,itr]
OP=tf.while_loop(w_c,w_b,[i2,itr])
print(sess.run(OP))
I get following error:
ValueError: Input tensor 'i2_variable/read:0' enters the
loop with shape (2, 1), but has shape (?, 1) after one iteration.
To allow the shape to vary across iterations,
use the `shape_invariants` argument of tf.while_loop to specify a less-specific shape.