with open(args.config) as f:
config = yaml.load(f)
print("\n**************************")
for k, v in config['common'].items():
setattr(args, k, v)
print('\n[%s]:'%(k), v)
print("\n**************************\n")
batch_size = args.batch_size # 通过这种方式获取参数
import torch
import torch.nn.functional as F
# way1
class Net(torch.nn.Module): # 自己定义的网络都需要继承Module类,并实现init方法和forward方法
def __init__(self, n_feature, n_hidden, n_output):
super(Net, self).__init__()
self.hidden = torch.nn.Linear(n_feature, n_hidden)
self.out = torch.nn.Linear(n_hidden, n_output)
def forward(self, x):
x = F.relu(self.hidden(x))
x = self.out(x)
return x
net = Net(1, 10, 1)
net(x)
# way2
net = torch.nn.Sequential(
torch.nn.Linear(1, 10),
torch.nn.ReLU(),
torch.nn.Linear(10, 1)
)
net(x)
# way3 整合以上两种方法,推荐
class Net(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.net1 = torch.nn.Sequential(
torch.nn.Linear(1, 10),
torch.nn.ReLU(),
torch.nn.Linear(10, 1)
)
def forward(self, x):
output = self.net1(x)
return output
net = Net()
net(x)
optimizer = torch.optim.SGD(net.parameters(), lr=learning_rate) # 优化器,里面存放网络参数和梯度
loss_func = torch.nn.CrossEntropyLoss() # 定义使用的损失函数模型
for _ in range(train_step):
out = net(x) # 网络预测值
loss = loss_func(out,y) # 预测值与真实值计算损失函数,返回关于网络参数的函数
optimizer.zero_grad() # 清除梯度
loss.backward() # 反向传播,计算梯度
optimizer.step() # 参数调整,完成一步优化
# 2 ways to save the net
torch.save(net, 'net.pkl') # save entire net
torch.save(net.state_dict(), 'net_params.pkl') # save only the parameters
def restore_net(): # restore entire net
net = torch.load('net.pkl')
prediction = net(x)
def restore_params(): # restore only the parameters
net = torch.nn.Sequential(
torch.nn.Linear(1, 10),
torch.nn.ReLU(),
torch.nn.Linear(10, 1)
) # 先搭建网络
net.load_state_dict(torch.load('net_params.pkl')) # 再恢复参数
prediction = net(x)
import torch
import torch.utils.data as Data
torch_dataset = Data.TensorDataset(input_tensor, lable_tensor)
loader = Data.DataLoader(
dataset=torch_dataset, # 数据集
batch_size=BATCH_SIZE, # mini batch size
shuffle=True, # 是否打乱数据集
num_workers=workers,
)
for epoch in range(3): # 训练完整训练集的次数
for step, (batch_x, batch_y) in enumerate(loader):
# train your data...
# 测试集
test_x = test_data.test_data.cuda()
test_y = test_data.test_labels.cuda()
# net
net.cuda()
# train
for epoch in range(EPOCH):
for step, (x, y) in enumerate(train_loader):
x = x.cuda()
y = y.cuda()
# prediction
pred_y = net(test_x).cuda().data
import torch
import torch.nn as nn
import torch.utils.data as Data
import torchvision
import os
EPOCH = 1 # 训练多少遍完整的训练集
BATCH_SIZE = 50
LR = 0.001 # learning rate
############# train data and test data start ###########
train_data = torchvision.datasets.MNIST(
root='./mnist/',
train=True,
transform=torchvision.transforms.ToTensor(),
download=False,
)
train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
# pick 2000 samples to speed up testing
test_data = torchvision.datasets.MNIST(root='./mnist/', train=False)
test_x = torch.unsqueeze(test_data.test_data, dim=1).type(torch.FloatTensor)[:2000]/255.
test_y = test_data.test_labels[:2000]
############# train data and test data end #############
# Network
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Sequential( # input shape (1, 28, 28)
nn.Conv2d(
in_channels=1, # input height
out_channels=16, # n_filters
kernel_size=5, # filter size
stride=1, # filter movement/step
padding=2, # if want same width and length of this image after Conv2d, padding=(kernel_size-1)/2 if stride=1
), # output shape (16, 28, 28)
nn.ReLU(), # activation
nn.MaxPool2d(kernel_size=2), # choose max value in 2x2 area, output shape (16, 14, 14)
)
self.conv2 = nn.Sequential( # input shape (16, 14, 14)
nn.Conv2d(16, 32, 5, 1, 2), # output shape (32, 14, 14)
nn.ReLU(), # activation
nn.MaxPool2d(2), # output shape (32, 7, 7)
)
self.out = nn.Linear(32 * 7 * 7, 10) # fully connected layer, output 10 classes
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = x.view(x.size(0), -1) # flatten the output of conv2 to (batch_size, 32 * 7 * 7)
output = self.out(x)
return output
cnn = CNN()
optimizer = torch.optim.Adam(cnn.parameters(), lr=LR) # optimize all cnn parameters
loss_func = nn.CrossEntropyLoss() # the target label is not one-hotted
for epoch in range(EPOCH):
for step, (b_x, b_y) in enumerate(train_loader):
output = cnn(b_x) # cnn output
loss = loss_func(output, b_y) # cross entropy loss
optimizer.zero_grad() # clear gradients for this training step
loss.backward() # backpropagation, compute gradients
optimizer.step() # apply gradients
if step % 50 == 0:
test_output = cnn(test_x)
pred_y = torch.max(test_output, 1)[1].data.numpy()
accuracy = float((pred_y == test_y.data.numpy()).astype(int).sum()) / float(test_y.size(0))
print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy)