【问题标题】:TensorFlow: Alternative to tf.stack() operationTensorFlow:替代 tf.stack() 操作
【发布时间】:2020-12-07 17:23:26
【问题描述】:

TensorFlow 支持栈操作如下: “将 rank-R 张量列表堆叠成一个 rank-(R+1) 张量”。

我的问题是,我们可以使用其他操作(如 tf.concat 或 tf.expand_dims)或其他任何操作来模拟 tf.stack 的行为吗?我的意图是跳过使用 tf.stack

【问题讨论】:

  • 您可以使用 concat 后跟 reshape。但是……为什么?
  • 例如 tflite GPU 委托缺少堆栈
  • 是的,我使用的委托人没有实现堆栈操作。所以我需要结合其他操作来实现功能。 concat 后跟 reshape 是如何工作的?你能详细说明一下吗?

标签: tensorflow deep-learning neural-network pack


【解决方案1】:

您可以使用tf.concat 操作和tf.expand_dims 来实现此目的,下面是一个示例。

使用堆栈:

t1 = tf.constant([1,2,3])
t2 = tf.constant([4,5,6]) 

tf.stack((t1,t2),axis=0)

结果:

<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[1, 2, 3],
       [4, 5, 6]], dtype=int32)> 

使用连接:

tf.concat((tf.expand_dims(t1,0),tf.expand_dims(t2,0)),axis=0)

结果:

<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[1, 2, 3],
       [4, 5, 6]], dtype=int32)>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多