1导入实验所需要的包
import torch
import numpy as np
import random
from IPython import display
from matplotlib import pyplot as plt
from torch import nn
import torch.utils.data as Data
import torch.optim as optim
from torch.nn import init
import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
2自定义数据
num_inputs = 200
x1 = torch.normal(2,1,(10000, num_inputs))
y1 = torch.ones(10000,1) # 标签1
x1_train = x1[:7000]
x1_test = x1[7000:]
x2 = torch.normal(-2,1,(10000, num_inputs))
y2 = torch.zeros(10000,1) # 标签0
x2_train = x2[:7000]
x2_test = x2[7000:]
# 注意 x, y 数据的数据形式一定要像下面一样 (torch.cat 是合并数据)---按行合并
trainfeatures = torch.cat((x1_train,x2_train), 0).type(torch.FloatTensor) #[14000, 200]
trainlabels = torch.cat((y1[:7000], y2[:7000]), 0).type(torch.FloatTensor) #[14000, 1]
testfeatures = torch.cat((x1_test,x2_test), 0).type(torch.FloatTensor) #[6000, 200]
testlabels = torch.cat((y1[7000:], y2[7000:]), 0).type(torch.FloatTensor) #[6000, 1]
3 读取数据
batch_size = 50
dataset = Data.TensorDataset(trainfeatures, trainlabels)
train_iter = Data.DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True, num_workers=0 )
4 模型定义和参数初始化
#模型定义和参数初始化
num_hiddens,num_outputs = 256,1
net = nn.Sequential(
nn.Linear(num_inputs,num_hiddens),
nn.ReLU(),
nn.Linear(num_hiddens,num_outputs)
)
for params in net.parameters():
init.normal_(params,mean=0,std=0.01)
5 定义交叉熵损失函数和优化器
lr = 0.0005
num_epochs = 100
loss_fn = torch.nn.BCEWithLogitsLoss()
optimizer = torch.optim.SGD(net.parameters(),lr)
6 定义模型训练函数
#定义模型训练函数
def train(net,train_iter,loss_fn,num_epochs,batch_size,params=None,lr=None,optimizer=None):
train_ls = []
test_ls = []
for epoch in range(num_epochs):
train_l_sum, train_acc_num,n = 0.0,0.0,0
for X, y in train_iter:
y_hat = net(X)
loss = loss_fn(y_hat, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_l_sum += loss.item()*y.shape[0]
n+= y.shape[0]
train_labels = trainlabels.view(-1,1)
test_labels = testlabels.view(-1,1)
train_ls.append(train_l_sum/n)
test_ls.append(loss_fn(net(testfeatures),test_labels).item()*testfeatures.shape[0])
print('epoch %d,train_loss %.6f,test_loss %f'%(epoch+1,train_ls[epoch],test_ls[epoch]))
return train_ls,test_ls
7 开始训练模型
train_loss,test_loss = train(net,train_iter,loss_fn,num_epochs,batch_size,net.parameters,lr,optimizer)