Pytorch学习笔记(二)张量的操作

这篇博客详细介绍了PyTorch中张量的操作,包括张量的拼接(cat和stack)、切分(chunk和split)、按索引选择、mask操作以及张量的转换(transpose、squeeze和unsqueeze)。此外,还展示了ones_like、add等函数的使用,是理解PyTorch张量操作的实用教程。
摘要由CSDN通过智能技术生成
import torch
import numpy as np

# 1.张量拼接
t = torch.ones((2, 3))
t_0 = torch.cat([t, t], dim=0)
t_1 = torch.cat([t, t], dim=1)

# 2.张量拼接,扩展维度
# torch.stack
t = torch.ones((2, 3))
t_stack = torch.stack([t, t], dim=0)

# 3.chunk切分张量
a = torch.ones((2, 7))
list_of_tensor = torch.chunk(a, dim=1, chunks=3)

for idx, t in enumerate(list_of_tensor):
    print("idx:{},tensor:{}".format(idx, t))

# 4.split将张量按维度dim切分
a = torch.ones((2, 7))
list_of_tensor = torch.split(a, [2, 4, 1], dim=1)

for idx, t in enumerate(list_of_tensor):
    print("idx:{},tensor:{}".format(idx, t))

#  5.按照索引选择
t = torch.randint(0, 9, size=(3, 3))
print(t)
idx = torch.tensor([0, 2], dtype=torch.long)
print(idx)
t_select = torch.index_select(t, dim=1, index=idx)
print(t_select)
# 6.mask操作,生成的是向量,ge函数按值
t = torch.randint(0, 9, size=(3, 3))
print(t)
mask = t.ge(5)
print(mask)
t_select = torch.masked_select(t, mask)
print(t_select)
# 7.张量的变换:transpose,包括不限于转置,交换维度
t = torch.rand((2, 3, 4))
# print(t)
t_transpose = torch.transpose(t, dim0=1, dim1=2)
# print(t_transpose)
# 二维向量转置
t_test = torch.rand(3, 4)
t_trans = torch.t(t_test)
print(t_test)
print(t_trans)

# torch.squeeze()压缩长度为1的维度
# torch.unsqueeze() expand the length of dim

# 8. ones_like,同大小全1矩阵
t_0 = torch.randn((3, 3))
t_1 = torch.ones_like(t_0)
print(t_0)
print(t_1)

t_add = torch.add(t_0, t_1, alpha=10)
print(t_add)
print(t_add.data)
print(t_add.data.numpy())

 

 

 

 

 

 

 

 

 

 

Pytorch是机器学习中的一个重要框架,它与TensorFlow一起被认为是机器学习的两大框架。Pytorch学习可以从以下几个方面入手: 1. Pytorch基本语法:了解Pytorch的基本语法和操作,包括张量(Tensors)的创建、导入torch库、基本运算等\[2\]。 2. Pytorch中的autograd:了解autograd的概念和使用方法,它是Pytorch中用于自动计算梯度的工具,可以方便地进行反向传播\[2\]。 3. 使用Pytorch构建一个神经网络:学习使用torch.nn库构建神经网络的典型流程,包括定义网络结构、损失函数、反向传播和更新网络参数等\[2\]。 4. 使用Pytorch构建一个分类器:了解如何使用Pytorch构建一个分类器,包括任务和数据介绍、训练分类器的步骤以及在GPU上进行训练等\[2\]。 5. Pytorch的安装:可以通过pip命令安装Pytorch,具体命令为"pip install torch torchvision torchaudio",这样就可以在Python环境中使用Pytorch了\[3\]。 以上是一些关于Pytorch学习笔记,希望对你有帮助。如果你需要更详细的学习资料,可以参考引用\[1\]中提到的网上帖子,或者查阅Pytorch官方文档。 #### 引用[.reference_title] - *1* [pytorch自学笔记](https://blog.csdn.net/qq_41597915/article/details/123415393)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Pytorch学习笔记](https://blog.csdn.net/pizm123/article/details/126748381)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值