0%

Pytorch之数据操作及其预处理

本节主要介绍了张量的简单操作、CSV数据集的读取以及自动求导机制

数据操作

1.张量的创建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#导入库
import torch

#1.创建张量
#使用arrange创建张量
x=torch.arange(24) #创建0~23的整数
print("x is:",x)
#我们还可以通过提供包含数值的Python列表(或嵌套列表),来为所需张量中的每个元素赋予确定值
print(torch.tensor([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]]))
print(40*"-")

#2.改变张量形状
#改变一个张量的形状而不改变元素数量和元素值
x=x.reshape(2,3,4)
print("x is:",x)
print(40*"-")

#3.查看张量的形状,数量
print("The shape of x is:",x.shape)
print("The size of x[0] is:",x.size(axis=0)) #可以根据axis指定计算哪个维度的元素个数
print(x.numel()) #返回张量中总共包含多少个元素
print(40*"-")

#4.创建特殊张量
#创建全0张量与全1张量
print(torch.zeros((2, 3, 4)))
print(torch.ones((2, 3, 4)))
#创建均值为0、标准差为1的标准高斯分布的张量
print(torch.randn(3, 4))
#利用randperm返回一个0~n-1的随机打乱数组
print(torch.randperm(10))
#创建随机均匀分布的张量,其中torch.Tensor(2,2)用来指定tensor的shape
print(torch.Tensor(2,2).uniform_(-1,1))

结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
x is: tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23])
tensor([[2, 1, 4, 3],
[1, 2, 3, 4],
[4, 3, 2, 1]])
----------------------------------------
x is: tensor([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],

[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
----------------------------------------
The shape of x is: torch.Size([2, 3, 4])
The size of x[0] is: 2
24
----------------------------------------
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., 1., 1.],
[1., 1., 1., 1.]],

[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]])
tensor([[ 1.4646, -0.6201, 0.6223, 1.7981],
[-2.5379, -1.5198, 0.2865, 1.6249],
[-0.3724, 0.7659, 2.1007, -1.8540]])
tensor([2, 9, 6, 4, 0, 1, 7, 5, 8, 3])
tensor([[ 0.8090, -0.2894],
[-0.3646, -0.4624]])

2.张量的属性

  • Tensor的属性:
    • 每个Tensor有torch.dtype、torch.device、torch.layout三种属性
    • torch.device标识了torch.Tensor对象在创建之后所存储在的设备名称
    • torch.layout表示torch.Tensor内存布局的对象
  • 稀疏张量的定义
    • torch.sparse_coo_tensor,coo类型表示非零元素的坐标形式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import torch

#1.tensor的相关属性
dev = torch.device("cpu") #这里的cpu区分大小写
#dev = torch.device("cuda")

a = torch.tensor([2, 2], dtype = torch.float32, device = dev)
print(a)

#2.稀疏矩阵的表示与使用
i = torch.tensor([[0, 1, 2], [0, 1, 2]]) #张量的位置
v = torch.tensor([1, 2, 3]) #每个位置对应的值
a = torch.sparse_coo_tensor(i, v, (4, 4)).to_dense()#使用to_dense()将其转化为正常的矩阵形式
print(a)

结果如下:

1
2
3
4
5
tensor([2., 2.])
tensor([[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 0]])

3.运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import torch

x = torch.tensor([1.0, 2, 4, 8])
y = torch.tensor([2, 2, 2, 2])
print("x is:",x)
print("y is:",y)
#1.普通运算符操作
print("x+y is:",x + y)
print("x-y is:",x - y)
print("x*y is:",x * y)
print("x/y is:",x/y)
print("x^y is:",x**y) #**运算符是求幂运算
print("e^x is:",torch.exp(x))
print(40*"-")

X = torch.arange(12, dtype=torch.float32).reshape((3,4))
Y = torch.tensor([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
print("X is:",X)
print("Y is:",Y)
#2.拼接运算符
print(torch.cat((X, Y), dim=0))
print(torch.cat((X, Y), dim=1))
print(40*"-")
#3.逻辑运算符:在每个对应元素位置上比较
print(X == Y)
print(40*"-")
#4.求和运算符
print("The sum is:",X.sum())
print("The sum is:",X.sum(axis=1))

结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
x is: tensor([1., 2., 4., 8.])
y is: tensor([2, 2, 2, 2])
x+y is: tensor([ 3., 4., 6., 10.])
x-y is: tensor([-1., 0., 2., 6.])
x*y is: tensor([ 2., 4., 8., 16.])
x/y is: tensor([0.5000, 1.0000, 2.0000, 4.0000])
x^y is: tensor([ 1., 4., 16., 64.])
e^x is: tensor([2.7183e+00, 7.3891e+00, 5.4598e+01, 2.9810e+03])
----------------------------------------
X is: tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]])
Y is: tensor([[2, 1, 4, 3],
[1, 2, 3, 4],
[4, 3, 2, 1]])
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.]])
----------------------------------------
tensor([[False, True, False, True],
[False, False, False, False],
[False, False, False, False]])
----------------------------------------
The sum is: tensor(66.)
The sum is: tensor([ 6., 22., 38.])

4.张量索引

1
2
3
4
5
6
7
8
9
10
11
12
13
import torch

X=torch.tensor([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1],[5,9,8,7]])
print("X is:",X)
print("X[-1] is:",X[-1]) #-1代表取最后
print(40*'-')
print("X[1:3] is:",X[1:3])
print(40*'-')
X[1, 2] = 9
print("X is:",X)
print(40*'-')
X[0:2, :] = 12
print("X is:",X)

结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
X is: tensor([[2, 1, 4, 3],
[1, 2, 3, 4],
[4, 3, 2, 1],
[5, 9, 8, 7]])
X[-1] is: tensor([5, 9, 8, 7])
----------------------------------------
X[1:3] is: tensor([[1, 2, 3, 4],
[4, 3, 2, 1]])
----------------------------------------
X is: tensor([[2, 1, 4, 3],
[1, 2, 9, 4],
[4, 3, 2, 1],
[5, 9, 8, 7]])
----------------------------------------
X is: tensor([[12, 12, 12, 12],
[12, 12, 12, 12],
[ 4, 3, 2, 1],
[ 5, 9, 8, 7]])

5.张量切片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import torch

#1.torch.chunk
print('torch.chunk')
a = torch.rand(3, 4)
print(a)
out = torch.chunk(a, 2, dim = 0)
print(out[0], out[0].shape)
print(out[1], out[1].shape)
print(50*'-')

#2.torch.split
print('torch.split')
a = torch.rand(6, 4)
print(a)
out = torch.split(a, 2, dim = 0) #split的拆分方式一
for i in out:
print(i, i.shape)

out = torch.split(a, [1, 3, 2], dim = 0) #split的拆分方式二
for i in out:
print(i, i.shape)

结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
torch.chunk
tensor([[0.2251, 0.0545, 0.7452, 0.7359],
[0.1663, 0.7839, 0.9890, 0.5553],
[0.4612, 0.9375, 0.9323, 0.0996]])
tensor([[0.2251, 0.0545, 0.7452, 0.7359],
[0.1663, 0.7839, 0.9890, 0.5553]]) torch.Size([2, 4])
tensor([[0.4612, 0.9375, 0.9323, 0.0996]]) torch.Size([1, 4])
--------------------------------------------------
torch.split
tensor([[0.3003, 0.9461, 0.4454, 0.1446],
[0.8573, 0.5407, 0.9421, 0.2710],
[0.4454, 0.8425, 0.0993, 0.9189],
[0.3519, 0.9107, 0.2126, 0.4638],
[0.0914, 0.5672, 0.5462, 0.7540],
[0.8343, 0.8356, 0.4863, 0.5255]])
tensor([[0.3003, 0.9461, 0.4454, 0.1446],
[0.8573, 0.5407, 0.9421, 0.2710]]) torch.Size([2, 4])
tensor([[0.4454, 0.8425, 0.0993, 0.9189],
[0.3519, 0.9107, 0.2126, 0.4638]]) torch.Size([2, 4])
tensor([[0.0914, 0.5672, 0.5462, 0.7540],
[0.8343, 0.8356, 0.4863, 0.5255]]) torch.Size([2, 4])
tensor([[0.3003, 0.9461, 0.4454, 0.1446]]) torch.Size([1, 4])
tensor([[0.8573, 0.5407, 0.9421, 0.2710],
[0.4454, 0.8425, 0.0993, 0.9189],
[0.3519, 0.9107, 0.2126, 0.4638]]) torch.Size([3, 4])
tensor([[0.0914, 0.5672, 0.5462, 0.7540],
[0.8343, 0.8356, 0.4863, 0.5255]]) torch.Size([2, 4])

6.排序运算与最大值求取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import torch

#1.张量的排序torch.sort
a = torch.tensor([[1, 4, 4, 3, 5],[2, 3, 1, 3, 5]])
print(a.shape)
print(torch.sort(a, dim = 1, descending = True)) #descending = True代表降序排列,为False代表升序排列
print('---------------------------------')

#2.张量中:沿着指定维度返回最大k个数值及其索引值torch.topk
a = torch.tensor([[1, 4, 4, 3, 5],[2, 3, 1, 3, 5]])
print(a.shape)
print(torch.topk(a, k = 2, dim = 0))
print('---------------------------------')

#3.张量中:沿着指定维度返回第k个最小值及其索引值torch.kthvalue
a = torch.tensor([[1, 4, 4, 3, 5],[2, 3, 1, 3, 5]])
print(a.shape)
print(torch.kthvalue(a, k = 2, dim = 0))
print('---------------------------------')

结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
torch.Size([2, 5])
torch.return_types.sort(
values=tensor([[5, 4, 4, 3, 1],
[5, 3, 3, 2, 1]]),
indices=tensor([[4, 1, 2, 3, 0],
[4, 1, 3, 0, 2]]))
---------------------------------
torch.Size([2, 5])
torch.return_types.topk(
values=tensor([[2, 4, 4, 3, 5],
[1, 3, 1, 3, 5]]),
indices=tensor([[1, 0, 0, 0, 0],
[0, 1, 1, 1, 1]]))
---------------------------------
torch.Size([2, 5])
torch.return_types.kthvalue(
values=tensor([2, 4, 4, 3, 5]),
indices=tensor([1, 0, 0, 1, 1]))
---------------------------------

7.类型转换

1
2
3
4
5
6
7
8
9
10
11
12
import torch

X=torch.tensor([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1],[5,9,8,7]])
A = X.numpy() #将tensor类型转化为numpy类型
B = torch.tensor(A) #将numpy类型转化为tensor类型
print("type of A is:",type(A))
print("type of B is:",type(B))
print(40*'-')

#要将大小为1的张量转换为Python标量,我们可以调用item函数或Python的内置函数
a = torch.tensor([3.5])
print(a, a.item(), float(a), int(a))

结果如下:

1
2
3
4
type of A is: <class 'numpy.ndarray'>
type of B is: <class 'torch.Tensor'>
----------------------------------------
tensor([3.5000]) 3.5 3.5 3

数据预处理

1.设置随机种子

1
2
3
4
5
6
7
8
9
import torch

#设置随机种子
torch.manual_seed(1)

mean = torch.rand(1, 2) #rand是从区间[0, 1)的均匀分布中抽取的一组随机数
std = torch.rand(1, 2)

print(torch.normal(mean, std))

结果如下:

image-20230803211605227

2.读取数据集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import os

#将数据写入一个CSV文件中
os.makedirs(os.path.join('.', 'data'), exist_ok=True)
data_file = os.path.join('.', 'data', 'house_tiny.csv')
with open(data_file, 'w') as f:
f.write('NumRooms,Alley,Price\n') # 列名
f.write('NA,Pave,127500\n') # 每行表示一个数据样本
f.write('2,NA,106000\n')
f.write('4,NA,178100\n')
f.write('NA,NA,140000\n')

#通过pandas库读取数据
import pandas as pd
data = pd.read_csv(data_file)
print(data)

结果如下:

1
2
3
4
5
   NumRooms Alley   Price
0 NaN Pave 127500
1 2.0 NaN 106000
2 4.0 NaN 178100
3 NaN NaN 140000

3.张量裁剪

1
2
3
4
5
6
7
import torch

a = torch.rand(2, 2)*10
print(a)

a = a.clamp(2, 5) #将数据限制在2到5之间,小于2的数为2,大于5的数为5
print(a)

结果如下:

1
2
3
4
tensor([[5.5637, 0.2108],
[2.4912, 3.2161]])
tensor([[5.0000, 2.0000],
[2.4912, 3.2161]])

4.数据筛选

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import torch

#1.torch.where(condition, x, y):按照条件从x和y中选出满足条件的元素组成新的tensor
print('torch.where')
a = torch.rand(2, 3)
b = torch.rand(2, 3)
print(a)
print(b)
out = torch.where(a > 0.5, a, b) #若a大于0.5输出a,否则输出b
print(out)
print(50*'-')

#2.torch.index_select(input, dim, index, out = None):按照指定索引输出tensor
print('torch.index_select')
a = torch.rand(4, 4)
print(a)
out = torch.index_select(a, dim = 0, index = torch.tensor([0, 3, 2]))
print(out)
print(50*'-')

#3.torch.gather(input, dim, index, out = None):在指定维度上按照索引赋值输出tensor
#dim = 0, out[i, j, k] = input[index[i, j, k], j, k]
#dim = 1, out[i, j, k] = input[i, index[i, j, k], k]
#dim = 2, out[i, j, k] = input[i, j, index[i, j, k]]
print('torch.gather')
a = torch.linspace(1, 16, 16).view(4, 4)
print(a)
out = torch.gather(a, dim = 0, index = torch.tensor([[0, 1, 1, 1],
[0, 1, 2, 2],
[0, 1, 3, 3]]))
print(out)
print(50*'-')

#4.torch.masked_select(input, mask, out = None):按照mask输出tensor,输出为向量
print('torch.masked_select')
a = torch.linspace(1, 16, 16).view(4, 4)
print(a)
mask = torch.gt(a, 8)
print(mask)
out = torch.masked_select(a, mask)
print(out)
print(50*'-')

#5.torch.take(input, indices):将输入看成1D-tensor,按照索引得到输出tensor
print('torch.take')
a = torch.linspace(1, 16, 16).view(4, 4)
b = torch.take(a, index = torch.tensor([0, 15, 13, 10]))
print(b)
print(50*'-')

#6.torch.nonzero(input, out = None):输出非0元素的坐标
print('torch.nonzero')
a = torch.tensor([[0, 1, 2, 0], [2, 3, 0, 1]])
out = torch.nonzero(a)
print(out)

结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
torch.where
tensor([[0.8185, 0.1795, 0.9199],
[0.0798, 0.4916, 0.7768]])
tensor([[0.7131, 0.4783, 0.1815],
[0.6423, 0.9752, 0.2171]])
tensor([[0.8185, 0.4783, 0.9199],
[0.6423, 0.9752, 0.7768]])
--------------------------------------------------
torch.index_select
tensor([[0.8434, 0.7979, 0.2893, 0.9534],
[0.0751, 0.2321, 0.8226, 0.6785],
[0.9144, 0.2765, 0.5214, 0.9405],
[0.0807, 0.1131, 0.1234, 0.2099]])
tensor([[0.8434, 0.7979, 0.2893, 0.9534],
[0.0807, 0.1131, 0.1234, 0.2099],
[0.9144, 0.2765, 0.5214, 0.9405]])
--------------------------------------------------
torch.gather
tensor([[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.],
[13., 14., 15., 16.]])
tensor([[ 1., 6., 7., 8.],
[ 1., 6., 11., 12.],
[ 1., 6., 15., 16.]])
--------------------------------------------------
torch.masked_select
tensor([[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.],
[13., 14., 15., 16.]])
tensor([[False, False, False, False],
[False, False, False, False],
[ True, True, True, True],
[ True, True, True, True]])
tensor([ 9., 10., 11., 12., 13., 14., 15., 16.])
--------------------------------------------------
torch.take
tensor([ 1., 16., 14., 11.])
--------------------------------------------------
torch.nonzero
tensor([[0, 1],
[0, 2],
[1, 0],
[1, 1],
[1, 3]])

5.处理缺失值

  • 通过位置索引iloc,将data分成inputsoutputs, 其中前者为data的前两列,而后者为data的最后一列。 对于inputs中缺少的数值,我们用同一列的均值替换“NaN”项。
  • 或通过独热码的形式填充NaN
1
2
3
4
5
6
7
8
9
10
11
12
13
#3.处理缺失值
inputs, outputs = data.iloc[:,0:2], data.iloc[:,2:3]
print("The inputs data is:\n",inputs)
print(50*'-')
print("The outputs data is:\n",outputs)
print(50*'-')
inputs = inputs.fillna(inputs.mean(numeric_only=True))
print(inputs)
print(50*'-')

#4.利用get_dummies实现独热码
inputs = pd.get_dummies(inputs, dummy_na=True)
print(inputs)

结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
--------------------------------------------------
The inputs data is:
NumRooms Alley
0 NaN Pave
1 2.0 NaN
2 4.0 NaN
3 NaN NaN
--------------------------------------------------
The outputs data is:
Price
0 127500
1 106000
2 178100
3 140000
--------------------------------------------------
NumRooms Alley
0 3.0 Pave
1 2.0 NaN
2 4.0 NaN
3 3.0 NaN
--------------------------------------------------
NumRooms Alley_Pave Alley_nan
0 3.0 1 0
1 2.0 0 1
2 4.0 0 1
3 3.0 0 1

6.转化为张量格式

1
2
3
4
5
#将输入CSV数据转化为张量形式
import torch
X, y = torch.tensor(inputs.values), torch.tensor(outputs.values)
print("X is:\n",X)
print("y is:\n",y)

结果如下:

1
2
3
4
5
6
7
8
9
10
X is:
tensor([[3., 1., 0.],
[2., 0., 1.],
[4., 0., 1.],
[3., 0., 1.]], dtype=torch.float64)
y is:
tensor([[127500],
[106000],
[178100],
[140000]])

线性代数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import torch
##1.标量
x = torch.tensor(12.98)
y = torch.tensor(13.78)
print(x,',',y)
print('-'*40)

##2.向量
z = torch.arange(24)
#与普通的Python数组一样,我们可以通过调用Python的内置len()函数来访问张量的长度
print(len(z))
z = z.reshape(2,3,4)
print(z.shape) #形状(shape)是一个元素组,列出了张量沿每个轴的长度(维数)
print(z.size()) #size()与shape的功能一样,只是用法不同
print(z.shape[0])
print(z.size()[0])
print('-'*40)

##3.矩阵
A = torch.arange(20).reshape(5, 4)
print(A)
#求转置
print(A.T)
#点积:是相同位置的按元素乘积的和
x1 = torch.arange(4, dtype=torch.float32)
x2 = torch.ones(4, dtype=torch.float32)
print(torch.dot(x1,x2))
#矩阵-向量积:当我们为矩阵A和向量x调用torch.mv(A, x)时,会执行矩阵-向量积
y1 = torch.arange(20, dtype=torch.float32).reshape(5,4)
y2 = torch.arange(4, dtype=torch.float32)
print(torch.mv(y1,y2))
#矩阵乘法
z1 = torch.ones((4, 3), dtype=torch.float32)
print(torch.mm(y1, z1))
print('-'*40)

##4.张量算法的基本性质
A = torch.arange(8, dtype=torch.float32).reshape(2, 4)
B = A.clone() #通过分配新内存,将A的一个副本分配给B
B = B + 1
print(B)
print(A) #A结果没变,说明B是新开辟了一个空间,并不是A的引用
print('-'*40)

#5.求和.sum(),求均值.mean()其用法与求和一致
A = torch.arange(20, dtype=torch.float32).reshape(5, 4)
print(A.sum()) #默认对所有值求和
print(A.sum(axis = 1)) #对每一行求和
print(A.sum(axis=[0, 1])) #效果等同sum()
print(A)
sum_A = A.sum(axis=1, keepdims=True) #有时在调用函数来计算总和或均值时保持轴数不变会很有用
print(sum_A)
print(A.cumsum(axis=0)) #沿某个轴计算A元素的累积总和,此函数不会沿任何轴降低输入张量的维度
print('-'*40)

#6.范数
#L2范数
u = torch.tensor([3.0, -4.0])
print(torch.norm(u)) #元素平方求和再开根号
#L1范数
u1 = torch.abs(u).sum() #元素绝对值求和
print(u1)

结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
tensor(12.9800) , tensor(13.7800)
----------------------------------------
24
torch.Size([2, 3, 4])
torch.Size([2, 3, 4])
2
2
----------------------------------------
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19]])
tensor([[ 0, 4, 8, 12, 16],
[ 1, 5, 9, 13, 17],
[ 2, 6, 10, 14, 18],
[ 3, 7, 11, 15, 19]])
tensor(6.)
tensor([ 14., 38., 62., 86., 110.])
tensor([[ 6., 6., 6.],
[22., 22., 22.],
[38., 38., 38.],
[54., 54., 54.],
[70., 70., 70.]])
----------------------------------------
tensor([[1., 2., 3., 4.],
[5., 6., 7., 8.]])
tensor([[0., 1., 2., 3.],
[4., 5., 6., 7.]])
----------------------------------------
tensor(190.)
tensor([ 6., 22., 38., 54., 70.])
tensor(190.)
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[12., 13., 14., 15.],
[16., 17., 18., 19.]])
tensor([[ 6.],
[22.],
[38.],
[54.],
[70.]])
tensor([[ 0., 1., 2., 3.],
[ 4., 6., 8., 10.],
[12., 15., 18., 21.],
[24., 28., 32., 36.],
[40., 45., 50., 55.]])
----------------------------------------
tensor(5.)
tensor(7.)

自动求导

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import torch

x = torch.arange(4.0)
print('x is:',x)
#requires_grad_用来指明是否需要求导
#在张量间的计算过程中,如果在所有输入中,有一个输入需要求导,那么输出一定会需要求导;
#相反,只有当所有输入都不需要求导的时候,输出才会不需要
#虽然输入的训练数据是默认不求导的,但是,我们的 model 中的所有参数,它默认是求导的
x.requires_grad_(True) #等价于x=torch.arange(4.0,requires_grad=True)
print('the gradient of x is:',x.grad) #默认值是None
y = 2 * torch.dot(x, x)
print('y is:',y)
y.backward()
print('the gradient of y to x is:',x.grad)

x.grad.zero_() #在默认情况下,PyTorch会累积梯度,我们需要清除之前的值
y = x.sum()
y.backward()
print('the new gardient of y to x is:',x.grad)

结果如下:

1
2
3
4
5
x is: tensor([0., 1., 2., 3.])
the gradient of x is: None
y is: tensor(28., grad_fn=<MulBackward0>)
the gradient of y to x is: tensor([ 0., 4., 8., 12.])
the new gardient of y to x is: tensor([1., 1., 1., 1.])

Anconda下Pytorch的安装方法,并在pycharm中使用


相关资料

欢迎来到ssy的世界