05PyTorch 笔记复习-1

import torch
import numpy as np

1. Tensor的基本概念和操作

# 1. 标量
a = torch.tensor(4.0) 

print("标量:", a)
标量: tensor(4.)
# 2. 向量
b = torch.tensor([1.5, 0, 3])

print("向量:", b)
向量: tensor([1.5000, 0.0000, 3.0000])
# 3. 矩阵
c = torch.tensor([[1, 2],[3, 4]])

print("矩阵:", c)
矩阵: tensor([[1, 2],
        [3, 4]])
# 4. 多维矩阵
d = torch.tensor([[[1, 2],[3, 4]],[[5,6],[7,8]]])

print("多为矩阵:", d)
多为矩阵: tensor([[[1, 2],
         [3, 4]],

        [[5, 6],
         [7, 8]]])
# 5. 创建一个空的tensor
e = torch.empty(5, 3)
print(e)
tensor([[0.0000e+00, -0.0000e+00, 0.0000e+00],
        [0.0000e+00, 3.5873e-43, 3.6013e-43],
        [3.5873e-43, 0.0000e+00, 0.0000e+00],
        [0.0000e+00, 0.0000e+00, 0.0000e+00],
        [0.0000e+00, 1.1704e-41, 0.0000e+00]])
# 6. 创建一个矩阵,元素随机产生
f = torch.rand(5, 3)

print(f)
tensor([[0.4301, 0.2845, 0.1114],
        [0.5832, 0.4048, 0.0277],
        [0.7959, 0.9355, 0.1906],
        [0.5628, 0.7529, 0.0688],
        [0.5275, 0.0540, 0.6070]])
# 7. 创建一个全0的矩阵
g = torch.zeros(5, 3, dtype=torch.long)

print(g)
tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])
# 8. 创建一个全1的矩阵
h = torch.ones(5, 3)

print(h)
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
# 9. 创建一个产生随机元素的矩阵,其shape与另一个矩阵的shape大小一致
i = torch.rand_like(h, dtype=torch.float)

print(i)
tensor([[0.5062, 0.2639, 0.9956],
        [0.6048, 0.7033, 0.2503],
        [0.3588, 0.4485, 0.7846],
        [0.1614, 0.1567, 0.3434],
        [0.2343, 0.0906, 0.8587]])

2. Tensor的基本运算

# 10. Tensor 加法

## (1)写法1
j = torch.tensor([1, 2, 3])
k = torch.tensor([4, 5, 6])

print("Tensor 加法:", j + k)
Tensor 加法: tensor([5, 7, 9])
## (2)写法2
print("Tensor 加法:", torch.add(j, k))
Tensor 加法: tensor([5, 7, 9])
## (3)写法3:会改变原始的j
print("Tensor 加法:", j.add_(k))
Tensor 加法: tensor([5, 7, 9])
print(j)
tensor([5, 7, 9])
print(k)
tensor([4, 5, 6])
# 11. Tensor 除法

## (1)写法1
l = torch.tensor([6, 12])
m = torch.tensor([3, 6])

print("Tensor 除法:", l // m)
Tensor 除法: tensor([2, 2])
## (2)写法2

print("Tensor 除法:", torch.true_divide(l, m))
Tensor 除法: tensor([2., 2.])
# 12. 二维矩阵乘法

## (1)写法1
n = torch.ones(1, 2)
p = torch.ones(2, 1)

print("Tensor 乘法:", torch.mm(n, p))
Tensor 乘法: tensor([[2.]])
## (2) 写法2

print("Tensor 乘法:", torch.matmul(n, p))
Tensor 乘法: tensor([[2.]])
## (3) 写法3

print("Tensor 乘法:", n@p)
Tensor 乘法: tensor([[2.]])
## (4) 写法4

print("Tensor 乘法:", n.mm(p))
Tensor 乘法: tensor([[2.]])
# 13. 幂运算

##(1)写法1
q = torch.tensor([2, 3])

print("Tensor 幂运算:", torch.pow(q, 2))
Tensor 幂运算: tensor([4, 9])
## (2)写法2

print("Tensor 幂运算:", q.pow(2))
Tensor 幂运算: tensor([4, 9])
## (3)写法3

print("Tensor 幂运算:", q**2)
Tensor 幂运算: tensor([4, 9])
# 14. 开根号运算(开方运算)

## 写法1
r = torch.tensor([4,16], dtype=torch.float)

print("Tensor 开方运算:", r.sqrt())
Tensor 开方运算: tensor([2., 4.])
## 写法2

print("Tensor 开方运算:", torch.sqrt(r))
Tensor 开方运算: tensor([2., 4.])
# 15. 对数运算

## 写法1
s1 = torch.tensor([8, 16, 32], dtype=torch.float)

print("Tensor 对数运算:", torch.log2(s1))
Tensor 对数运算: tensor([3., 4., 5.])
## 写法2
s2 = torch.tensor([100, 1000, 10000], dtype=torch.float)

print("Tensor 对数运算:", torch.log10(s2))
Tensor 对数运算: tensor([2., 3., 4.])
# 写法3
s3 = torch.tensor([20, 30, 40], dtype=torch.float)

print("Tensor 对数运算:", torch.log(s3))
Tensor 对数运算: tensor([2.9957, 3.4012, 3.6889])
# 16. 取整/取余

t = torch.tensor([3.5, 9.982, 4.215])

## 写法1
print("向下取整数 floor() :", t.floor())
向下取整数 floor() : tensor([3., 9., 4.])
## 写法2
print("向上取整数 ceil() :", t.ceil())
向上取整数 ceil() : tensor([ 4., 10.,  5.])
## 写法3
print("四舍五入 round() :", t.round())
四舍五入 round() : tensor([ 4., 10.,  4.])
## 写法4
print("裁剪,只取整数部分 trunc() :", t.trunc())
裁剪,只取整数部分 trunc() : tensor([3., 9., 4.])
## 写法5
print("只取小数部分 frac() :", t.frac())
只取小数部分 frac() : tensor([0.5000, 0.9820, 0.2150])
## 写法6
print("取余 % :", torch.tensor([25, 31]) % 7)
取余 % : tensor([4, 3])

3. 其它Tensor操作

# 17. Resize, Reshape, View
u = torch.tensor([[1, 2, 3],
                  [4, 5, 6]])
print(u.shape)
torch.Size([2, 3])
## 写法1:resize
print("将(2,3) resize (3,2) : \n",u.resize(3,2)) 
将(2,3) resize (3,2) : 
 tensor([[1, 2],
        [3, 4],
        [5, 6]])


/Users/tommy/Torch/Torch/lib/python3.7/site-packages/torch/tensor.py:358: UserWarning: non-inplace resize is deprecated
  warnings.warn("non-inplace resize is deprecated")
## 写法2:reshape
print("将(2,3)reshape(3,2): \n", u.reshape(3,2))
将(2,3)reshape(3,2): 
 tensor([[1, 2],
        [3, 4],
        [5, 6]])
## 写法3:view
print("将(2,3)view(3,2): \n", u.view(3,2))
将(2,3)view(3,2): 
 tensor([[1, 2],
        [3, 4],
        [5, 6]])
# 原始的shape不变
u.shape
torch.Size([2, 3])
# 18. 获取一个tensor的值(注意:只针对一个值的tensor)
v = torch.tensor([2.3])

print("获取Tensor的值:item() : \n", v.item())
获取Tensor的值:item() : 
 2.299999952316284
# 19. Tensor 排序
w = torch.tensor([2, 22, 35, 9, 11, 4, 18, 13])

print("Tensor 升序排序:", torch.sort(w))
Tensor 升序排序: torch.return_types.sort(
values=tensor([ 2,  4,  9, 11, 13, 18, 22, 35]),
indices=tensor([0, 5, 3, 4, 7, 6, 1, 2]))
print("Tensor 逆序排序:", torch.sort(w, descending=True))
Tensor 逆序排序: torch.return_types.sort(
values=tensor([35, 22, 18, 13, 11,  9,  4,  2]),
indices=tensor([2, 1, 6, 7, 4, 3, 5, 0]))
# 20. 沿着指定纬度返回最大k个数值及其索引值
x = torch.tensor([2, 22, 35, 9, 11, 4, 18, 13])

print("Tensor 前4大元素:", torch.topk(x, k=4, dim=0, largest=True, sorted=True, out=None))
Tensor 前4大元素: torch.return_types.topk(
values=tensor([35, 22, 18, 13]),
indices=tensor([2, 1, 6, 7]))
print("Tensor 沿着指定纬度返回第k个最小值及其索引值:", torch.kthvalue(x, 5, dim=0))
Tensor 沿着指定纬度返回第k个最小值及其索引值: torch.return_types.kthvalue(
values=tensor(13),
indices=tensor(7))
# 21. 判断是否为nan,inf,finite
y = torch.tensor([2, 3, 4])

print(torch.isnan(y))
tensor([False, False, False])
print(torch.isfinite(y)) # 用于检查其参数是否是无穷大,如果number是有效数字(或可转换为有限数字),返回true
tensor([True, True, True])
print(torch.isinf(y)) # 用于检查其参数是否是无穷大
tensor([False, False, False])
# 22. tensor 转 numpy ,numpy 转 tensor

## 写法1:tensor 转 numpy
z = torch.tensor([4, 5, 6])

print("Tensor 转 Numpy :\n", z.numpy())
Tensor 转 Numpy :
 [4 5 6]
## 写法2:numpy 转 tensor
z2 = np.ones(6)
print("Numpy 转 Tensor :\n", torch.from_numpy(z2))
Numpy 转 Tensor :
 tensor([1., 1., 1., 1., 1., 1.], dtype=torch.float64)
# 23. 判断是否有GPU,如果有,则部署到GPU上运行

if torch.cuda.is_available():
    device = torch.device('cuda')
else:
    device = torch.device('cpu')
a1 = torch.tensor([1, 2, 3], device=device)
a2 = torch.tensor([4, 5, 6], device=device)
res = a1 + a2
print("a1 + a2 : ", res)
a1 + a2 :  tensor([5, 7, 9])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值