简体   繁体   中英

how to repeat (2, 1) tensors to (50, 1) tensors in TensorFlow 1.10

For example,

# x is a tensor
print(x)
[1, 0] 

# after repeating it
print(x)
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

There is no tf.repeat in TensorFlow 1.10 so if there is the best replaceable way to implement it?
Thanks in advance.

If you can really only use Tensorflow 1.10 then try something like this:

import tensorflow as tf

x = tf.constant([1, 0])
x = tf.reshape(tf.tile(tf.expand_dims(x, -1), [1, 25]), (50, 1))
print(x)

'''
tf.Tensor(
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0], shape=(50, 1), dtype=int32)
'''

You could go with this

import tensorflow as tf

x = tf.constant([1, 0])
temp = tf.zeros(shape=(25, 2), dtype=tf.int32)

result = tf.reshape(tf.transpose(temp + x), (-1,))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM