TensorFlow获取Tensor维度

获取Tensor维度

比如一个Tensor为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    1. a.shape
2. a.get_shape()
3. tf.shape(a)
前两种返回类型是TensorShape,代表静态shape,a.shape.as_list()返回list类型的shape
第三种返回类型是Tensor,代表动态shape

### 动态shape与静态shape
当需要reshape Tensor时,就能体现出差异了
``` python
import tensorflow as tf
b = tf.placeholder(tf.float32,[None,10,32])
b_static = b.shape.as_list()
b_dynamic = tf.unstack(tf.shape(b))
dim = [s[1] if s[0] if None else s[0] for s in zip(b_static,b_dynamic)]
tf.reshape(b,[dim[0],dim[1]*dim[2]])
#此时b的维度变成[None,320]
#如果直接用静态shape对b进行reshape会报错

0%