对于此分析,我们将使用scikit-learn 库中的 make regression() 函数生成的合成数据集。数据集由输入特征和目标变量组成。输入特征代表自变量,而目标变量代表我们想要预测的因变量。
import seaborn as sns import numpy as sns import torch import torch.nn as nn import torch.optim as optim import sklearn from sklearn import datasets import pandas as pd
# from sklearn we are going to select one dataset data=datasets.make_regression() df = pd.DataFrame(data[0], columns=[f"feature_{i+1}"for i in range(data[0].shape[1])]) df["target"] = data[1]
classlinearRegression(nn.Module):# all the dependencies from torch will be given to this class [parent class] # nn.Module contains all the building block of neural networks: def__init__(self,input_dim): super(linearRegression,self).__init__() # building connection with parent and child classes self.fc1=nn.Linear(input_dim,10) # hidden layer 1 self.fc2=nn.Linear(10,5) # hidden layer 2 self.fc3=nn.Linear(5,3) # hidden layer 3 self.fc4=nn.Linear(3,1) # last layer
defforward(self,d): out=torch.relu(self.fc1(d)) # input * weights + bias for layer 1 out=torch.relu(self.fc2(out)) # input * weights + bias for layer 2 out=torch.relu(self.fc3(out)) # input * weights + bias for layer 3 out=self.fc4(out) # input * weights + bias for last layer return out # final outcome
input_dim=X_train.shape[1] torch.manual_seed(42) # to make initilized weights stable: model=linearRegression(input_dim)
训练过程
为了训练模型,我们使用均方误差 (MSE) 损失函数,该函数测量预测值与实际目标值之间的平均平方差。使用 Adam 优化器执行优化,该优化器根据计算的梯度调整模型的参数。该模型经过指定数量的 epoch 进行训练,其中每个 epoch 都涉及前向传播、损失计算、反向传播和权重更新。
# select loss and optimizers
loss=nn.MSELoss() # loss function optimizers=optim.Adam(params=model.parameters(),lr=0.01)
# training the model:
num_of_epochs=1000 for i in range(num_of_epochs): # give the input data to the architecure y_train_prediction=model(X_train) # model initilizing loss_value=loss(y_train_prediction.squeeze(),y_train) # find the loss function: optimizers.zero_grad() # make gradients zero for every iteration so next iteration it will be clear loss_value.backward() # back propagation optimizers.step() # update weights in NN
# print the loss in training part: if i % 10 == 0: print(f'[epoch:{i}]: The loss value for training part={loss_value}')
with torch.no_grad(): model.eval() # make model in evaluation stage y_test_prediction=model(X_test) test_loss=loss(y_test_prediction.squeeze(),y_test) print(f'Test loss value : {test_loss.item():.4f}')
# Inference with own data: pr = torch.tensor(torch.arange(1, 101).unsqueeze(dim=0), dtype=torch.float32).clone().detach() print(pr)
# we can load the saved model and do the inference again:
load_model=linearRegression(input_dim) # creating an instance again for loaded model load_model.load_state_dict(torch.load('/content/models/linear_regression.pth'))