随机梯度下降法——python实现

随机梯度下降法——python实现

梯度下降法及题目见此博客

Exercise 3:

题目

Now, we turn to use other optimization methods to get the optimal parameters. Can you use Stochastic Gradient Descent to get the optimal parameters? Plots the training error and the testing error at each K-step iterations (the size of K is set by yourself). Can you analyze the plots and make comparisons to those findings in Exercise 1?
Exercise 3: 现在,我们使用其他方法来获得最优的参数。你是否可以用随机梯度下降法获得最优的参数?请使用随机梯度下降法画出迭代次数(每K次,这里的K你自己设定)与训练样本和测试样本对应的误差的图。比较Exercise 1中的实验图,请总结你的发现。

答案

  • 误差

    迭代次数训练集误差测试集误差
    1000002349.46727516266633096.2253976063507
    200000435.2953157918752867.2532946436027
    3000009191.94783043780866.91437740326361
    4000005340.357593925614110.47975170582515
    5000001090.07352674794582296.224940582353
    6000001108.7868485577676581.28082487602
    700000292.19258541269441050.4866035021282
    800000278.5534963045991157.3717939063465
    9000005638.09779325940780.34942002974526
    10000008791.76407086875464.02723320568694
    1100000249.097106165234271529.4686707317628
    1200000204.691535063224821366.17101417547
    1300000205.557987604145441227.418573865224
    1400000202.758004232742281228.0429258477968
    1500000549.766187456897877.6109789872294
  • 误差图
    在这里插入图片描述

  • 代码

    import numpy as np
    import math
    import matplotlib.pyplot as plt
    
    
    def loss(train_data, weight, train_real):
        result = np.matmul(train_data, weight.T)
        loss = result - train_real
        losses = pow(loss, 2)
        losses = losses.T
        return losses.sum()
    
    
    def gradient(train_data, weight, train_real):
        num = np.random.randint(0, 50)
        one_train_data = train_data[num]
    
        one_train_real = train_real[num]
        result = np.matmul(one_train_data, weight.T)
        loss = one_train_real - result
        # print (train_real)
        # print (result)
        # print (loss)
        x1 = one_train_data[0]
        x1 = x1
        gradient1 = loss * x1
        x2 = one_train_data[1]
        x2 = x2
        gradient2 = loss * x2
        x3 = train_data.T[2]
        x3 = x3.T
        gradient3 = loss * x3
        return gradient1.T.sum(), gradient2.T.sum(), gradient3.T.sum()
    
    
    filename = "./dataForTraining.txt"
    filename1 = "./dataForTesting.txt"
    
    # input the train data
    read_file = open(filename)
    lines = read_file.readlines()
    
    list_all = []
    list_raw = []
    list_real = []
    for line in lines:
        list1 = line.split()
        for one in list1:
            list_raw.append(float(one))
        list_all.append(list_raw[0:2])
        list_real.append(list_raw[2])
        list_raw = []
    
    for one in list_all:
        one.append(1.0)
    
    train_data = np.array(list_all)
    train_real = np.array(list_real)
    train_real = train_real.T
    
    # input the test data
    read_test = open(filename1)
    test_lines = read_test.readlines()
    
    list_all_test = []
    list_raw_test = []
    list_real_test = []
    for test_line in test_lines:
        another_list = test_line.split()
        for one in another_list:
            list_raw_test.append(float(one))
        list_all_test.append(list_raw_test[0:2])
        list_real_test.append(list_raw_test[2])
        list_raw_test = []
    
    for one in list_all_test:
        one.append(1.0)
    
    train_test_data = np.array(list_all_test)
    train_test_real = np.array(list_real_test)
    train_test_real = train_test_real.T
    
    
    # set the parameter
    weight = np.array([0.0, 0.0, 0.0])
    lr = 0.00015
    x = []
    y = []
    z = []
    
    for num in range(1, 1500001):
        losses = loss(train_data, weight, train_real)
        real_losses = loss(train_test_data, weight, train_test_real)
        gra1, gra2, gra3 = gradient(train_data, weight, train_real)
        gra = np.array([gra1, gra2, gra3])
        weight = weight + (gra * lr)
        # print (losses, real_losses)
        if num % 100000 == 0:
            x.append(num)
            y.append(losses)
            z.append(real_losses)
            print (losses, real_losses)
    
    plt.plot(x, y)
    plt.plot(x, z)
    plt.show()
    
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值