每天5个PyTorch函数(2)
1. torch.linspace(start, end, steps)
输出一个1维张量,包含在区间start 和 end 上均匀间隔的steps个点。 输出1维张量的长度为steps。
start (float) – 序列的起始点
end (float) – 序列的最终值
steps (int) – 在start 和 end间生成的样本数
注意:包括start和end值(闭区间),steps表示区间内样本的个数。
import torch
m = torch.linspace(start=-10,end=10,steps=100)
print(m.shape) # 100
# 或者采用print(m.size())
2. torch.logspace(start, end, steps=100)
返回一个1维张量,包含在区间
1
0
s
t
a
r
t
和
1
0
e
n
d
10^{start} 和10^{end}
10start和10end上以对数刻度均匀间隔的steps个点。 输出1维张量的长度为steps。
start (float) – 序列的起始点
end (float) – 序列的最终值
steps (int) – 在start 和 end间生成的样本数
注意:包括start和end值(闭区间),steps表示区间内样本的个数。
import torch
m = torch.logspace(start=-10,end=10,steps=5)
print(m.shape[0])
print(m)
3. torch.ones(int1,int2,int3,…)
返回一个全为1 的张量,形状由可变参数sizes定义。
int1,int2,int3,表示整数序列,定义了输出形状
import torch
print(torch.ones(3,3))
# 输出为
# tensor([[1., 1., 1.],
# [1., 1., 1.],
# [1., 1., 1.]])
print(torch.ones(3,3,3))
# 输出为
# tensor([[[1., 1., 1.],
# [1., 1., 1.],
# [1., 1., 1.]],
# [[1., 1., 1.],
# [1., 1., 1.],
# [1., 1., 1.]],
# [[1., 1., 1.],
# [1., 1., 1.],
# [1., 1., 1.]]])
4. torch.rand(int1,int2,int3,…)
返回一个张量,包含了从区间 [0,1) 的均匀分布中抽取的一组随机数,形状由可变参数sizes 定义。
int1,int2,int3,表示整数序列,定义了输出形状
imort torch
print(torch.rand(2,2,2))
# 输出
# tensor([[[0.4600, 0.4005],
# [0.7309, 0.3760]],
# [[0.2829, 0.5866],
# [0.0075, 0.9303]]])
print(torch.rand(2,2))
# tensor([[0.1657, 0.5557],
# [0.0112, 0.0800]])
print(torch.rand(3))
# tensor([0.7393, 0.8022, 0.1149])
5. torch.randn(int1,int2,int3,)
返回一个张量,包含了从标准正态分布(均值为0,方差为 1,即高斯白噪声)中抽取一组随机数,形状由可变参数sizes定义。
int1,int2,int3,表示整数序列,定义了输出形状
print(torch.randn(2,2,2))
# tensor([[[ 0.5791, 1.2387],
# [-0.5589, 0.0575]],
# [[ 1.5684, 1.0949],
# [-0.2644, -0.0189]]])
感谢大家的观看,如果有问题,欢迎批评指正,明天见。
[1]: https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch/