tf.split (API r1.3)
https://github.com/tensorflow/docs/blob/r1.3/site/en/api_docs/api_docs/python/tf/split.md
site/en/api_docs/api_docs/python/tf/split.md
split(
value,
num_or_size_splits,
axis=0,
num=None,
name='split'
)
Defined in tensorflow/python/ops/array_ops.py
.
See the guide: Tensor Transformations > Slicing and Joining
Splits a tensor into sub tensors.
將張量分割成子張量。
If num_or_size_splits
is an integer type, num_split
, then splits value
along dimension axis
into num_split
smaller tensors. Requires that num_split
evenly divides value.shape[axis]
.
如果 num_or_size_splits 是整數類型 (num_split),則沿維度 axis 分割 value 成爲 num_split 更小的張量。要求 num_split 均勻分配 value.shape[axis]。
If num_or_size_splits
is not an integer type, it is presumed to be a Tensor size_splits
, then splits value
into len(size_splits)
pieces. The shape of the i
-th piece has the same size as the value
except along dimension axis
where the size is size_splits[i]
.
如果 num_or_size_splits 不是整數類型,則它被認爲是一個張量 size_splits,然後將 value 分割成 len(size_splits) 塊。第 i 部分的形狀與 value 的大小相同,除了沿維度 axis 之外的大小 size_splits[i]。
For example:
# 'value' is a tensor with shape [5, 30]
# Split 'value' into 3 tensors with sizes [4, 15, 11] along dimension 1
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]
# Split 'value' into 3 tensors along dimension 1
split0, split1, split2 = tf.split(value, num_or_size_splits=3, axis=1)
tf.shape(split0) # [5, 10]
1. Args
value
: TheTensor
to split.num_or_size_splits
: Either a 0-D integerTensor
indicating the number of splits along split_dim or a 1-D integerTensor
integer tensor containing the sizes of each output tensor along split_dim. If a scalar then it must evenly dividevalue.shape[axis]
; otherwise the sum of sizes along the split dimension must match that of thevalue
. (指示沿 split_dim 分割數量爲 0-D 整數 Tensor 或包含沿 split_dim 每個輸出張量大小的 1-D 整數 Tensor。如果爲一個標量,那麼它必須均勻分割 value.shape[axis],否則沿分割維度的大小總和必須與該 value 相匹配。)axis
: A 0-Dint32
Tensor
. The dimension along which to split. Must be in the range[-rank(value), rank(value))
. Defaults to 0. (表示分割的尺寸,必須在 [-rank(value), rank(value)) 範圍內,默認爲 0。)num
: Optional, used to specify the number of outputs when it cannot be inferred from the shape ofsize_splits
. (可選的,用於指定無法從 size_splits 的形狀推斷出的輸出數目。)name
: A name for the operation (optional). (操作的名稱 (可選)。)
如果 num_or_size_splits 傳入的是一個整數,直接在 axis 維度上把張量平均切分成幾個小張量。
如果 num_or_size_splits 傳入的是一個向量 (向量各個元素的和要與原本這個維度的數值相等) 就根據這個向量有幾個元素分爲幾項。
2. Returns
if num_or_size_splits
is a scalar returns num_or_size_splits
Tensor
objects; if num_or_size_splits
is a 1-D Tensor returns num_or_size_splits.get_shape[0]
Tensor
objects resulting from splitting value
.
如果 num_or_size_splits 是標量,返回 num_or_size_splits Tensor對象。如果 num_or_size_splits 是一維張量,則返回由 value 分割產生的 num_or_size_splits.get_shape[0] Tensor 對象。
3. Raises
ValueError
: Ifnum
is unspecified and cannot be inferred. (如果 num 沒有指定並且無法推斷。)
4. Example
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import os
import sys
import numpy as np
import tensorflow as tf
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))
print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")
value = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]]
split0, split1, split2 = tf.split(value, [1, 1, 1], 0)
split3, split4, split5 = tf.split(value, [1, 2, 1], 1)
with tf.Session() as sess:
print("split0:\n", sess.run(split0))
print('-' * 32)
print("split1:\n", sess.run(split1))
print('-' * 32)
print("split2:\n", sess.run(split2))
print('-' * 32)
print("split3:\n", sess.run(split3))
print('-' * 32)
print("split4:\n", sess.run(split4))
print('-' * 32)
print("split5:\n", sess.run(split5))
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
2019-08-20 08:15:08.428010: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2019-08-20 08:15:08.485819: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-08-20 08:15:08.486035: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties:
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335
pciBusID: 0000:01:00.0
totalMemory: 7.92GiB freeMemory: 3.80GiB
2019-08-20 08:15:08.486046: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
split0:
[[1 2 3 4]]
--------------------------------
split1:
[[5 6 7 8]]
--------------------------------
split2:
[[ 9 10 11 12]]
--------------------------------
split3:
[[1]
[5]
[9]]
--------------------------------
split4:
[[ 2 3]
[ 6 7]
[10 11]]
--------------------------------
split5:
[[ 4]
[ 8]
[12]]
Process finished with exit code 0
5. Example
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import os
import sys
import numpy as np
import tensorflow as tf
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))
print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")
value = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]]
split0, split1, split2 = tf.split(value, 3, axis=0)
split3, split4, split5, split6 = tf.split(value, num_or_size_splits=4, axis=1)
with tf.Session() as sess:
print("split0:\n", sess.run(split0))
print('-' * 32)
print("split1:\n", sess.run(split1))
print('-' * 32)
print("split2:\n", sess.run(split2))
print('-' * 32)
print("split3:\n", sess.run(split3))
print('-' * 32)
print("split4:\n", sess.run(split4))
print('-' * 32)
print("split5:\n", sess.run(split5))
print('-' * 32)
print("split6:\n", sess.run(split6))
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
2019-08-19 09:13:37.946792: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2019-08-19 09:13:38.027963: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-08-19 09:13:38.028232: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties:
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335
pciBusID: 0000:01:00.0
totalMemory: 7.92GiB freeMemory: 7.42GiB
2019-08-19 09:13:38.028256: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
split0:
[[1 2 3 4]]
--------------------------------
split1:
[[5 6 7 8]]
--------------------------------
split2:
[[ 9 10 11 12]]
--------------------------------
split3:
[[1]
[5]
[9]]
--------------------------------
split4:
[[ 2]
[ 6]
[10]]
--------------------------------
split5:
[[ 3]
[ 7]
[11]]
--------------------------------
split6:
[[ 4]
[ 8]
[12]]
Process finished with exit code 0
6. Example
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import os
import sys
import numpy as np
import tensorflow as tf
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))
print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")
batch_size = 1
num_step = 6
num_input = 2
# x_anchor shape: (batch_size, num_step, num_input)
x_anchor = tf.constant([[[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9],
[10, 11]]], dtype=np.float32)
# Permute num_step and batch_size.
y_anchor = tf.transpose(x_anchor, perm=[1, 0, 2])
# (num_step * batch_size, num_input)
y_reshape = tf.reshape(y_anchor, [num_step * batch_size, num_input])
# Split data because rnn cell needs a list of inputs for the RNN inner loop num_step * (batch_size, num_input)
y_split = tf.split(y_reshape, num_step, 0)
with tf.Session() as sess:
input_x_anchor = sess.run(x_anchor)
print("type(input_x_anchor):", type(input_x_anchor))
print("input_x_anchor.shape:", input_x_anchor.shape)
print(8 * "++--")
output_y_anchor = sess.run(y_anchor)
print("type(output_y_anchor):", type(output_y_anchor))
print("output_y_anchor.shape:", output_y_anchor.shape)
print("output_y_anchor:\n", output_y_anchor)
print(8 * "++--")
output_y_reshape = sess.run(y_reshape)
print("type(output_y_reshape):", type(output_y_reshape))
print("output_y_reshape.shape:", output_y_reshape.shape)
print("output_y_reshape:\n", output_y_reshape)
print(8 * "++--")
output_y_split = sess.run(y_split)
print("type(output_y_split):", type(output_y_split))
print("output_y_split:\n", output_y_split)
print(8 * "++--")
print("output_y_split[0-5]:")
for step in range(num_step):
print("type(output_y_split[%d]):%s" % (step, type(output_y_split[step])))
print("output_y_split[%d]:\n" % (step), output_y_split[step])
print(8 * "++--")
print("output_y_split[0-5]:")
for step in range(num_step):
print("type([output_y_split[%d]]):%s" % (step, type([output_y_split[step]])))
print("[output_y_split[%d]]:\n" % (step), [output_y_split[step]])
print(8 * "++--")
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
2019-08-19 10:39:35.983470: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2019-08-19 10:39:36.065438: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-08-19 10:39:36.065680: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties:
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335
pciBusID: 0000:01:00.0
totalMemory: 7.92GiB freeMemory: 7.39GiB
2019-08-19 10:39:36.065692: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
type(input_x_anchor): <type 'numpy.ndarray'>
input_x_anchor.shape: (1, 6, 2)
++--++--++--++--++--++--++--++--
type(output_y_anchor): <type 'numpy.ndarray'>
output_y_anchor.shape: (6, 1, 2)
output_y_anchor:
[[[ 0. 1.]]
[[ 2. 3.]]
[[ 4. 5.]]
[[ 6. 7.]]
[[ 8. 9.]]
[[10. 11.]]]
++--++--++--++--++--++--++--++--
type(output_y_reshape): <type 'numpy.ndarray'>
output_y_reshape.shape: (6, 2)
output_y_reshape:
[[ 0. 1.]
[ 2. 3.]
[ 4. 5.]
[ 6. 7.]
[ 8. 9.]
[10. 11.]]
++--++--++--++--++--++--++--++--
type(output_y_split): <type 'list'>
output_y_split:
[array([[0., 1.]], dtype=float32), array([[2., 3.]], dtype=float32), array([[4., 5.]], dtype=float32), array([[6., 7.]], dtype=float32), array([[8., 9.]], dtype=float32), array([[10., 11.]], dtype=float32)]
++--++--++--++--++--++--++--++--
output_y_split[0-5]:
type(output_y_split[0]):<type 'numpy.ndarray'>
output_y_split[0]:
[[0. 1.]]
type(output_y_split[1]):<type 'numpy.ndarray'>
output_y_split[1]:
[[2. 3.]]
type(output_y_split[2]):<type 'numpy.ndarray'>
output_y_split[2]:
[[4. 5.]]
type(output_y_split[3]):<type 'numpy.ndarray'>
output_y_split[3]:
[[6. 7.]]
type(output_y_split[4]):<type 'numpy.ndarray'>
output_y_split[4]:
[[8. 9.]]
type(output_y_split[5]):<type 'numpy.ndarray'>
output_y_split[5]:
[[10. 11.]]
++--++--++--++--++--++--++--++--
output_y_split[0-5]:
type([output_y_split[0]]):<type 'list'>
[output_y_split[0]]:
[array([[0., 1.]], dtype=float32)]
type([output_y_split[1]]):<type 'list'>
[output_y_split[1]]:
[array([[2., 3.]], dtype=float32)]
type([output_y_split[2]]):<type 'list'>
[output_y_split[2]]:
[array([[4., 5.]], dtype=float32)]
type([output_y_split[3]]):<type 'list'>
[output_y_split[3]]:
[array([[6., 7.]], dtype=float32)]
type([output_y_split[4]]):<type 'list'>
[output_y_split[4]]:
[array([[8., 9.]], dtype=float32)]
type([output_y_split[5]]):<type 'list'>
[output_y_split[5]]:
[array([[10., 11.]], dtype=float32)]
++--++--++--++--++--++--++--++--
Process finished with exit code 0
7. Example
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import os
import sys
import numpy as np
import tensorflow as tf
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))
print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")
batch_size = 2
num_step = 6
num_input = 2
# x_anchor shape: (batch_size, num_step, num_input)
x_anchor = tf.constant([[[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9],
[10, 11]],
[[12, 13],
[14, 15],
[16, 17],
[18, 19],
[20, 21],
[22, 23]]], dtype=np.float32)
# Permute num_step and batch_size.
y_anchor = tf.transpose(x_anchor, perm=[1, 0, 2])
# (num_step * batch_size, num_input)
y_reshape = tf.reshape(y_anchor, [num_step * batch_size, num_input])
# Split data because rnn cell needs a list of inputs for the RNN inner loop num_step * (batch_size, num_input)
y_split = tf.split(y_reshape, num_step, 0)
with tf.Session() as sess:
input_x_anchor = sess.run(x_anchor)
print("type(input_x_anchor):", type(input_x_anchor))
print("input_x_anchor.shape:", input_x_anchor.shape)
print(8 * "++--")
output_y_anchor = sess.run(y_anchor)
print("type(output_y_anchor):", type(output_y_anchor))
print("output_y_anchor.shape:", output_y_anchor.shape)
print("output_y_anchor:\n", output_y_anchor)
print(8 * "++--")
output_y_reshape = sess.run(y_reshape)
print("type(output_y_reshape):", type(output_y_reshape))
print("output_y_reshape.shape:", output_y_reshape.shape)
print("output_y_reshape:\n", output_y_reshape)
print(8 * "++--")
output_y_split = sess.run(y_split)
print("type(output_y_split):", type(output_y_split))
print("output_y_split:\n", output_y_split)
print(8 * "++--")
print("output_y_split[0-5]:")
for step in range(num_step):
print("type(output_y_split[%d]):%s" % (step, type(output_y_split[step])))
print("output_y_split[%d]:\n" % (step), output_y_split[step])
print(8 * "++--")
print("output_y_split[0-5]:")
for step in range(num_step):
print("type([output_y_split[%d]]):%s" % (step, type([output_y_split[step]])))
print("[output_y_split[%d]]:\n" % (step), [output_y_split[step]])
print(8 * "++--")
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
2019-08-19 11:18:03.818784: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2019-08-19 11:18:03.896519: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-08-19 11:18:03.896755: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties:
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335
pciBusID: 0000:01:00.0
totalMemory: 7.92GiB freeMemory: 7.38GiB
2019-08-19 11:18:03.896766: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
type(input_x_anchor): <type 'numpy.ndarray'>
input_x_anchor.shape: (2, 6, 2)
++--++--++--++--++--++--++--++--
type(output_y_anchor): <type 'numpy.ndarray'>
output_y_anchor.shape: (6, 2, 2)
output_y_anchor:
[[[ 0. 1.]
[12. 13.]]
[[ 2. 3.]
[14. 15.]]
[[ 4. 5.]
[16. 17.]]
[[ 6. 7.]
[18. 19.]]
[[ 8. 9.]
[20. 21.]]
[[10. 11.]
[22. 23.]]]
++--++--++--++--++--++--++--++--
type(output_y_reshape): <type 'numpy.ndarray'>
output_y_reshape.shape: (12, 2)
output_y_reshape:
[[ 0. 1.]
[12. 13.]
[ 2. 3.]
[14. 15.]
[ 4. 5.]
[16. 17.]
[ 6. 7.]
[18. 19.]
[ 8. 9.]
[20. 21.]
[10. 11.]
[22. 23.]]
++--++--++--++--++--++--++--++--
type(output_y_split): <type 'list'>
output_y_split:
[array([[ 0., 1.],
[12., 13.]], dtype=float32), array([[ 2., 3.],
[14., 15.]], dtype=float32), array([[ 4., 5.],
[16., 17.]], dtype=float32), array([[ 6., 7.],
[18., 19.]], dtype=float32), array([[ 8., 9.],
[20., 21.]], dtype=float32), array([[10., 11.],
[22., 23.]], dtype=float32)]
++--++--++--++--++--++--++--++--
output_y_split[0-5]:
type(output_y_split[0]):<type 'numpy.ndarray'>
output_y_split[0]:
[[ 0. 1.]
[12. 13.]]
type(output_y_split[1]):<type 'numpy.ndarray'>
output_y_split[1]:
[[ 2. 3.]
[14. 15.]]
type(output_y_split[2]):<type 'numpy.ndarray'>
output_y_split[2]:
[[ 4. 5.]
[16. 17.]]
type(output_y_split[3]):<type 'numpy.ndarray'>
output_y_split[3]:
[[ 6. 7.]
[18. 19.]]
type(output_y_split[4]):<type 'numpy.ndarray'>
output_y_split[4]:
[[ 8. 9.]
[20. 21.]]
type(output_y_split[5]):<type 'numpy.ndarray'>
output_y_split[5]:
[[10. 11.]
[22. 23.]]
++--++--++--++--++--++--++--++--
output_y_split[0-5]:
type([output_y_split[0]]):<type 'list'>
[output_y_split[0]]:
[array([[ 0., 1.],
[12., 13.]], dtype=float32)]
type([output_y_split[1]]):<type 'list'>
[output_y_split[1]]:
[array([[ 2., 3.],
[14., 15.]], dtype=float32)]
type([output_y_split[2]]):<type 'list'>
[output_y_split[2]]:
[array([[ 4., 5.],
[16., 17.]], dtype=float32)]
type([output_y_split[3]]):<type 'list'>
[output_y_split[3]]:
[array([[ 6., 7.],
[18., 19.]], dtype=float32)]
type([output_y_split[4]]):<type 'list'>
[output_y_split[4]]:
[array([[ 8., 9.],
[20., 21.]], dtype=float32)]
type([output_y_split[5]]):<type 'list'>
[output_y_split[5]]:
[array([[10., 11.],
[22., 23.]], dtype=float32)]
++--++--++--++--++--++--++--++--
Process finished with exit code 0