在神經網絡入門回顧(感知器、多層感知器)中整理了關於感知器和多層感知器的理論,這里實現關於與門、與非門、或門、異或門的代碼,以便對感知器有更好的感覺。
此外,我們使用 pytest 框架進行測試。
pip install pytest
與門、與非門、或門
通過一層感知器就可以實現與門、與非門、或門。
先寫測試代碼 test_perception.py:
1 from perception import and_operate, nand_operate, or_operate 2 3 4 def test_and_operate(): 5 """ 6 測試與門 7 :return: 8 """ 9 assert and_operate(1, 1) == 1 10 assert and_operate(1, 0) == 0 11 assert and_operate(0, 1) == 0 12 assert and_operate(0, 0) == 0 13 14 15 def test_nand_operate(): 16 """ 17 測試與非門 18 :return: 19 """ 20 assert nand_operate(1, 1) == 0 21 assert nand_operate(1, 0) == 1 22 assert nand_operate(0, 1) == 1 23 assert nand_operate(0, 0) == 1 24 25 26 def test_or_operate(): 27 """ 28 測試或門 29 :return: 30 """ 31 assert or_operate(1, 1) == 1 32 assert or_operate(1, 0) == 1 33 assert or_operate(0, 1) == 1 34 assert or_operate(0, 0) == 0
寫完測試代碼,后面直接輸入命令 pytest -v 即可測試代碼。
這三個門的權重和偏置是根據人的直覺或者畫圖得到的,並且不是唯一的。以下是簡單的實現,在 perception.py 中寫上:
1 import numpy as np 2 3 4 def step_function(x): 5 """ 6 階躍函數 7 :param x: 8 :return: 9 """ 10 if x <= 0: 11 return 0 12 else: 13 return 1 14 15 16 def and_operate(x1, x2): 17 """ 18 與門 19 :param x1: 20 :param x2: 21 :return: 22 """ 23 x = np.array([x1, x2]) 24 w = np.array([0.5, 0.5]) 25 b = -0.7 26 return step_function(np.sum(w * x) + b) 27 28 29 def nand_operate(x1, x2): 30 """ 31 與非門 32 :param x1: 33 :param x2: 34 :return: 35 """ 36 x = np.array([x1, x2]) 37 w = np.array([-0.5, -0.5]) 38 b = 0.7 39 return step_function(np.sum(w * x) + b) 40 41 42 def or_operate(x1, x2): 43 """ 44 或門 45 :param x1: 46 :param x2: 47 :return: 48 """ 49 x = np.array([x1, x2]) 50 w = np.array([0.5, 0.5]) 51 b = -0.3 52 return step_function(np.sum(w * x) + b)
運行 pytest -v 確認測試通過。
========================================================================== test session starts =========================================================================== platform darwin -- Python 3.6.8, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- /Users/mac/.virtualenvs/work/bin/python3 ... collected 3 items test_perception.py::test_and_operate PASSED [ 33%] test_perception.py::test_nand_operate PASSED [ 66%] test_perception.py::test_or_operate PASSED [100%] =========================================================================== 3 passed in 0.51s ============================================================================
異或門

如上圖所示,由於異或門不是線性可分的,因此需要多層感知器的結構。
使用兩層感知器可以實現異或門。
修改 test_perception.py 文件,加入異或門的測試代碼 :
from perception import and_operate, nand_operate, or_operate, xor_operate
以及
def test_xor_operate(): """ 測試異或門 :return: """ assert xor_operate(1, 1) == 0 assert xor_operate(1, 0) == 1 assert xor_operate(0, 1) == 1 assert xor_operate(0, 0) == 0
在 perception.py 文件里加入異或門的函數:
def xor_operate(x1, x2): """ 異或門 :param x1: :param x2: :return: """ s1 = nand_operate(x1, x2) s2 = or_operate(x1, x2) return and_operate(s1, s2)
我們通過與非門和或門的線性組合實現了異或門。
運行命令 pytest -v 測試成功。
========================================================================== test session starts =========================================================================== platform darwin -- Python 3.6.8, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- /Users/mac/.virtualenvs/work/bin/python3 ... collected 4 items test_perception.py::test_and_operate PASSED [ 25%] test_perception.py::test_nand_operate PASSED [ 50%] test_perception.py::test_or_operate PASSED [ 75%] test_perception.py::test_xor_operate PASSED [100%] =========================================================================== 4 passed in 0.60s ============================================================================
原文作者:雨先生
原文鏈接:https://www.cnblogs.com/noluye/p/11465389.html
許可協議:知識共享署名-非商業性使用 4.0 國際許可協議
參考
- 神經網絡入門回顧(感知器、多層感知器)
- 《Neural networks and deep learning》by Aurélien Géron
- 《Deep learning from scratch》by 齋藤康毅
