PyTorch之torch.randn()如何創(chuàng)建正態(tài)分布隨機(jī)數(shù)
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
返回一個張量,包含了從區(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
返回一個張量,包含了從標(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
返回一個張量,包含了從指定均值means和標(biāo)準(zhǔn)差std的離散正態(tài)分布中抽取的一組隨機(jī)數(shù)。
標(biāo)準(zhǔn)差std是一個張量,包含每個輸出元素相關(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.線性間距向量
torch.linspace(start, end, steps=100, out=None) → Tensor
返回一個1維張量,包含在區(qū)間start和end上均勻間隔的step個點(diǎn)。
輸出張量的長度由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é)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
matplotlib在python上繪制3D散點(diǎn)圖實(shí)例詳解
這篇文章主要介紹了matplotlib在python上繪制3D散點(diǎn)圖實(shí)例詳解,首先介紹了官網(wǎng)的實(shí)例,然后分享了本文簡單代碼示例,具有一定借鑒價值,需要的朋友可以了解下。2017-12-12
Python for Informatics 第11章之正則表達(dá)式(四)
這篇文章主要介紹了Python for Informatics 第11章之正則表達(dá)式(四) 的相關(guān)資料,需要的朋友可以參考下2016-04-04
python實(shí)現(xiàn)讀取Excel內(nèi)容并展示成json
這篇文章主要為大家詳細(xì)介紹了如何使用python實(shí)現(xiàn)讀取Excel內(nèi)容并展示成json功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2023-12-12
Python如何存儲數(shù)據(jù)到j(luò)son文件
這篇文章主要介紹了Python如何存儲數(shù)據(jù)到j(luò)son文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03

