0
点赞
收藏
分享

微信扫一扫

学习笔记 Day 52 (深度学习的回归,分类问题)

杏花疏影1 2022-03-11 阅读 48

回归问题:

求最优的w,b

import numpy as np
import  pandas as pd

# y = wx + b
def compute_error_for_line_given_points(b,w,points):# 计算损失
    totalError = 0
    for i in range(len(points)):
        x = points[i,0]
        y = points[i,1]

        totalError += (y - (w*x + b))**2
    return  totalError / float(len(points))

def step_gradient(b_current,w_current,points,learningRate): # 计算梯度
    b_gradient = 0
    w_gradient = 0
    N = float(len(points))
    for i in range(len(points)):
        x = points[i,0]
        y = points[i,1]
        b_gradient += -(2/N)*(y-((w_current*x) + b_current))# 在式子中对b求导,在求平均,
        w_gradient += -(2/N) * (y-((w_current*x) + b_current)) * x

    new_b = b_current - (learningRate*b_gradient)# 更新新的b,w
    new_w = w_current - (learningRate * w_gradient)
    return  [new_w,new_b]

def gradient_descent_runner(points,starting_b,starting_w,learning_rate,num_iterations):
    """
    迭代更新 w,b
    :param points:
    :param starting_b:
    :param starting_w:
    :param learning_rate:
    :param num_iterations:
    :return:
    """
    b = starting_b
    w = starting_w
    for i in range(num_iterations):
        b,w = step_gradient(b,w,np.array(points),learning_rate)
    return [w,b]

def run():
    # points = pd.read_csv('data.csv')
    points = np.genfromtxt("data.csv", delimiter=",")
    # print(points)
    learning_rate = 0.0001

    initial_b = 0 # 预测最初的b,w
    initial_w = 0
    num_iterations = 1000
    print('开始梯度下降时,b = {},m = {},error={}'
          .format(initial_b,initial_w,
                  compute_error_for_line_given_points(initial_b,initial_w,points)))
    print('Running...')
    [b,w] = gradient_descent_runner(points,initial_b,initial_w,learning_rate,num_iterations)
    print('经过 {0} 次迭代,最终的  b = {1},w = {2},error = {3}'
          .format(num_iterations,b,w,compute_error_for_line_given_points(b,w,points)))

if __name__ == '__main__':
    run()


 分类问题:

最后求得的是维度

 手写数字识别器

import  torch
from torch import  nn # 神经网络相关工作
from  torch.nn import  functional as F #
from  torch import  optim # 优化工具包
import  torchvision # 视觉相关数据包
import  matplotlib.pyplot as plt
from  utils import plot_image,plot_curve,one_hot

# 加载数据
batch_size = 512
train_loader = torch.utils.data.DataLoader(
    torchvision.datasets.MNIST('mnist_data', train=True, download=True,
                               transform=torchvision.transforms.Compose([
                                   torchvision.transforms.ToTensor(),
                                   torchvision.transforms.Normalize(
                                       (0.1307,), (0.3081,))
                               ])),
    batch_size=batch_size, shuffle=True)

test_loader = torch.utils.data.DataLoader(
    torchvision.datasets.MNIST('mnist_data/', train=False, download=True,
                               transform=torchvision.transforms.Compose([
                                   torchvision.transforms.ToTensor(),
                                   torchvision.transforms.Normalize(
                                       (0.1307,), (0.3081,))
                                        ])),
    batch_size=batch_size, shuffle=False)

x, y = next(iter(train_loader))
print(x.shape, y.shape, x.min(), x.max())
plot_image(x, y, 'image sample')

# 建立三层网络
class Net(nn.Module):
    
    def __init__(self):
        super(Net, self).__init__()

        # 每一层都是wx + b
        self.fc1 = nn.Linear(28*28,256) # 28*28是图片大小,256是经验决定的
        self.fc2 = nn.Linear(256,64)
        self.fc3 = nn.Linear(64,10) # 10代表的是维度,不是由经验决定

        # 计算过程
    def forward(self,x):
        """
        会接收一张张图片
        :param seld:
        :param x: [b,1,28,28]
        :return:
        """
        # h1 = relu(xw1+b1)
        x = F.relu(self.fc1(x)) # 第一层,前面要加激活函数relu
        # h2 = relu(h1w2+b2)
        x = F.relu(self.fc2(x))
        # h3 = h2w3+b3
        x = self.fc3(x)

        return x
# 训练
net = Net()

train_loss = []

optimizer = optim.SGD(net.parameters(),lr=0.01,momentum=0.9)# 设置一个优化器
# net.parameters() [w1,b1,w2,b2,w3,b3]

for epoch in range(3):
    for batch_idx,(x,y) in enumerate(train_loader):
        # x [b,1.28,28] => [b,feature]
        x = x.view(x.size(0),28*28)
        out = net(x)

        y_onehot = one_hot(y)

        loss = F.mse_loss(out,y_onehot)

        optimizer.zero_grad() # 清零梯度
        loss.backward()# 计算梯度
        optimizer.step() # 更新梯度

        train_loss.append(loss.item()) # loss.item() 把loss转化为具体的数值

        if batch_idx % 10 == 0:
            print(epoch,batch_idx,loss.item())
# 得到比较好的 w,b


plot_curve(train_loss)

total_correct = 0
# 准确度测试
for x,y in test_loader:
    x = x.view(x.size(0),28*28)
    out = net(x)
    pred = out.argmax(dim=1) # 返回最大值对应的索引值
    correct = pred.eq(y).sum().float() # 比较预测值真实值
    total_correct += correct.item() # item()转换成数值类型

total_num = len(test_loader.dataset)
acc = total_correct / total_num
print('准确率为:{}'.format(acc))

x,y = next(iter(test_loader))
out = net(x.view(x.size(0),28*28))
pred = out.argmax(dim=1)
plot_image(x,pred,'test')

 

举报

相关推荐

0 条评论