张量流中的分裂张量

Abh*_*tia 2 tensorflow

我想把张量分成两部分:

ipdb> mean_log_std
<tf.Tensor 'pi/add_5:0' shape=(?, 2) dtype=float32>
Run Code Online (Sandbox Code Playgroud)

背景:?对于样本数量而另一个维度是2.我想沿着第二维分割成沿该维度的形状1的两个张量流.

我尝试了什么?(https://www.tensorflow.org/api_docs/python/tf/slice)

ipdb> tf.slice(mean_log_std,[0,2],[0,1])
<tf.Tensor 'pi/Slice_6:0' shape=(0, 1) dtype=float32>
ipdb> tf.slice(mean_log_std,[0,1],[0,1])
<tf.Tensor 'pi/Slice_7:0' shape=(0, 1) dtype=float32>
ipdb>
Run Code Online (Sandbox Code Playgroud)

我希望上面两个分割的形状为(?,1)和(?,1).

Psi*_*dom 7

您可以使用以下方法在第二维切片张量:

x[:,0:1], x[:,1:2]
Run Code Online (Sandbox Code Playgroud)

或者在第二轴上拆分:

y, z = tf.split(x, 2, axis=1)
Run Code Online (Sandbox Code Playgroud)

示例:

import tensorflow as tf

x = tf.placeholder(tf.int32, shape=[None, 2])

y, z = x[:,0:1], x[:,1:2]

y
#<tf.Tensor 'strided_slice_2:0' shape=(?, 1) dtype=int32>

z
#<tf.Tensor 'strided_slice_3:0' shape=(?, 1) dtype=int32>

with tf.Session() as sess:
    print(sess.run(y, {x: [[1,2],[3,4]]}))
    print(sess.run(z, {x: [[1,2],[3,4]]}))
#[[1]
# [3]]
#[[2]
# [4]]
Run Code Online (Sandbox Code Playgroud)

拆分:

y, z = tf.split(x, 2, axis=1)

with tf.Session() as sess:
    print(sess.run(y, {x: [[1,2],[3,4]]}))
    print(sess.run(z, {x: [[1,2],[3,4]]}))
#[[1]
# [3]]
#[[2]
# [4]]
Run Code Online (Sandbox Code Playgroud)

  • `x [:,0:1]`切片张量并保持原始尺寸,即2,而`x [:,0]`从张量中提取单个列,因此将尺寸减小1. (2认同)