首页 > 其他分享 >手动实现前馈神经网络解决回归问题

手动实现前馈神经网络解决回归问题

时间:2022-10-24 13:26:24浏览次数:59  
标签:loss torch num 手动 前馈 神经网络 train test size

1、手动实现前馈神经网络解决回归问题

#导入必要的包
import torch  
import numpy as np  
import random  
from IPython import display  
from matplotlib import pyplot as plt  
import torch.utils.data as Data  
#自定义数据---训练
num_inputs = 500  
num_examples = 10000  
true_w = torch.ones(500,1)*0.0056  
true_b = 0.028  
x_features = torch.tensor(np.random.normal(0, 1, (num_examples, num_inputs)), dtype=torch.float)  
y_labels = torch.mm(x_features,true_w) + true_b  
y_labels += torch.tensor(np.random.normal(0, 0.01, size=y_labels.size()), dtype=torch.float)  
#训练集  
trainfeatures =x_features[:7000]  
trainlabels = y_labels[:7000]  
print(trainfeatures.shape)  
#测试集  
testfeatures =x_features[7000:]  
testlabels = y_labels[7000:]  
print(testfeatures.shape) 
torch.Size([7000, 500])
torch.Size([3000, 500])
#读取数据  
batch_size = 50  
# 将训练数据的特征和标签组合  
dataset = Data.TensorDataset(trainfeatures, trainlabels)  
train_iter = Data.DataLoader(  
    dataset=dataset, # torch TensorDataset format  
    batch_size=batch_size, # mini batch size  
    shuffle=True, # 是否打乱数据 (训练集一般需要进行打乱)  
    num_workers=0, # 多线程来读数据, 注意在Windows下需要设置为0  
)  
# 将测试数据的特征和标签组合  
dataset = Data.TensorDataset(testfeatures, testlabels)  
# 把 dataset 放入 DataLoader  
test_iter = Data.DataLoader(  
    dataset=dataset, # torch TensorDataset format  
    batch_size=batch_size, # mini batch size  
    shuffle=True, # 是否打乱数据 
    num_workers=0, # 多线程来读数据, 注意在Windows下需要设置为0  
)
#初始化参数  
num_hiddens,num_outputs = 256,1  
 
W1 = torch.tensor(np.random.normal(0, 0.01, (num_hiddens,num_inputs)), dtype=torch.float32)  
b1 = torch.zeros(1, dtype=torch.float32)  
W2 = torch.tensor(np.random.normal(0, 0.01, (num_outputs,num_hiddens)), dtype=torch.float32)  
b2 = torch.zeros(1, dtype=torch.float32)  
params =[W1,b1,W2,b2]  
for param in params:  
    param.requires_grad_(requires_grad=True)  
#自定义relu激活函数
def relu(x):  
    x = torch.max(input=x,other=torch.tensor(0.0))  
    return x 
#定义模型  
def net(X):  
    X = X.view((-1,num_inputs))  
    H = relu(torch.matmul(X,W1.t())+b1)  #经过第一层(包括激活函数)
    return torch.matmul(H,W2.t())+b2     #第二层
#定义最小化均方误差  
loss = torch.nn.MSELoss()  
  
#定义随机梯度下降法  
def SGD(paras,lr,batch_size):  
    for param in params:  
        param.data -= lr * param.grad/batch_size  
#定义模型训练函数  
def train(net,train_iter,test_iter,loss,num_epochs,batch_size,params=None,lr=None,optimizer=None):  
    train_ls = []  
    test_ls = []  
    for epoch in range(num_epochs): # 训练模型一共需要num_epochs个迭代周期  
        train_l_sum, train_acc_num,n = 0.0,0.0,0  
        # 在每一个迭代周期中,会使用训练数据集中所有样本一次  
        for X, y in train_iter: # x和y分别是小批量样本的特征和标签  
            y_hat = net(X)  
            l = loss(y_hat, y.view(-1,1)) # l是有关小批量X和y的损失  
            #梯度清零  
            if optimizer is not None:  #手动实现梯度清零
                optimizer.zero_grad()  
            elif params is not None and params[0].grad is not None:  
                for param in params:  
                    param.grad.data.zero_()  
            l.backward() # 小批量的损失对模型参数求梯度  
            if optimizer is None:  
                SGD(params,lr,batch_size)  
            else:  
                optimizer.step()  
            #计算每个epoch的loss  
            train_l_sum += l.item()*y.shape[0]   
            n+= y.shape[0]  
        test_labels = testlabels.view(-1,1)  
        train_ls.append(train_l_sum/n)  
        test_ls.append(loss(net(testfeatures),test_labels).item())  
        print('epoch %d, train_loss %.6f,test_loss %.6f'%(epoch+1, train_ls[epoch],test_ls[epoch]))
    return train_ls,test_ls  
lr = 0.01  #学习率
num_epochs = 50  #迭代次数
train_loss,test_loss = train(net,train_iter,test_iter,loss,num_epochs,batch_size,params,lr)  #开始训练
#结果可视化
x = np.linspace(0,len(train_loss),len(train_loss))  
plt.plot(x,train_loss,label="train_loss",linewidth=1.5)  
plt.plot(x,test_loss,label="test_loss",linewidth=1.5)  
plt.xlabel("epoch")  
plt.ylabel("loss")  
plt.legend()  
plt.show()  

标签:loss,torch,num,手动,前馈,神经网络,train,test,size
From: https://www.cnblogs.com/cyberbase/p/16821138.html

相关文章