如何在 TensorFlow 1.10 中将 (2, 1) 张量重复为 (50, 1) 张量
how to repeat (2, 1) tensors to (50, 1) tensors in TensorFlow 1.10
例如,
# 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]
TensorFlow 1.10
中没有tf.repeat
所以有没有最好的可替换实现方式?
如果你真的只能使用 Tensorflow 1.10
然后尝试这样的事情:
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)
'''
你可以用这个
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,))
例如,
# 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]
TensorFlow 1.10
中没有tf.repeat
所以有没有最好的可替换实现方式?
如果你真的只能使用 Tensorflow 1.10
然后尝试这样的事情:
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)
'''
你可以用这个
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,))