get_shape_list

def get_shape_list(tensor, expected_rank=None, name=None)
參數(shù):
tensor:一個(gè)需要返回shape的tf.Tensor
expected_rank:int或者是一個(gè)int的list氛雪。輸入tensor期望的rank(也就是矩陣的維度)婿牍,如果輸入tensor的rank不等于這個(gè)數(shù),或者不是這個(gè)list的元素之一著榴,會(huì)拋異常夺姑。

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import tensorflow as tf
import six

def get_shape_list(tensor, expected_rank=None, name=None):
    """Returns a list of the shape of tensor, preferring static dimensions.

  Args:
    tensor: A tf.Tensor object to find the shape of.
    expected_rank: (optional) int. The expected rank of `tensor`. If this is
      specified and the `tensor` has a different rank, and exception will be
      thrown.
    name: Optional name of the tensor for the error message.

  Returns:
    A list of dimensions of the shape of tensor. All static dimensions will
    be returned as python integers, and dynamic dimensions will be returned
    as tf.Tensor scalars.
  """
    if name is None:
        name = tensor.name

    if expected_rank is not None:
        assert_rank(tensor, expected_rank, name)

    shape = tensor.shape.as_list()

    non_static_indexes = []
    for (index, dim) in enumerate(shape):
        if dim is None:
            non_static_indexes.append(index)

    if not non_static_indexes:
        return shape

    dyn_shape = tf.shape(tensor)
    for index in non_static_indexes:
        print(str(index))
        shape[index] = dyn_shape[index]
    return shape

def assert_rank(tensor, expected_rank, name=None):
    """Raises an exception if the tensor rank is not of the expected rank.

  Args:
    tensor: A tf.Tensor to check the rank of.
    expected_rank: Python integer or list of integers, expected rank.
    name: Optional name of the tensor for the error message.

  Raises:
    ValueError: If the expected shape doesn't match the actual shape.
  """
    if name is None:
        name = tensor.name

    expected_rank_dict = {}
    if isinstance(expected_rank, six.integer_types):
        expected_rank_dict[expected_rank] = True
    else:
        for x in expected_rank:
            expected_rank_dict[x] = True

    actual_rank = tensor.shape.ndims
    if actual_rank not in expected_rank_dict:
        scope_name = tf.get_variable_scope().name
        raise ValueError(
            "For the tensor `%s` in scope `%s`, the actual rank "
            "`%d` (shape = %s) is not equal to the expected rank `%s`" %
            (name, scope_name, actual_rank, str(tensor.shape), str(expected_rank)))

if __name__ == '__main__':
    zero = tf.constant([0.0, 0.1, 0.2])
    zero_shape = get_shape_list(zero, expected_rank=[1, 3])
    print('zero_shape:')
    print(zero_shape)
    one = tf.constant([[0.0, 0.1, 0.2],[0.0, 0.1, 0.2]])
    one_shape = get_shape_list(one, expected_rank=[2, 3])
    print('one_shape:')
    print(one_shape)
    two = tf.compat.v1.placeholder(tf.int32, [32, 512])
    two_shape = get_shape_list(two, expected_rank=2)
    print('two_shape:')
    print(two_shape)

輸出結(jié)果:

zero_shape:
[3]
one_shape:
[2, 3]
two_shape:
[32, 512]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末锤躁,一起剝皮案震驚了整個(gè)濱河市涯呻,隨后出現(xiàn)的幾起案子黍翎,更是在濱河造成了極大的恐慌面徽,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,464評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件匣掸,死亡現(xiàn)場(chǎng)離奇詭異趟紊,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)碰酝,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)霎匈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人送爸,你說(shuō)我怎么就攤上這事铛嘱。” “怎么了袭厂?”我有些...
    開(kāi)封第一講書(shū)人閱讀 169,078評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵墨吓,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我纹磺,道長(zhǎng)帖烘,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,979評(píng)論 1 299
  • 正文 為了忘掉前任橄杨,我火速辦了婚禮秘症,結(jié)果婚禮上照卦,老公的妹妹穿的比我還像新娘。我一直安慰自己历极,他們只是感情好窄瘟,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,001評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著趟卸,像睡著了一般蹄葱。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上锄列,一...
    開(kāi)封第一講書(shū)人閱讀 52,584評(píng)論 1 312
  • 那天图云,我揣著相機(jī)與錄音,去河邊找鬼邻邮。 笑死竣况,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的筒严。 我是一名探鬼主播丹泉,決...
    沈念sama閱讀 41,085評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼鸭蛙!你這毒婦竟也來(lái)了摹恨?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 40,023評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤娶视,失蹤者是張志新(化名)和其女友劉穎晒哄,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體肪获,經(jīng)...
    沈念sama閱讀 46,555評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡寝凌,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,626評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了孝赫。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片较木。...
    茶點(diǎn)故事閱讀 40,769評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖青柄,靈堂內(nèi)的尸體忽然破棺而出劫映,到底是詐尸還是另有隱情,我是刑警寧澤刹前,帶...
    沈念sama閱讀 36,439評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站雌桑,受9級(jí)特大地震影響喇喉,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜校坑,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,115評(píng)論 3 335
  • 文/蒙蒙 一拣技、第九天 我趴在偏房一處隱蔽的房頂上張望千诬。 院中可真熱鬧,春花似錦膏斤、人聲如沸徐绑。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,601評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)傲茄。三九已至,卻和暖如春沮榜,著一層夾襖步出監(jiān)牢的瞬間盘榨,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,702評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工蟆融, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留草巡,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,191評(píng)論 3 378
  • 正文 我出身青樓型酥,卻偏偏與公主長(zhǎng)得像山憨,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子弥喉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,781評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容