一、代码中的数据集可以点击以下链接进行下载
百度网盘提取码:lala
二、代码运行环境
Pytorch-gpu==1.7.1 Python==3.7
三、数据集处理的代码如下所示
import pandas as pd
import torch
def make_dataset():
data = pd.read_csv(r'dataset/credit.csv', header=None)
X = data.iloc[:, :-1]
Y = data.iloc[:, -1].replace(-1, 0)
X = torch.from_numpy(X.values).type(torch.float32)
Y = torch.from_numpy(Y.values.reshape(-1, 1)).type(torch.float32)
return X, Y
if __name__ == '__main__':
x, y = make_dataset()
print(x)
print(y)
四、模型的构建代码如下所示
from torch import nn
def make_model():
model = nn.Sequential(
nn.Linear(in_features=15, out_features=1),
nn.Sigmoid()
)
return model
if __name__ == '__main__':
my_model = make_model()
print(my_model)
五、模型的训练代码如下所示
from torch import nn
from data_loader import make_dataset
from model_loader import make_model
import torch
import tqdm
X, Y = make_dataset()
model = make_model()
loss_fn = nn.BCELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.0001)
batches = 16
num_of_batch = 653 // 16
epoches = 1000
train_epoches = tqdm.tqdm(range(epoches))
for epoch in train_epoches:
for i in range(num_of_batch):
start = i * batches
end = start + batches
x = X[start:end]
y = Y[start:end]
y_pred = model(x)
loss = loss_fn(y_pred, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
torch.save(model.state_dict(), r'model_data\model.pth')
六、模型的预测代码如下所示
import torch
from data_loader import make_dataset
from model_loader import make_model
X, Y = make_dataset()
model = make_model()
model_state_dict = torch.load(r'model_data/model.pth')
model.load_state_dict(model_state_dict)
print('模型的参数情况如下:')
print(model.state_dict())
print('------------------------------------------------------------------------------')
print('模型的识别正确率为 {:14f}'.format(((model(X).data.numpy() > 0.5).astype('int') == Y.numpy()).mean()))
七、代码的运行结果如下所示
|