網站首頁 編程語言 正文
Pytorch torch.distributions庫
包介紹
torch.distributions包包含可參數化的概率分布和采樣函數。 這允許構建用于優化的隨機計算圖和隨機梯度估計器。
不可能通過隨機樣本直接反向傳播。 但是,有兩種主要方法可以創建可以反向傳播的代理函數。
這些是
評分函數估計量 score function estimato
似然比估計量 likelihood ratio estimator
REINFORCE
路徑導數估計量 pathwise derivative estimator
REINFORCE 通常被視為強化學習中策略梯度方法的基礎,
路徑導數估計器常見于變分自編碼器的重新參數化技巧中。
雖然評分函數只需要樣本 f(x)的值,但路徑導數需要導數 f'(x)。
本文重點講解Pytorch中的 torch.distributions庫。
pytorch 的 torch.distributions 中可以定義正態分布:
import torch from torch.distributions import Normal mean=torch.Tensor([0,2]) normal=Normal(mean,1)
sample()
就是直接在定義的正太分布(均值為mean,標準差std是1)上采樣:
result = normal.sample() print("sample():",result)
輸出:
sample(): tensor([-1.3362, ?3.1730])
rsample()
不是在定義的正太分布上采樣,而是先對標準正太分布 N(0,1) 進行采樣,然后輸出: mean + std × 采樣值
result = normal.rsample() print("rsample():",result)
輸出:
rsample: tensor([ 0.0530, ?2.8396])
log_prob(value)
是計算value在定義的正態分布(mean,1)中對應的概率的對數,正太分布概率密度函數是:
對其取對數可得:
這里我們通過對數概率還原其對應的真實概率:
print("result log_prob:",normal.log_prob(result).exp())
輸出:
result log_prob: tensor([ 0.1634, ?0.2005])
原文鏈接:https://blog.csdn.net/qq_38789531/article/details/104950940
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-07-08 一文詳解C++中運算符的使用_C 語言
- 2022-11-28 golang進程在docker中OOM后hang住問題解析_Golang
- 2023-07-09 Python Django 零基礎從零到一部署服務,Hello Django!全文件夾目錄和核心代碼
- 2022-05-13 e engine “node“ is incompatible with this module.
- 2022-12-08 用C語言求解一元二次方程的簡單實現_C 語言
- 2022-03-26 c語言實現可自定義的游戲地圖_C 語言
- 2022-04-17 算法時間復雜度和空間復雜度
- 2022-10-23 C#各種異常處理方式總結_C#教程
- 欄目分類
-
- 最近更新
-
- 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同步修改后的遠程分支