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

學無先后,達者為師

網站首頁 編程語言 正文

PyTorch之torch.randn()如何創建正態分布隨機數_python

作者:gy笨瓜 ? 更新時間: 2023-04-01 編程語言

torch.randn()如何創建正態分布隨機數

torch.randn(*size)從均值為0,方差為1的正態分布中獲取隨機數

【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之隨機數生成方式

torch.rand()?? ?

torch.randn()

torch.normal()

torch.linespace()

1. 均勻分布

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

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

參數:

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

例子:

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

2. 標準正態分布

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

返回一個張量,包含了從標準正態分布(均值為0,方差為1,即高斯白噪聲)中抽取的一組隨機數。張量的形狀由參數sizes定義。

參數:

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

例子:

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

3.離散正態分布

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

返回一個張量,包含了從指定均值means和標準差std的離散正態分布中抽取的一組隨機數。

標準差std是一個張量,包含每個輸出元素相關的正態分布標準差。

參數:

  • means (float, optional) - 均值
  • std (Tensor) - 標準差
  • 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.線性間距向量

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

返回一個1維張量,包含在區間start和end上均勻間隔的step個點。

輸出張量的長度由steps決定。

參數:

  • start (float) - 區間的起始點
  • end (float) - 區間的終點
  • steps (int) - 在start和end間生成的樣本數
  • out (Tensor, optional) - 結果張量

例子:

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

總結

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

欄目分類
最近更新