均方误差损失函数
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())