作者:Sabastian Raschka
编辑:Python数据科学
国外大佬 Sabastian Raschka (威斯康星大学统计学助理教授、《Python of Machine Learning》 作者)最近在 GitHub 上创建了一个深度学习模型的项目。项目主要收集了深度学习的多种模型、架构和注意事项。
该项目发布两天即获得了 2000 多星,目前在 GitHub Trending 上名列第一,所有代码均在Jupyter notebook上实现,Python版本使用3.7,配合作者的注释和原创笔记讲解,对于深度学习爱好者和学习者是非常好的入门学习材料。
项目地址:https://github.com/rasbt/deeplearning-models
目录
该项目的目录如下:
传统机器学习
Perceptron的Tensorflow 部分代码实现如下:
import numpy as np
data = np.genfromtxt('../../ch02_perceptron/perceptron_toydata.txt', delimiter='\t')
X, y = data[:, :2], data[:, 2]
y = y.astype(np.int)
print('Class label counts:', np.bincount(y))
print('X.shape:', X.shape)
print('y.shape:', y.shape)
# Shuffling & train/test split
shuffle_idx = np.arange(y.shape[0])
shuffle_rng = np.random.RandomState(123)
shuffle_rng.shuffle(shuffle_idx)
X, y = X[shuffle_idx], y[shuffle_idx]
X_train, X_test = X[shuffle_idx[:70]], X[shuffle_idx[70:]]
y_train, y_test = y[shuffle_idx[:70]], y[shuffle_idx[70:]]
# Normalize (mean zero, unit variance)
mu, sigma = X_train.mean(axis=0), X_train.std(axis=0)
X_train = (X_train - mu) / sigma
X_test = (X_test - mu) / sigma
import matplotlib.pyplot as plt
%matplotlib inline
plt.scatter(X_train[y_train==0, 0], X_train[y_train==0, 1], label='class 0', marker='o')
plt.scatter(X_train[y_train==1, 0], X_train[y_train==1, 1], label='class 1', marker='s')
plt.xlabel('feature 1')
plt.ylabel('feature 2')
plt.legend()
plt.show()
多层感知机
卷积神经网络
比如,用Pytorch实现实现带有跳跃式连接的residual blocks,这样通过shortcut的输入可与主路径输出的维度匹配,从而允许网络学习标识功能。
代码实现如下所示:
class ConvNet(torch.nn.Module):
def __init__(self, num_classes):
super(ConvNet, self).__init__()
#########################
### 1st residual block
#########################
# 28x28x1 => 28x28x4
self.conv_1 = torch.nn.Conv2d(in_channels=1,
out_channels=4,
kernel_size=(1, 1),
stride=(1, 1),
padding=0)
self.conv_1_bn = torch.nn.BatchNorm2d(4)
# 28x28x4 => 28x28x1
self.conv_2 = torch.nn.Conv2d(in_channels=4,
out_channels=1,
kernel_size=(3, 3),
stride=(1, 1),
padding=1)
self.conv_2_bn = torch.nn.BatchNorm2d(1)
#########################
### 2nd residual block
#########################
# 28x28x1 => 28x28x4
self.conv_3 = torch.nn.Conv2d(in_channels=1,
out_channels=4,
kernel_size=(1, 1),
stride=(1, 1),
padding=0)
self.conv_3_bn = torch.nn.BatchNorm2d(4)
# 28x28x4 => 28x28x1
self.conv_4 = torch.nn.Conv2d(in_channels=4,
out_channels=1,
kernel_size=(3, 3),
stride=(1, 1),
padding=1)
self.conv_4_bn = torch.nn.BatchNorm2d(1)
#########################
### Fully connected
#########################
self.linear_1 = torch.nn.Linear(28*28*1, num_classes)
def forward(self, x):
#########################
### 1st residual block
#########################
shortcut = x
out = self.conv_1(x)
out = self.conv_1_bn(out)
out = F.relu(out)
out = self.conv_2(out)
out = self.conv_2_bn(out)
out += shortcut
out = F.relu(out)
#########################
### 2nd residual block
#########################
shortcut = out
out = self.conv_3(out)
out = self.conv_3_bn(out)
out = F.relu(out)
out = self.conv_4(out)
out = self.conv_4_bn(out)
out += shortcut
out = F.relu(out)
#########################
### Fully connected
#########################
logits = self.linear_1(out.view(-1, 28*28*1))
probas = F.softmax(logits, dim=1)
return logits, probas
torch.manual_seed(random_seed)
model = ConvNet(num_classes=num_classes)
model = model.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
自动编码器
生成对抗网络(GANs)
循环神经网络(RNNs)
PyTorch 和 TensorFlow 的工作流和机制
最后介绍了 PyTorch 和 TensorFlow 的工作流和机制,涉及数据集、训练和预处理等内容。
比如,Parallel Computing 部分,将多个CPUs与DataParallel结合使用,作者讲解的原理图如下:
项目地址:https://github.com/rasbt/deeplearning-models