網站首頁 編程語言 正文
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
相關推薦
- 2022-02-15 使用數組的sort方法完成項目中的排序功能(數組sort方法與chart圖表展示結合)
- 2022-06-07 ASP.NET?Core服務生命周期_基礎應用
- 2023-10-17 css標簽畫圓形進度條
- 2023-06-04 Android?ButterKnife依賴注入框架使用教程_Android
- 2022-05-31 如何使用正則表達式判斷郵箱(以C#為例)_C#教程
- 2023-02-28 css字體10px方法
- 2022-09-20 Redis超詳細分析分布式鎖_Redis
- 2022-08-03 Django框架中表單的用法_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支