感知機(jī),邏輯回歸,或者說單層神經(jīng)網(wǎng)絡(luò)只能解決線性可分的問題,比如AND和OR問題,圖中的直線是決策邊界。

image.png
對(duì)于XOR問題,由于線性不可分,我們希望生成非線性的決策邊界,如圖

image.png
我們可以通過組合多個(gè)線性可分的決策邊界,來實(shí)現(xiàn)這一目的

image.png
在神經(jīng)網(wǎng)絡(luò)中,這個(gè)操作就是增加隱藏層

image.png
相當(dāng)于將原始的輸入通過隱藏層之后,轉(zhuǎn)化為了一個(gè)更有效的表達(dá)

image.png
使用keras簡單實(shí)現(xiàn)一下吧
import numpy as np
from keras.models import Sequential
from keras.layers.core import Dense
# the four different states of the XOR gate
training_data = np.array([[0,0],[0,1],[1,0],[1,1]], "float32")
# the four expected results in the same order
target_data = np.array([[0],[1],[1],[0]], "float32")
model = Sequential()
model.add(Dense(2, input_dim=2, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='mean_squared_error',
optimizer='adam',
metrics=['binary_accuracy'])
model.fit(training_data, target_data, nb_epoch=500, verbose=2)
print model.predict(training_data).round()
提供一個(gè)純numpy實(shí)現(xiàn):https://github.com/omar-florez/scratch_mlp
中文翻譯:https://zhuanlan.zhihu.com/p/74631214