Python如何實現(xiàn)感知器的邏輯電路
在神經(jīng)網(wǎng)絡入門回顧(感知器、多層感知器)中整理了關于感知器和多層感知器的理論,這里實現(xiàn)關于與門、與非門、或門、異或門的代碼,以便對感知器有更好的感覺。
此外,我們使用 pytest 框架進行測試。
pip install pytest與門、與非門、或門
通過一層感知器就可以實現(xiàn)與門、與非門、或門。
先寫測試代碼 test_perception.py:
from perception import and_operate, nand_operate, or_operatedef test_and_operate(): ''' 測試與門 :return: ''' assert and_operate(1, 1) == 1 assert and_operate(1, 0) == 0 assert and_operate(0, 1) == 0 assert and_operate(0, 0) == 0def test_nand_operate(): ''' 測試與非門 :return: ''' assert nand_operate(1, 1) == 0 assert nand_operate(1, 0) == 1 assert nand_operate(0, 1) == 1 assert nand_operate(0, 0) == 1def test_or_operate(): ''' 測試或門 :return: ''' assert or_operate(1, 1) == 1 assert or_operate(1, 0) == 1 assert or_operate(0, 1) == 1 assert or_operate(0, 0) == 0
寫完測試代碼,后面直接輸入命令 pytest -v 即可測試代碼。
這三個門的權重和偏置是根據(jù)人的直覺或者畫圖得到的,并且不是唯一的。以下是簡單的實現(xiàn),在 perception.py 中寫上:
import numpy as npdef step_function(x): ''' 階躍函數(shù) :param x: :return: ''' if x <= 0: return 0 else: return 1def and_operate(x1, x2): ''' 與門 :param x1: :param x2: :return: ''' x = np.array([x1, x2]) w = np.array([0.5, 0.5]) b = -0.7 return step_function(np.sum(w * x) + b)def nand_operate(x1, x2): ''' 與非門 :param x1: :param x2: :return: ''' x = np.array([x1, x2]) w = np.array([-0.5, -0.5]) b = 0.7 return step_function(np.sum(w * x) + b)def or_operate(x1, x2): ''' 或門 :param x1: :param x2: :return: ''' x = np.array([x1, x2]) w = np.array([0.5, 0.5]) b = -0.3 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 ============================================================================異或門
如上圖所示,由于異或門不是線性可分的,因此需要多層感知器的結構。
使用兩層感知器可以實現(xiàn)異或門。
修改 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 文件里加入異或門的函數(shù):
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)
我們通過與非門和或門的線性組合實現(xiàn)了異或門。
運行命令 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 許可協(xié)議:知識共享署名-非商業(yè)性使用 4.0 國際許可協(xié)議
以上就是Python如何實現(xiàn)感知器的邏輯電路的詳細內(nèi)容,更多關于python 感知器的邏輯電路的資料請關注好吧啦網(wǎng)其它相關文章!
相關文章:
1. .NET SkiaSharp 生成二維碼驗證碼及指定區(qū)域截取方法實現(xiàn)2. ASP中if語句、select 、while循環(huán)的使用方法3. CentOS郵件服務器搭建系列—— POP / IMAP 服務器的構建( Dovecot )4. MyBatis JdbcType 與Oracle、MySql數(shù)據(jù)類型對應關系說明5. ASP中實現(xiàn)字符部位類似.NET里String對象的PadLeft和PadRight函數(shù)6. jsp網(wǎng)頁實現(xiàn)貪吃蛇小游戲7. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向8. idea自定義快捷鍵的方法步驟9. django創(chuàng)建css文件夾的具體方法10. css代碼優(yōu)化的12個技巧
