tf.unstack参数了如下:
def unstack(value,
num=None,
axis=0,
name="unstack"):
Args:
value: A rank `R > 0` `Tensor` to be unstacked.
num: An `int`. The length of the dimension `axis`. Automatically inferred if `None` (the default).
axis: An `int`. The axis to unstack along. Defaults to the first dimension.Negative values wrap around, so the valid range is `[-R, R)`.
name: A name for the operation (optional).
value:就是要分解的矩阵
num:指定维度上数据长度,如果为None自动推断,一般不需要参数
axis:进行操作的维度
name:操作的名称
\qquad
tf.unstack参数了如下:
def stack(values,
axis=0,
name="stack"):
Args:
values: A list of `Tensor` objects with the same shape and type.
axis: An `int`. The axis to stack along. Defaults to the first dimension.
Negative values wrap around, so the valid range is `[-(R+1), R+1)`.
name: A name for this operation (optional).
value:是一个所要拼接矩阵的列表,是list类型
axis:也是进行操作的维度
name:操作的名称
测试代码如下:
import tensorflow as tf
import numpy as np
a_6=tf.constant(value=np.arange(6).reshape(2,3))
b_6=tf.constant(value=np.arange(7,13).reshape(2,3))
a_unstack0=tf.unstack(a_6,axis=0)
a_unstack1=tf.unstack(a_6,axis=1)
ab_stack0=tf.stack([a_6,b_6],axis=0)
ab_stack1=tf.stack([a_6,b_6],axis=1)
with tf.Session() as sess:
print('a_6:\n', sess.run(a_6))
print('b_6:\n', sess.run(b_6))
print('a_unstack0:\n',sess.run(a_unstack0))
print('a_unstack1:\n', sess.run(a_unstack1))
print('ab_stack0:\n',sess.run(ab_stack0))
print('ab_stack1:\n',sess.run(ab_stack1))
测试结果如下:
原始数据:
进行unstack操作后的输出:
tf.unstack操作之后生成的是一个列表。
进行stack操作后的输出:
print('typeunstack:',type(a_unstack0))
print('typestack:',type(ab_stack1))
输出结果:
也就是说unstack操作之后生成一个列表,stack操作之后还是一个tensor向量。