# 基础

[En]

A tensor represents an array of values that may have multiple dimensions. A tensor with one axis corresponds to a mathematical vector (vector); a tensor with two axes corresponds to a mathematical matrix (matrix); and a tensor with more than two axes has no special mathematical name.

[En]

Each value in a tensor is called the element of the tensor (element).

arange 创建一个行向量，可以指定数据类型，tensor 可以使用 python 列表的形式指定张量：

x1 = torch.arange(12)
x2 = torch.arange(10,dtype=torch.float32)

print(x1)
print(x2)

x3 = torch.tensor([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
print(x3)

tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
tensor([[2, 1, 4, 3],
[1, 2, 3, 4],
[4, 3, 2, 1]])


x1 = torch.arange(5)
x2 = torch.tensor([[1,2,3],[4,5,6]])

print(x1.shape)
print(x2.shape)

print(x1.numel())
print(x2.numel())

torch.Size([5])
torch.Size([2, 3])
5
6


reshape 函数可以改变一个张量的形状而不改变元素数量和元素值，可以通过 -1 来自动计算出维度：

x1 = torch.arange(12).reshape(4,3)
x2 = torch.arange(12).reshape(-1,6)
x3 = torch.arange(12).reshape(2,3,-1)

print(x1)
print(x2)
print(x3)

tensor([[ 0,  1,  2],
[ 3,  4,  5],
[ 6,  7,  8],
[ 9, 10, 11]])
tensor([[ 0,  1,  2,  3,  4,  5],
[ 6,  7,  8,  9, 10, 11]])
tensor([[[ 0,  1],
[ 2,  3],
[ 4,  5]],

[[ 6,  7],
[ 8,  9],
[10, 11]]])


zeros 函数和 ones 函数可以分别用来生成全 0 和 全 1 张量，randn 函数可以用来生成每个元素都从均值为0、标准差为1的标准高斯分布（正态分布）中随机采样：

x1 = torch.zeros((2, 3, 4))
print(x1)

x2 = torch.ones((1,2,3))
print(x2)

x3 = torch.randn((2,2,3))
print(x3)

tensor([[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]],

[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]])
tensor([[[1., 1., 1.],
[1., 1., 1.]]])
tensor([[[ 0.3054, -0.2960,  0.1536],
[-0.7202,  1.5987, -0.3821]],

[[ 0.3733,  0.2818, -1.2353],
[-0.7004,  0.6075,  0.5294]]])


# 运算符

x = torch.tensor([1.0, 2, 4, 8])
y = torch.tensor([2, 2, 2, 2])

print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x ** y)

print(torch.exp(x))

print(x == y)

tensor([ 3.,  4.,  6., 10.])
tensor([-1.,  0.,  2.,  6.])
tensor([ 2.,  4.,  8., 16.])
tensor([0.5000, 1.0000, 2.0000, 4.0000])
tensor([ 1.,  4., 16., 64.])
tensor([2.7183e+00, 7.3891e+00, 5.4598e+01, 2.9810e+03])
tensor([False,  True, False, False])


[En]

Tensor linking, that is, stacking tensors end to end to form a larger tensor, you need to specify which axis to connect:

X = torch.arange(12, dtype=torch.float32).reshape((3,4))
Y = torch.tensor([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
P = torch.cat((X, Y), dim=0)
Q = torch.cat((X, Y), dim=1)

print(P)
print(Q)

tensor([[ 0.,  1.,  2.,  3.],
[ 4.,  5.,  6.,  7.],
[ 8.,  9., 10., 11.],
[ 2.,  1.,  4.,  3.],
[ 1.,  2.,  3.,  4.],
[ 4.,  3.,  2.,  1.]])
tensor([[ 0.,  1.,  2.,  3.,  2.,  1.,  4.,  3.],
[ 4.,  5.,  6.,  7.,  1.,  2.,  3.,  4.],
[ 8.,  9., 10., 11.,  4.,  3.,  2.,  1.]])


[En]

Sum all the elements:

print(torch.sum(X))
print(torch.sum(Y))

tensor(66.)
tensor(30.)


# 广播机制

[En]

In some cases, we can still perform per-element operations by calling the broadcast mechanism (broadcasting mechanism), even if the shapes are different. This mechanism works as follows: first, extend one or two arrays by copying elements appropriately, so that after transformation, the two tensors have the same shape. Second, perform a per-element operation on the generated array:

a = torch.arange(3).reshape((3, 1))
b = torch.arange(2).reshape((1, 2))

print(a)
print(b)
print(a + b)

tensor([[0],
[1],
[2]])
tensor([[0, 1]])
tensor([[0, 1],
[1, 2],
[2, 3]])


# 索引和切片

[En]

The elements in the tensor can be accessed through the index. As with any Python array: the index of the first element is 0 and the index of the last element is-1; you can specify a range to include the first element and the element before the last:

X = torch.arange(16).reshape(4,4)

print(X)
print(X[-1])
print(X[0:2])
print(X[3:])
print(X[-1][-1])
print(X[-1][1:3])

tensor([[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11],
[12, 13, 14, 15]])
tensor([12, 13, 14, 15])
tensor([[0, 1, 2, 3],
[4, 5, 6, 7]])
tensor([[12, 13, 14, 15]])
tensor(15)
tensor([13, 14])


[En]

Using slices and indexes, you can assign values to elements:

X[-1][-1] = 99
print(X)

X[-1][:] = 1099
print(X)

X[:2][:] = 10
print(X)

tensor([[   0,    1,    2,    3],
[   4,    5,    6,    7],
[   8,    9,   10,   11],
[1099, 1099, 1099,   99]])
tensor([[   0,    1,    2,    3],
[   4,    5,    6,    7],
[   8,    9,   10,   11],
[1099, 1099, 1099, 1099]])
tensor([[  10,   10,   10,   10],
[  10,   10,   10,   10],
[   8,    9,   10,   11],
[1099, 1099, 1099, 1099]])


# 节省内存

Y = torch.arange(12)
before = id(Y)
print(before)

Y = Y + torch.ones(12)
after = id(Y)
print(after)

print(after == before)

1923522705264
1923522706464
False


Z = torch.zeros_like(Y)  # 创建一个新的矩阵Z，其形状与另一个Y相同， 使用zeros_like来分配一个全的块
print('id(Z):', id(Z))
Z[:] = torch.ones(12) + Y
print('id(Z):', id(Z))

before = id(Y)
Y += torch.ones(12)
print(id(Y) == before)

id(Z): 1923522863488
id(Z): 1923522863488
True


# 转换为其他Python对象

torch 张量和 numpy 数组将共享它们的底层内存，就地操作更改一个张量也会同时更改另一个张量:

A = X.numpy()
B = torch.tensor(A)
type(A), type(B)

(numpy.ndarray, torch.Tensor)


a = torch.tensor([3.5])

print(a)
print(a.item())
print(float(a))
print(int(a))

tensor([3.5000])
3.5
3.5
3


Original: https://www.cnblogs.com/xiaojianliu/p/16149751.html
Author: 6小贱
Title: pytorch 深度学习之数据操作

(0)