Step1 深度学习——吴恩达
[双语字幕]吴恩达深度学习deeplearning.ai_哔哩哔哩_bilibili
感觉这个讲的挺好的 看到了P17
Step2 多元分类
一.代码:
import torch
import matplotlib.pyplot as plt
import torch.nn.functional as F
import torch.nn as nn
from torch.optim import SGD
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
# 生成一批数据样本,并分为3类
cluster = torch.ones(500, 2)
data0 = torch.normal(4 * cluster, 2)
data1 = torch.normal(-4 * cluster, 1)
data2 = torch.normal(-8 * cluster, 1)
label0 = torch.zeros(500)
label1 = torch.ones(500)
label2 = label1 * 2
x = torch.cat((data0, data1, data2), ).type(torch.FloatTensor)
y = torch.cat((label0, label1, label2), ).type(torch.LongTensor)
plt.scatter(x.numpy()[:, 0], x.numpy()[:, 1], c=y.numpy(), s=10, lw=0, cmap='RdYlGn')
plt.show()
# 定义神经网络Net
class Net(nn.Module):
def __init__(self, input_feature, num_hidden, outputs):
super(Net, self).__init__()
self.hidden = nn.Linear(input_feature, num_hidden)
self.out = nn.Linear(num_hidden, outputs)
def forward(self, x):
x = F.relu(self.hidden(x))
x = self.out(x)
x = F.softmax(x)
return x
CUDA = torch.cuda.is_available()
if CUDA:
net = Net(input_feature=2, num_hidden=20, outputs=3).cuda()
inputs = x.cuda()
target = y.cuda()
else:
net = Net(input_feature=2, num_hidden=20, outputs=3)
inputs = x
target = y
# 优化器:随机梯度下降
optimizer = SGD(net.parameters(), 0.02)
criterion = nn.CrossEntropyLoss()
def draw(output):
if CUDA:
output = output.cpu()
plt.cla()
output = torch.max((output), 1)[1]
pred_y = output.data.numpy().squeeze()
target_y = y.numpy()
plt.scatter(x.numpy()[:, 0], x.numpy()[:, 1], c=pred_y, s=10, lw=0, cmap='RdYlGn')
accuracy = sum(pred_y == target_y) / 1500.0
plt.text(1.5, -4, 'Accuracy=%s' % (accuracy), fontdict={'size': 20, 'color': 'red'})
plt.pause(0.01)
#训练模型
def train(model, criterion, optimizer, epochs):
for epoch in range(epochs):
output = model(inputs)
loss = criterion(output, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if epoch % 40 == 0:
draw(output)
if __name__ == '__main__':
train(net, criterion, optimizer, 10000)
代码和上周的逻辑回归很相似(毕竟逻辑回归是二元分类)
提示:如果出现以下问题,则是需要更新包,或者在别人电脑运行以下也可
Step3 CNN的代码(手写数字识别)
#coding=utf-8
#from __future__ import print_function, division
import torch
import torch.nn as nn
#import torch.optim as optim
from torch.optim import SGD
import torchvision
from torchvision import datasets,transforms,models
import matplotlib.pyplot as plt
import os
data_transforms = {
'train': transforms.Compose([
transforms.Scale(230),
transforms.CenterCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
]),
'test': transforms.Compose([
transforms.Scale(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
]),
}
data_directory = 'data'
trainset = datasets.ImageFolder(os.path.join(data_directory, 'train'), data_transforms['train'])
testset = datasets.ImageFolder(os.path.join(data_directory, 'test'), data_transforms['test'])
trainloader = torch.utils.data.DataLoader(trainset, batch_size=5,shuffle=True, num_workers=0)
testloader = torch.utils.data.DataLoader(testset, batch_size=5,shuffle=True, num_workers=0)
'''
def imshow(inputs):
inputs = inputs / 2 + 0.5
inputs = inputs.numpy().transpose((1, 2, 0))
print inputs
plt.imshow(inputs)
plt.show()
inputs,classes = next(iter(trainloader))
imshow(torchvision.utils.make_grid(inputs))
'''
alexnet = models.alexnet(pretrained=True)
for param in alexnet.parameters():
param.requires_grad = False
alexnet.classifier=nn.Sequential(
nn.Dropout(),
nn.Linear(256*6*6,4096),
nn.ReLU(inplace =True),
nn.Dropout(),
nn.Linear(4096,4096),
nn.ReLU(inplace=True),
nn.Linear(4096,2),)
CUDA = torch.cuda.is_available()
if CUDA:
alexnet = alexnet.cuda()
criterion = nn.CrossEntropyLoss()
#optimizer = optim.SGD(alexnet.classifier.parameters(), lr=0.001, momentum=0.9)
optimizer = SGD(alexnet.classifier.parameters(), 0.001, 0.9)
def train(model,criterion,optimizer,epochs=1):
for epoch in range(epochs):
running_loss = 0.0
for i, data in enumerate(trainloader,0):
inputs,labels = data
if CUDA:
inputs,labels = inputs.cuda(),labels.cuda()
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs,labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if i%10==9:
print('[Epoch:%d, Batch:%5d] Loss: %.3f' % (epoch+1, i+1, running_loss / 100))
running_loss = 0.0
print('Finished Training')
def test(testloader,model):
correct = 0
total = 0
for data in testloader:
images, labels = data
if CUDA:
images = images.cuda()
labels = labels.cuda()
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum()
print('Accuracy on the test set: %d %%' % (100 * correct / total))
def load_param(model,path):
if os.path.exists(path):
model.load_state_dict(torch.load(path))
def save_param(model,path):
torch.save(model.state_dict(),path)
load_param(alexnet,'tl_model.pkl')
train(alexnet,criterion,optimizer,epochs=2)
save_param(alexnet,'tl_model.pkl')
test(testloader,alexnet)
结果:
Step3 音频检索领域的文献阅读
下载了几篇,还没开始看