Pytorch学习之torch----创建操作

更新时间:2023-05-24 10:22:29 阅读: 评论:0

Pytorch学习之torch----创建操作
1. (n, m=None, out=None)
说明:创建⼀个2维张量,对⾓线数字为1, 其他位置为0。也就是⼀个单位矩阵。
my favourite food
参数:
n -- ⾏数,
m -- 列数,如果为None,默认等于n,
out -- 输出张量
>>> import torch
>>> (3)
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
>>> (3, 4)
tensor([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.]])
2. torch.from_numpy(ndarray)
说明:将numpy.ndarray转换为Tensor。返回的Tensor和numpy的ndarray共享同⼀内存空间。修改⼀个会导致另外⼀个也被修改。返回的张量不能调整⼤⼩。
>>> import numpy
>>> a = numpy.array([1, 2, 3])
>>> t = torch.from_numpy(a)
>>> t
tensor([1, 2, 3], dtype=torch.int32)
>>> t[0] = -1
>>> a
array([-1,  2,  3])
拼音字母写法>caring3. torch.linspace(start, end, steps=100, out=None)
说明:返回start和end之间长度为steps的⼀维张量,也就是start和end之间的steps个数。并且其返回的是⼀个等差数列。
参数:
start(float) -- 点集的起始值,
end(float) -- 点集的最终值,
steps(int) -- start和end之间的采样数,即返回多少个数
out(Tensor,可选) -- 结果张量
>>> torch.linspace(-10, 10, steps=5)
tensor([-10.,  -5.,  0.,  5.,  10.])
>>> torch.linspace(0, 5, 5)
tensor([0.0000, 1.2500, 2.5000, 3.7500, 5.0000])
>>> torch.linspace(start=-10, end=10, steps=10)
tensor([-10.0000,  -7.7778,  -5.5556,  -3.3333,  -1.1111,  1.1111,  3.3333,
5.5556,  7.7778,  10.0000])
2016考研国家分数线4. torch.logspace(start, end, steps=100, out=None)
说明:返回⼀个1维张量,包含在区间和上,以对数刻度均匀间隔的steps个点。输出1维张量的长度为steps。参数:
start(float) -- 点集的起始点
end(float) -- 点集的最终点
steps(int) -- 在start和end间⽣成的样本数
out(Tensor,可选) -- 结果张量
>>> torch.logspace(start=-10, end=10, steps=5)
tensor([1.0000e-10, 1.0000e-05, 1.0000e+00, 1.0000e+05, 1.0000e+10])
>>> torch.logspace(0.1, 1.0, 5)
gageetensor([ 1.2589,  2.1135,  3.5481,  5.9566, 10.0000])
5. s(*sizes, out=None)
说明:返回⼀个全为1的张量,形状由可变参数sizes定义。
参数:
sizes(int) -- 整数序列,定义了输出的形状。如(3,3)
out(Tensor, 可选) -- 结果张量
>>> s(2, 3)
tensor([[1., 1., 1.],
[1., 1., 1.]])
>>> s(5)
tensor([1., 1., 1., 1., 1.])
6. torch.rand(*size, out=None)
asmuchas说明:返回⼀个张量,填充在[0,1]区间的⼀组均匀分布随机数。Tensor的形状由变量sizes定义。
参数:
sizes(int) -- 整数序列,定义了输出形状
out(Tensor, 可选) -- 结果张量
>>> torch.rand(4)
tensor([0.4962, 0.0724, 0.0478, 0.3524])
>>> torch.rand(2, 3)
tensor([[0.3200, 0.7308, 0.3226],
[0.8039, 0.2359, 0.7256]])
7. torch.randn(*size, out=None)
说明:返回⼀个张量,包含了从正态分布(均值为0,⽅差为1)中抽取⼀组随机数。Tensor的形状由变量sizes定义。
参数:
sizes(int) -- 整数序列,定义了输出形状
out(Tensor, 可选) -- 结果张量
dirac
>>> torch.randn(4)
tensor([ 0.3094,  0.4774, -0.1807,  0.9894])
>>> torch.randn(2, 3)
tensor([[-0.3299, -0.0495, -1.4758],
[-0.0680, -0.3875,  0.9846]])
8. torch.randperm(n, out=None)
说明:返回以LongTenor,输⼊参数n,返回⼀个从0到n-1的随机整数排列。
参数:
n(int) -- 上限,即最⼤值。
sg什么意思
>>> torch.randperm(4)
tensor([3, 1, 0, 2])
9. torch.arange(start, end, step=1, out=None)
说明: 返回⼀个1维张量,长度为,中间计算值,向下取整的意思。包含从start到end,以
sre
step为步长的⼀组序列值。默认步长为1。
参数:
start(float) -- 该点集的起始点
end(float) -- 点集的终⽌点
step(float) -- 相邻点的间隔⼤⼩
out(Tensor, 可选的) -- 结果张量
>>> torch.arange(1, 4)
tensor([1, 2, 3])
>>> torch.arange(1, 2.5, 0.5)
tensor([1.0000, 1.5000, 2.0000])
10. torch.range(start, end, step=1, out=None)
说明:返回⼀维张量,长度为+1,从start开始,到end结束。以step为步长的⼀组值。step
是两个值之间的间隔。
参数:
start(float) -- 点集的起始点
end(float) -- 点集的最终值
step(int) -- 相邻点之间的间隔⼤⼩
out(Tensor, 可选的) -- 结果张量
>>> torch.range(1, 4)
__main__:1: UrWarning: torch.range is deprecated in favor of torch.arange and will be removed in 0.5. Note that arange generates values in [start; end), not [s tensor([1., 2., 3., 4.])
>>> torch.range(1, 4)
tensor([1., 2., 3., 4.])
>>> torch.range(1, 4, 0.5)
tensor([1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000])
11. s(*size, out=None)
说明:返回⼀个全0的张量,形状由可变参数sizes定义。
参数:
sizes(int) -- 整数序列,定义了输出形状
out(Tensor, 可选) -- 结果张量
>>> s(2, 3)
restauranttensor([[0., 0., 0.],
[0., 0., 0.]])
>>> s(5)
tensor([0., 0., 0., 0., 0.])
总结:这部分内容主要是创建操作,创建⼀些随机数,或者是矩阵。在pytorch中,他们都是张量类型。简单的创建操作。反复练习。⽅可掌握。如果有numpy的基础。那就更加容易学习。其实⽆论numpy,tensorflow,pytorch等。在创建⼀些随机数/矩阵中,他们都是相同的。只不过是存在不同的第三⽅库中。

本文发布于:2023-05-24 10:22:29,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/757066.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:返回   定义   创建
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图