CNN
convolutional neural network,不瞭解的可以學習https://arxiv.org/pdf/1901.06032.pdf,非常全面。
這裏只是CNN的二次抽象,可以認爲是這個話題的再次“全連接”層。
核心步驟則是:卷積、池化
對於分類問題,主要的流程:
至於卷積和池化則在後面代碼介紹,一句話理解:
卷積就是抽取某些特徵。filter就是卷積核,抽取某類特徵,如果想抽取不同的特徵,則就是多個filter,抽取之後形成feature map。
我們要學習什麼參數呢?
filter.
比如我們有3個filter,每個filter是3*3的matrix,那麼要學習的參數就是3個matrix對應的weight。
爲什麼要cnn呢?不用fully connected?
本質上是可以fully connnect的,如果計算足夠牛逼。其實主要是減少參數訓練,並且可以根據task設計更優秀的框架一樣可以做到同樣的performance。
核心是:卷積的特徵抽取能力和參數共享。
比如利用33的filter1進行特徵抽取的時候。如果把66的image弄成一列,那麼全連接形成3節點和-1節點,其實我們利用的filter1是一樣的,那就是weight一樣。這個就叫:shared weights。
最終的還有另外一個:不是所有的節點都參與計算到下一個節點,到底選擇那些節點呢? 這就是kernel大小的點,上圖中的紅色框裏的東西。 最新的kernel計算有了一個新的kernel叫做dilated kernel,叫空洞卷積。
空洞卷積,其實可以擴大感受野,捕獲更多的信息。
代碼實戰
#!/usr/bin/env python
# coding: utf-8
# ## 引入時間和導入數據
# - **pip install ipython-autotime**
# - **提前下載**
# sudo wget https://s3.amazonaws.com/img-datasets/mnist.npz*
# In[11]:
get_ipython().run_line_magic('load_ext', 'autotime')
from keras.datasets import mnist
# In[34]:
(X_train, y_train),(X_test,y_test) = mnist.load_data(path='mnist.npz')
# In[35]:
#numpy.ndarray
print(X_train.shape)
X_test.shape
# ## 展示圖片、向量
# - 圖片尺寸 28*28
# - 圖片展示plt.imshow
#
# <hline>
#
# In[36]:
import matplotlib.pyplot as plt
plt.imshow(X_test[0])
plt.imshow(X_train[0])
plt.show()
print(X_train[0].shape)
print(X_train[0])#rgb
# In[37]:
#reshape to fit model
X_train = X_train.reshape(60000,28,28,1) # last is grey
X_test = X_test.reshape(10000,28,28,1)
X_train[0]
# In[38]:
from keras.utils import to_categorical
#one-hot, y向量化
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
y_train[0]
# In[39]:
from keras.models import Sequential
from keras.layers import Dense,Conv2D,Flatten
model = Sequential()
# 3*3 filter_size; 64: filter number
model.add(Conv2D(64,kernel_size=3,activation='relu', input_shape=(28,28,1)))
model.add(Conv2D(32,kernel_size=3,activation='relu'))
model.add(Flatten())
model.add(Dense(10,activation='softmax'))
# In[41]:
model.compile(optimizer='adam',
loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, validation_data=(X_test,y_test),epochs=3)
# In[48]:
## 隨機預測一副圖片
ret = model.predict(X_test[0:1])
ret
# In[49]:
plt.imshow(X_test[0].reshape(28,28))
plt.show()
# In[ ]:
文本處理
總結
- 特點一稀疏交互(sparse interactions);也就是用小窗口卷積核
- 參數共享,卷積核的參數是一樣的
- 等變表示;其實就是平移和卷積操作對結果是無影響的。
參考文獻
Multi-scale context aggregation with dilated convolutions
https://arxiv.org/pdf/1901.06032.pdf
http://cs231n.github.io/convolutional-networks/
https://www.bilibili.com/video/av44989461/?spm_id_from=trigger_reload
https://arxiv.org/pdf/1510.03820.pdf