【问题标题】:how to repeat (2, 1) tensors to (50, 1) tensors in TensorFlow 1.10如何在 TensorFlow 1.10 中将 (2, 1) 张量重复到 (50, 1) 张量
【发布时间】:2021-10-27 09:10:58
【问题描述】:

例如,

# 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,那么是否有最好的可替换方式来实现它?

【问题讨论】:

  • 您可能还应该报告您是如何产生x 的以及您的边界条件是什么......例如,这个任务在 NumPy 中非常简单......如果没有额外的信息,它会很简单建议使用它,然后来回移动你想要的任何对象类型。
  • @norok2 但是tf 1.10中没有tensor.numpy(),并且在注册会话时不能使用tensor.eval(),所以可能很难使用numpy来解决这个问题。

标签: python tensorflow tensor


【解决方案1】:

如果你真的只能使用 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)
'''

【讨论】:

    【解决方案2】:

    你可以用这个

    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,))
    

    【讨论】:

      猜你喜欢
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      相关资源
      最近更新 更多