Python实现感知器逻辑电路:与门、或非门及异或门代码详解
本文通过Python代码实现感知器模型,涵盖与门、与非门、或门及异或门的逻辑电路构建。利用NumPy处理权重与偏置计算,并采用pytest框架进行单元测试,验证单层感知器对线性可分逻辑的实现,以及多层感知器解决异或门非线性问题的方法。
在神经网络基础回顾(包括感知器与多层感知器)中梳理了相关理论,此处通过编写与门、与非门、或门及异或门的Python代码,帮助读者建立对感知器机制的直观理解。
同时,引入pytest框架以执行自动化测试,确保逻辑实现的准确性。
pip install pytest
与门、与非门、或门
单层感知器足以实现与门、与非门和或门的逻辑功能。
首先编写测试脚本test_perception.py:
from perception import and_operate, nand_operate, or_operate
def 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) == 0
def 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) == 1
def 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即可运行测试。
这三个逻辑门的权重和偏置参数基于直观理解或几何绘图确定,且并非唯一解。以下为简化实现,在perception.py文件中定义如下:
import numpy as np
def step_function(x):
"""
阶跃函数
: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)
执行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 国际许可协议
以上就是Python如何实现感知器的逻辑电路的详细内容,更多关于python 感知器的逻辑电路的资料请关注本站其它相关文章!
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













