获取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
點(diǎn)擊查看更多內(nèi)容
為 TA 點(diǎn)贊
評(píng)論
評(píng)論
共同學(xué)習(xí),寫下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦