【tensorflow 学习】tf.split()和tf.squeeze()
2018-01-15 22:06
477 查看
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]
相关文章推荐
- 3. Tensorflow学习笔记之tf.placeholder函数
- TensorFlow 学习(十三)—— tf.app.flags
- 学习笔记TF045:人工智能、深度学习、TensorFlow、比赛、公司
- TensorFlow学习--tf.reduce_mean()
- TensorFlow 学习(二)—— tf.Session() 与 tf.Session().run()
- TensorFlow学习笔记----TF生成数据的方法
- TensorFlow学习笔记-tf.estimator
- Tensorflow学习之tfrecords_reader
- 【tensorflow】tf.one_hot,tf.reduce_mean,tf.argmax,tf.equal,tf.cast学习
- 学习TensorFlow之tf.placeholder()
- Tensorflow实战学习(四十四)【TF.Contrib组件,统计分布,Layer,性能分析器tfprof】
- tensorflow学习之常用函数总结:tensorflow官方例子中的诸如tf.reduce_mean()这类函数
- TensorFlow学习笔记12----Creating Estimators in tf.contrib.learn
- TensorFlow 学习:tensor.get_shape() 与 tf.shape(a)
- 【深度学习】TensorFlow的TFRecord存储
- TensorFlow学习笔记 速记1——tf.nn.dropout
- Tensorflow实战学习(十六)【CNN实现、数据集、TFRecord、加载图像、模型、训练、调试】
- 深度学习笔记——深度学习框架TensorFlow(九)[Building Input Functions with tf.contrib.learn]
- TensorFlow基础笔记(13) tf.name_scope tf.variable_scope学习
- tensorflow学习之常用函数总结:tensorflow官方例子中的诸如tf.reduce_mean()这类函数