Python如何實(shí)現(xiàn)感知器的邏輯電路
在神經(jīng)網(wǎng)絡(luò)入門回顧(感知器、多層感知器)中整理了關(guān)于感知器和多層感知器的理論,這里實(shí)現(xiàn)關(guān)于與門、與非門、或門、異或門的代碼,以便對(duì)感知器有更好的感覺(jué)。
此外,我們使用 pytest 框架進(jìn)行測(cè)試。
pip install pytest
與門、與非門、或門
通過(guò)一層感知器就可以實(shí)現(xiàn)與門、與非門、或門。
先寫(xiě)測(cè)試代碼 test_perception.py:
from perception import and_operate, nand_operate, or_operate def test_and_operate(): """ 測(cè)試與門 :return: """ assert and_operate(1, 1) == 1 assert and_operate(1, 0) == 0 assert and_operate(0, 1) == 0 assert and_operate(0, 0) == 0 def test_nand_operate(): """ 測(cè)試與非門 :return: """ assert nand_operate(1, 1) == 0 assert nand_operate(1, 0) == 1 assert nand_operate(0, 1) == 1 assert nand_operate(0, 0) == 1 def test_or_operate(): """ 測(cè)試或門 :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
寫(xiě)完測(cè)試代碼,后面直接輸入命令 pytest -v 即可測(cè)試代碼。
這三個(gè)門的權(quán)重和偏置是根據(jù)人的直覺(jué)或者畫(huà)圖得到的,并且不是唯一的。以下是簡(jiǎn)單的實(shí)現(xiàn),在 perception.py 中寫(xiě)上:
import numpy as np def step_function(x): """ 階躍函數(shù) :param x: :return: """ if x <= 0: return 0 else: return 1 def 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)
運(yùn)行 pytest -v 確認(rèn)測(cè)試通過(guò)。
========================================================================== 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 ============================================================================
異或門

如上圖所示,由于異或門不是線性可分的,因此需要多層感知器的結(jié)構(gòu)。
使用兩層感知器可以實(shí)現(xiàn)異或門。
修改 test_perception.py 文件,加入異或門的測(cè)試代碼 :
from perception import and_operate, nand_operate, or_operate, xor_operate
以及
def test_xor_operate(): """ 測(cè)試異或門 :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)
我們通過(guò)與非門和或門的線性組合實(shí)現(xiàn)了異或門。
運(yùn)行命令 pytest -v 測(cè)試成功。
========================================================================== 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é)議:知識(shí)共享署名-非商業(yè)性使用 4.0 國(guó)際許可協(xié)議
以上就是Python如何實(shí)現(xiàn)感知器的邏輯電路的詳細(xì)內(nèi)容,更多關(guān)于python 感知器的邏輯電路的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于python的Tkinter編寫(xiě)登陸注冊(cè)界面
這篇文章主要為大家詳細(xì)介紹了基于python的Tkinter編寫(xiě)登陸注冊(cè)界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
python如何遍歷指定路徑下所有文件(按按照時(shí)間區(qū)間檢索)
這篇文章主要給大家介紹了關(guān)于python如何遍歷指定路徑下所有文件(按按照時(shí)間區(qū)間檢索)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
django第一個(gè)項(xiàng)目127.0.0.1:8000不能訪問(wèn)的解決方案詳析
django項(xiàng)目服務(wù)啟動(dòng)后無(wú)法通過(guò)127.0.0.1訪問(wèn),下面這篇文章主要給大家介紹了關(guān)于django第一個(gè)項(xiàng)目127.0.0.1:8000不能訪問(wèn)的解決方案,需要的朋友可以參考下2022-10-10
python實(shí)現(xiàn)字符串和字典的轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)字符串和字典的轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
pandas pd.cut()與pd.qcut()的具體實(shí)現(xiàn)
本文主要介紹了pandas pd.cut()與pd.qcut()的具體實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
python接口自動(dòng)化框架實(shí)戰(zhàn)
這篇文章主要介紹了python接口自動(dòng)化框架實(shí)戰(zhàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
自動(dòng)化測(cè)試Pytest單元測(cè)試框架的基本介紹
這篇文章主要介紹了Pytest單元測(cè)試框架的基本介紹,包含了Pytest的概念,Pytest特點(diǎn),其安裝流程步驟以及相關(guān)配置流程,有需要的朋友可以參考下2021-08-08
基于Python實(shí)現(xiàn)大文件分割和命名腳本過(guò)程解析
這篇文章主要介紹了基于Python實(shí)現(xiàn)大文件分割和命名腳本過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
Python中的TfidfVectorizer參數(shù)使用解析
這篇文章主要介紹了Python中的TfidfVectorizer參數(shù)使用解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11

