代码部分:
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import torch
from torch import nn
import torch.nn.functional as F
from torch.utils.data import Dataset,DataLoader,TensorDataset"""
准备数据
"""#正负样本数量
n_positive,n_negative = 2000,2000#生成正样本, 小圆环分布
r_p = 5.0 + torch.normal(0.0,1.0,size = [n_positive,1])
theta_p = 2*np.pi*torch.rand([n_positive,1])
Xp = torch.cat([r_p*torch.cos(theta_p),r_p*torch.sin(theta_p)],axis = 1)
Yp = torch.ones_like(r_p)#生成负样本, 大圆环分布
r_n = 8.0 + torch.normal(0.0,1.0,size = [n_negative,1])
theta_n = 2*np.pi*torch.rand([n_negative,1])
Xn = torch.cat([r_n*torch.cos(theta_n),r_n*torch.sin(theta_n)],axis = 1)
Yn = torch.zeros_like(r_n)#汇总样本
X = torch.cat([Xp,Xn],axis = 0)
Y = torch.cat([Yp,Yn],axis = 0)#可视化
plt.figure(figsize = (6,6))
plt.scatter(Xp[:,0],Xp[:,1],c = "r")
plt.scatter(Xn[:,0],Xn[:,1],c = "g")
plt.legend(["positive","negative"])plt.show()"""
#构建输入数据管道
"""
ds = TensorDataset(X,Y)
dl = DataLoader(ds,batch_size = 10,shuffle=True)"""
2, 定义模型
"""
class DNNModel(nn.Module):def __init__(self):super(DNNModel, self).__init__()self.fc1 = nn.Linear(2,4)self.fc2 = nn.Linear(4,8)self.fc3 = nn.Linear(8,1)def forward(self,x):x = F.relu(self.fc1(x))x = F.relu(self.fc2(x))y = nn.Sigmoid()(self.fc3(x))return ydef loss_func(self,y_pred,y_true):return nn.BCELoss()(y_pred,y_true)def metric_func(self,y_pred,y_true):y_pred = torch.where(y_pred > 0.5, torch.ones_like(y_pred, dtype=torch.float32),torch.zeros_like(y_pred, dtype=torch.float32))acc = torch.mean(1 - torch.abs(y_true - y_pred))return acc@propertydef optimizer(self):return torch.optim.Adam(self.parameters(), lr=0.001)model = DNNModel()# 测试模型结构
(features,labels) = next(iter(dl))
predictions = model(features)loss = model.loss_func(predictions,labels)
metric = model.metric_func(predictions,labels)print("init loss:",loss.item())
print("init metric:",metric.item())"""
3,训练模型
"""
def train_step(model, features, labels):# 正向传播求损失predictions = model(features)loss = model.loss_func(predictions,labels)metric = model.metric_func(predictions,labels)# 反向传播求梯度loss.backward()# 更新模型参数model.optimizer.step()model.optimizer.zero_grad()return loss.item(),metric.item()# 测试train_step效果
features,labels = next(iter(dl)) #非for循环就用next
train_step(model,features,labels)def train_model(model,epochs):for epoch in range(1,epochs+1):loss_list,metric_list = [],[]for features, labels in dl:lossi,metrici = train_step(model,features,labels)loss_list.append(lossi)metric_list.append(metrici)loss = np.mean(loss_list)metric = np.mean(metric_list)if epoch%100==0:print("epoch =",epoch,"loss = ",loss,"metric = ",metric)train_model(model,epochs = 300)# 结果可视化
fig, (ax1,ax2) = plt.subplots(nrows=1,ncols=2,figsize = (12,5))
ax1.scatter(Xp[:,0],Xp[:,1], c="r")
ax1.scatter(Xn[:,0],Xn[:,1],c = "g")
ax1.legend(["positive","negative"])
ax1.set_title("y_true")Xp_pred = X[torch.squeeze(model.forward(X)>=0.5)]
Xn_pred = X[torch.squeeze(model.forward(X)<0.5)]ax2.scatter(Xp_pred[:,0],Xp_pred[:,1],c = "r")
ax2.scatter(Xn_pred[:,0],Xn_pred[:,1],c = "g")
ax2.legend(["positive","negative"])
ax2.set_title("y_pred")plt.show()
结果展示:
数据部分:
结果分类:
思考:
本文中的DNN模型,将loss(损失),metric(准确率),optimizer(优化器)的定义放在了DNN网络中,这也产生了一系列的问题。首先在调用这些函数时,需要用网络名+“.”来调用。例如:loss = model.loss_func(predictions,labels)
但是这里最重要的一点是 def optimizer(self):
在optimizer函数上面必须有:@property
,若没有将会出现AttributeError: 'function' object has no attribute 'step'
的报错。
@property装饰器的作用:我们可以使用@property装饰器来创建只读属性,@property装饰器会将方法转换为相同名称的只读属性,可以与所定义的属性配合使用,这样可以防止属性被修改。