1. 程式人生 > >Tensorflow之tf.split() 與 tf.squeeze()

Tensorflow之tf.split() 與 tf.squeeze()

split(
    value,
    num_or_size_splits,
    axis=0,
    num=None,
    name='split'
)

輸入: 
value: 輸入的tensor 
num_or_size_splits: 如果是個整數n,就將輸入的tensor分為n個子tensor。如果是個tensor T,就將輸入的tensor分為len(T)個子tensor。 
axis: 預設為0,計算value.shape[axis], 一定要能被num_or_size_splits整除。

舉例:

# num_or_size_splits是tensor T,len(T)為3,所以分為3個子tensor,
# axis為1,所以value.shape[1]為30,4+15+11正好為30
split0, split1, split2 = tf.split(value, [4, 15, 11], 1)

tf.shape(split0)  # [5, 4]
tf.shape(split1)  # [5, 15]
tf.shape(split2)  # [5, 11]

# num_or_size_splits是整數3, 分為3個tensor,value.shape[1]為30,能被3整除。
split0, split1, split2 = tf.split(value, num_or_size_splits=3, axis=1)
tf.shape(split0)  # [5, 10]

再舉個例項:

>>> a=np.reshape(range(24),(4,2,3))
>>> a
array([[[ 0,  1,  2],
        [ 3,  4,  5]],

       [[ 6,  7,  8],
        [ 9, 10, 11]],

       [[12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23]]])

>>> sess=tf.InteractiveSession()
# 將a分為兩個tensor,a.shape(1)為2,可以整除,不會報錯。
# 輸出應該為2個shape為[4,1,3]的tensor
>>> b= tf.split(a,2,1)
>>> b
[<tf.Tensor 'split:0' shape=(4, 1, 3) dtype=int32>, <tf.Tensor 'split:1' shape=(4, 1, 3) dtype=int32>]
>>> sess.run(b)
[array([[[ 0,  1,  2]],

       [[ 6,  7,  8]],

       [[12, 13, 14]],

       [[18, 19, 20]]]), array([[[ 3,  4,  5]],

       [[ 9, 10, 11]],

       [[15, 16, 17]],

       [[21, 22, 23]]])]
>>> c= tf.split(a,2,0)
# a.shape(0)為4,被2整除,輸出2個[2,2,3]的Tensor
>>> c
[<tf.Tensor 'split_1:0' shape=(2, 2, 3) dtype=int32>, <tf.Tensor 'split_1:1' shape=(2, 2, 3) dtype=int32>]
>>> sess.run(c)
[array([[[ 0,  1,  2],
        [ 3,  4,  5]],

       [[ 6,  7,  8],
        [ 9, 10, 11]]]), array([[[12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23]]])]
>>>d= tf.split(a,2,2)
#  a.shape(2)為3,不被2整除,報錯。
Traceback (most recent call last):
  File "D:\Anaconda2\envs\tensorflow\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 671, in _call_cpp_shape_fn_impl
    input_tensors_as_shapes, status)
  File "D:\Anaconda2\envs\tensorflow\lib\contextlib.py", line 66, in __exit__
    next(self.gen)
  File "D:\Anaconda2\envs\tensorflow\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 466, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimension size must be evenly divisible by 2 but is 3
        Number of ways to split should evenly divide the split dimension for 'split_1' (op: 'Split') with input shapes: [], [4,2,3] and with computed input tensors: input[0] = <2>.
>>> d= tf.split(a,3,2)
# 改成3,a.shape(2)為3,整除,不報錯,返回3個[4,2,1]的Tensor
>>> d
[<tf.Tensor 'split_2:0' shape=(4, 2, 1) dtype=int32>, <tf.Tensor 'split_2:1' shape=(4, 2, 1) dtype=int32>, <tf.Tensor 'split_2:2' shape=(4, 2, 1) dtype=int32>]
>>> sess.run(d)
[array([[[ 0],
        [ 3]],

       [[ 6],
        [ 9]],

       [[12],
        [15]],

       [[18],
        [21]]]), array([[[ 1],
        [ 4]],

       [[ 7],
        [10]],

       [[13],
        [16]],

       [[19],
        [22]]]), array([[[ 2],
        [ 5]],

       [[ 8],
        [11]],

       [[14],
        [17]],

       [[20],
        [23]]])]

注意: 
tf.split和reshape不同,不會改變數值之間的相對順序。只能每個維度只能變小,不增大。 
取值時按照axis-1的順序來取。

tf.squeeze
squeeze(
    input,
    axis=None,
    name=None,
    squeeze_dims=None
)

去掉維數為1的維度。 
舉個例子:

# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
tf.shape(tf.squeeze(t))  # [2, 3]

也可以指定去掉哪個維度:

# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
tf.shape(tf.squeeze(t, [2, 4]))  # [1, 2, 3, 1]