Home > Back-end >  How to add a value of right zeros in tensorflow
How to add a value of right zeros in tensorflow

Time:11-04

What I want is to add a certain number of zeros, for example, 3, at the end of the tensor. This is an example (with an invented tf funcion):

tn = tf.constant([1, 2])
# out: <tf.Tensor: shape(2,), dtype=int32, numpy=array([1, 2])>

tn = tf.add_zeros(tn, 3, 'right')
# out: <tf.Tensor: shape(5,), dtype=int32, numpy=array([1, 2, 0, 0, 0])>

Is there any way I can do that?

CodePudding user response:

You could try using tf.concat:

import tensorflow as tf

tn = tf.constant([1, 2])
# out: <tf.Tensor: shape(2,), dtype=int32, numpy=array([1, 2])>

tn = tf.concat([tn, tf.zeros((3), dtype=tf.int32)], axis=0)
print(tn)
tf.Tensor([1 2 0 0 0], shape=(5,), dtype=int32)

Or with tf.pad

t = tf.constant([1, 2])
paddings = tf.constant([[0, 3]])
tf.pad(t, paddings, "CONSTANT") 
<tf.Tensor: shape=(5,), dtype=int32, numpy=array([1, 2, 0, 0, 0], dtype=int32)>
  • Related