昨天终于把毕业论文的所有事情都完成了,之前学的一些深度学习知识有些又不记得了,一点点的复习,不过,复习起来还是比较容易的的,看一遍书,大致又都记起来了。学到了卷积神经网络,先利用pytorch搭建简单模型,然后再想着利用pytorch搭建别人的网络模型。
搭建模型的步骤
- 准备数据集
- 设计模型,计算y_hat,同时构造计算图
- 构建loss
- 构建优化器
- 训练更新
线性回归问题
import torch
#准备数据集
x_data = torch.tensor([[1.0],[2.0],[3.0]])
y_data = torch.tensor([[2.0],[4.0],[6.0]])
#设计模型 计算y_hat 构造计算图
#必须要实现__init__() 和forward()函数
class LinearModel(torch.nn.Module):
def __init__(self):
#just do it
super(LinearModel,self).__init__()
#构造自己的模型 输入1维 输出1维 y = ax+b
# 参数: in_features: int, out_features: int, bias: bool = True
self.linear = torch.nn.Linear(1,1)
def forward(self,x):
y_pre =