成都網(wǎng)站排名 生客seo自己搭建網(wǎng)站
張量的創(chuàng)建
- 張量(Tensors)類似于NumPy的ndarrays ,但張量可以在GPU上進行計算。從本質(zhì)上來說,PyTorch是一個處理張量的庫。一個張量是一個數(shù)字、向量、矩陣或任何n維數(shù)組。
?
import torch
import numpy
torch.manual_seed(7) # 固定隨機數(shù)種子
直接創(chuàng)建?
- torch.tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False)
- 功能:從data創(chuàng)建tensor
- data: 數(shù)據(jù),可以是list,numpy
- dtype: 數(shù)據(jù)類型,默認與data的一致
- device: 所在設(shè)備,cuda/cpu
- requires_grad: 是否需要梯度
- pin_memory: 是否存于鎖頁內(nèi)存
torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]])tensor([[0.1000, 1.2000],
, [2.2000, 3.1000],
, [4.9000, 5.2000]])
- torch.from_numpy(ndarray)
- 功能:從numpy創(chuàng)建tensor
從torch.from_numpy創(chuàng)建的tensor于原ndarray共享內(nèi)存,當修改其中一個數(shù)據(jù),另一個也將會被改動。
a = numpy.array([1, 2, 3])
t = torch.from_numpy(a)
?依據(jù)數(shù)值創(chuàng)建?
- torch.zeros(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
- 功能:依size創(chuàng)建全0張量
- size: 張量的形狀
- out: 輸出的張量
- layout: 內(nèi)存中布局形式
- device: 所在設(shè)備
- requires_grad: 是否需要梯度
torch.zeros(2, 3)tensor([[0., 0., 0.],
, [0., 0., 0.]])
- torch.zeros_like(input, dtype=None, layout=None, device=None, requires_grad=False)
- 功能:依input形狀創(chuàng)建全0張量
- input: 創(chuàng)建與input同形狀的全0張量
- dtype: 數(shù)據(jù)類型
- layout: 內(nèi)存中布局形式
input = torch.empty(2, 3)
torch.zeros_like(input)tensor([[0., 0., 0.],
, [0., 0., 0.]])
torch.ones(2, 3)tensor([[1., 1., 1.],
, [1., 1., 1.]])
- torch.ones_like(input, dtype=None, layout=None, device=None, requires_grad=False)
- 功能:依input形狀創(chuàng)建全1張量
- size: 張量的形狀
- dtype: 數(shù)據(jù)類型
- layout: 內(nèi)存中布局形式
- device: 所在設(shè)備
- requires_grad: 是否需要梯度
input = torch.empty(2, 3)
torch.ones_like(input)tensor([[1., 1., 1.],
, [1., 1., 1.]])
?
- torch.full_like(input, dtype=None, layout=torch.strided, device=None, requires_grad=False)
- 功能: 依input形狀創(chuàng)建指定數(shù)據(jù)的張量
- size: 張量的形狀
- fill_value: 張量的值
- torch.arange(start=0, end. step=1, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
- 功能:創(chuàng)建等差的1維張量
- start: 數(shù)列起始值
- end: 數(shù)列結(jié)束值
- step: 數(shù)列公差,默認為1
torch.arange(1, 2.5, 0.5)tensor([1.0000, 1.5000, 2.0000])
?
?依概率分布創(chuàng)建張量
torch.normal(mean, std, out=None) : 生成正態(tài)分布
# mean為張量, std為張量
torch.normal(mean=torch.arange(1., 11.), std=torch.arange(1, 0, -0.1))tensor([0.8532, 2.7075, 3.7575, 3.2200, 6.0145, 5.5526, 6.8577, 8.3697, 9.0276,
, 9.8318])
?
torch.randn(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) : 生成標準正態(tài)分布
?
torch.randn(2, 3)tensor([[1.3955, 1.3470, 2.4382],
, [0.2028, 2.4505, 2.0256]])
?
torch.rand(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) :?在[0,1)上,生成均勻分布。
torch.rand(2, 3)tensor([[0.7405, 0.2529, 0.2332],
, [0.9314, 0.9575, 0.5575]])
?張量拼接與切分
torch.cat(tensors, dim=0, out=None) :?將張量按維度進行拼接
?
x = torch.randn(2, 3)
torch.cat((x, x, x), 1)#
tensor([[-1.7038, 0.6248, 0.1196, -1.7038, 0.6248, 0.1196, -1.7038, 0.6248,
, 0.1196],
, [-0.8049, 1.6162, 0.2516, -0.8049, 1.6162, 0.2516, -0.8049, 1.6162,
, 0.2516]])
?
torch.stack(tensors, dim=0, out=None) :?在新創(chuàng)建的維度上進行拼接
torch.chunk(input, chunks, dim=0)? :?將張量按維度進行平均切分