且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

撒花!《神经网络与深度学习》中文教程正式开源!全书 pdf、ppt 和代码一同放出

更新时间:2022-07-18 10:29:20


红色石头之前在某乎上回答“机器学习该怎么入门”这个问题的时候,曾经给入门学者提过一个建议,就是放弃海量资料。确实,资料不在多而在精!一份优秀的资料完全可以帮助我们快速地入门和进阶。


今天给大家推荐一份最近新出的非常火热的深度学习入门教程:《神经网络与深度学习》,这本书由复旦大学的邱锡鹏老师所著。


撒花!《神经网络与深度学习》中文教程正式开源!全书 pdf、ppt 和代码一同放出


神经网络与深度学习》排在首位的特点就是它是完全的中文教程。我相信大部分深度学习入门学者面对英文教程的时候,战斗力多半会削减大半。而邱锡鹏老师的这本书恰恰为中国学生而著,大大降低了深度学习的语言门槛,让大家有更多的精力放在核心知识内容的学习上。


关于本书


关于本书,邱锡鹏是这样评价的:


近年来,以机器学习、知识图谱为代表的人工智能技术逐渐变得普及。从车牌识别、人脸识别、语音识别、智能问答、推荐系统到自动驾驶,人们在日常生活中都可能有意无意地使用到了人工智能技术。这些技术的背后都离不开人工智能领域研究者们的长期努力。特别是最近这几年,得益于数据的增多、计算能力的增强、学习算法的成熟以及应用场景的丰富,越来越多的人开始关注这一个“崭新”的研究领域:深度学习。深度学习以神经网络为主要模型,一开始用来解决机器学习中的表示学习问题。但是由于其强大的能力,深度学习越来越多地用来解决一些通用人工智能问题,比如推理、决策等。目前,深度学习技术在学术界和工业界取得了广泛的成功,受到高度重视,并掀起新一轮的人工智能热潮。


这本书的作者邱锡鹏老师,目前是复旦大学计算机科学技术学院的博士生导师、自然语言处理与深度学习组的副教授。


神经网络与深度学习》主要介绍神经网络与深度学习中的基础知识、主要模型(卷积神经网络、递归神经网络等)以及在计算机视觉、自然语言处理等领域的实际应用。


主要内容


这本书目前已经更新完毕,总共包含了 15 章。内容涉及神经网络集基础知识以及经典的 CNN、RNN 模型,还有其在 CV 和 NLP 方面的应用。15 章内容分为三大部分:第一部分为入门篇,包括 1~3 章;第二部分为基础模型,包括 4~10 章;第三部分为进阶模型,包括 11~15 章。


完整书籍目录如下:


  • 第 1 章:绪论
  • 第 2 章:机器学习概述
  • 第 3 章:线性模型
  • 第 4 章:前馈神经网络
  • 第 5 章:卷积神经网络
  • 第 6 章:循环神经网络
  • 第 7 章:网络优化与正则化
  • 第 8 章:注意力机制与外部记忆
  • 第 8 章:无监督学习
  • 第 10 章:模型独立的学习方式
  • 第 11 章:概率图模型
  • 第 12 章:深度信念网络
  • 第 13 章:深度生成模型
  • 第 14 章:深度强化学习
  • 第 15 章:序列生成模型


除了 15 章正文内容外,作者还为我们提供了详细的数学基础知识,放在了附录部分。数学基础总共包含 4 方面内容:


  • 附录 A:线性代数
  • 附录 B:微积分
  • 附录 C:数学优化
  • 附录 D:概率论


撒花!《神经网络与深度学习》中文教程正式开源!全书 pdf、ppt 和代码一同放出


这些数学基础知识,可谓是神经网络与深度学习的内功心法!也是本书的最大亮点之一,能够极大提升我们在阅读本书的效率。


课程资源


目前,邱锡鹏老师已经开源了该课程所有的资源,包括书籍 pdf,课程 ppt,书籍相关习题参考代码等。


课程主页:

https://nndl.github.io/


全书 pdf:

https://nndl.github.io/nndl-book.pdf


3 小时课程概要:

https://nndl.github.io/ppt/%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C%E4%B8%8E%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0-3%E5%B0%8F%E6%97%B6.pdf


示例代码:

https://github.com/nndl/nndl-codes


课程练习:

https://github.com/nndl/exercise


关于课程练习,作者大都提供了最热门的 PyTorch 和 TensorFlow 两种框架的实现方式。以第 5 章 CNN 为例,我们来看一下相关代码。


PyTorch 实现:


import os
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.utils.data as Data
import torchvision
import torch.nn.functional as F
import numpy as np
learning_rate = 1e-4
keep_prob_rate = 0.7 #
max_epoch = 3
BATCH_SIZE = 50
DOWNLOAD_MNIST = False
if not(os.path.exists('./mnist/')) or not os.listdir('./mnist/'):
# not mnist dir or mnist is empyt dir
DOWNLOAD_MNIST = True
train_data = torchvision.datasets.MNIST(root='./mnist/',train=True, transform=torchvision.transforms.ToTensor(), download=DOWNLOAD_MNIST,)
train_loader = Data.DataLoader(dataset = train_data ,batch_size= BATCH_SIZE ,shuffle= True)
test_data = torchvision.datasets.MNIST(root = './mnist/',train = False)
test_x = Variable(torch.unsqueeze(test_data.test_data,dim = 1),volatile = True).type(torch.FloatTensor)[:500]/255.
test_y = test_data.test_labels[:500].numpy()
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d( # ???
# patch 7 * 7 ; 1 in channels ; 32 out channels ; ; stride is 1
# padding style is same(that means the convolution opration's input and output have the same size)
in_channels= ,
out_channels= ,
kernel_size= ,
stride= ,
padding= ,
),
nn.ReLU(), # activation function
nn.MaxPool2d(2), # pooling operation
)
self.conv2 = nn.Sequential( # ???
# line 1 : convolution function, patch 5*5 , 32 in channels ;64 out channels; padding style is same; stride is 1
# line 2 : choosing your activation funciont
# line 3 : pooling operation function.
)
self.out1 = nn.Linear( 7*7*64 , 1024 , bias= True) # full connection layer one
self.dropout = nn.Dropout(keep_prob_rate)
self.out2 = nn.Linear(1024,10,bias=True)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = x.view( ) # flatten the output of coonv2 to (batch_size ,32 * 7 * 7) # ???
out1 = self.out1(x)
out1 = F.relu(out1)
out1 = self.dropout(out1)
out2 = self.out2(out1)
output = F.softmax(out2)
return output
def test(cnn):
global prediction
y_pre = cnn(test_x)
_,pre_index= torch.max(y_pre,1)
pre_index= pre_index.view(-1)
prediction = pre_index.data.numpy()
correct = np.sum(prediction == test_y)
return correct / 500.0
def train(cnn):
optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate )
loss_func = nn.CrossEntropyLoss()
for epoch in range(max_epoch):
for step, (x_, y_) in enumerate(train_loader):
x ,y= Variable(x_),Variable(y_)
output = cnn(x)
loss = loss_func(output,y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if step != 0 and step % 20 ==0:
print("=" * 10,step,"="*5,"="*5, "test accuracy is ",test(cnn) ,"=" * 10 )
if __name__ == '__main__':
cnn = CNN()
train(cnn)

TensorFlow 实现:


import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
learning_rate = 1e-4
keep_prob_rate = 0.7 #
max_epoch = 2000
def compute_accuracy(v_xs, v_ys):
global prediction
y_pre = sess.run(prediction, feed_dict={xs: v_xs, keep_prob: 1})
correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys, keep_prob: 1})
return result
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
# 每一维度 滑动步长全部是 1, padding 方式 选择 same
# 提示 使用函数 tf.nn.conv2d
return
def max_pool_2x2(x):
# 滑动步长 是 2步; 池化窗口的尺度 高和宽度都是2; padding 方式 请选择 same
# 提示 使用函数 tf.nn.max_pool
return
# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 784])/255.
ys = tf.placeholder(tf.float32, [None, 10])
keep_prob = tf.placeholder(tf.float32)
x_image = tf.reshape(xs, [-1, 28, 28, 1])
# 卷积层 1
## conv1 layer ##
W_conv1 = # patch 7x7, in size 1, out size 32
b_conv1 =
h_conv1 = # 卷积 自己选择 选择激活函数
h_pool1 = # 池化
# 卷积层 2
W_conv2 = # patch 5x5, in size 32, out size 64
b_conv2 =
h_conv2 = # 卷积 自己选择 选择激活函数
h_pool2 = # 池化
# 全连接层 1
## fc1 layer ##
W_fc1 = weight_variable([7*7*64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
# 全连接层 2
## fc2 layer ##
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
# 交叉熵函数
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),
reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy)
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
for i in range(max_epoch):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys, keep_prob:keep_prob_rate})
if i % 100 == 0:
print(compute_accuracy(
mnist.test.images[:1000], mnist.test.labels[:1000]))


开源万岁!这份优秀的深度学习资源,赶快试试吧~