深度学习的基于pytorch实现
基本运算
python
import torch #pytorch的速度比numpy更快 |
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
python
x.shape # 显示矩阵的阶数 |
torch.Size([12])
python
X = x.reshape(3,4) #转换矩阵的形状 |
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
python
X.shape |
torch.Size([3, 4])
python
x.numel() # 显示所有元素的个数 |
12
python
torch.ones((2,3,4)) #生成一个全为1的矩阵 2表示维度 3表示行数 4表示列数 |
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.]]])
python
torch.zeros((2,3,4)) #生成一个全为0的矩阵 2表示维度 3表示行数 4表示列数 |
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.]]])
python
torch.randn(3,4) # 生成一个3行4列的随即元素的矩阵 |
tensor([[ 0.8254, 0.5382, 1.3708, 1.4770],
[-1.1002, -0.2319, -0.0071, 0.2910],
[-0.3470, 0.4826, 0.2891, -0.1019]])
python
torch.tensor([[2,3,1,4],[1,2,3,4],[1,1,1,1]]) #为元素赋予值 |
tensor([[2, 3, 1, 4],
[1, 2, 3, 4],
[1, 1, 1, 1]])
python
torch.tensor([[2,3,1,4],[1,2,3],[1,1,1,1]]) #在这里面他的元素的个数必须是相同的否则报错 |
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In [11], line 1
----> 1 torch.tensor([[2,3,1,4],[1,2,3],[1,1,1,1]])
ValueError: expected sequence of length 4 at dim 1 (got 3)
python
X = torch |
python
torch.ones((10)) |
tensor([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
python
x = torch.tensor([1.0,2,4,8]) |
tensor([ 3., 4., 6., 10.])
python
x - y |
tensor([-1., 0., 2., 6.])
python
x * y |
tensor([ 2., 4., 8., 16.])
python
x / y |
tensor([0.5000, 1.0000, 2.0000, 4.0000])
python
x ** y |
tensor([ 1., 4., 16., 64.])
python
torch.exp(x) |
tensor([2.7183e+00, 7.3891e+00, 5.4598e+01, 2.9810e+03])
连结两个矩阵,堆叠起来形成更大的张量,第一个是沿着行合并,第二个是沿着列合并。
python
X = torch.arange(12,dtype=torch.float32).reshape((3,4)) #dtype定义矩阵是float类型 |
(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.]]))
对于每个位置,如果相同则返回true否则返回false
python
X == Y |
tensor([[False, True, False, True],
[False, False, False, False],
[False, False, False, False]])
python
X.sum() # 返回元素中所有的和 |
tensor(66.)
广播机制
如果两个矩阵的行列数不同就会转变成为一个更大的矩阵进行运算,如下一个是31的矩阵一个是12的矩阵,进行相加变成了3*2的矩阵。
python
a = torch.arange(3).reshape((3,1)) |
(tensor([[0],
[1],
[2]]),
tensor([[0, 1]]))
python
a+b |
tensor([[0, 1],
[1, 2],
[2, 3]])
python
X |
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]])
索引和切片
左后一个元素,在矩阵中就是最后一行
python
X[-1] |
tensor([ 8., 9., 10., 11.])
1:3选取第二个和第三个元素
python
X[1:3] |
tensor([[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]])
python
X[1,2] = 9 |
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 9., 7.],
[ 8., 9., 10., 11.]])
为多元素赋相同的值,只需要索引所有元素,:代表沿轴(列)的所有元素
python
X[0:2,:] = 12 |
tensor([[12., 12., 12., 12.],
[12., 12., 12., 12.],
[20., 21., 22., 23.]])
节省内存
在这个例子中,运行Y+X之后会重新分配内存,使Y指向新内存。这是不可取的,我们不能浪费内存
python
before = id(Y) |
False
可以使用切片表示法来将操作的结果分配给原先分配的数组
python
Z = torch.zeros_like(Y) |
id(Z): 3227757014528
id(Z): 3227757014528
如果在后续计算中没有重复使用X,可以用以下方法
python
before = id(X) |
True
转换为其他python对象
python
A = X.numpy() |
(numpy.ndarray, torch.Tensor)
将大小唯一的张量转换为python标量,可以用item和内置函数
python
a = torch.tensor([3.5]) |
(tensor([3.5000]), 3.5, 3.5, 3)
python
X < Y # 如果是比较两个张量的大小的话是对每个元素进行比较,结果返回布尔类型 |
tensor([[False, False, False, False],
[False, False, False, False],
[False, False, False, False]])
对于广播机制的维度不同的两个张量,不能运算,只能两个矩阵完全一样才可以运算
python
m = torch.arange(24).reshape((2,3,4)) |
tensor([[[ 0, 2, 4, 6],
[ 8, 10, 12, 14],
[16, 18, 20, 22]],
[[24, 26, 28, 30],
[32, 34, 36, 38],
[40, 42, 44, 46]]])
python
m = torch.arange(24).reshape((2,3,4)) |
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Cell In [57], line 3
1 m = torch.arange(24).reshape((2,3,4))
2 n = torch.arange(36).reshape((3,3,4))
----> 3 m+n
RuntimeError: The size of tensor a (2) must match the size of tensor b (3) at non-singleton dimension 0
python
m = torch.arange(24).reshape((2,3,4)) |
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Cell In [58], line 3
1 m = torch.arange(24).reshape((2,3,4))
2 n = torch.arange(45).reshape((3,3,5))
----> 3 m+n
RuntimeError: The size of tensor a (4) must match the size of tensor b (5) at non-singleton dimension 2
python
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 宇のBlog!