日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

pytorch創(chuàng)建tensor函數(shù)詳情_(kāi)python

作者:就是一頓騷操作 ? 更新時(shí)間: 2022-05-24 編程語(yǔ)言

1、通過(guò)復(fù)制數(shù)據(jù)構(gòu)造張量

1.1 torch.tensor()

torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]])
Out[111]:?
tensor([[0.1000, 1.2000],
? ? ? ? [2.2000, 3.1000],
? ? ? ? [4.9000, 5.2000]])
torch.tensor([0, 1])?
Out[112]: tensor([0, 1])
torch.tensor([[0.11111, 0.222222, 0.3333333]],
? ? ? ? ? ? ?dtype=torch.float64, ? ? ? ? ? ? ?device=torch.device('cpu'))
Out[113]: tensor([[0.1111, 0.2222, 0.3333]], dtype=torch.float64)
torch.tensor(3.14159)
Out[114]: tensor(3.1416)
torch.tensor([])?
Out[115]: tensor([])

torch.tensor([[0.11111, 0.222222, 0.3333333]],
? ? ? ? ? ? ?dtype=torch.float64, ? ? ? ? ? ? ?device=torch.device('cpu'), requires_grad=True, pin_memory=False)
Out[117]: tensor([[0.1111, 0.2222, 0.3333]], dtype=torch.float64, requires_grad=True)
  • dtype(torch.dtype,可選)–返回張量的所需數(shù)據(jù)類型。默認(rèn)值:如果沒(méi)有,則根據(jù)數(shù)據(jù)推斷數(shù)據(jù)類型。
  • device(torch.device,可選)–構(gòu)造張量的裝置。如果沒(méi)有,并且數(shù)據(jù)是張量,那么就使用數(shù)據(jù)設(shè)備。如果沒(méi)有且數(shù)據(jù)不是張量,則結(jié)果張量在CPU上構(gòu)造。
  • require_grad(bool,可選)– 是否需要保留梯度信息。默認(rèn)值:False。
  • pin_memory(bool,可選)–如果設(shè)置了,返回的張量將分配到pind內(nèi)存中。僅適用于CPU張量。默認(rèn)值:False。

1.2 將numpy的ndarray轉(zhuǎn)為tensor

>>> a = numpy.array([1, 2, 3])
>>> t = torch.as_tensor(a)
>>> t
tensor([1, 2, 3])
>>> t[0] = -1
>>> a
array([-1, ?2, ?3])
>>> a = numpy.array([1, 2, 3])
>>> t = torch.as_tensor(a, device=torch.device('cuda'))
>>> t[0] = -1
>>> a
array([1, 2, 3])

t = torch.as_tensor([2, 2, 2], device=torch.device('cuda'))
>>> t
tensor([2, 2, 2], device='cuda:0')

a = numpy.array([1, 2, 3])
t = torch.from_numpy(a)
t
Out[38]: tensor([1, 2, 3])
t[0] = -1
a
Out[40]: array([-1, ?2, ?3])

2、生成全0或者全1的tensor

torch.zeros(2, 3)
Out[41]:?
tensor([[0., 0., 0.],
? ? ? ? [0., 0., 0.]])
torch.zeros(5)
Out[42]: tensor([0., 0., 0., 0., 0.])

torch.ones(2, 3)
Out[43]:?
tensor([[1., 1., 1.],
? ? ? ? [1., 1., 1.]])
torch.ones(5)
Out[44]: tensor([1., 1., 1., 1., 1.])

參數(shù)列表:

  • out:輸出的對(duì)象
  • dtype: 返回的張量的所需數(shù)據(jù)類型。默認(rèn)值:如果沒(méi)有,則使用全局默認(rèn)值(請(qǐng)參閱torch.set_Default_tensor_type())。
  • layout
  • device: 構(gòu)造張量的裝置。如果沒(méi)有,并且數(shù)據(jù)是張量,那么就使用數(shù)據(jù)設(shè)備。如果沒(méi)有且數(shù)據(jù)不是張量,則結(jié)果張量在CPU上構(gòu)造。
  • requires_grad: 是否需要保留梯度信息。默認(rèn)值:False。

3、生成序列

3.1、 生成一個(gè)指定步長(zhǎng)的等差序列

torch.arange(5)
Out[45]: tensor([0, 1, 2, 3, 4])
torch.arange(1, 4)
Out[46]: tensor([1, 2, 3])
torch.arange(1, 2.5, 0.5)
Out[47]: tensor([1.0000, 1.5000, 2.0000])
  • start: 點(diǎn)集的開(kāi)始值。默認(rèn)值:0。
  • end: 點(diǎn)集的結(jié)束值
  • step: 每對(duì)相鄰點(diǎn)之間的間隙。默認(rèn)值:1,可以是小數(shù)。

3.2 生成一個(gè)指定步數(shù)的等差數(shù)列

torch.linspace(3, 10, steps=5)
Out[49]: tensor([ 3.0000, ?4.7500, ?6.5000, ?8.2500, 10.0000])
torch.linspace(-10, 10, steps=5)
Out[50]: tensor([-10., ?-5., ? 0., ? 5., ?10.])
torch.linspace(start=-10, end=10, steps=1)
Out[51]: tensor([-10.])

4、生成指定大小的單位矩陣

torch.eye(3)
Out[58]:?
tensor([[1., 0., 0.],
? ? ? ? [0., 1., 0.],
? ? ? ? [0., 0., 1.]])

5、生成一個(gè)指定大小張量

torch.empty((2,3), dtype=torch.int64)
Out[59]:?
tensor([[0, 0, 0],
? ? ? ? [0, 0, 2]])

6、 創(chuàng)建一個(gè)指定大小的張量。張量的數(shù)據(jù)是填充的指定值

torch.full((2, 3), 3.141592)
Out[67]:?
tensor([[3.1416, 3.1416, 3.1416],
? ? ? ? [3.1416, 3.1416, 3.1416]])

原文鏈接:https://blog.csdn.net/weixin_36893273/article/details/123544373

欄目分類
最近更新