当前位置: 首页 > article >正文

深度学习——Tensor

一、理解

1.常数:scaler:0阶张量——标量

2.向量:vector:1阶张量

3.矩阵:matrix:2阶张量

4.三阶张量

二、创建张量的方法

1.使用列表创建Tensor

2.使用numpy数组创建Tensor

3.通过torch的API创建Tensor

# 1.使用列表创建Tensor
t1 = torch.Tensor([1, 2, 3])
print(t1)
"""
tensor([1., 2., 3.])
"""

# 2.使用numpy数组创建Tensor
array1 = np.arange(12).reshape(3, 4)

t2 = torch.Tensor(array1)
print(t2)

# 3.通过torch的API创建Tensor
"""
torch.empty(3,4):创建3行四列的空的tensor,会用无用的数据进行填充
torch.ones([3,4]):三行四列全为1的tensor
torch.zeros([3,4]):三行四列全为0的tensor
torch.rand([3,4]):三行四列随机值在[0,1]之间的值
torch.randint(low = 0, high = 10, size = [3, 4]) 创建3*4的随机整数的Tensor,值区间:[low, high]
torch.randn([3,4]) 均值为0,方差为1,3*4的tensor
"""
print(torch.empty(3,4))
print(torch.ones([3,4]))
print(torch.zeros([3,4]))
print(torch.rand([3,4]))

三、张量的方法和属性

1.tensor.item(),当tensor中只有一个元素可以用的时候
2.Tensor转为ndarray
3.形状修改,tensor.view((3, 4)), 类似numpy中的reshape,是一种浅拷贝
4.获取维数、转置、轴滚动。
5.在方法后加_,会原地修改,相当于Tensorflow里的inplace

# 1.tensor.item(),当tensor中只有一个元素可以用的时候
a =torch.tensor(np.arange(1))
print(a)
print(a.item()) #只有一个元素的时候可以用,返回一个python的标量,不可以用于多个元素的tensor
print('-'*50)
print(torch.Tensor([[[1]]]).item()) #获取一个tensor中的元素值
print('-'*50)

#%%
# 2.Tensor转为ndarray
t2 = torch.Tensor([[3,4]])
print(t2.numpy()) #tensor转为ndarray
print('-'*50)
print(t2.shape) #获取形状
print(t2.size()) #获取形状
print(t2.size(1))  #获取某个维度的数据,维度后的张量切片
#%%
t2
#%%
#写一个ndarray
array1 = np.array([[1,2,3],[4,5,6]])
print(id(array1))
array2=array1.reshape(3,2)
print(id(array2))
#%%
array2[0,0]=100
print(array1)
print(array2)
#%%
array1.ndim #获取维数
#%%
# 3.3.形状修改,tensor.view((3, 4)), 类似numpy中的reshape,是一种浅拷贝,仅仅形状发生改变,返回一个新的结果
t2 = torch.Tensor([[[3,4]]])
print(t2.size())
print(t2.view([1,2])) #[1,2]表示1行2列
print(t2.view([2])) # 一维tensor
#%%
b=t2.view([2, -1]) # -1表示自动计算
print(b)
print('-'*50)
print(t2) #t2的形状并没有发生改变
# https://pytorch.org/docs/stable/tensor_view.html
t2.untyped_storage().untyped().data_ptr() == b.untyped_storage().untyped().data_ptr() #判断两个tensor是否共享内存
#%%
b[0,0]=100
print(b)
print(t2) #t2的形状并没有发生改变
#%%
#3. 获取维数
print(t2.dim())

#4.获取最大值
print(t2.max())

#5.转置
t3 = torch.tensor([[1,3,4], [2,4,6]])
print(t3)
print(t3.t()) #转置


#%%
# 交换轴,这里的permute和rollaxis功能类型
t4 = torch.tensor(np.arange(24).reshape(2,3,4))
print(t4.shape)
print("-"*50)
print(t4.transpose(0,1).shape)#交换0轴和1轴
print("-"*50)
print(t4.permute(1, 0, 2).shape)#交换0轴和1轴,功能同上
print("-"*50)
print(t4.permute(1, 2, 0).shape)#变为了3*4*2
print("-"*50)
print(t4.permute(2, 1, 0).shape)#变为了4*3*2
#%%
t4.dtype
#%%
# 在方法后加_,会原地修改
x = torch.tensor(np.arange(12).reshape(3,4),dtype = torch.int8)
print(x)
y= torch.ones([3,4], dtype = torch.int64)
print(y)

print('-'*50)
x.sub_(y) # add_就地修改,不加下划线的会创建一个新的tensor来存储
print(x)
#%%
#tensor取值,和np完全相同
t5 = torch.tensor(np.arange(12).reshape(3,4))
print(t5)
print(t5[1,2]) #取值
print(t5[1]) #取一行
print(t5[:,1]) #取一列
print(t5[1:3,1:3]) #取一部分
print(t5[1:3,:]) #取一部分


#%%
#两个16行,1列的张量相减,求均值
t6 = torch.tensor(np.arange(16).reshape(16,1),dtype=torch.float32)
t7 = torch.tensor(np.arange(16,32).reshape(16,1),dtype=torch.float32)
print(t6)
print(t7)
((t6-t7)**2).mean()
#%%
#初始化两个张量,一个3,4,一个3,1,运算与ndarray相同
t8 = torch.tensor(np.arange(12).reshape(3,4),dtype=torch.float32)
t9 = torch.tensor(np.arange(3).reshape(3,1),dtype=torch.float32)
print(t8)
print(t9)
t8-t9


http://www.kler.cn/a/576170.html

相关文章:

  • esp32的IDF开发学习-驱动ws2812B
  • NTIRE比赛:技术前沿、国内企业表现与计算机视觉未来展望
  • Qwen 模型与 LlamaFactory 结合训练详细步骤教程
  • e2studio开发RA4M2(15)----配置RTC时钟及显示时间
  • STM32G431RBT6--(3)片上外设及其关系
  • STM32_IIC外设工作流程
  • nature genetics | SCENT:单细胞多模态数据揭示组织特异性增强子基因图谱,并可识别致病等位基因
  • 监听-追溯
  • 光路科技将携最新TSN交换机亮相高速展,展示智慧交通创新解决方案
  • AI 实战 - pytorch框架基于retinaface实现face检测
  • 游戏引擎学习第143天
  • Nginx多服务器转发接口数据,实现单接口多服务器处理数据,达到数据共享
  • 【微信小程序】uniapp开发微信小程序
  • 网络安全区划分
  • 使用PHP实现微服务架构:挑战与解决方案
  • SQL-labs13-16闯关记录
  • Docker Desktop常见问题记录
  • 微信小程序+SpringBoot的单词学习小程序平台(程序+论文+讲解+安装+修改+售后)
  • 【计算机网络】计算机网络的性能指标——时延、时延带宽积、往返时延、信道利用率
  • Java 面试篇-SSM 框架专题(什么是 AOP?Spring 中事务时如何实现的?事务失效的场景?Spring 中循环引用怎么解决?Springboot 的自动配置原理?Spring 常见注解?)