每天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} 10start10end上以对数刻度均匀间隔的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/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值