学习记录总结专栏:机器学习:PyTorch框架-个人学习总结(专栏)
https://blog.youkuaiyun.com/spiderb/category_12838886.html
1 前言
在(3)中,通过学习一个实例对pytorch框架有了一个初步认识,但如果想要对pytorch框架实现随心所欲的调整和运用,那就需要对一些基本内容有更加深入的理解和实操。所以基于框架核心的张量概念,做进一步的学习。
2 张量的操作(拼接、切分、索引和变换)
2.1 张量拼接
# (1)将张量按照维度dim进行拼接(不会扩展张量的维度)
torch.cat(
tensors, # 张量序列
dim=0, # 要拼接的维度
out=None
)
# 示例
t = torch.ones((2,3))
t_cat0 = torch.cat([t,t],dim=0)
t_cat1 = torch.cat([t,t],dim=1)
print("t_cat0:{} shape:{}\n t_cat1:{} shape:{}".format(t_cat0,t_cat0.shape,t_cat1,t_cat1.shape))
# 输出结果
t_cat0:tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]) shape:torch.Size([4, 3])
t_cat1:tensor([[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]]) shape:torch.Size([2, 6])
# (2)在新创建的维度dim上进行拼接(会扩展张量的维度)
torch.stack(
tensors, # 张量序列
dim=0, # 要拼接的维度
out=None
)
# 示例
t = torch.ones((2,3))
t_stack0 = torch.stack([t,t],dim=2)
print("t_stack0:{} shape:{}".format(t_stack0,t_stack0.shape))
# 输出结果
t_stack0:tensor([[[1., 1.],
[1., 1.],
[1., 1.]],
[[1., 1.],
[1., 1.],
[1., 1.]]]) shape:torch.Size([2, 3, 2])

最低0.47元/天 解锁文章
996

被折叠的 条评论
为什么被折叠?



