最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于tf.shape(tensor)和tensor.shape()的區(qū)別說明

 更新時間:2020年06月30日 09:43:26   作者:yinheju  
這篇文章主要介紹了基于tf.shape(tensor)和tensor.shape()的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

#tf.shape(tensor)和tensor.shape()的區(qū)別

a=tf.zeros([4,5,4,5,6])
print(type(a.shape))
print(a.shape.ndims)#多少個維度
print(a.shape.as_list())#返回列表
print(type(tf.shape(a)))
print(type(tf.shape(a)[0]))
b=a.shape.as_list()
c=tf.shape(a)
b[1]=tf.shape(a)[1]
print(b)
sess=tf.Session()
d=sess.run(c)
print(d)
outputs:
<class 'tensorflow.python.framework.tensor_shape.TensorShape'>
5
[4, 5, 4, 5, 6]
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'tensorflow.python.framework.ops.Tensor'>
[4, <tf.Tensor 'strided_slice_1:0' shape=() dtype=int32>, 4, 5, 6]
[4 5 4 5 6]

其中tf.shape(tensor)使用的是動態(tài)的,即必須要在session中運行后才能顯示出來,但是tensor.shape()是靜態(tài)的,即通過定義的shape可以驚天的運行出來。

原因:在我們定義的時候,比如進行placeholder的時候我們可能會定義某些維度為None,在靜態(tài)的時候是看不出來的,只能在運行的時候找到維度。

**使用:**可以在獲得某些tensor的維度的時候進行檢驗,防止維度為None。

補充知識:tensorflow.python.framework.tensor_shape.TensorShape 類

TensorShape 是tensorflow中關于張量shape的類(class).

使用示例如下:

import tensorflow.compat.v1 as tf
from tensorflow.python.framework import tensor_shape
from tensorflow.python.framework import constant_op
 
tensor_test1=[10,10,10]
tensor_test2 = [None,10,10]
 
p1 = tensor_shape.as_shape(tensor_test1) # 得到的是一個類實例,該類實例包含一個屬性,是 tensor_test1 的value
const = constant_op.constant(p1.as_list())
 
print("type(p1) = ",type(p1))
print("p1 = ",p1) # 使用p1時會自動調用p1中的value屬性
print("p1.is_fully_defined() = ",p1.is_fully_defined())# is_fully_defined 是 TensorShape 類的一個內部函數(shù)
print("p1.ndims = ",p1.ndims) # ndims 也是TensorShape的一個屬性值
print("p1.as_list() = ",p1.as_list()) # 把TensorShape的value屬性轉換成python中的list類型
print("const = ",const)

結果如下:

type(p1) = <class 'tensorflow.python.framework.tensor_shape.TensorShape'>
p1 = (10, 10, 10)
p1.is_fully_defined() = True
p1.ndims = 3
p1.as_list() = [10, 10, 10]
const = Tensor("Const:0", shape=(3,), dtype=int32)

以上這篇基于tf.shape(tensor)和tensor.shape()的區(qū)別說明就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論

梓潼县| 望城县| 安图县| 五莲县| 松原市| 河源市| 图片| 潮安县| 新竹市| 靖江市| 中宁县| 吉安市| 宜宾县| 镇雄县| 青田县| 芦山县| 乐都县| 阿拉善左旗| 会昌县| 博白县| 灵川县| 四川省| 连平县| 蒲江县| 梅州市| 普宁市| 桃江县| 奉新县| 卢氏县| 福州市| 吉安县| 桦川县| 襄汾县| 宽城| 山阳县| 石嘴山市| 高邑县| 大化| 三都| 六盘水市| 景洪市|