tensorflow: `size` argument of TensorArray works only when specified by pythonic int but tf.Tensor doesn't
System information
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04):
Linux lz 5.4.0-48-generic #52~18.04.1-Ubuntu - TensorFlow installed from (source or binary):
(in conda env) pip install tensorflow-gpu -i https://pypi.tuna.tsinghua.edu.cn/simple - TensorFlow version (use command below):
v2.3.0-rc2-23-gb36436b087 2.3.0 - Python version:
3.8 - CUDA/cuDNN version:
conda install cudatoolkit=10.1 cudnn=7.6.5 - GPU model and memory:
4 x Titan xp 12GB
Describe the current behavior
Reference issue: #43698
Thanks for @saxenasaurabh 's answer. I could initialized the TensorArray with a fixed size. However, when I tried to specify the size of the TensorArray with tf.Tensor from tf.shape, the length of the tensor after stacked together will be None:
emb.shape: TensorShape([2, 3, 180, 320, 32])
cor_l.shape: TensorShape([None, 2, 180, 320])
cor_prob1.shape: TensorShape([2, None, 180, 320])
cor_prob2.shape: TensorShape([2, None, 180, 320, 1])
cor_prob3.shape: TensorShape([2, None, 180, 320, 32])
cor_prob4.shape: TensorShape([2, 180, 320, None, 32])
cor_prob5.shape: TensorShape([2, 180, 320, None])
aligned_fea.shape: TensorShape([2, 180, 320, 3, 32])
aligned_fea.shape: TensorShape([2, 180, 320, 96])
Parts of the code are as following shows:
aligned_fea_shape = tf.shape(aligned_fea) # B, N, H, W, C
B = aligned_fea_shape[0]
N = aligned_fea_shape[1]
H = aligned_fea_shape[2]
W = aligned_fea_shape[3]
C = aligned_fea_shape[4]
####
some other codes omit here
####
emb = tf.reshape(emb, [B, N, H, W, -1])
tf.print("emb.shape:", emb.shape)
cor_l = tf.TensorArray(dtype=tf.float32, size=N) # TENSORFLOW BUG HERE !
def cond(i, N, input, arr):
return tf.less(i, N)
def body(i, N, input, arr):
emb_nbr = input[:, i, :, :, :]
cor_tmp = tf.reduce_sum(emb_nbr * emb_ref, axis=3) # B, H, W
arr = arr.write(i, cor_tmp)
i = tf.add(i, 1)
return i, N, input, arr
_, _, _, cor_l = tf.while_loop(cond, body, [0, N, emb, cor_l]) # N * (B, H, W)
cor_l = cor_l.stack() # N, B, H, W
tf.print("cor_l.shape:", cor_l.shape)
cor_l = tf.transpose(cor_l, [1, 0, 2, 3]) # B, N, H, W
cor_prob = tf.sigmoid(cor_l) # B, N, H, W
tf.print("cor_prob1.shape:", cor_prob.shape)
cor_prob = tf.expand_dims(cor_prob, axis=4) # B, N, H, W, 1
tf.print("cor_prob2.shape:", cor_prob.shape)
cor_prob = tf.tile(cor_prob, [1, 1, 1, 1, C]) # B, N, H, W, C
cor_prob = tf.transpose(cor_prob, [0, 2, 3, 1, 4]) # B, H, W, N, C
cor_prob = tf.reshape(cor_prob, [B, H, W, -1]) # B, H, W, NC
aligned_fea = tf.transpose(aligned_fea, [0, 2, 3, 1, 4]) # [B, H, W, N, C]
tf.print("aligned_fea.shape:", aligned_fea.shape)
aligned_fea = tf.reshape(aligned_fea, [B, H, W, -1]) * cor_prob
tf.print("aligned_fea.shape:", aligned_fea.shape)
Look into the comment where TENSORFLOW BUG points out:
If size is specified by N, which is tf.Tensor, the output will be None. Otherwise, the size is designated by python int, eg. cor_l = tf.TensorArray(dtype=tf.float32, size=self.nframe) where self.nframe is pythonic int, everything is fine and the first size of the tensor will be fixed. The output is as follows:
emb.shape: TensorShape([2, 3, 180, 320, 32])
cor_l.shape: TensorShape([3, 2, 180, 320])
cor_prob1.shape: TensorShape([2, 3, 180, 320])
cor_prob2.shape: TensorShape([2, 3, 180, 320, 1])
cor_prob3.shape: TensorShape([2, 3, 180, 320, 32])
cor_prob4.shape: TensorShape([2, 180, 320, 3, 32])
cor_prob5.shape: TensorShape([2, 180, 320, 96])
aligned_fea.shape: TensorShape([2, 180, 320, 3, 32])
aligned_fea.shape: TensorShape([2, 180, 320, 96])
Thus I am wondering if this is a bug of TensorFlow since the documentation says:
size: (optional) int32 scalar `Tensor`: the size of the TensorArray. Required if handle is not provided.
Please feel free to contact me if more testing code is required.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 15 (5 by maintainers)
Was able to reproduce your issue in Tensorflow 2.11, please find the gist here. Thanks!
@mdanatg Thanks for your suggestion. I knew it could be solved by recording the shape of the tensor before the
TensorArrayloop, and after performing thestackoperation,reshapecould deal with theNoneproblem well. However, it is very confusing for the user to get such kind of Tensor withNoneat the first dimension. Moreover, what I want to do is morepythonicusing TF2. Look at the following code:In Autograph mode, which means the training loop is in the decorator of
@tf.function, the user can not iteratively operates the tensor in the above way, which is much more intuitive and pythonic and Pytorch supports. I am also using TF Profiler to test ifTensorArrayis the best way to deal with such loop operations in an efficient way, while the result shows that the running time is exactly right limited by thisTensorArray stackstep. The reference issue has been pushed to #44118 This problem has troubled me for a long time. I could make my total repository public to you if you are interested in the bug/tricky problem I pointed out. The coding work is with another Googler working in Mountain View.