您好,登錄后才能下訂單哦!
本篇內容主要講解“PyTorch create_tensor怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“PyTorch create_tensor怎么使用”吧!
import torch import numpy as np a = np.ones((3, 3)) print(a, id(a)) b = torch.tensor(a) print(b, id(b), b.device) # b_gpu = torch.tensor(a, device = 'cuda') b_gpu = torch.tensor(a, device = 'cpu') print(b_gpu, id(b_gpu), b_gpu.device) c = torch.from_numpy(a) print(c, id(c)) a[0, 0] = 2 print(a, c) c[0, 1] = 3 print(a, c) d = torch.zeros((3, 3, 3)) print(d, d.dtype, d.shape) dd = torch.zeros_like(d) print(d, d.type, d.shape) e = torch.full((2, 2), 233) print(e, e.dtype) ee = torch.full((2, 2), 233.) print(ee, ee.dtype) f = torch.arange(1, 5) print(f, f.dtype) ff = torch.arange(1., 5.1) print(ff, ff.dtype) g = torch.linspace(1, 6, 6) print(g, g.dtype) h = torch.normal(0, 1, (3, 3)) print(h, h.dtype) hh = torch.randn((3, 3)) print(hh, hh.dtype) i = torch.rand((2, 2)) print(i) ii = torch.randint(1, 5, (2, 2)) print(ii) j = torch.randperm(20) print(j, j.dtype)
import torch import numpy as np a = torch.arange(0, 10, dtype = torch.int64) b = torch.reshape(a, (2, 5)) print(b) b_T = torch.t(b) print(b_T, b_T.shape) c = torch.reshape(torch.arange(0, 24, dtype = torch.int64), (2, 3, 4)) print(c) d = torch.transpose(c, 0, 1) print(d) e = torch.tensor([1]) print(e, e.shape) f = torch.squeeze(e) print(f, f.shape) f = f * 2 print(f, e) ee = torch.unsqueeze(f, dim = 0) print(ee)
import torch import numpy as np t1 = torch.ones((2, 2)) t2 = torch.zeros((2, 2)) a = torch.cat([t1, t2], dim = 0) print(a, a.shape) b = torch.stack([t1, t2], dim = 0) print(b, b.shape) print(b[0], b[1]) x = torch.split(b, [1, 1], dim = 0) print(type(x)) c, d = x print(c, d) e = torch.index_select(a, dim = 0, index = torch.tensor([0, 2])) print(e) mask = a.ge(1) f = torch.masked_select(a, mask) print(mask, f)
# 通過一元線性回歸, 來熟悉和展示常用的tensor的運算操作 import torch import numpy as np torch.manual_seed(10) # data x = torch.rand((20, 1)) * 10 y = 2 * x + 5 + torch.randn(20, 1) # model w = torch.tensor(np.asarray([0.3]), requires_grad=True) b = torch.tensor(np.asarray([0.]), requires_grad=True) print(w, b) # iteration for _ in range(1000): # flow y_pre = w * x + b loss = ( 0.5 * (y_pre - y) ** 2 ).mean() # backwords loss.backward() w.data.sub_(0.05 * w.grad) b.data.sub_(0.05 * b.grad) w.grad.zero_() b.grad.zero_() # show if _ % 100 == 0: print(str(_) + ' loss is', loss.data.numpy()) if loss.data.numpy() < 0.47: break print('finish...')
1. 安裝anaconda,pycharm, CUDA+CuDNN(可選),虛擬環境,pytorch,并實現hello pytorch查看pytorch的版本
2. 張量與矩陣、向量、標量的關系是怎么樣的?
3. Variable“賦予”張量什么功能?
4. 采用torch.from_numpy創建張量,并打印查看ndarray和張量數據的地址;
5. 實現torch.normal()創建張量的四種模式。
conda create -n torch_p36 python=3.6.5
conda activate torch_p36
pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 torchaudio===0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
標量(scalar)
一個標量表示一個單獨的數,它不同于線性代數中研究的其他大部分對象
向量(vector)
一個向量表示一組有序排列的數。通過次序中的索引,我們可以確定每個單獨的數
矩陣(matrix)
矩陣是具有相同特征和緯度的對象的集合,表現為一張二維數據表。其意義是一個對象表示為矩陣中的一行,一個特征表示為矩陣中的一列,每個特征都有數值型的取值
張量(tensor)
在某些情況下,我們會討論坐標超過兩維的數組。一般地,一個數組中的元素分布在若干維坐標的規則網格中,我們將其稱之為張量
Variable是torch.autograd中的數據類型,主要用于封裝Tensor,使得tensor可以進行自動求導
主要有五個屬性:
1.data:被包裝的Tensor
2.grad:data的梯度
3.grad_fn:創建Tensor的Function(創建張量所用到的方法,如加法或乘法),是自動求導的關鍵
4.requires.grad:指示張量是否需要梯度,不需要梯度的張量可以設置為false
5.is_leaf:指示張量在計算圖中是否是葉子結點。
現在variable不需要出現在代碼中了, 并入到了tensor
dtype
shape
device
import torch import numpy as np a = np.ones((3, 3)) print(a, id(a)) b = torch.tensor(a) print(b, id(b), b.device) # b_gpu = torch.tensor(a, device = 'cuda') b_gpu = torch.tensor(a, device = 'cpu') print(b_gpu, id(b_gpu), b_gpu.device) c = torch.from_numpy(a) print(c, id(c)) a[0, 0] = 2 print(a, c) c[0, 1] = 3 print(a, c) d = torch.zeros((3, 3, 3)) print(d, d.dtype, d.shape) dd = torch.zeros_like(d) print(d, d.type, d.shape) e = torch.full((2, 2), 233) print(e, e.dtype) ee = torch.full((2, 2), 233.) print(ee, ee.dtype) f = torch.arange(1, 5) print(f, f.dtype) ff = torch.arange(1., 5.1) print(ff, ff.dtype) g = torch.linspace(1, 6, 6) print(g, g.dtype) h = torch.normal(0, 1, (3, 3)) print(h, h.dtype) hh = torch.randn((3, 3)) print(hh, hh.dtype) i = torch.rand((2, 2)) print(i) ii = torch.randint(1, 5, (2, 2)) print(ii) j = torch.randperm(20) print(j, j.dtype)
到此,相信大家對“PyTorch create_tensor怎么使用”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。