矩阵乘法
创建torch
此处只做一些简单介绍,将展示一些常用的创建torch的方式。
从numpy数据转换
torch.from_numpy``(``_ndarray_``)`` → Tensor
>>> a = numpy.array([1, 2, 3])
>>> t = torch.from_numpy(a)
>>> t
tensor([ 1, 2, 3])
>>> t[0] = -1
>>> a
array([-1, 2, 3])
从list转换
torch.tensor``(``data, dtype=None, device=None, requires_grad=False, pin_memory=False``)`` → Tensor
- data (array_like) – Initial data for the tensor. Can be a list, tuple, NumPy
ndarray
, scalar, and other types. - dtype (
torch.dtype
, optional) – the desired data type of returned tensor. Default: ifNone
, infers data type fromdata
. - device (
torch.device
, optional) – the desired device of returned tensor. Default: ifNone
, uses the current device for the default tensor type (seetorch.set_default_tensor_type()
).device
will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types. - requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default:
False
. pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default:
False
.>>> torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]])
tensor([[ 0.1000, 1.2000],
[ 2.2000, 3.1000],
[ 4.9000, 5.2000]])
随机torch
torch.rand(*sizes, out=None) ``→ Tensor
返回一个张量,包含了从区间[0, 1)的均匀分布中抽取的一组随机数。张量的形状由参数sizes定义。
参数:
- sizes (int…) - 整数序列,定义了输出张量的形状
- out (Tensor, optinal) - 结果张量
torch.randn(*sizes, out=None)`` → Tensor
返回一个张量,包含了从标准正态分布(均值为0,方差为1,即高斯白噪声)中抽取的一组随机数。张量的 形状由参数sizes定义。
参数:
- sizes (int…) - 整数序列,定义了输出张量的形状
- out (Tensor, optinal) - 结果张量
torch.normal(means, std, out=None) ``→ Tensor
返回一个张量,包含了从指定均值means和标准差std的离散正态分布中抽取的一组随机数。
标准差std是一个张量,包含每个输出元素相关的正态分布标准差。
参数:
- means (float, optional) - 均值
- std (Tensor) - 标准差
- out (Tensor) - 输出张量
torch.tensor() VS torch.Tensor()
torch.tensor()
能够控制数据的类型,当dtype=None
时,默认采用输入数据的原本类型;torch.Tensor()
相对于torch.FloatTensor()
,它会将数据类型变为float类似NumPy的方法
torch.``ones
(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensortorch.``zeros
(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor-
torch属性
torch.dtype
- torch.device
- torch.layout
- shape
-
维度扩充和压缩
压缩:去掉维数为1的维度;
b = torch.squeeze(a)
扩充:在指定dim增加维数为1的维度;
d = torch.unsqueeze(c, 1)
narrow函数实现数据截取
torch.narrow(input, dim, start, length) → Tensor
输出tensor和输入的维数还是相同,但是相同维度上维数存在截取。
如下列:>>> import torch
>>> a = torch.randn((3,3,3))
>>> a
tensor([[[-1.8521, 1.4378, 0.9785],
[-0.7449, 0.5619, -0.0150],
[-2.0747, -1.9051, 2.4881]],
[[ 0.3092, -1.6075, -1.0128],
[ 1.5866, 0.6185, -0.3448],
[ 1.3768, 0.6300, -1.0388]],
[[ 2.3150, 1.1148, 0.1757],
[-0.2820, 0.1473, -0.7576],
[ 0.4451, -1.3251, 0.3433]]])
>>> a.narrow(2,0,2)
tensor([[[-1.8521, 1.4378],
[-0.7449, 0.5619],
[-2.0747, -1.9051]],
[[ 0.3092, -1.6075],
[ 1.5866, 0.6185],
[ 1.3768, 0.6300]],
[[ 2.3150, 1.1148],
[-0.2820, 0.1473],
[ 0.4451, -1.3251]]])
nonzero获取非零索引
>>> a = torch.randn((2,2,))
>>> a
tensor([[-0.0703, 0.7614],
[-1.4132, 0.0907]])
>>> a.nonzero()
tensor([[0, 0],
[0, 1],
[1, 0],
[1, 1]])
注意:输出都是二维数组 ```python
a[:,3] tensor([-0.1084, 0.1881]) torch.nonzero(a[:,3]) tensor([[0],
[1]])
c = torch.randn((2,2,2)) torch.nonzero(c) tensor([[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]])
<a name="qMMdk"></a>
## new创建新的Tensor
创建一个新的Tensor,该Tensor的type和device都和原有Tensor一致,且无内容。
```python
# 第一种
a
tensor([[-0.0703, 0.7614],
[-1.4132, 0.0907]])
b = a.new()
b
tensor([])
b.shape
torch.Size([0])
# 第二种
b = torch.Tensor.new(a)
b
tensor([])
b = a.new(1, 3)
b
tensor([[0., 0., nan]])
max函数获取Tensor中最大值及其下标
a
tensor([[-0.0703, 0.7614],
[-1.4132, 0.0907]])
torch.max(a, 1)
torch.return_types.max(
values=tensor([0.7614, 0.0907]),
indices=tensor([1, 1]))
sort排序
c = torch.randn(10)
c
tensor([ 1.8778, 0.9451, 0.0307, 0.7266, -1.5963, -0.7301, 0.6295, 0.6368,
0.7351, -1.6350])
torch.sort(c,descending=True)
torch.return_types.sort(
values=tensor([ 1.8778, 0.9451, 0.7351, 0.7266, 0.6368, 0.6295, 0.0307, -0.7301,
-1.5963, -1.6350]),
indices=tensor([0, 1, 8, 3, 7, 6, 2, 5, 4, 9]))
返回两个tensor,第一个为value,第二个为index。可以利用index对应的tensor去索引原tensor从而得到sort的value。
index_select进行下标筛选
a = torch.randn((2,3))
a
tensor([[-0.8288, 0.6638, -0.1492],
[ 0.3325, 0.8650, 0.0925]])
torch.index_select(a,1,torch.LongTensor([0,1]))
tensor([[-0.8288, 0.6638],
[ 0.3325, 0.8650]])
参数说明:index_select(x, dim, indices)
repeat函数
repeat(*sizes) -> Tensor
*size(torch.Size or int) - The number of times to repeat this tensor along each dimension.
Repeats this tensor along the specified dimensions.
a
tensor([[-0.8288, 0.6638, -0.1492],
[ 0.3325, 0.8650, 0.0925]])
# 第一个维度repeat2次,第二个维度repeat两次
a.repeat(2,2)
tensor([[-0.8288, 0.6638, -0.1492, -0.8288, 0.6638, -0.1492],
[ 0.3325, 0.8650, 0.0925, 0.3325, 0.8650, 0.0925],
[-0.8288, 0.6638, -0.1492, -0.8288, 0.6638, -0.1492],
[ 0.3325, 0.8650, 0.0925, 0.3325, 0.8650, 0.0925]])