张量操作
一、张量拼接与切分
torch.cat()
功能:将张量按照维度dim
进行拼接
torch.cat(tensors, # 张量序列
dim=0, # 要拼接的维度
out=None)
例如:按照张量的第0
维和第1
维进行拼接
t = torch.ones((2, 3))
t_0 = torch.cat([t, t], dim=0)
t_1 = torch.cat([t, t, t], dim=1)
print("t_0:{} shape:{}\nt_1:{} shape:{}".format(t_0, t_0.shape, t_1, t_1.shape))
输出的结果:
t_0:tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]) shape:torch.Size([4, 3])
t_1:tensor([[1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1.]]) shape:torch.Size([2, 9])
通过观察输出可知:第0
维拼接:2+2
,第1维拼接:3+3+3
torch.stack
功能:在新创建的维度dim
上进行拼接
torch.stack(tensors, # 张量序列
dim=0, # 要拼接的维度
out=None)
例如:
t = torch.ones((2, 3))
t_stack = torch.stack([t, t, t], dim=0)
print("\nt_stack:{} shape:{}".format(t_stack, t_stack.shape))
输出的结果为:
t_stack:tensor([[[1., 1., 1.],
[1., 1., 1.]],
[[1., 1., 1.],
[1., 1., 1.]],
[[1., 1., 1.],
[1., 1., 1.]]]) shape:torch.Size([3, 2, 3])
通过观察输出可以发现其实就是在原有的(2, 3)
维度的基础上创建一个第0
维度,变成(1, 2, 3)
,然后再进行拼接从而输出(3, 2, 3)
torch.chunk()
功能:将张量按维度dim进行平均切分
返回值:张量列表
注意事项:若不能整除,最后一份张量小于其他张量
torch.chunk(input, # 要切分的张量
chunks, # 要切分的份数
dim=0 # 要切分的维度
)
例如:
a = torch.ones((2, 7)) # 7
list_of_tensors = torch.chunk(a, dim=1, chunks=3) # 3
for idx, t in enumerate(list_of_tensors):
print("第{}个张量:{}, shape is {}".format(idx+1, t, t.shape))
输出的结果为:
第1个张量:tensor([[1., 1., 1.],
[1., 1., 1.]]), shape is torch.Size([2, 3])
第2个张量:tensor([[1., 1., 1.],
[1., 1., 1.]]), shape is torch.Size([2, 3])
第3个张量:tensor([[1.],
[1.]]), shape is torch.Size([2, 1])
输入的是7
列的张量,然后按照列维度切分为3
块,即:3、3、1
torch.split()
功能:将张量按照维度dim进行切分
返回值:张量的列表
torch.split(tensor, # 要切分的张量
split_size_or_sections, # 为int时,表示每一份的长度;为list,按list元素切分
dim=0 # 要切分的维度
)
例如:
list_of_tensors = torch.split(t, [2, 1, 2], dim=1)
for idx, t in enumerate(list_of_tensors):
print("第{}个张量:{}, shape is {}".format(idx, t, t.shape))
输出的结果:
第0个张量:tensor([[1., 1.],
[1., 1.]]), shape is torch.Size([2, 2])
第1个张量:tensor([[1.],
[1.]]), shape is torch.Size([2, 1])
第2个张量:tensor([[1., 1.],
[1., 1.]]), shape is torch.Size([2, 2])
按照列表中的2、1、2
进行切分,数字一定要对应2+1+2=5
,不等会出现问题
二、张量索引
torch.index_select()
功能:在维度dim
上,按index
索引数据
返回值:依index索引数据拼接的张量
torch.index_select(input, # 要索引的张量
dim, # 要索引的维度
index, # 要索引数据的序号
out=None
)
例如:
t = torch.randint(0, 9, size=(3, 3))
idx = torch.tensor([0, 2], dtype=torch.long) # float
t_select = torch.index_select(t, dim=0, index=idx)
print("t:\n{}\nt_select:\n{}".format(t, t_select))
输出的结果:
t:
tensor([[4, 5, 0],
[5, 7, 1],
[2, 5, 8]])
t_select:
tensor([[4, 5, 0],
[2, 5, 8]])
通过观察输出可知:index
这里索引的需要是张量形式的,且一定要注意输出的格式为:torch.long
类型的输出形式,float
类型会出现报错
torch.masked_select()
功能:按mask
中的True
进行索引
返回值:一维张量
torch.masked_select(input, # 要求索引的张量
mask, # 与input同形状的布尔类型张量
out=None)
例如:
t = torch.randint(0, 9, size=(3, 3))
mask = t.le(5) # ge is mean greater than or equal/ gt: greater than le lt
t_select = torch.masked_select(t, mask)
print("t:\n{}\nmask:\n{}\nt_select:\n{} ".format(t, mask, t_select))
输出的结果为:
t:
tensor([[4, 5, 0],
[5, 7, 1],
[2, 5, 8]])
mask:
tensor([[ True, True, True],
[ True, False, True],
[ True, True, False]])
t_select:
tensor([4, 5, 0, 5, 1, 2, 5])
最后的输出中>5
的数会直接筛选出去,变成一个一维的张量
三、张量变换
torch.reshape()
功能:变换张量形状
注意事项:当张量在内存中是连续的时,新张量与input
共享数据内存
torch.reshape(input,
shape # 新张量的形状
)
例如:
t = torch.randperm(8)
t_reshape = torch.reshape(t, (-1, 2, 2)) # -1
print("t:{}\nt_reshape:\n{}".format(t, t_reshape))
t[0] = 1024
print("t:{}\nt_reshape:\n{}".format(t, t_reshape))
print("t.data 内存地址:{}".format(id(t.data)))
print("t_reshape.data 内存地址:{}".format(id(t_reshape.data)))
输出的结果为:
t:tensor([5, 4, 2, 6, 7, 3, 1, 0])
t_reshape:
tensor([[[5, 4],
[2, 6]],
[[7, 3],
[1, 0]]])
t:tensor([1024, 4, 2, 6, 7, 3, 1, 0])
t_reshape:
tensor([[[1024, 4],
[ 2, 6]],
[[ 7, 3],
[ 1, 0]]])
t.data 内存地址:1611608930888
t_reshape.data 内存地址:1611608930888
通过观察输出可以发现,更改张量形状之后,地址并没发生变化
torch.transpose
功能:变换张量的两个维度
torch.transpose(input,
dim0, # 要交换的维度
dim1 # 要交换的维度
)
例如:将张量的第二维度和第三维度进行交换
# torch.transpose
t = torch.rand((2, 3, 4))
t_transpose = torch.transpose(t, dim0=1, dim1=2) # c*h*w h*w*c
print("t shape:{}\nt_transpose shape: {}".format(t.shape, t_transpose.shape))
输出的结果为:
t shape:torch.Size([2, 3, 4])
t_transpose shape: torch.Size([2, 4, 3])
torch.t()
功能:2
维张量进行转置,对矩阵而言,等价于torch.transpose(input, 0, 1)
torch.squeeze()
功能:压缩长度为1
的维度(轴)
torch.squeeze(input,
dim=None,
out=None)
注:dim
:若为None
,移除所有长度为1
的轴;若指定维度,当且仅当该轴长度为1
时,可以被移除
t = torch.rand((1, 2, 3, 1))
t_sq = torch.squeeze(t)
t_0 = torch.squeeze(t, dim=0)
t_1 = torch.squeeze(t, dim=1)
print(t.shape)
print(t_sq.shape)
print(t_0.shape)
print(t_1.shape)
输出的结果为:
torch.Size([1, 2, 3, 1])
torch.Size([2, 3])
torch.Size([2, 3, 1])
torch.Size([1, 2, 3, 1])
torch.usqueeze()
功能:依据dim
扩展维度
torch.usqueeze(input,
dim, # 扩展的维度
out=None)
四、张量数学运算
- 加减乘除
torch.add()
torch.addcdiv()
torch.addcmul()
torch.sub()
torch.div()
torch.mul()
例如:
t_0 = torch.randn((3, 3))
t_1 = torch.ones_like(t_0)
t_add = torch.add(t_0, 10, t_1)
输出的结果:
t_0:
tensor([[ 0.6614, 0.2669, 0.0617],
[ 0.6213, -0.4519, -0.1661],
[-1.5228, 0.3817, -1.0276]])
t_1:
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
t_add_10:
tensor([[10.6614, 10.2669, 10.0617],
[10.6213, 9.5481, 9.8339],
[ 8.4772, 10.3817, 8.9724]])
- 对数、指数、幂函数
torch.log(input, out=None)
torch.log10(input, out=None)
torch.log2(input, out=None)
torch.exp(input, out=None)
torch.pow()
- 三角函数
torch.abs(input, out=None)
torch.acos(input, out=None)
torch.cosh(input, out=None)
torch.cos(input, out=None)
torch.asin(input, out=None)
torch.atan(input, out=None)
torch.atan2(input, other, out=None)
线性回归
lr = 0.05 # 学习率
# 创建训练数据
x = torch.rand(20, 1) * 10 # x data (tensor), shape=(20, 1)
y = 2*x + (5 + torch.randn(20, 1)) # y data (tensor), shape=(20, 1)
# 构建线性回归参数
w = torch.randn((1), requires_grad=True)
b = torch.zeros((1), requires_grad=True)
for iteration in range(1000):
# 前向传播
wx = torch.mul(w, x)
y_pred = torch.add(wx, b)
# 计算 MSE loss
loss = (0.5 * (y - y_pred) ** 2).mean()
# 反向传播
loss.backward()
# 更新参数
b.data.sub_(lr * b.grad)
w.data.sub_(lr * w.grad)
# 清零张量的梯度
w.grad.zero_()
b.grad.zero_()
# 绘图
if iteration % 20 == 0:
plt.scatter(x.data.numpy(), y.data.numpy())
plt.plot(x.data.numpy(), y_pred.data.numpy(), 'r-', lw=5)
plt.text(2, 20, 'Loss=%.4f' % loss.data.numpy(), fontdict={'size': 20, 'color': 'red'})
plt.xlim(1.5, 10)
plt.ylim(8, 28)
plt.title("Iteration: {}\nw: {} b: {}".format(iteration, w.data.numpy(), b.data.numpy()))
plt.pause(0.5)
if loss.data.numpy() < 1:
break
编程的思路很好理解,就是创建一个2x+5
的函数取样,在它的基础上增加噪声,从而生成数据集。然后用模型去拟合这个函数的曲线。