Python自帶的random庫,numpy的隨機庫,torch的隨機函數


Python自帶的random庫

例如:

Python產生一個數值范圍內的不重復的隨機數,可以使用random模塊中的random.sample函數。例如從0~99中,隨機取10個不重復的數: random.sample(range(100), 10) 

 

numpy的random庫

 

torch的隨機函數

 torch.rand(*sizes) ,size可以是列表、元組或直接寫數字

Returns a tensor filled with random numbers from a uniform distribution on the interval [0, 1)

 

torch.randn(*sizes) ,size可以是列表、元組或直接寫數字

Returns a tensor filled with random numbers from a normal distribution with mean `0` and variance `1`

 

torch.randint(low, high, size),size是元組形式

Returns a tensor filled with random integers generated uniformly on the interval [low, high)

 

torch.randperm():返回一個0到n-1的數組。

 

random_()

返回的是恰好為整數的浮點型數字,例如:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM