不使用numpy库的python版实现

  • 与门
In [1]:
def AND1(x1,x2):
    w1,w2,theta=0.5,0.5,0.7
    if w1*x1+w2*x2<=theta:
        return 0
    if w1*x1+w2*x2>theta:
        return 1
print('输入为1和0时为',AND1(1,0))
print('输入为0和1时为',AND1(0,1))
print('输入为1和1时为',AND1(1,1))
print('输入为0和0时为',AND1(0,0))
输入为1和0时为 0
输入为0和1时为 0
输入为1和1时为 1
输入为0和0时为 0
  • 与非门

烦了,还是用类写比较快

In [18]:
class fn:
    def __init__(self,w1,w2,theta):
        self.w1=w1
        self.w2=w2
        self.theta=theta

    def output(self,x1,x2):
        result=0
        if self.w1*x1+self.w2*x2<=self.theta:
            result=0
        if self.w1*x1+self.w2*x2>self.theta:
            result=1
        print(f'输入为{x1}{x2}时为',result)
        return result
# 与门
print('与门')
AND1=fn(0.5,0.5,0.7)
AND1.output(0,1)
AND1.output(1,0)
AND1.output(1,1)
AND1.output(0,0)
print('-'*20)
# 与非门
print('与非门')
NAND1=fn(-0.5,-0.5,-0.7)
NAND1.output(0,1)
NAND1.output(1,0)
NAND1.output(1,1)
NAND1.output(0,0)
print('-'*20)
# 或门
print('或门')
OR1=fn(0.5,0.5,0.2)
OR1.output(0,1)
OR1.output(1,0)
OR1.output(1,1)
OR1.output(0,0)
与门
输入为0和1时为 0
输入为1和0时为 0
输入为1和1时为 1
输入为0和0时为 0
--------------------
与非门
输入为0和1时为 1
输入为1和0时为 1
输入为1和1时为 0
输入为0和0时为 1
--------------------
或门
输入为0和1时为 1
输入为1和0时为 1
输入为1和1时为 1
输入为0和0时为 0
Out[18]:
0

我错了,偏函数更快

In [5]:
import numpy as np
from functools import partial
def gzj(x1,x2,w1,w2,b):
    w=np.array([w1,w2])
    x=np.array([x1,x2])
    c=np.sum(w*x)+b
    if c<=0:
        return 0
    if c>0:
        return 1
AND1=partial(gzj,w1=0.5,w2=0.5,b=-0.7)#与门
print(AND1(1,1))
1

用numpy写的版本

In [1]:
import numpy as np
class fn:
    '''
    输出方法为output()
    '''
    def __init__(self,w1,w2,theta):
        self.w=np.array([w1,w2])
        self.theta=theta
    def output(self,x1,x2):
        c=0
        if x1!=0 and x1!=1: 
            raise Exception('输入有问题')
        if x2!=0 and x2!=1:
            raise Exception('输入有问题')

        x=np.array([x1,x2])
        result=np.sum(self.w*x)+self.theta
        if result<=0:
            c=0
        if result>0:
            c=1
        print(f'输入为{x1}{x2}时为',c)
        return c
# 与门
print('与门')
AND1=fn(0.5,0.5,-0.7)
AND1.output(0,1)
AND1.output(1,0)
AND1.output(1,1)
AND1.output(0,0)
print('-'*20)
# 与非门
print('与非门')
NAND1=fn(-0.5,-0.5,0.7)
NAND1.output(0,1)
NAND1.output(1,0)
NAND1.output(1,1)
NAND1.output(0,0)
print('-'*20)
# 或门
print('或门')
OR1=fn(0.5,0.5,-0.2)
OR1.output(0,1)
OR1.output(1,0)
OR1.output(1,1)
OR1.output(0,0)
与门
输入为0和1时为 0
输入为1和0时为 0
输入为1和1时为 1
输入为0和0时为 0
--------------------
与非门
输入为0和1时为 1
输入为1和0时为 1
输入为1和1时为 0
输入为0和0时为 1
--------------------
或门
输入为0和1时为 1
输入为1和0时为 1
输入为1和1时为 1
输入为0和0时为 0
Out[1]:
0
  • 添加了抛出输入异常的提示

以下为异或门的实现

In [2]:
import numpy as np
from functools import partial
class fn:
    '''
    输出方法为outputf()
    简单逻辑电路感知机类
    '''
    def __init__(self,w1,w2,theta):
        self.w=np.array([w1,w2])
        self.theta=theta
    def output(self,x1,x2):
        c=0
        if x1!=0 and x1!=1: 
            raise Exception('输入有问题')
        if x2!=0 and x2!=1:
            raise Exception('输入有问题')

        x=np.array([x1,x2])
        result=np.sum(self.w*x)+self.theta
        if result<=0:
            c=0
        if result>0:
            c=1
        print(f'输入为{x1}{x2}时为',c)
        return c
    
    def outputf(self,x1,x2):
        c=0
        if x1!=0 and x1!=1: 
            raise Exception('输入有问题')
        if x2!=0 and x2!=1:
            raise Exception('输入有问题')
    
        x=np.array([x1,x2])
        result=np.sum(self.w*x)+self.theta
        if result<=0:
            c=0
        if result>0:
            c=1
        return c

AND1=fn(0.5,0.5,-0.7)
NAND1=fn(-0.5,-0.5,0.7)
OR1=fn(0.5,0.5,-0.2)


def NOR1(x1,x2,x,y,z):
    s1=x.outputf(x1,x2)
    s2=y.outputf(x1,x2)
    result=z.outputf(s1,s2)
    return result

NNOR1=partial(NOR1,x=NAND1,y=OR1,z=AND1)
#使用偏函数来减少必要
print(NNOR1(1,0))
print(NNOR1(0,1))
print(NNOR1(0,0))
print(NNOR1(1,1))
1
1
0
0