import torch
import numpy as np
1. Tensor的基本概念和操作
a = torch.tensor(4.0)
print("标量:", a)
标量: tensor(4.)
b = torch.tensor([1.5, 0, 3])
print("向量:", b)
向量: tensor([1.5000, 0.0000, 3.0000])
c = torch.tensor([[1, 2],[3, 4]])
print("矩阵:", c)
矩阵: tensor([[1, 2],
[3, 4]])
d = torch.tensor([[[1, 2],[3, 4]],[[5,6],[7,8]]])
print("多为矩阵:", d)
多为矩阵: tensor([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
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]])
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]])
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]])
h = torch.ones(5, 3)
print(h)
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
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的基本运算
j = torch.tensor([1, 2, 3])
k = torch.tensor([4, 5, 6])
print("Tensor 加法:", j + k)
Tensor 加法: tensor([5, 7, 9])
print("Tensor 加法:", torch.add(j, k))
Tensor 加法: tensor([5, 7, 9])
print("Tensor 加法:", j.add_(k))
Tensor 加法: tensor([5, 7, 9])
print(j)
tensor([5, 7, 9])
print(k)
tensor([4, 5, 6])
l = torch.tensor([6, 12])
m = torch.tensor([3, 6])
print("Tensor 除法:", l // m)
Tensor 除法: tensor([2, 2])
print("Tensor 除法:", torch.true_divide(l, m))
Tensor 除法: tensor([2., 2.])
n = torch.ones(1, 2)
p = torch.ones(2, 1)
print("Tensor 乘法:", torch.mm(n, p))
Tensor 乘法: tensor([[2.]])
print("Tensor 乘法:", torch.matmul(n, p))
Tensor 乘法: tensor([[2.]])
print("Tensor 乘法:", n@p)
Tensor 乘法: tensor([[2.]])
print("Tensor 乘法:", n.mm(p))
Tensor 乘法: tensor([[2.]])
q = torch.tensor([2, 3])
print("Tensor 幂运算:", torch.pow(q, 2))
Tensor 幂运算: tensor([4, 9])
print("Tensor 幂运算:", q.pow(2))
Tensor 幂运算: tensor([4, 9])
print("Tensor 幂运算:", q**2)
Tensor 幂运算: tensor([4, 9])
r = torch.tensor([4,16], dtype=torch.float)
print("Tensor 开方运算:", r.sqrt())
Tensor 开方运算: tensor([2., 4.])
print("Tensor 开方运算:", torch.sqrt(r))
Tensor 开方运算: tensor([2., 4.])
s1 = torch.tensor([8, 16, 32], dtype=torch.float)
print("Tensor 对数运算:", torch.log2(s1))
Tensor 对数运算: tensor([3., 4., 5.])
s2 = torch.tensor([100, 1000, 10000], dtype=torch.float)
print("Tensor 对数运算:", torch.log10(s2))
Tensor 对数运算: tensor([2., 3., 4.])
s3 = torch.tensor([20, 30, 40], dtype=torch.float)
print("Tensor 对数运算:", torch.log(s3))
Tensor 对数运算: tensor([2.9957, 3.4012, 3.6889])
t = torch.tensor([3.5, 9.982, 4.215])
print("向下取整数 floor() :", t.floor())
向下取整数 floor() : tensor([3., 9., 4.])
print("向上取整数 ceil() :", t.ceil())
向上取整数 ceil() : tensor([ 4., 10., 5.])
print("四舍五入 round() :", t.round())
四舍五入 round() : tensor([ 4., 10., 4.])
print("裁剪,只取整数部分 trunc() :", t.trunc())
裁剪,只取整数部分 trunc() : tensor([3., 9., 4.])
print("只取小数部分 frac() :", t.frac())
只取小数部分 frac() : tensor([0.5000, 0.9820, 0.2150])
print("取余 % :", torch.tensor([25, 31]) % 7)
取余 % : tensor([4, 3])
3. 其它Tensor操作
u = torch.tensor([[1, 2, 3],
[4, 5, 6]])
print(u.shape)
torch.Size([2, 3])
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")
print("将(2,3)reshape(3,2): \n", u.reshape(3,2))
将(2,3)reshape(3,2):
tensor([[1, 2],
[3, 4],
[5, 6]])
print("将(2,3)view(3,2): \n", u.view(3,2))
将(2,3)view(3,2):
tensor([[1, 2],
[3, 4],
[5, 6]])
u.shape
torch.Size([2, 3])
v = torch.tensor([2.3])
print("获取Tensor的值:item() : \n", v.item())
获取Tensor的值:item() :
2.299999952316284
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]))
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))
y = torch.tensor([2, 3, 4])
print(torch.isnan(y))
tensor([False, False, False])
print(torch.isfinite(y))
tensor([True, True, True])
print(torch.isinf(y))
tensor([False, False, False])
z = torch.tensor([4, 5, 6])
print("Tensor 转 Numpy :\n", z.numpy())
Tensor 转 Numpy :
[4 5 6]
z2 = np.ones(6)
print("Numpy 转 Tensor :\n", torch.from_numpy(z2))
Numpy 转 Tensor :
tensor([1., 1., 1., 1., 1., 1.], dtype=torch.float64)
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])