PyTorch_001. 张量

0. 前言

提起 PyTorch ,需要先了解一下 Torch 这个概念。Torch 是一个含有大量机器学习算法支持的科学计算框架,
是一个与 Numpy 类似的张量操作库,它是 PyTorch 的前身。PyTorch 是一个基于 Torch 的 Python 开源机器学习库,可用于自然语言等应用程序。

1. 张量 (Tensor)

下面通一些简单的代码来熟悉张量

  • 导入相关的库
from __future__ import print_function
import torch
  • 构造矩阵
    下面的代码构造一个 3X4 和 2X3 的矩阵,并且未进行初始化
X = torch.empty(3, 4)
print(X)
Y = torch.empty(2, 3)
print(Y)

输出结果:

tensor([[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]])
tensor([[0.0000e+00, 0.0000e+00, 1.6816e-44],
        [0.0000e+00, 1.4013e-45, 0.0000e+00]])
  • 构造随机初始化的矩阵
Rand_X = torch.rand(3, 4)
print(Rand_X)

输出结果:

tensor([[0.2340, 0.4137, 0.9732, 0.4207],
        [0.2691, 0.4998, 0.3148, 0.8954],
        [0.4806, 0.8397, 0.5089, 0.4403]])
  • 构造全 0 矩阵
Zero_X = torch.zeros(size=(3, 4), dtype=torch.int32)   
print(Zero_X)

输出结果:

tensor([[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]], dtype=torch.int32)
  • 直接使用数据构造一个张量
tensor_X = torch.tensor([3.2, 4])
print(tensor_X)

输出结果:

tensor([3.2000, 4.0000])
  • 在已有Tensor上创建新的Tensor
X = torch.empty(3, 4)
One_X = X.new_ones([3, 2], dtype=torch.double)  # 新Tensor的是全1的
print(One_X)

输出结果:

tensor([[1., 1.],
        [1., 1.],
        [1., 1.]], dtype=torch.float64)
  • 生成相同维度的随机的矩阵
    下面代码中先创建了一个Tensor—temp_X,其 Size 是 [3, 2],下一行代码创建了一个与其维度相同的随机的 Tensor,所以最后分别打印两个 Tensor 的 Size 它们的 Size 是相同的。
temp_X = torch.tensor([[1, 2], [3, 4], [5, 6]])
same_size_temp_X = torch.rand_like(temp_X, dtype=torch.float)
print(same_size_temp_X)
print("temp_X 的 Size:{}".format(temp_X.size()))
print("same_size_temp_X 的 Size:{}".format(same_size_temp_X.size()))

输出结果:

tensor([[0.4356, 0.6443],
        [0.2995, 0.5819],
        [0.7697, 0.5433]])
temp_X 的 Size:torch.Size([3, 2])
same_size_temp_X 的 Size:torch.Size([3, 2])

以上是关于创建 Tensor 的一些语法,下面将对其的一些基本操作进行说明。

2. 基本操作

  • 加法
X = tc.tensor([[1, 2], [3, 4], [5, 6]])
Y = tc.ones(3, 2)  # 全 1
# 1. 加法操作
# 1.1 直接相加
add_XY1 = X + Y
print("add_XY1 = {}".format(add_XY1))
# 1.2 通过 torch.add
add_XY2 = tc.add(X, Y)
print("add_XY2 = {}".format(add_XY2))

输出结果:

add_XY1 = tensor([[2., 3.],
        [4., 5.],
        [6., 7.]])
add_XY2 = tensor([[2., 3.],
        [4., 5.],
        [6., 7.]])

除了上面的这种形式之外,这里的加法求得的结果,还可以“提供一个输出 tensor 作为参数”。这句话的意思是,对于输出的结果在add中提供一个参数 out 来表示它的输出,下面代码中我们对于传进的这个tensor并未初始化,且其维度也跟矩阵的维度设置相同。

add_temp = tc.empty(3, 2)
tc.add(X, Y, out=add_temp)
print(add_temp)

输出结果:

tensor([[2., 3.],
        [4., 5.],
        [6., 7.]])

还可以通过 add_()的方式,如下:

Y.add_(X)
print(Y)

输出结果:

tensor([[2., 3.],
        [4., 5.],
        [6., 7.]])
  • 索引操作
X = tc.tensor([[1, 2], [3, 4], [5, 6]])
X_col_0 = X[:, 0]  # 第0列
print(X_col_0)

输出结果:

tensor([1, 3, 5])
X_col_1 = X[:, 1]  # 第1列
print(X_col_1)

输出结果:

tensor([2, 4, 6])
X_row_0 = X[0, :]  # 第0行
print(X_row_0)

输出结果:

tensor([1, 2])
X_col_0_row_1 = X[1, 0]  # 第1行第0列
print(X_col_0_row_1)

输出结果:

tensor(3)
  • 改变 Tensor 大小或形状
# 3. 改变 Tensor 大小或形状
temp = tc.ones(3, 4)
print("temp:", temp)
print("temp Size:", temp.size())
re_temp1 = temp.view(12)
print("re_temp1:", re_temp1)
print("re_temp1 Size:", re_temp1.size())
re_temp2 = temp.view(-1, 6)
print("re_temp2:", re_temp2)
print("re_temp2 Size:", re_temp2.size())

其中,temp.view(-1, 6)表示-1只是先表示一个占位,即事先并不知道是多少,是要根据后面的数字来进行计算的,这里就是因为后面是6,所以 3*4/6 = 2,所以在下面的输出的 Size 也可以看出来,-1 处是 2

输出结果:

temp: tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]])
temp Size: torch.Size([3, 4])
re_temp1: tensor([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
re_temp1 Size: torch.Size([12])
re_temp2: tensor([[1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1.]])
re_temp2 Size: torch.Size([2, 6])
  • 获取一个元素 tensor 的 value
Z = tc.rand(1)
print(Z)
print(Z.item())

输出结果:

tensor([0.9753])
0.9752973318099976

猜你喜欢

转载自blog.csdn.net/weixin_41857483/article/details/112972203