LinerProgression

手动实现线性回归

点击查看代码
import torch  
	import pandas as pd  
	import numpy as np  
	import matplotlib.pyplot as plt  
	import random  
	from torch.utils import data  

构造一个人造数据集

点击查看代码
	def synthetic_data(w, b, num_examples):  
		"""生成 y = Xw + b +噪声"""  
	 x = torch.normal(0, 1, (num_examples, len(w)))  # 均值为0,方差为1 的随机数,行数为num,列数为len(x)  
	 y = torch.matmul(x, w) + b  
		y += torch.normal(0, 0.1, y.shape)  # 随机噪音  
	 return x, y.reshape(-1, 1)  # 将y转换成一列  


	true_w = torch.tensor([2, -3.4])  
	true_b = 4.2  
	features, labels = synthetic_data(true_w, true_b, 1000)  

每次读取一个batch数据量

点击查看代码
	def data_iter(batch_size, features, labels):  
		num_examples = len(features)  # 样本数量  
	 indices = list(range(num_examples))  # 生成一个下标列表  
	 random.shuffle(indices)  # 将列表中顺序打乱,否则就会有序提取不好,我们要随机取样本  
	 for i in range(0, num_examples, batch_size):  # 从0开始到num_examples结束,每次拿batch_size个数据  
	 batch_indices = torch.tensor(indices[i:min(i + batch_size, num_examples)])  # 将拿出的下标拿出来,如果最后不够一个batchsize则拿到最后位置  
	 yield features[batch_indices], labels[batch_indices]  # 每次返回一个x,一个y直到完全返回  


	batch_size = 10  

	for x, y in data_iter(batch_size, features, labels):  
		print(x, '\n', y)  
		break  

	w = torch.normal(0, 0.01, size=(2, 1), requires_grad=True)  # 生成一个均值为0方差为0.1 的两行一列的张量  
	b = torch.zeros(1, requires_grad=True)  # 生成了一个0  

定义模型

点击查看代码
	def linreg(x, w, b):  
		return torch.matmul(x, w) + b  

损失函数 均方误差

点击查看代码
	def squared_loss(y_hat, y):  
		return (y_hat - y.reshape(y_hat.shape)) ** 2 / 2  
  

优化算法 小批量下降

点击查看代码
	def sgd(params, lr, batch_size):  
		"""小批量下降"""  
	 with torch.no_grad():  
			for param in params:  
				param -= lr * param.grad / batch_size  
				param.grad.zero_()  

实现

点击查看代码
	lr = 0.01  
	num_epochs = 5  
	net = linreg  
	loss = squared_loss  

	for epoch in range(num_epochs):  
		for x, y in data_iter(batch_size, features, labels):  
			l = loss(net(x, w, b), y)   # x, y的小批量损失  
	 l.sum().backward()  
			sgd([w, b], lr, batch_size)  
		with torch.no_grad():  
			train_l = loss(net(features, w, b), labels)  
			print(f'epoch {epoch + 1}, loss {float(train_l.mean()):f}')  

	print(f'w的估计误差:{true_w - w.reshape(true_w.shape)}')  
	print(f'b的估计误差:{true_b - b}')

给笔者点个赞呀!

posted @ 2021-11-13 12:12  茶柒每天要学习  阅读(37)  评论(0编辑  收藏  举报