深度学习(神经网络)[1]——单层感知器

深度学习(神经网络)[1] —— 单层感知器

算法描述

最原始的神经网络模型,类似于神经网络中的单个神经元,该算法局限性也很大,只适用于解决线性可分的问题,对于线性不可分的问题和异或问题则无法解决。但作为神经网络的基本单元,学习和理解单层感知器,对后续的学习是很有帮助的。

python实现

# ************************** Perception ******************
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
class CyrusPerception(object):
    def __init__(self,**kargs):
        self.X = None
        self.Y = None
        self.W = None
        self.output = None
        if "epoch" in kargs.keys():
            self.epoch = kargs["epoch"]
        else:
            self.epoch = 1000
        if "lr" in kargs.keys():
            self.lr = kargs["lr"]
        else:
            self.lr = 0.1
    def fit(self,X,Y,**kargs):
        self.X = np.hstack((np.ones((np.array(X).shape[0],1)),np.array(X)))
        self.Y = np.array(Y).reshape(-1,1)
        # 1、初始化权值
        if "W" in kargs.keys():
            self.W = kargs["W"]
        else:
            self.W = (np.random.random([self.X.shape[1],1])-0.5)*2
        # 2、更新权值
        for i in range(self.epoch):
            self.update_w()
            print("*"*20)
            print("epoch:",i+1)
            print("w:",self.W)
            if (self.Y == self.output).all():
                print("*"*20)
                print("Finihed")
                print("epoch",self.epoch)
                break
    def update_w(self):
        self.output = np.sign(self.X.dot(self.W)).reshape(-1,1)
        self.W += self.lr*(self.X.T.dot(self.Y - self.output))/int(self.X.shape[0])
if __name__ == "__main__":
    model = CyrusPerception()
    x = [[3,3],[4,3],[1,1],[0,2]]
    y = [1,1,-1,-1]
    model.fit(x,y)
    # 绘图
    k = -model.W[1]/model.W[2]
    b = -model.W[0]/model.W[2]
    x_lineal = np.array([0,5])
    y_lineal = k*x_lineal + b
    plt.figure()
    colors = "rgb"
    [plt.scatter(x[i][0],x[i][1],color = colors[y[i]]) for i in range(len(x))]
    plt.plot(x_lineal,y_lineal,color = "r")
    plt.show()

示例运行结果

********************
epoch: 1
w: [[-0.31672519]
 [-0.16285498]
 [ 0.19094236]]
********************
epoch: 2
w: [[-0.26672519]
 [ 0.18714502]
 [ 0.39094236]]
********************
epoch: 3
w: [[-0.36672519]
 [ 0.13714502]
 [ 0.24094236]]
********************
epoch: 4
w: [[-0.46672519]
 [ 0.08714502]
 [ 0.09094236]]
********************
epoch: 5
w: [[-0.46672519]
 [ 0.08714502]
 [ 0.09094236]]
********************
Finihed
epoch 1000

可视化

在这里插入图片描述
by CyrusMay 2020 05 05

当烟雾随晨光飘散
枕畔的湖已风干
期待已退化成等待
而我告别了突然

——五月天(后青春期的诗)——

猜你喜欢

转载自blog.csdn.net/Cyrus_May/article/details/105932951