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

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

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

PyTorch之torch.randn()如何創(chuàng)建正態(tài)分布隨機(jī)數(shù)_python

作者:gy笨瓜 ? 更新時(shí)間: 2023-04-01 編程語(yǔ)言

torch.randn()如何創(chuàng)建正態(tài)分布隨機(jī)數(shù)

torch.randn(*size)從均值為0,方差為1的正態(tài)分布中獲取隨機(jī)數(shù)

【sample】

In [1]: import torch
In [2]: torch.randn(3)
Out[2]: tensor([1.7896, 0.7974, 0.7416])
In [3]: torch.randn(2,3)
Out[3]: tensor([[ 0.4030, -0.3138, -0.7579],
? ? ? ? [-0.1486, ?1.0306, ?0.0734]])
In [4]: torch.randn(())
Out[4]: tensor(-0.8383) # 維度為0

torch之隨機(jī)數(shù)生成方式

torch.rand()?? ?

torch.randn()

torch.normal()

torch.linespace()

1. 均勻分布

torch.rand(*sizes, out=None) → Tensor

返回一個(gè)張量,包含了從區(qū)間[0, 1)的均勻分布中抽取的一組隨機(jī)數(shù)。張量的形狀由參數(shù)sizes定義。

參數(shù):

  • sizes (int...) - 整數(shù)序列,定義了輸出張量的形狀
  • out (Tensor, optinal) - 結(jié)果張量

例子:

torch.rand(2, 3)
0.0836 0.6151 0.6958
0.6998 0.2560 0.0139
[torch.FloatTensor of size 2x3]

2. 標(biāo)準(zhǔn)正態(tài)分布

torch.randn(*sizes, out=None) → Tensor

返回一個(gè)張量,包含了從標(biāo)準(zhǔn)正態(tài)分布(均值為0,方差為1,即高斯白噪聲)中抽取的一組隨機(jī)數(shù)。張量的形狀由參數(shù)sizes定義。

參數(shù):

  • sizes (int...) - 整數(shù)序列,定義了輸出張量的形狀
  • out (Tensor, optinal) - 結(jié)果張量

例子:

torch.randn(2, 3)
0.5419 0.1594 -0.0413
-2.7937 0.9534 0.4561
[torch.FloatTensor of size 2x3]

3.離散正態(tài)分布

torch.normal(means, std, out=None) → → Tensor

返回一個(gè)張量,包含了從指定均值means和標(biāo)準(zhǔn)差std的離散正態(tài)分布中抽取的一組隨機(jī)數(shù)。

標(biāo)準(zhǔn)差std是一個(gè)張量,包含每個(gè)輸出元素相關(guān)的正態(tài)分布標(biāo)準(zhǔn)差。

參數(shù):

  • means (float, optional) - 均值
  • std (Tensor) - 標(biāo)準(zhǔn)差
  • out (Tensor) - 輸出張量

例子:

torch.normal(mean=0.5, std=torch.arange(1, 6))
-0.1505
-1.2949
-4.4880
-0.5697
-0.8996
[torch.FloatTensor of size 5]

4.線(xiàn)性間距向量

torch.linspace(start, end, steps=100, out=None) → Tensor

返回一個(gè)1維張量,包含在區(qū)間start和end上均勻間隔的step個(gè)點(diǎn)。

輸出張量的長(zhǎng)度由steps決定。

參數(shù):

  • start (float) - 區(qū)間的起始點(diǎn)
  • end (float) - 區(qū)間的終點(diǎn)
  • steps (int) - 在start和end間生成的樣本數(shù)
  • out (Tensor, optional) - 結(jié)果張量

例子:

torch.linspace(3, 10, steps=5)
3.0000
4.7500
6.5000
8.2500
10.0000
[torch.FloatTensor of size 5]

總結(jié)

原文鏈接:https://blog.csdn.net/u012633319/article/details/110456104

欄目分類(lèi)
最近更新