线性回归

在这里插入图片描述
在这里插入图片描述
均方误差损失函数
在这里插入图片描述
在这里插入图片描述

import torch
from torch import nn
import time
import pandas as pd
import sklearn
import math
import matplotlib.pyplot as plt
import numpy as np
import random
from sklearn import datasets
from sklearn.model_selection import train_test_split

n = 1000
a = torch.ones(n)
b = torch.ones(n)


class Timer(object):
    def __init__(self):
        self.times = []
        self.start_time = 0

    def start(self):
        self.start_time = time.time()

    def stop(self):
        self.times.append(time.time() - self.start_time)
        return self.times[-1]

    def avg(self):
        return sum(self.times) / len(self.times)

    def sum(self):
        return sum(self.times)


# t = Timer()
# c = torch.zeros_like(a)
# t.start()
# for i in range(len(a)):
#     c[i] = a[i] + b[i]
# print(t.stop())
# print(c)
#
# t.start()
# c = a + b
# print(t.stop())
# print(c)


def get_datas():
    df = pd.DataFrame(datasets.load_boston()['data'], columns=datasets.load_boston()['feature_names'])
    df['label'] = datasets.load_boston()['target']
    x = np.array(df.iloc[:, :-1])
    y = np.array(df.iloc[:, -1])
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)
    return [torch.tensor(x_train), torch.tensor(y_train), torch.tensor(x_test), torch.tensor(y_test)]


# def get_init_parameter(x_train):
#     # 传参进来m*n维x训练数据,传出去x的w,b初始化
#     return torch.tensor(np.random.normal(0, 0.01, len(x_train[0]))), np.random.normal(0, 0.01, 1)


def get_init_parameter(xi_train):
    # 传参进来m*n维x训练数据,传出去x的w,b初始化
    return torch.tensor(np.random.normal(0, 1, len(xi_train))), np.random.normal(0, 1, 1)


# def linreg(x_train, w, b):
#     return torch.mm(x_train, w.t()) + b


def linreg(xi_train, w, b):
    return np.dot(xi_train, w) + b


def square_loss(xi_train, yi_train, w, b):
    yi_pre = torch.tensor(linreg(xi_train, w, b))
    y_loss = math.pow((yi_pre - yi_train), 2) / 2
    return y_loss


def sgd(w, b, lr, xi, yi):
    w = w - lr * xi
    b = b + lr * float(yi)
    return w, b


class LinerModel():
    def __init__(self, w, b, lr):
        self.datas = get_datas()
        self.w = w
        self.b = b
        self.lr = lr
        pass

    def fit(self):
        x_train = self.datas[0]
        y_train = self.datas[1]

        temp = True
        while temp:
            for xi, yi in zip(x_train, y_train):
                if square_loss(xi, yi, self.w, self.b) > 10:
                    self.w, self.b = sgd(self.w, self.b, self.lr, xi, yi)
                    temp = True
                    print(self.w, self.b)
                    break
                else:
                    temp = False


    def score(self):
        acc = 0
        i = 0
        for xi, yi in zip(self.datas[2], self.datas[3]):
            if (linreg(xi, self.w, self.b) - yi) < 0.1:
                acc += 1
        return acc / len(self.datas[3])


w, b = get_init_parameter(get_datas()[0][0])
clf = LinerModel(w, b, 2)
clf.fit()
print(clf.score())


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值