torch
包 torch 包含了多维张量的数据结构以及基于其上的多种数学操作。另外,它也提供了多种工
具,其中一些可以更有效地对张量和任意类型进行序列化。
它有 CUDA 的对应实现,可以在 NVIDIA GPU 上进行张量运算(计算能力>=2.0)。
torch.is_tensor[source]
torch.is_tensor(obj)
如果 obj 是一个 pytorch 张量,则返回 True
参数: obj (Object) – 判断对象
torch.is_storage [source]
torch.is_storage(obj)
如何 obj 是一个 pytorch storage 对象,则返回 True
参数: input (Object) – 判断对象
torch.__set_default_tensor_type__[source]
torch.set_default_tensor_type(t)
torch.numel
torch.numel(input)->int
返回 input 张量中的元素个数
参数: input (Tensor) – 输入张量
例子:
>>> a = torch.randn(1,2,3,4,5)
>>> torch.numel(a)
120
>>> a = torch.zeros(4,4)
>>> torch.numel(a)
16
torch.set_printoptions[source]
torch.set_printoptions(precision=None, threshold=None, edgeitems=None,
linewidth=None, profile=None)
设置打印选项。 完全参考自 Numpy。
参数:
precision – 浮点数输出的精度位数 (默认为 8 )
threshold – 阈值,触发汇总显示而不是完全显示(repr)的数组元素的总数 (默认为 1000)
edgeitems – 汇总显示中,每维(轴)两端显示的项数(默认值为 3)
linewidth – 用于插入行间隔的每行字符数(默认为 80)。Thresholded matricies will ignore
this parameter.
profile – pretty 打印的完全默认值。 可以覆盖上述所有选项 (默认为 short, full)
创建操作 Creation Ops
torch.eye
torch.eye(n, m=None, out=None)
返回一个 2 维张量,对角线位置全 1,其它位置全 0
参数:
n (int ) – 行数
m (int, optional) – 列数.如果为 None,则默认为 n
out (Tensor, optinal) - Output tensor
返回值: 对角线位置全 1,其它位置全 0 的 2 维张量
返回值类型: Tensor
例子:
>>> torch.eye(3)
1 0 0
0 1 0
0 0 1
[torch.FloatTensor of size 3x3]
from_numpy
torch.from_numpy(ndarray) → Tensor
Numpy 桥,将 numpy.ndarray 转换为 pytorch 的 Tensor。 返回的张量 tensor 和 numpy 的
ndarray 共享同一内存空间。修改一个会导致另外一个也被修改。返回的张量不能改变大小。
例子:
>>> a = numpy.array([1, 2, 3])
>>> t = torch.from_numpy(a)
>>> t
torch.LongTensor([1, 2, 3])
>>> t[0] = -1
>>> a
array([-1, 2, 3])
torch.linspace
torch.linspace(start, end, steps=100, out=None) → Tensor
返回一个 1 维张量,包含在区间 start 和 end 上均匀间隔的 step s 个点。 输出 1 维张量的长
度为 steps。
参数:
start (float) – 序列的起始点
end (float) – 序列的最终值
steps (int) – 在 start 和 end 间生成的样本数
out (Tensor, optional) – 结果张量
例子:
>>> torch.linspace(3, 10, steps=5)
3.0000
4.7500
6.5000
8.2500
10.0000
[torch.FloatTensor of size 5]
>>> torch.linspace(-10, 10, steps=5)
-10
-5
0
5
10
[torch.FloatTensor of size 5]
>>> torch.linspace(start=-10, end=10, steps=5)
-10
-5
0
5
10
[torch.FloatTensor of size 5]
torch.logspace
torch.logspace(start, end, steps=100, out=None) → Tensor
返回一个 1 维张量,包含在区间 10start10start 和 10end10end 上以对数刻度均匀间隔的 steps
个点。 输出 1 维张量的长度为 steps。
参数:
start (float) – 序列的起始点
end (float) – 序列的最终值
steps (int) – 在 start 和 end 间生成的样本数
out (Tensor, optional) – 结果张量
例子:
>>> torch.logspace(start=-10, end=10, steps=5)
1.0000e-10
1.0000e-05
1.0000e+00
1.0000e+05
1.0000e+10
[torch.FloatTensor of size 5]
>>> torch.logspace(start=0.1, end=1.0, steps=5)
1.2589
2.1135
3.5481
5.9566
10.0000
[torch.FloatTensor of size 5]
torch.ones
torch.ones(*sizes, out=None) → Tensor
返回一个全为 1 的张量,形状由可变参数 sizes 定义。
参数:
sizes (int...) – 整数序列,定义了输出形状
out (Tensor, optional) – 结果张量 例子:
>>> torch.ones(2, 3)
1 1 1
1 1 1
[torch.FloatTensor of size 2x3]
>>> torch.ones(5)
1
1
1
1
1
[torch.FloatTensor of size 5]
torch.rand
torch.rand(*sizes, out=None) → Tensor
返回一个张量,包含了从区间[0,1)的均匀分布中抽取的一组随机数,形状由可变参数 sizes 定
义。
参数:
sizes (int...) – 整数序列,定义了输出形状
out (Tensor, optinal) - 结果张量 例子:
>>> torch.rand(4)
0.9193
0.3347
0.3232
0.7715
[torch.FloatTensor of size 4]
>>> torch.rand(2, 3)
0.5010 0.5140 0.0719
0.1435 0.5636 0.0538
[torch.FloatTensor of size 2x3]
torch.randn
torch.randn(*sizes, out=None) → Tensor
返回一个张量,包含了从标准正态分布(均值为 0,方差为 1,即高斯白噪声)中抽取一组随机数,
形状由可变参数 sizes 定义。 参数:
sizes (int...) – 整数序列,定义了输出形状
out (Tensor, optinal) - 结果张量
例子::
>>> torch.randn(4)
-0.1145
0.0094
-1.1717
0.9846
[torch.FloatTensor of size 4]
>>> torch.randn(2, 3)
1.4339 0.3351 -1.0999
1.5458 -0.9643 -0.3558
[torch.FloatTensor of size 2x3]
torch.randperm
torch.randperm(n, out=None) → LongTensor
给定参数 n,返回一个从 0 到 n -1 的随机整数排列。
参数:
n (int) – 上边界(不包含)
例子:
>>> torch.randperm(4)
2
1
3
0
[torch.LongTensor of size 4]
torch.arange
torch.arange(start, end, step=1, out=None) → Tensor
返回一个 1 维张量,长度为 floor((end−start)/step)floor((end−start)/step)。包含从 start 到
end,以 step 为步长的一组序列值(默认步长为 1)。
参数:
start (float) – 序列的起始点
end (float) – 序列的终止点
step (float) – 相邻点的间隔大小
out (Tensor, optional) – 结果张量
例子:
>>> torch.arange(1, 4)
1
2
3
[torch.FloatTensor of size 3]
>>> torch.arange(1, 2.5, 0.5)
1.0000
1.5000
2.0000
[torch.FloatTensor of size 3]
torch.range
torch.range(start, end, step=1, out=None) → Tensor
返回一个 1 维张量,有 floor((end−start)/step)+1floor((end−start)/step)+1 个元素。包含在半
开区间[start, end)从 start 开始,以 step 为步长的一组值。 step 是两个值之间的间隔,
即 xi+1=xi+stepxi+1=xi+step
警告:建议使用函数 torch.arange()
参数:
start (float) – 序列的起始点
end (float) – 序列的最终值
step (int) – 相邻点的间隔大小
out (Tensor, optional) – 结果张量
例子:
>>> torch.range(1, 4)
1
2
3
4
[torch.FloatTensor of size 4]
>>> torch.range(1, 4, 0.5)
1.0000
1.5000
2.0000
2.5000
3.0000
3.5000
4.0000
[torch.FloatTensor of size 7]
torch.zeros
torch.zeros(*sizes, out=None) → Tensor
返回一个全为标量 0 的张量,形状由可变参数 sizes 定义。
参数:
sizes (int...) – 整数序列,定义了输出形状
out (Tensor, optional) – 结果张量