【无标题】

numpy使用random生成一个float(Double)类随机矩阵

一、np.random.rand(a1,a2,a3,… )生成一个a1Xa2Xa3的矩阵,元素的值为0~1。

import numpy as np
input = np.random.rand(2,3,4)*10 #乘以10,把元素的值变为0~10,同理可以变为任意范围
input = input.astype(np.float32)  #使用astype()可以变为指定类型的数据,doubel就是float32
input = torch.from_numpy(input)

二、np.random.randn(a1,a2,a3,… )生成一个a1Xa2Xa3的矩阵,元素的值服从正太分布,均值为0,方差为1。

import numpy as np
input = np.random.randn(2,3,4)*10 #乘以10,把元素的值变为0~10,同理可以变为任意范围
input = input.astype(np.float32)  #使用astype()可以变为指定类型的数据,doubel就是float32
input = torch.from_numpy(input)
上一篇:成为华尔街金融巨鳄第一课:学会使用NumPy


下一篇:NumPy