pytorch中的torch.rand(),torch.randn(),torch.randerm()的關係

1. torch.rand()

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

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

1.1 參數

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

1.2 例子

>>> torch.rand(4)

 0.9193
 0.3347
 0.3232
 0.7715
[torch.FloatTensor of size 4]

>>> torch.rand(2, 3)

 0.5010  0.5140  0.0719
 0.1435  0.5636  0.0538
[torch.FloatTensor of size 2x3]

2. torch.randn()

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

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

2.1 參數

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

2.2 例子

>>> torch.randn(4)

-0.1145
 0.0094
-1.1717
 0.9846
[torch.FloatTensor of size 4]

>>> torch.randn(2, 3)

 1.4339  0.3351 -1.0999
 1.5458 -0.9643 -0.3558
[torch.FloatTensor of size 2x3]

3. torch.randperm

torch.randperm(n, out=None) → LongTensor

給定參數n,返回一個從0 到n -1 的隨機整數排列。

3.1 參數

  • n (int) – 上邊界(不包含)

3.2 例子

>>> torch.randperm(4)

 2
 1
 3
 0
[torch.LongTensor of size 4]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章