撒花!《神經網路與深度學習》中文教程正式開源!復旦邱錫鵬所著
紅色石頭之前在某乎上回答“機器學習該怎麼入門”這個問題的時候,曾經給入門學者提過一個建議,就是放棄海量資料。確實,資料不在多而在精!一份優秀的資料完全可以幫助我們快速地入門和進階。
今天給大家推薦一份最近新出的非常火熱的深度學習入門教程:《神經網路與深度學習》,這本書由復旦大學的邱錫鵬老師所著。
《神經網路與深度學習》排在首位的特點就是它是完全的中文教程。我相信大部分深度學習入門學者面對英文教程的時候,戰鬥力多半會削減大半。而邱錫鵬老師的這本書恰恰為中國學生而著,大大降低了深度學習的語言門檻,讓大家有更多的精力放在核心知識內容的學習上。
關於本書
關於本書,邱錫鵬是這樣評價的:
近年來,以機器學習、知識圖譜為代表的人工智慧技術逐漸變得普及。從車牌識別、人臉識別、語音識別、智慧問答、推薦系統到自動駕駛,人們在日常生活中都可能有意無意地使用到了人工智慧技術。這些技術的背後都離不開人工智慧領域研究者們的長期努力。特別是最近這幾年,得益於資料的增多、計算能力的增強、學習演算法的成熟以及應用場景的豐富,越來越多的人開始關注這一個“嶄新”的研究領域:深度學習。深度學習以神經網路為主要模型,一開始用來解決機器學習中的表示學習問題。但是由於其強大的能力,深度學習越來越多地用來解決一些通用人工智慧問題,比如推理、決策等。目前,深度學習技術在學術界和工業界取得了廣泛的成功,受到高度重視,並掀起新一輪的人工智慧熱潮。
這本書的作者邱錫鵬老師,目前是復旦大學計算機科學技術學院的博士生導師、自然語言處理與深度學習組的副教授。
《神經網路與深度學習》主要介紹神經網路與深度學習中的基礎知識、主要模型(卷積神經網路、遞迴神經網路等)以及在計算機視覺、自然語言處理等領域的實際應用。
主要內容
這本書目前已經更新完畢,總共包含了 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,書籍相關習題參考程式碼等。
課程主頁:
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]))
開源萬歲!這份優秀的深度學習資源,趕快試試吧~