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

## 主要内容

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

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

## 课程资源

https://nndl.github.io/

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

3 小时课程概要：

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

https://github.com/nndl/exercise

PyTorch 实现：

import os
import torch
import torch.nn as nn
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
if not(os.path.exists('./mnist/')) or not os.listdir('./mnist/'):
# not mnist dir or mnist is empyt dir
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= ,
),
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):
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)
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
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]))
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]))

|
1天前
|

Python网络爬虫教程概览
【6月更文挑战第21天】Python网络爬虫教程概览：安装requests和BeautifulSoup库抓取网页；使用HTTP GET请求获取HTML，解析标题；利用CSS选择器提取数据；处理异步内容可选Selenium；遵循爬虫策略，处理异常，尊重法律与网站规定。
7 1
|
3天前
|

17 4
|
2天前
|

5 0
|
4天前
|

|
8天前
|

23 1
|
8天前
|

26 0
|
2天前
|

【机器学习】深度神经网络（DNN）：原理、应用与代码实践
【机器学习】深度神经网络（DNN）：原理、应用与代码实践
12 0
|
8天前
|

19 0
|
8天前
|

42 0
|
8天前
|

35 0