日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

shape的各种获取、更改以及设置方式辨析

發布時間:2025/3/19 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 shape的各种获取、更改以及设置方式辨析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

各種shape獲取和shape設置以及更改

1. shape獲取

  • tensor.get_shape:只有tensor才有這個方法,(相對于op來說的),用于返回該 tensor的維度,返回的是一個數組。不需要在session里面使用。x.get_shape()
  • tensor.shape:用法同tensor.get_shape()類似。如x.shape
  • tf.shape():以列表的形式返回形狀,需要在Session里面使用

代碼示例

def test_shape():v1 = tf.placeholder(tf.float32,shape=[2,3])print(v1.get_shape()) # 輸出:(2,3)print(v1.shape) # 輸出:(2,3)print(tf.shape(v1)) # 輸出:Tensor("Shape:0", shape=(2,), dtype=int32)with tf.Session() as sess:print(sess.run(tf.shape(v1))) # 輸出:[2 3] test_shape()

2. shape設置(set)以及更改(reshape)

  • tensor.set_shape:如果原tensor已經存在shape了,則不能使用該函數改變shape了;該函數只能用來設置shape(set)
  • tf.reshape(tensor,shape):該函數并不改變原tensor的shape,而是新產生一個tensor,并在原tensor的形狀基礎上重新組合形成不同的形狀。總維度數即:維度1維度2維度3*···是不變的

代碼示例

def test_reshape():v1 = tf.placeholder(tf.float32)v1.set_shape([2,3])v2 = tf.reshape(v1,[3,2]) # 這里的總維度數必須為:2*3t1 = tf.placeholder(tf.float32)t2 = tf.reshape(t1,[3,4])p1 = tf.placeholder(tf.float32,shape=[2,2])# p1.set_shape([1,4]) 如果該句不被注釋則會出錯p2 = tf.reshape(p1,[4,1])test_reshape()

參考

  • https://blog.csdn.net/Jinlong_Xu/article/details/71405305
  • https://blog.csdn.net/qq_21949357/article/details/77987928
  • 記錄時間

    2018/9/18 20:34 第一次

    總結

    以上是生活随笔為你收集整理的shape的各种获取、更改以及设置方式辨析的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。