PyTorch的前身是Torch,相比于TensorFlow,PyTorch更易学。这个Blog记录在Coggle学习活动中,我的Pytorch入门深度学习和CV内容。
目录
PyTorch张量计算与Numpy的转换
数据科学中的数据结构:
- 标量scalar:单个数叫标量 1,2,3,4,5
- 向量,矢量vector:【一维数组】,[1,2,3,4,5]标量按一定的顺序排成行和列
- 矩阵matrix:【二维数组】成行成列的数据
- 张量tensor:【三维数组】,【四维数组】等等高维数组
1.配置运行环境:本地Notebook环境或使用天池DSW
Jupyter Notebook配置环境:推荐Anaconda环境安装,科学计算包齐全!
Anaconda | The World's Most Popular Data Science Platform
天池DSW
下面是我用ANACONDA本地启动Notebook环境,选择open with Jupyter Notebook
Notebook打开就是这样啦,我晚点出个Jupyter使用的Blog
2.学习Pytorch的基础语法,成功运行下面代码
入门:
PyTorch深度学习:60分钟入门(Translation) - 知乎
Learn the Basics — PyTorch Tutorials 1.10.0+cu102 documentation
c = np.ones((3,3))
d = torch.from_numpy(c) #numpy 转tensor
NoteBook Code 内容
import numpy as np
import torch
c = np.ones((3,3))
d = torch.form_numpy(c)
3.梯度计算和梯度下降过程
学习链接:
Automatic Differentiation with torch.autograd — PyTorch Tutorials 1.10.0+cu102 documentation
线性回归、梯度下降(Linear Regression、Gradient Descent) - BYRans - 博客园
使用numpy创建一个的数据,其中x是0到100的范围,以0.01进行等差数列,使用pytroch定义w和b,并使用随机梯度下降,完成回归拟合。
import torch
import numpy as np
x = np.linspace(0,100,num = 10000)
y = 10 * x + 4 + np.random.normal(size = 10000)
torch.set_default_tensor_type(torch.DoubleTensor)
x = torch.from_numpy(x)
y = torch.from_numpy(y)
w = torch.randn(10000,10000,requires_grad=True)
b = torch.randn(10000,requires_grad=True)
z = torch.matmul(x, w)+b
loss = torch.nn.functional.binary_cross_entropy_with_logits(z, y)
print(z.requires_grad)
with torch.no_grad():
z = torch.matmul(x, w)+b
print(z.requires_grad)
NoteBook中的运行结果:
4.PyTorch全连接层原理和使用
全连接网络原理↓
对于全连接层的理解_xiaodong_11的博客-CSDN博客_全连接层
在pytorch中使用矩阵乘法实现全连接层
在pytorch中使用nn.Linear层
在这里卡住了 T_T ,想通了再来更新
恭喜EDG夺冠,EDG都赢了,我也要好好努力!