torch记录:张量、采样、操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/NockinOnHeavensDoor/article/details/82260937


参考:http://pytorch.apachecn.org/cn/0.3.0/torch.html#

Tensors (张量)

torch.zeros(*sizes, out=None) → Tensor

返回填充了标量值为 0 的 Tensor, 其形状由可变参量 sizes 定义.

例子:

>>> torch.zeros(2, 3)

 0  0  0
 0  0  0
[torch.FloatTensor of size 2x3]

Indexing, Slicing, Joining, Mutating Ops (索引, 切片, 连接, 换位) 操作

torch.cat(seq, dim=0, out=None) → Tensor

Parameters:
seq (sequence of Tensors) – 可以是任何相同类型的 Tensor 的 Python 序列.
dim (int, optional) – tensors 级联的维数
out (Tensor, optional) – 输出参数

例子:

>>> x = torch.randn(2, 3)
>>> x

 0.5983 -0.0341  2.4918
 1.5981 -0.5265 -0.8735
[torch.FloatTensor of size 2x3]

>>> torch.cat((x, x, x), 0)

 0.5983 -0.0341  2.4918
 1.5981 -0.5265 -0.8735
 0.5983 -0.0341  2.4918
 1.5981 -0.5265 -0.8735
 0.5983 -0.0341  2.4918
 1.5981 -0.5265 -0.8735
[torch.FloatTensor of size 6x3]

>>> torch.cat((x, x, x), 1)

 0.5983 -0.0341  2.4918  0.5983 -0.0341  2.4918  0.5983 -0.0341  2.4918
 1.5981 -0.5265 -0.8735  1.5981 -0.5265 -0.8735  1.5981 -0.5265 -0.8735
[torch.FloatTensor of size 2x9]

torch.squeeze(input, dim=None, out=None) → Tensor

Returns a tensor with all the dimensions of input of size 1 removed.
For example, if input is of shape: (A×1×B×C×1×D) then the out tensor will be of shape: (A×B×C×D).

Note
作为上述的一个例外,size 为 1 的一维张量不会改变维度.
返回张量与输入张量共享内存,所以改变其中一个的内容会改变另一个.

Parameters:
input (Tensor) – 输入张量
dim (int, optional) – 如果给定 dim 时,则 input 只会在给定维度执行挤压
out (Tensor, optional) – 结果张量

例子:

x = torch.zeros(2,1,2,1,2)
print(x.size())
y = torch.squeeze(x)
print(y.size())
y = torch.squeeze(x, 0)
print(y.size())
# 第二个维度做压缩
y = torch.squeeze(x, 1)
print(y.size())


torch.Size([2, 1, 2, 1, 2])
torch.Size([2, 2, 2])
torch.Size([2, 1, 2, 1, 2])
torch.Size([2, 2, 1, 2])

Math operations (数学操作)

Other Operations (其它操作)

torch.bmm(batch1, batch2, out=None) → Tensor

执行保存在 batch1 和 batch2 中的矩阵的批量点乘.
batch1 和 batch2 必须是三维的张量, 且每个包含相同数量的矩阵.
如果 batch1 是一个 b x n x m 的张量, batch2 是一个 b x m x p 的张量, out 将是一个 b x n x p 的张量.

Parameters:
batch1 (Tensor) – 要相乘的第一批矩阵
batch2 (Tensor) – 要相乘的第二批矩阵
out (Tensor, optional) – 输出结果

例子:

# batch1:10个2*1的矩阵
batch1 = torch.randn(10, 2, 1)
batch2 = torch.randn(10, 1, 3)
res = torch.bmm(batch1, batch2)
res.size()

torch.Size([10, 2, 3])
print(batch1[0,:,:]*batch2[0,:,:])
print(res[0,:,:])

tensor([[-0.7956,  0.7705,  1.9109],
        [-1.3829,  1.3391,  3.3212]])
tensor([[-0.7956,  0.7705,  1.9109],
        [-1.3829,  1.3391,  3.3212]])

torch.eig(a, eigenvectors=False, out=None) -> (Tensor, Tensor)

计算实数方阵的特征值和特征向量.

Parameters:
a (Tensor) – 一个要被计算特征值与特征向量的方阵
eigenvectors (bool) – 若为 True, 表示特征值与特征向量都被计算. 否则, 仅计算特征值.
out (tuple, optional) – 输出张量
Returns:
包含

e (Tensor): a 的左特征值
v (Tensor): 如果 eigenvectors 为 True, 表示 a 的特征向量;
否则是一个空的张量

例子:

matrix = torch.randn(2,2)
torch.eig(matrix)

(tensor([[-2.1827,  0.0000],
         [ 1.9254,  0.0000]]), tensor([]))

猜你喜欢

转载自blog.csdn.net/NockinOnHeavensDoor/article/details/82260937