How to explicitly broadcast a tensor to match another's shape in tensorflow?

前端 未结 4 1829
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-13 09:17

I have three tensors, A, B and C in tensorflow, A and B are both of shape (m, n, r), C is a binary tensor of sha

4条回答
  •  无人共我
    2021-02-13 09:48

    EDIT: In all versions of TensorFlow since 0.12rc0, the code in the question works directly. TensorFlow will automatically stack tensors and Python numbers into a tensor argument. The solution below using tf.pack() is only needed in versions prior to 0.12rc0. Note that tf.pack() was renamed to tf.stack() in TensorFlow 1.0.


    Your solution is very close to working. You should replace the line:

    C = tf.tile(C, [1,1,tf.shape(C)[2]])
    

    ...with the following:

    C = tf.tile(C, tf.pack([1, 1, tf.shape(A)[2]]))
    

    (The reason for the issue is that TensorFlow won't implicitly convert a list of tensors and Python literals into a tensor. tf.pack() takes a list of tensors, so it will convert each of the elements in its input (1, 1, and tf.shape(C)[2]) to a tensor. Since each element is a scalar, the result will be a vector.)

提交回复
热议问题