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

關于numpy和torch.tensor的張量的操作

 更新時間:2023年02月20日 10:03:04   作者:comli_cn  
這篇文章主要介紹了關于numpy和torch.tensor的張量的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

1. 張量的拼接

(1) numpy.concatenate

np.concatenate((a1,a2,a3,…), axis=0)

張量的拼接要用np.concatenate這個方法的,其中 a1,a2,a3,…是拼接的子張量,axis是維數,axis=0表示按照第一維進行拼接。

例如將兩個二維的張量按照第一維拼接成一個二維的張量:

import numpy as np
a=np.array([[1,2,3]])
b=np.array([[4,5,6]])
c=np.concatenate((a,b),axis=0)
print(c)
d=np.concatenate((c,a),axis=0)
print(d)
e=np.concatenate((c,c),axis=1)
print(e)

結果

array([[1, 2, 3],
       [4, 5, 6]])
array([[1, 2, 3],
       [4, 5, 6],
       [1, 2, 3]])
array([[1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6]])

對于axis選擇的更簡單直接的理解是我們可以從將被拼接的兩個矩陣的形狀上來看,比如

a.shape=(3,1,2), b.shape=(6,1,2),則我們對其進行拼接的話目的是讓拼接之后的shape=(9,1,2),那么我們就選擇axis=0,即代表對第0維的進行相加。

代碼如下:

import numpy as np
a = np.zeros((3, 1, 2))
b = np.zeros((6, 1, 2))
c = np.concatenate((a, b), axis=0)
print(c.shape)

結果為:

(9, 1, 2)

(2) torch.cat

這里的拼接和上面介紹的numpy的拼接功能是一樣的

C = torch.cat( (A,B),0 ) ?#按維數0拼接(豎著拼)
C = torch.cat( (A,B),1 ) ?#按維數1拼接(橫著拼)

例:

import torch
A=torch.ones(2,3) ?#2x3的張量(矩陣) ??
B=2*torch.ones(4,3) ?#4x3的張量(矩陣) ? ?
C=torch.cat((A,B),0) ?#按維數0(行)拼接
print(C) ? ? ? ? ? ? ? ? ? ? ?

結果:

tensor([[ 2.,  2.,  2.],
        [ 2.,  2.,  2.],
        [ 2.,  2.,  2.],
        [ 2.,  2.,  2.]])

接著上面

D=2*torch.ones(2,4) #2x4的張量(矩陣)
C=torch.cat((A,D),1)#按維數1(列)拼接
print(C)

結果:

tensor([[ 1.,  1.,  1.,  2.,  2.,  2.,  2.],
        [ 1.,  1.,  1.,  2.,  2.,  2.,  2.]])

2. 張量的重構

(1) np.reshape

>>> import numpy as np
>>> a = np.array([[1,2,3],[4,5,6]])
>>> a
array([[1, 2, 3],
? ? ? ?[4, 5, 6]])
>>> b = np.reshape(a, (2,3,1))
>>> b
array([[[1],
? ? ? ? [2],
? ? ? ? [3]],

? ? ? ?[[4],
? ? ? ? [5],
? ? ? ? [6]]])
>>> b.shape
(2, 3, 1)

(2) array.shape

>>> import numpy as np
>>> a = np.array([1,2,3,4,5,6,7,8])
>>> a.shape = (2, 4)
>>> a
array([[1, 2, 3, 4],
? ? ? ?[5, 6, 7, 8]])

(3) torch.view

在pytorch中view函數的作用為重構張量的維度,相當于numpy中resize()的功能,但是用法可能不太一樣。

1.torch.view(參數a,參數b,…)

例如:

import torch
tt1=torch.tensor([-0.3623, -0.6115, ?0.7283, ?0.4699, ?2.3261, ?0.1599])
result=tt1.view(3,2)
print(result)

結果

tensor([[-0.3623, -0.6115],
        [ 0.7283,  0.4699],
        [ 2.3261,  0.1599]])

在上面例子中參數a=3和參數b=2決定了將一維的tt1重構成3x2維的張量。

2.有的時候會出現torch.view(-1)或者torch.view(參數a,-1)這種情況。

例:

import torch
tt2=torch.tensor([[-0.3623, -0.6115],
? ? ? ? ?[ 0.7283, ?0.4699],
? ? ? ? ?[ 2.3261, ?0.1599]])
result=tt2.view(-1)
print(result)

結果:

tensor([-0.3623, -0.6115,  0.7283,  0.4699,  2.3261,  0.1599])

由上面的案例可以看到,如果是torch.view(-1),則原張量會變成一維的結構。

例:

import torch
tt3=torch.tensor([[-0.3623, -0.6115],
? ? ? ? ?[ 0.7283, ?0.4699],
? ? ? ? ?[ 2.3261, ?0.1599]])
>>> result=tt3.view(2,-1)

結果:

tensor([[-0.3623, -0.6115,  0.7283],
        [ 0.4699,  2.3261,  0.1599]])

由上面的案例可以看到,如果是torch.view(參數a,-1),則表示在參數b未知,參數a已知的情況下自動補齊列向量長度,在這個例子中a=2,tt3總共由6個元素,則b=6/2=3。

例:

import torch
inputs = torch.randn(1,3)
print(inputs)
print(inputs.view(1, 1, -1))

結果:

tensor([[-0.5525,  0.6355, -0.3968]])
tensor([[[-0.5525,  0.6355, -0.3968]]])

將二維變?yōu)槿S,a=1,b=1,c=3/(1*1)

3. 張量的形狀

(1) torch.size

import torch
inputs = torch.randn(1,3)
print(inputs.size())

結果:

torch.Size([1, 3])

4. 張量的擴展

(1) torch.tensor擴展方法

用unsqueeze方法將原張量進行維度擴張,unsqueeze后面括號里的數字代表在哪個維度擴張

import torch

a = torch.tensor([[1, 2, 3], [4, 5, 6]])
b = torch.tensor([[7, 8, 9], [4, 5, 6]])
print(a)
print(b)
a = a.unsqueeze(0)
b = b.unsqueeze(0)
print(a)
print(b)
c = torch.cat((a, b), 0)
print(c)
print(c.shape)

結果為

tensor([[1, 2, 3],
        [4, 5, 6]])
tensor([[7, 8, 9],
        [4, 5, 6]])
tensor([[[1, 2, 3],
         [4, 5, 6]]])
tensor([[[7, 8, 9],
         [4, 5, 6]]])
tensor([[[1, 2, 3],
         [4, 5, 6]],

        [[7, 8, 9],
         [4, 5, 6]]])
torch.Size([2, 2, 3])

用squeeze方法將原張量進行維度縮減,squeeze后面括號里的數字代表在哪個維度縮減

import torch

a = torch.tensor([[1, 2, 3], [4, 5, 6]])
b = torch.tensor([[7, 8, 9], [4, 5, 6]])
print(a)
print(b)
a = a.unsqueeze(0)
b = b.unsqueeze(0)
print(a)
print(b)
a = a.squeeze(0)
b = b.squeeze(0)
print(a)
print(b)

結果為

tensor([[1, 2, 3],
        [4, 5, 6]])
tensor([[7, 8, 9],
        [4, 5, 6]])
tensor([[[1, 2, 3],
         [4, 5, 6]]])
tensor([[[7, 8, 9],
         [4, 5, 6]]])
tensor([[1, 2, 3],
        [4, 5, 6]])
tensor([[7, 8, 9],
        [4, 5, 6]])

(2) np.array擴展方法

np.expand_dims:用于擴展數組的形狀

原始數組:

import numpy as np
?
In [12]:
a = np.array([[[1,2,3],[4,5,6]]])
a.shape
Out[12]:
(1, 2, 3)

np.expand_dims(a, axis=0)表示在0位置添加數據,轉換結果如下:

In [13]:
b = np.expand_dims(a, axis=0)
b
Out[13]:
array([[[[1, 2, 3],
         [4, 5, 6]]]])
 
In [14]:
b.shape
Out[14]:
(1, 1, 2, 3)

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Python爬取求職網requests庫和BeautifulSoup庫使用詳解

    Python爬取求職網requests庫和BeautifulSoup庫使用詳解

    這篇文章主要為大家介紹了Python爬取求職網及其他網頁時requests庫和BeautifulSoup庫的使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • Python多線程編程threading模塊使用最佳實踐及常見問題解析

    Python多線程編程threading模塊使用最佳實踐及常見問題解析

    這篇文章主要為大家介紹了Python多線程編程threading模塊使用最佳實踐及常見問題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • 淺析Python中的多重繼承

    淺析Python中的多重繼承

    這篇文章主要介紹了Python中的多重繼承,是Python學習中的基本知識,代碼基于Python2.x版本,需要的朋友可以參考下
    2015-04-04
  • Python繪制熱力圖示例

    Python繪制熱力圖示例

    這篇文章主要介紹了Python繪制熱力圖,結合實例形式分析了Python使用pyheatmap及matplotlib模塊進行數值計算與圖形繪制相關操作技巧,需要的朋友可以參考下
    2019-09-09
  • Python?多線程知識點總結及實例用法

    Python?多線程知識點總結及實例用法

    在本篇內容里小編給大家整理了一篇關于Python?多線程知識點總結及實例用法,對想好學習PY的用戶非常友好,需要的參考下吧。
    2021-12-12
  • NumPy中的實用函數clip詳解

    NumPy中的實用函數clip詳解

    這篇文章主要介紹了NumPy中的實用函數clip詳解,NumPy函數clip()用于保留數組中在間隔范圍內的值,給定一個范圍,范圍外的值將剪裁到范圍邊界,需要的朋友可以參考下的相關資料
    2023-08-08
  • python中使用np.delete()的實例方法

    python中使用np.delete()的實例方法

    在本篇文章里小編給大家整理的是一篇關于python中使用np.delete()的實例方法,對此有興趣的朋友們可以學習參考下。
    2021-02-02
  • python實現在列表中查找某個元素的下標示例

    python實現在列表中查找某個元素的下標示例

    這篇文章主要介紹了python實現在列表中查找某個元素的下標示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • Python argparse 解析命令行參數模塊詳情

    Python argparse 解析命令行參數模塊詳情

    這篇文章主要介紹了Python argparse 解析命令行參數模塊詳情,argparse是python用于解析命令行參數和選項的標準模塊,用于代替已經過時的optparse模塊
    2022-07-07
  • Python讀大數據txt

    Python讀大數據txt

    本文通過2個例子給大家介紹了如何使用python實現讀取大文件txt的方法,有需要的小伙伴可以參考下
    2016-03-03

最新評論

崇阳县| 廉江市| 革吉县| 吐鲁番市| 都江堰市| 涿鹿县| 江华| 江西省| 武汉市| 阿克| 苏尼特左旗| 临安市| 西林县| 黄陵县| 泸水县| 荥经县| 本溪市| 马公市| 泰兴市| 行唐县| 台山市| 乐平市| 四川省| 涪陵区| 成安县| 卢湾区| 泸州市| 开封市| 清新县| 杭州市| 鹤山市| 楚雄市| 油尖旺区| 怀化市| 赞皇县| 尚志市| 郑州市| 万载县| 玉林市| 焦作市| 东安县|