Numpy实现简单的线性回归

import numpy as np


def gradient_test():
    h_in = 64
    h1 = 1000
    h2 = 100
    h_out = 10

    x = np.random.randn(h_in, h1)
    w1 = np.random.randn(h1, h2)
    w2 = np.random.randn(h2, h_out)
    y = np.random.randn(h_in, h_out)
    # y = x.dot(w1).dot(w2)

    lr = 1e-6
    batch_size = 100
    for i in range(batch_size):
        # propagation size (h_in, h2)
        x1 = x.dot(w1)
        # get relu x,
        x_relu = np.maximum(x1, 0)
        # get y_pred, size (h_in, h_out)
        y_pred = x_relu.dot(w2)
        # calculate loss
        loss = np.sum((y_pred - y)**2)
        print('batch no:', i, ', loss:', loss)
        # get the gradient of y for loss, size (h_in, h_out)
        grad_y = 2 * (y_pred - y)
        # get gradient of x_relu, size (h_in, h2) = (h_in, h_out).(h_out, h2)
        grad_x_relu = grad_y.dot(w2.T)
        # get the gradient of w2, size (h2, h_out) = (h2, h_in).(h_in, h_out)
        grad_w2 = x_relu.T.dot(grad_y)
        # backpropagation for w2
        w2 -= lr * grad_w2
        # get the gradient of x according to gradient of x_relu, size (h_in, h1) = (h_in, h2).(h2, h1)
        grad_x = grad_x_relu.dot(w1.T)
        # get the gradient of w1 according to gradient of x_relu, size (h1, h2) = (h1, h_in).(h_in, h2)
        grad_w1 = x.T.dot(grad_x_relu)
        # backpropagation for x and w1
        x -= lr * grad_x
        w1 -= lr * grad_w1



if __name__ == '__main__':
    gradient_test()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值