TensorFlow获取Tensor维度

Welcome To My Blog

获取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会报错
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容