感知机的实现

与门、与非门、或门具有相同结构的感知机,区别只在于权重参数的值

# -*- coding: utf-8 -*-
def AND(x1,x2):
    w1, w2, theta = 0.5, 0.5, 0.7
    tmp = x1*w1 + x2*w2
    if tmp < theta:
        return 0
    elif tmp >= theta:
        return 1
    
AND(0,0)    #0
AND(1,0)    #0
AND(0,1)    #0
AND(1,1)    #1
'''引入numpy的各种门'''
import numpy as np

def PAND(x1,x2):
    x = np.array([x1,x2])
    w = np.array([0.5,0.5])
    b = -0.7
    tmp = np.sum(x*w) + b
    if tmp <= 0:
        return 0
    elif tmp > 0:
        return 1
    
PAND(0,0)    #0
PAND(1,0)    #0
PAND(0,1)    #0
PAND(1,1)    #1
   
def NOAND(x1,x2):
    x = np.array([x1,x2])
    w = np.array([-0.5,-0.5])
    b = 0.7
    tmp = np.sum(x*w) + b
    if tmp <= 0:
        return 0
    elif tmp > 0:
        return 1
    
NOAND(0,0)    #1
NOAND(1,0)    #1
NOAND(0,1)    #1
NOAND(1,1)    #0

def OR(x1,x2):
    x = np.array([x1,x2])
    w = np.array([0.5,0.5])
    b = -0.2 #只要小于0.5就可以啊
    tmp = np.sum(x*w) + b
    if tmp <= 0:
        return 0
    elif tmp > 0:
        return 1

OR(0,0)    #0
OR(1,0)    #1
OR(0,1)    #1
OR(1,1)    #1

异或门的实现

def XOR(x1,x2):
    s1 = NOAND(x1,x2)
    s2 = OR(x1,x2)
    y = PAND(s1,s2)
    return y

XOR(0,0)    #0
XOR(1,0)    #1
XOR(0,1)    #1
XOR(1,1)    #0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值