感知機(jī),邏輯回歸巷波,或者說單層神經(jīng)網(wǎng)絡(luò)只能解決線性可分的問題萎津,比如AND和OR問題,圖中的直線是決策邊界抹镊。
image.png
對于XOR問題霜运,由于線性不可分楚里,我們希望生成非線性的決策邊界呐伞,如圖
image.png
我們可以通過組合多個線性可分的決策邊界淋昭,來實現(xiàn)這一目的
image.png
在神經(jīng)網(wǎng)絡(luò)中,這個操作就是增加隱藏層
image.png
相當(dāng)于將原始的輸入通過隱藏層之后终佛,轉(zhuǎn)化為了一個更有效的表達(dá)
image.png
使用keras簡單實現(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()
提供一個純numpy實現(xiàn):https://github.com/omar-florez/scratch_mlp
中文翻譯:https://zhuanlan.zhihu.com/p/74631214