国外大佬 Sabastian Raschka (威斯康星大学统计学助理教授、《Python of Machine Learning》 作者)最近在 GitHub 上创建了一个深度学习模型的项目。项目主要收集了深度学习的多种模型、架构和注意事项。


该项目发布两天即获得了 2000 多星,目前在 GitHub Trending 上名列第一,所有代码均在Jupyter notebook上实现,Python版本使用3.7,配合作者的注释和原创笔记讲解,对于深度学习爱好者和学习者是非常好的入门学习材料。



项目地址:https://github.com/rasbt/deeplearning-models


目录


该项目的目录如下:


  • 传统机器学习

  • 多层感知机

  • 卷积神经网络(CNN)

  • 度量学习(Metric Learning)

  • 自编码器

  • 生成对抗网络(GAN)

  • 循环神经网络(RNN)

  • 有序回归

  • 技巧和窍门

  • PyTorch 工作流和机制

  • TensorFlow 工作流和机制



传统机器学习



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==00], X_train[y_train==01], label='class 0', marker='o')
plt.scatter(X_train[y_train==10], X_train[y_train==11], 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=(11),
                                      stride=(11),
                                      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=(33),
                                      stride=(11),
                                      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=(11),
                                      stride=(11),
                                      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=(33),
                                      stride=(11),
                                      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(-128*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


©著作权归作者所有:来自51CTO博客作者mb5fe18e9fef50b的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. Python数据科学:神经网络
  2. 使用Logic App进行Azure安全中心工作流自动化
  3. 如何在git特性分支工作流中处理xml/html ?
  4. 【懒懒的Tensorflow学习笔记三之搭建简单的神经网络模型】
  5. 毕业设计-基于深度神经网络的语音关键词检出系统-使用python脚本
  6. 【Python】keras神经网络识别mnist

随机推荐

  1. 【机器学习笔记】:大话线性回归(二)
  2. 【机器学习笔记】:一文让你彻底记住什么是
  3. 【SQL刷题系列】:leetcode178 Rank Scores
  4. 谁才是权游的真正主角。
  5. Scrapy框架的使用之Downloader Middlewar
  6. 【SQL刷题系列】:leetcode183 Customers W
  7. Python操作MySQL存储,这些你都会了吗?
  8. Scrapy框架的使用之Spider Middleware的
  9. 【SQL刷题系列】:leetcode180 Consecutive
  10. 【机器学习笔记】:大话线性回归(三)