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

Pytorch之?dāng)U充tensor的操作

 更新時間:2021年03月04日 16:12:29   作者:有為少年  
這篇文章主要介紹了Pytorch之?dāng)U充tensor的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

我就廢話不多說了,大家還是直接看代碼吧~

b = torch.zeros((3, 2, 6, 6))
a = torch.zeros((3, 2, 1, 1))
a.expand_as(b).size()
Out[32]: torch.Size([3, 2, 6, 6])
a = torch.zeros((3, 2, 2, 1))
a.expand_as(b).size()
Traceback (most recent call last):
 File "/home/lart/.conda/envs/pt/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
  exec(code_obj, self.user_global_ns, self.user_ns)
 File "<ipython-input-34-972575f79e92>", line 1, in <module>
  a.expand_as(b).size()
RuntimeError: The expanded size of the tensor (6) must match the existing size (2) at non-singleton dimension 2. Target sizes: [3, 2, 6, 6]. Tensor sizes: [3, 2, 2, 1]
a = torch.zeros((3, 2, 1, 2))
a.expand_as(b).size()
Traceback (most recent call last):
 File "/home/lart/.conda/envs/pt/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
  exec(code_obj, self.user_global_ns, self.user_ns)
 File "<ipython-input-36-972575f79e92>", line 1, in <module>
  a.expand_as(b).size()
RuntimeError: The expanded size of the tensor (6) must match the existing size (2) at non-singleton dimension 3. Target sizes: [3, 2, 6, 6]. Tensor sizes: [3, 2, 1, 2]
a = torch.zeros((3, 2, 2, 2))
a.expand_as(b).size()
Traceback (most recent call last):
 File "/home/lart/.conda/envs/pt/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
  exec(code_obj, self.user_global_ns, self.user_ns)
 File "<ipython-input-38-972575f79e92>", line 1, in <module>
  a.expand_as(b).size()
RuntimeError: The expanded size of the tensor (6) must match the existing size (2) at non-singleton dimension 3. Target sizes: [3, 2, 6, 6]. Tensor sizes: [3, 2, 2, 2]
a = torch.zeros((3, 2, 6, 2))
a.expand_as(b).size()
Traceback (most recent call last):
 File "/home/lart/.conda/envs/pt/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
  exec(code_obj, self.user_global_ns, self.user_ns)
 File "<ipython-input-40-972575f79e92>", line 1, in <module>
  a.expand_as(b).size()
RuntimeError: The expanded size of the tensor (6) must match the existing size (2) at non-singleton dimension 3. Target sizes: [3, 2, 6, 6]. Tensor sizes: [3, 2, 6, 2]
a = torch.zeros((3, 2, 6, 1))
a.expand_as(b).size()
Out[44]: torch.Size([3, 2, 6, 6])
a = torch.zeros((3, 2, 1, 6))
a.expand_as(b).size()
Out[46]: torch.Size([3, 2, 6, 6])

tensor.expand_as在這里用于擴展tensor到目標(biāo)形狀,常用的多是在H和W方向上的擴展。

假設(shè)目標(biāo)形狀為N, C, H, W,則要求tensor.size()=n, c, h, w(這里假設(shè)N,C不變):

1、h=w=1

2、h=1, w!=1

3、h!=1, w=1

補充:tensorflow 利用expand_dims和squeeze擴展和壓縮tensor維度

在利用tensorflow進(jìn)行文本挖掘工作的時候,經(jīng)常涉及到維度擴展和壓縮工作。

比如對文本進(jìn)行embedding操作完成之后,若要進(jìn)行卷積操作,就需要對embedded的向量擴展維度,將[batch_size, embedding_dims]擴展成為[batch_size, embedding_dims, 1],利用tf.expand_dims(input, -1)就可實現(xiàn),反過來用squeeze(input, -1)或者tf.squeeze(input)也可以把最第三維去掉。

tf.expand_dims()

tf.squeeze()

tf.expand_dims()

tf.expand_dims(input, axis=None, name=None, dim=None)

在第axis位置增加一個維度.

給定張量輸入,此操作在輸入形狀的維度索引軸處插入1的尺寸。 尺寸索引軸從零開始; 如果您指定軸的負(fù)數(shù),則從最后向后計數(shù)。

如果要將批量維度添加到單個元素,則此操作非常有用。 例如,如果您有一個單一的形狀[height,width,channels],您可以使用expand_dims(image,0)使其成為1個圖像,這將使形狀[1,高度,寬度,通道]。

例子

# 't' is a tensor of shape [2]
shape(expand_dims(t, 0)) ==> [1, 2]
shape(expand_dims(t, 1)) ==> [2, 1]
shape(expand_dims(t, -1)) ==> [2, 1]
# 't2' is a tensor of shape [2, 3, 5]
shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5]
shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5]
shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]

tf.squeeze()

tf.squeeze(input, axis=None, name=None, squeeze_dims=None)

直接上例子

# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
 shape(squeeze(t)) ==> [2, 3]
# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
 shape(squeeze(t, [2, 4])) ==> [1, 2, 3, 1]

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • python生成指定長度的隨機數(shù)密碼

    python生成指定長度的隨機數(shù)密碼

    這篇文章主要介紹了python生成指定長度的隨機密碼示例,密碼使用數(shù)字和字母組合,大家參考使用吧
    2014-01-01
  • Python+Pillow進(jìn)行圖形處理的示例詳解

    Python+Pillow進(jìn)行圖形處理的示例詳解

    PIL:Python Imaging Library,已經(jīng)是Python平臺事實上的圖像處理標(biāo)準(zhǔn)庫了。PIL功能非常強大,但API卻非常簡單易用。本文就將利用Pillow進(jìn)行簡單的圖形處理,需要的可以參考一下
    2022-10-10
  • Python實現(xiàn)二維曲線擬合的方法

    Python實現(xiàn)二維曲線擬合的方法

    今天小編就為大家分享一篇Python實現(xiàn)二維曲線擬合的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • Python字典取值全攻略之高效、簡潔地獲取字典值的多種技巧

    Python字典取值全攻略之高效、簡潔地獲取字典值的多種技巧

    這篇文章主要給大家介紹了關(guān)于Python字典取值全攻略之高效、簡潔地獲取字典值的多種技巧,dictionary(字典)是除列表以外Python之中最靈活的數(shù)據(jù)類型,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • python字符串string的內(nèi)置方法實例詳解

    python字符串string的內(nèi)置方法實例詳解

    這篇文章主要介紹了python字符串string的內(nèi)置方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友參考下吧
    2018-05-05
  • Python實現(xiàn)將DOC文檔轉(zhuǎn)換為PDF的方法

    Python實現(xiàn)將DOC文檔轉(zhuǎn)換為PDF的方法

    這篇文章主要介紹了Python實現(xiàn)將DOC文檔轉(zhuǎn)換為PDF的方法,涉及Python調(diào)用系統(tǒng)win32com組件實現(xiàn)文件格式轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下
    2015-07-07
  • 10個Python實現(xiàn)的最頻繁使用的聚類算法

    10個Python實現(xiàn)的最頻繁使用的聚類算法

    聚類或聚類分析是無監(jiān)督學(xué)習(xí)問題。它通常被用作數(shù)據(jù)分析技術(shù),用于發(fā)現(xiàn)數(shù)據(jù)中的有趣模式。本文為大家介紹了10個最頻繁使用的聚類算法,感興趣的可以了解一下
    2022-12-12
  • 對pandas讀取中文unicode的csv和添加行標(biāo)題的方法詳解

    對pandas讀取中文unicode的csv和添加行標(biāo)題的方法詳解

    今天小編就為大家分享一篇對pandas讀取中文unicode的csv和添加行標(biāo)題的方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • Python爬蟲之PhantomJS和handless的使用詳解

    Python爬蟲之PhantomJS和handless的使用詳解

    這篇文章主要介紹了Python爬蟲之PhantomJS和handless的使用詳解,PhantomJS是一個基于Webkit的headless瀏覽器,它會把網(wǎng)站加載到內(nèi)存并使用webkit來編譯解釋執(zhí)行頁面上的JavaScript代碼,由于不進(jìn)行css和gui渲染、不展示圖形界面,需要的朋友可以參考下
    2023-09-09
  • Python中enumerate函數(shù)代碼解析

    Python中enumerate函數(shù)代碼解析

    這篇文章主要介紹了Python中enumerate函數(shù)代碼解析,涉及函數(shù)說明以及相關(guān)示例,具有一定參考價值,需要的朋友可以了解下。
    2017-10-10

最新評論

新巴尔虎右旗| 五寨县| 体育| 绿春县| 泌阳县| 阳东县| 阜康市| 平乡县| 收藏| 涞水县| 紫云| 元阳县| 巴南区| 荔浦县| 星子县| 抚顺县| 温泉县| 肇庆市| 孝义市| 金塔县| 阜阳市| 吴忠市| 济源市| 漾濞| 五莲县| 青铜峡市| 丰都县| 文化| 华容县| 石柱| 防城港市| 白城市| 北海市| 香格里拉县| 安岳县| 平原县| 简阳市| 阿克苏市| 嘉禾县| 禄丰县| 贞丰县|