【Tensor】格式转换 | 基本操作 | 类型转换

格式转换

torch.tensor与numpy.ndarray/list的转换

print('\ntorch.tensor and numpy.ndarray / list')
import numpy as np

nda_list = [1, 2, 3]
nda = np.array(nda_list)  # list转numpy.ndarray
print(nda)  # [1 2 3]
nda_tensor = torch.from_numpy(nda)  # numpy.ndarray转torch.tensor
print(nda_tensor)  # tensor([1, 2, 3], dtype=torch.int32)
nda_arr = nda_tensor.numpy()  # torch.tensor转numpy.ndarray
print(nda_arr) # [1 2 3]
print(nda_tensor.tolist())  # [1, 2, 3];torch.tensor转list

基本操作

import torch

torch.manual_seed(2020)  # cpu的随机数种子
torch.cuda.manual_seed()  # 当前gpu的随机数种子
torch.cuda.manual_seed_all() # 所有gpu的随机数种子

创建tensor

a = torch.tensor([1, 2, 3])
print(a) 
# tensor([1, 2, 3])

b = torch.empty(1, 3)  # 创建一个指定大小但未初始化的tensor
print(b) # tensor([[9.7209e+14, 4.5914e-41, 9.7209e+14]])

c = torch.zeros(1, 3)  # 创建一个指定大小且值全为0的tensor
print(c)  # tensor([[0., 0., 0.]])
d = torch.ones(1, 3)   # 创建一个指定大小且值全为1的tensor
print(d)  # tensor([[1., 1., 1.]])
e = torch.full((1, 3), 2)  # 创建一个指定大小且值全为指定值的tensor
print(e)  # tensor([[2., 2., 2.]])

f = torch.rand(1, 3)   # 创建一个指定大小且值服从(0,1)间的均匀分布的tensor
print(f)  # tensor([[0.4869, 0.1052, 0.5883]])
g = torch.randn(1, 3)  # 创建一个指定大小且值服从标准正态分布的tensor
print(g)  # tensor([[ 0.6983, -0.3339, -0.3049]])
h = torch.randint(low=1, high=10, size=(1,3))  # 创建一个指定大小且值是指定区间整数的tensor
print(h)  # tensor([[8, 5, 6]])

数据处理操作

x = torch.tensor([-1, 1, 1, 1])
y = torch.tensor([3, 3, 3, 3])

print('\nsize:')
print(x.size(), type(x.size()))  # torch.Size([4]) ;torch.Size是tuple的子类

print(torch.abs(x)) # 取绝对值

加法

print('\naddition:')
print(x + y)
print(torch.add(input=x, alpha=1, other=y))  # 加法:output = input + alpha*other
print(x.add(y), x)  # tensor([2, 4, 4, 4]) tensor([-1,  1,  1,  1])
x.add_(y)  # 所有带_后缀的操作均为in-place操作
print(x)  # tensor([2, 4, 4, 4])

减法(类比加法)

print('\nsubtraction:')
print(torch.sub(input=y, alpha=1, other=x))  # tensor([ 1, -1, -1, -1])
print(y.sub(x))  # tensor([ 1, -1, -1, -1])

元素乘法

print('\nelement-wise multiplication:')
print(x * y)  # tensor([ 6, 12, 12, 12])
print(torch.mul(x, y))  # tensor([ 6, 12, 12, 12])
print(x.mul(y), x) # tensor([ 6, 12, 12, 12]) tensor([2, 4, 4, 4])
x.mul_(y)  # tensor([ 6, 12, 12, 12])
print(x)  # tensor([ 6, 12, 12, 12])

除法:类比元素乘法

print('\ndivision:')
print(torch.div(x, y))  # tensor([2, 4, 4, 4])
print(x.div_(y))  # tensor([2, 4, 4, 4])

矩阵乘法

# 对于高维的Tensor(dim>2),定义其矩阵乘法仅在最后的两个维度上,要求前面的维度必须保持一致
# 运算操作只有torch.matmul()和@
print('\ntensor multiplication:')
m = torch.rand(3, 4, 5)
n = torch.rand(3, 5, 4)
print(torch.matmul(m, n).size())  # torch.Size([3, 4, 4])
print((m @ n).size())  # torch.Size([3, 4, 4])

# 对于二维和三维的张量各有一个专用的乘法操作:
p = torch.tensor([[1, 2]])  # 注意方括号的数量
q = torch.tensor([[2], [1]])
print(torch.mm(p, q))  # 二维;tensor([[4]])
print(torch.bmm(m, n).size())  # 三维;torch.Size([3, 4, 4])

幂运算

print('\npower:')
print(p ** 2)  # tensor([[1, 4]])
print(torch.pow(p, 2))  # tensor([[1, 4]])
print(p.pow(2), p)  # tensor([[1, 4]]) tensor([[1, 2]])
p.pow_(2)
print(p)  # tensor([[1, 4]])

r = torch.tensor([4., 4.]) 
print(torch.sqrt(r))  # tensor([2., 2.])
print(r.sqrt(), r)  # tensor([2., 2.]) tensor([4., 4.])
r.sqrt_()
print(r)  # tensor([2., 2.])

指数和对数运算

print('\nexponent and logarithm:')
print(torch.exp(r))  # tensor([7.3891, 7.3891])
print(r.exp(), r)  # tensor([7.3891, 7.3891]) tensor([2., 2.])
r.exp_()
print(r) # tensor([7.3891, 7.3891])

print(torch.log(r))  # tensor([2., 2.]);log2, log10用法相同
print(r.log(), r)  # tensor([2., 2.]) tensor([7.3891, 7.3891])
r.log_()
print(r)  # tensor([2., 2.])

近似计算

print('\napproximate:')
z = torch.tensor([3.49, 3.51])
print(torch.floor(z), torch.ceil(z), torch.trunc(z), torch.frac(z))  # 向上取整,向下取整,取整数,取小数
# tensor([3., 3.]) tensor([4., 4.]) tensor([3., 3.]) tensor([0.4900, 0.5100])
print(z.floor(), z.ceil(), z.trunc(), z.frac())
# tensor([3., 3.]) tensor([4., 4.]) tensor([3., 3.]) tensor([0.4900, 0.5100])
print(torch.round(z), z.round())  # 四舍五入
# tensor([3., 4.]) tensor([3., 4.])
print(torch.clamp(z, 3.4, 3.5), z.clamp(3.4, 3.5))  # 限幅
# tensor([3.4900, 3.5000]) tensor([3.4900, 3.5000])
# 上述属性同样有带后置下划线的in-place版本

统计信息

# 最大值, 最小值, 均值, 方差, 中位数
print(torch.max(z), torch.min(z), torch.mean(z), torch.var(z), torch.median(z))  
# tensor(3.5100) tensor(3.4900) tensor(3.5000) tensor(0.0002) tensor(3.4900)
print(z.max(), z.min(), z.mean(), z.var(), z.median())
# tensor(3.5100) tensor(3.4900) tensor(3.5000) tensor(0.0002) tensor(3.4900)

类型转换

int float转换

import torch

tensor = torch.randn(3, 5)
print(tensor)

# torch.long() 将tensor投射为long类型
long_tensor = tensor.long()
print(long_tensor)

# torch.half()将tensor投射为半精度浮点类型
half_tensor = tensor.half()
print(half_tensor)

# torch.int()将该tensor投射为int类型
int_tensor = tensor.int()
print(int_tensor)

# torch.double()将该tensor投射为double类型
double_tensor = tensor.double()
print(double_tensor)

# torch.float()将该tensor投射为float类型
float_tensor = tensor.float()
print(float_tensor)

# torch.char()将该tensor投射为char类型
char_tensor = tensor.char()
print(char_tensor)

# torch.byte()将该tensor投射为byte类型
byte_tensor = tensor.byte()
print(byte_tensor)

# torch.short()将该tensor投射为short类型
short_tensor = tensor.short()
print(short_tensor)

参考链接:

  • Pytorch中Tensor与各种图像格式的相互转化
  • pytorch中tensor的基本数学运算
  • pytorch-张量-张量的计算-统计相关的计算
  • PyTorch 笔记(02)— 常用创建 Tensor 方法(torch.Tensor、ones、zeros、eye、arange、linspace、rand、randn、new)
  • python获取字典的key和value
  • pytorch: tensor类型的构建与相互转换
  • torch学习 (41):torch中的tensor初始化操作

你可能感兴趣的:(数据处理,numpy,python,深度学习)