机器学习和深度学习的介绍
神经元和神经网络的介绍
感知机和多层神经网络
激活函数和神经网络思想
Tensor基础
tensor的数据类型
使用GPU进行计算
梯度下降和反向传播
手动实现线性回归
优化器
损失函数
# #!/usr/bin/env python
# # -*- coding:utf-8 -*-
import torch
from torch import nn
from torch import optim
import numpy as np
from matplotlib import pyplot as plt
x = torch.rand([50,1])
y = x * 3+0.8
class Lr(nn.Module):
def __init__(self):
super(Lr, self).__init__()
self.linear = nn.Linear(1,1)
def forward(self,x):
out = self.linear(x)
return out
model = Lr()
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(),lr=1e-3)
for i in range(2000):
out = model(x)
loss = criterion(y,out)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1)%20==0:
print('Epoch[{}/{}],loss:{:.6f}'.format(i,2000,loss.data))
model.eval()
predict = model(x)
predict = predict.data.numpy()
plt.scatter(x.data.numpy(),y.data.numpy(),c="r")
plt.plot(x.data.numpy(),predict)
plt.show()