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

學無先后,達者為師

網站首頁 編程語言 正文

pytorch創建tensor函數詳情_python

作者:就是一頓騷操作 ? 更新時間: 2022-05-24 編程語言

1、通過復制數據構造張量

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,可選)–返回張量的所需數據類型。默認值:如果沒有,則根據數據推斷數據類型。
  • device(torch.device,可選)–構造張量的裝置。如果沒有,并且數據是張量,那么就使用數據設備。如果沒有且數據不是張量,則結果張量在CPU上構造。
  • require_grad(bool,可選)– 是否需要保留梯度信息。默認值:False。
  • pin_memory(bool,可選)–如果設置了,返回的張量將分配到pind內存中。僅適用于CPU張量。默認值:False。

1.2 將numpy的ndarray轉為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.])

參數列表:

  • out:輸出的對象
  • dtype: 返回的張量的所需數據類型。默認值:如果沒有,則使用全局默認值(請參閱torch.set_Default_tensor_type())。
  • layout
  • device: 構造張量的裝置。如果沒有,并且數據是張量,那么就使用數據設備。如果沒有且數據不是張量,則結果張量在CPU上構造。
  • requires_grad: 是否需要保留梯度信息。默認值:False。

3、生成序列

3.1、 生成一個指定步長的等差序列

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: 點集的開始值。默認值:0。
  • end: 點集的結束值
  • step: 每對相鄰點之間的間隙。默認值:1,可以是小數。

3.2 生成一個指定步數的等差數列

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、生成一個指定大小張量

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

6、 創建一個指定大小的張量。張量的數據是填充的指定值

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

欄目分類
最近更新