获取Tensor维度
比如一个Tensor为a = tf.constant([[1,2,],[3,4]],name='a')
,有三种方式可以获取a的维度
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时,就能体现出差异了
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会报错
作者:LittleSasuke
链接:https://www.jianshu.com/p/a93780eb16e5