1、自制一个线性数据集
首先需要安装numpy,生成x和y的数据,使用shuffle函数打乱,使用csv模块保存
import numpy as np
import csv
#定义线性方程的斜率和截距
m = 2
b = 5
#生成x和y数据
x = np.linspace(0,10,100)
y = m*x+b
#加入高斯噪声
mu, sigama = 0,0.5 #噪声的均值和标准差
noise = np.random.normal(mu, sigama, len(y)) #生成随机噪声
noisy_y = y + noise #将噪声加到y数据中
#组合x和y数据为一个二维数组
data = np.column_stack((x,noisy_y))
np.random.shuffle(data) #打乱数据
#打开csv文件并写入数据
with open ('linear_data.csv', mode='w', newline='')as file:
writer = csv.writer(file)
writer.writerow(['x','y'])
writer.writerows(data)
2、读取数据集并训练
要读取已保存为csv文件的数据,可以使用Python的pandas模块进行操作
import pandas as pd
data = pd.read_csv('linear_data.csv')
x = data['x'].to_numpy()
y = data['y'].to_numpy()
使用神经网络训练
import torch
from torch import nn
import torch.optim as optim
class LinearModel(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(100,10)
self.fc2 = nn.Linear(10,100)
def forward(self,x):
x = nn.functional.relu(self.fc1(x))
x = self.fc2(x)
return x
model = LinearModel()
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.01)
epochs = 1000
for epoch in range(epochs):
input = torch.from_numpy(x).float()
targets = torch.from_numpy(y).float()
optimizer.zero_grad()
output = model(input)
loss = criterion(output, targets)
loss.backward()
optimizer.step()
if epoch%100==0:
print(f'Epoch [{epoch+1}/{epochs}],loss:{loss.item():.4f}')
运行结果