pytorch搭建卷积神经网络【第五课_tensor基本操作】


前言

案例代码:github:https://github.com/2012Netsky/pytorch_cnn/blob/main/1_tensors.ipynb

一、创建列表

#!/usr/bin/env python
# coding: utf-8

# In[3]:
a = [1.0, 2.0, 1.0]

# In[4]:
a[0]

# In[5]:
a[2] = 3.0
a

在这里插入图片描述


二、创建tensor


# In[6]:
import torch # <1>
a = torch.ones(3) # <2>
a

# In[7]:
a[1]

# In[8]:
int(a[1])

# In[11]:
a[2] = 2.0
a

在这里插入图片描述


三、numpy/tensor互转

# In[9]:
# numpy转tensor
# tensor转numpy
import numpy as np
x = np.ones(5)
z = torch.tensor(x)
y = z.numpy()

# output
print(x)
print(z)
print(y)

# In[10]:
type(x)

在这里插入图片描述


四、创建修改tensor

# In[12]:
points = torch.zeros(6) # <1>
points[0] = 4.0 # <2>
points[1] = 1.0
points[2] = 5.0
points[3] = 3.0
points[4] = 2.0
points[5] = 1.0


五、查看tensor属性

# In[13]:
points = torch.tensor([4.0, 1.0, 5.0, 3.0, 2.0, 1.0])
points

# In[14]:
float(points[0]), float(points[1])


# In[15]:
points = torch.tensor([[4.0, 1.0], [5.0, 3.0], [2.0, 1.0]])
points


# In[16]:
points.shape


# In[17]:


points = torch.zeros(3, 2)
points

在这里插入图片描述


六、tensor切片

# In[18]:
points = torch.tensor([[4.0, 1.0], [5.0, 3.0], [2.0, 1.0]])
points


# In[19]:
points[0, 1]    # 0行1列


# In[20]:
points[0]      # 0行

在这里插入图片描述


七、tensor结构分析

# In[21]:
# pytorch中的一个tensor分为头信息区(Tensor)和存储区(Storage)
# 信息区主要保存着tensor的形状(size)、步长(stride)、数据类型(type)等信息。
# 而真正的数据则保存成连续数组,存储在存储区。
points = torch.tensor([[4.0, 1.0], [5.0, 3.0], [2.0, 1.0]])
points.storage()


# In[22]:
points_storage = points.storage()
points_storage[0]


# In[23]:
points.storage()[1]


# In[24]:
points = torch.tensor([[4.0, 1.0], [5.0, 3.0], [2.0, 1.0]])
points_storage = points.storage()
points_storage[0] = 2.0
points

# In[25]:
points = torch.tensor([[4.0, 1.0], [5.0, 3.0], [2.0, 1.0]])
second_point = points[1]
second_point.storage_offset()

在这里插入图片描述


八、tensor维度查询

# In[26]:
# tensor大小
second_point.size()


# In[27]:
# tensor形状
second_point.shape


# In[28]:
points.stride()


# In[29]:
second_point = points[1]
second_point.size()


# In[30]:
second_point.storage_offset()

# In[31]:
second_point.stride()

在这里插入图片描述


九、tensor编辑


# In[32]:
points = torch.tensor([[4.0, 1.0], [5.0, 3.0], [2.0, 1.0]])
second_point = points[1]
second_point[0] = 10.0
points

在这里插入图片描述


十、克隆tensor

# In[33]:
points = torch.tensor([[4.0, 1.0], [5.0, 3.0], [2.0, 1.0]])
second_point = points[1].clone()
second_point[0] = 10.0
points

# In[34]:
second_point

在这里插入图片描述


十一、tensor转置


points = torch.tensor([[4.0, 1.0], [5.0, 3.0], [2.0, 1.0]])
points


# In[36]:
points.storage()


# In[37]:
# 转置tensor
points_t = points.t()
points_t


# In[38]:
id(points.storage()) == id(points_t.storage())


# In[39]:
points.stride()


# In[40]:
points_t.stride()

在这里插入图片描述


十二、tensor维度转化


# In[41]:
some_t = torch.ones(3, 4, 5)
print(some_t)
# 维度转换
transpose_t = some_t.transpose(1, 2)
print(transpose_t)
some_t.shape


# In[42]:
transpose_t.shape


# In[43]:
some_t.stride()


# In[44]:
transpose_t.stride()

在这里插入图片描述


十三、tensor连续性判断及连续化

# In[45]:
#is_ontiguous()
# 判断tensor是否连续

# ontiguous()
# 普通的张良在内存中都是连续存放的,然而有些张量经过一些处理后(如permute)后就不是连续的了,
# 此时这些张量无法用基于连续内存空间的内置函数,如view
# 而contiguous()类似于clone,返回一个有连续内存的self张量的copy
points.is_contiguous()


# In[46]:
points_t.is_contiguous()

在这里插入图片描述


十四、tensor连续性判断及连续化

# In[47]:
points = torch.tensor([[4.0, 1.0], [5.0, 3.0], [2.0, 1.0]])
points_t = points.t()
points_t


# In[48]:
points_t.storage()


# In[49]:
points_t.stride()

# In[50]:
print(points_t.storage())
points_t_cont = points_t.contiguous()
print(points_t_cont.storage())


# In[51]:
points_t_cont.stride()


# In[52]:
points_t_cont.storage()

在这里插入图片描述


十五、tensor元素数据类型设定

# In[53]:
double_points = torch.ones(10, 2, dtype=torch.double)
short_points = torch.tensor([[1, 2], [3, 4]], dtype=torch.short)


# In[54]:
short_points.dtype

在这里插入图片描述


十六、tensor元素数据类型转换

# In[55]:
double_points = torch.zeros(10, 2).double()
short_points = torch.ones(10, 2).short()


# In[56]:
double_points = torch.zeros(10, 2).to(torch.double)
short_points = torch.ones(10, 2).to(dtype=torch.short)

# In[59]:
points_64 = torch.rand(5, dtype=torch.double)  # <1>
points_short = points_64.to(torch.short)
points_64 * points_short  # works from PyTorch 1.3 onwards

在这里插入图片描述


十七、tensor/list切片

# In[60]:
# reset points back to original value
points = torch.tensor([[4.0, 1.0], [5.0, 3.0], [2.0, 1.0]])


# In[61]:
some_list = list(range(6))
some_list[:]     # <1>
some_list[1:4]   # <2>
some_list[1:]    # <3>
some_list[:4]    # <4>
some_list[:-1]   # <5>
some_list[1:4:2] # <6>

# In[62]:
print(points)

# 从1行开始取所有行 所有列
points[1:]       # <1>
print(points[1:])

# 从1行开始取所有行 所有列
points[1:, :]    # <2>
print(points[1:, :])

# 从1行开始取所有行 第0列
points[1:, 0]    # <3>
print(points[1:, 0])

# None可以在所处维度中多一维,具体实现。从实验结果可以看出, None 功能类似torch.unsqueeze(),方便扩展维度,而不改变数据排列顺序。
points[None]     # <4>
print(points[None])

在这里插入图片描述


十八、tensor/numpy转换

# In[63]:
points = torch.ones(3, 4)
# tensor转numpy
points_np = points.numpy()
point_tensor = torch.tensor(points_np)

print(points)
print(points_np)
print(point_tensor)


# In[64]:
# numpy转tensor第二种方式
points = torch.from_numpy(points_np)

在这里插入图片描述


十九、tensor t格式保存与加载

# In[65]:
# 创建动态张量可能会比较慢或者使用随机数
# 将之前创建好的张量存起来可以直接加载使用

# 存张量的方法1
torch.save(points, '../data/p1ch3/ourpoints.t')


# In[66]:
# 存张量的方法2
with open('../data/p1ch3/ourpoints.t','wb') as f:
   torch.save(points, f)


# In[67]:
# 加载张量的方法1
points = torch.load('../data/p1ch3/ourpoints.t')


# In[68]:
# 加载张量的方法2
with open('../data/p1ch3/ourpoints.t','rb') as f:
   points = torch.load(f)

在这里插入图片描述


二十、tensor hdf5格式保存与加载

# In[69]:
#在深度学习中,通常会使用巨量的数据或图片来训练网络。对于如此大的数据集,如果对于每张图片都单独从硬盘读取、预处理、之后再送
#入网络进行训练、验证或是测试,这样效率可是太低了。如果将这些图片都放入一个文件中再进行处理,这样效率会更高。有多种数据模型
#和库可完成这种操作,如HDF5和TFRecord。
import h5py
f = h5py.File('../data/p1ch3/ourpoints.hdf5', 'w')
dset = f.create_dataset('coords', data=points.numpy())
f.close()


# In[70]:
f = h5py.File('../data/p1ch3/ourpoints.hdf5', 'r')
dset = f['coords']
last_points = dset[-2:]


# In[71]:
last_points = torch.from_numpy(dset[-2:])
f.close()

在这里插入图片描述


二十一、tensor在cpu/gpu设备上创建与转移

# In[72]:
import torch
points_gpu = torch.tensor([[4.0, 1.0], [5.0, 3.0], [2.0, 1.0]], device='cuda')


# In[73]:
points_gpu = points.to(device='cuda')


# In[76]:
points_gpu = points.to(device='cuda:0')


# In[79]:
print(points)
points = 2 * points  # <1>
print(points)
points_gpu = 2 * points.to(device='cuda')  # <2>
print(points_gpu)


# In[82]:
points_gpu = points_gpu + 4
print(points_gpu)


# In[83]:
points_cpu = points_gpu.to(device='cpu')


# In[84]:
points_gpu = points.cuda()  # <1>
points_gpu = points.cuda(0)
points_cpu = points_gpu.cpu()

在这里插入图片描述


二十二、tensor矩阵转换

# In[86]:
a = torch.ones(3, 2)
print(a)
# 转换矩阵方式1
a_t = torch.transpose(a, 0, 1)
print(a_t)
a.shape, a_t.shape


# In[87]:
a = torch.ones(3, 2)
# 转换矩阵方法2
a_t = a.transpose(0, 1)
a.shape, a_t.shape

# In[73]:
a = torch.ones(3, 2)


# In[88]:
a.zero_()
a

在这里插入图片描述


总结

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

【网络星空】

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值