pytorch入门学习记录

B站学习地址

9. Creating PyTorch Tensors for Deep Learning

# 前两个函数会copy一份data
torch.Tensor(data) # 采用默认数据类型torch.float32
torch.tensor(data) # 小写开头是工厂函数,工厂函数会根据输入确定数据类型
# 下面两个函数与原data共享内存
torch.as_tensor(data)  #接收所有python数据类型
torch.from_numpy(data) #只接收nump数组
#后三个均为工厂函数,实际应用中更多采用工厂函数

10. Flatten,Reshape,and Squeeze

t = torch.tensor([
	[1,1,1,1],
	[2,2,2,2],
	[3,3,3,3]
],dtype=torch.float32)
# 查看
>>> t.shape
torch.Size([3, 4])
>>> t.size()
torch.Size([3, 4])
>>> len(t.shape)
2
>>> torch.tensor(t.shape).prod() 
tensor(12)
>>> t.numel()
12
# 重塑 reshape
# 两个参数 秩不变
>>> t.reshape(2,6)
tensor([[1., 1., 1., 1., 2., 2.],
        [2., 2., 3., 3., 3., 3.]])
>>> t.reshape(12,1)
tensor([[1.],
        [1.],
        [1.],
        [1.],
        [2.],
        [2.],
        [2.],
        [2.],
        [3.],
        [3.],
        [3.],
        [3.]])
# 三个参数 改变秩
>>> t.reshape(2,2,3)
tensor([[[1., 1., 1.],
         [1., 2., 2.]],

        [[2., 2., 3.],
         [3., 3., 3.]]])

# 压缩squeeze

# 解压缩unsqueeze

# flatten

11. CNN Flatten Operation Visualized

t.flatten()
t.flatten(start_dim=1) #指定flatten时的channel

12. Tensor for Deep Learning

broadcasting 广播

可以直接 用张量和标量进行运算,会自动把对应标量转换成张量之后运算

t = torch.tensor([
	[0,5,7],
	[6,0,7],
	[0,8,0]
],dtype=torch.float32)

# 以下四种操作结果相同
t.add(1)
t + 1
t + torch.tensor([
	np.broadcast_to(7,t.shape)
	,dtype = torch.folat32
)
t + torch.tensor([
	[7,7,7],
	[7,7,7],
	[7,7,7]
],dtype=torch.float32)

13. Code for Deep learning

缩减操作

>>> t = torch.tensor([
... [0,1,0],
... [2,0,2],
... [0,3,0]
... ],dtype=torch.float32)
>>> t.sum() # 计算标量和
tensor(8.)
>>> t.numel() # 原始张量中元素个数
9 
>>> t.sum().numel() # sum操作后张量中元素个数
1
>>> t.sum().numel() < t.numel() # sum操作后元素数减少,sum是缩减操作
True

其他缩减操作

>>> t.prod()
tensor(0.)
>>> t.mean()
tensor(0.8889)
>>> t.std()
tensor(1.1667)

指定操作channel

>>> t = torch.tensor([
... [1,1,1,1],
... [2,2,2,2],
... [3,3,3,3]
... ],dtype = torch.float32)
>>> t.sum(dim=0) # t[0]+t[1]+t[2] 返回每列和
tensor([6., 6., 6., 6.])
>>> t.sum(dim=1) # t[0].sum()+t[1].sum()+t[2].sum() 返回每行和
tensor([ 4.,  8., 12.])

ArgMax 返回张量内最大值索引
可用于确定哪个类预测值最高

t.max() # 返回最大值
t.argmax() # 返回最大值索引(索引值为t.flatten()后张量的下标值)
t.max(dim=0) # 返回每列的最大值以及最大值在每列上的索引
>>> t.max(dim=0)
torch.return_types.max(
values=tensor([3., 3., 3., 3.]),
indices=tensor([2, 2, 2, 2]))

t.argmax(dim=0) # 返回最大值在每列上的索引
>>> t.argmax(dim=0)
tensor([2, 2, 2, 2])

14.Data in Deep Learning

15.CNN Image Preparetion Code Project

ETL
提取Extract
转换Transform
加载Load

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值