可以多分类的神经网络

‘‘‘
11行神经网络①
层数可变,多类
‘‘‘
import numpy as np
import matplotlib.pyplot as plt

class BPNN(object):
    def __init__(self, neurals, epsilon=0.01, lamda=0.01):
        if not isinstance(neurals, list): raise ‘出错:参数 neurals 必须是 list 类型‘
        self.neurals = neurals   # 各层神经元数目,如:[3,10,8,2]
        self.size = len(neurals)

        self.epsilon = epsilon  # 学习速率
        self.lamda = lamda      # 正则化强度

        self.w = [np.random.randn(i,j) for i,j in zip(neurals[:-1], neurals[1:])] + [None]
        self.b = [None] + [np.random.randn(1,j) for j in neurals[1:]]
        self.l = [None] * self.size
        self.l_delta = [None] * self.size

        self.probs = None

    # 前向传播
    def forward(self, X):
        self.l[0] = X
        for i in range(1, self.size-1):
            self.l[i] = np.tanh(np.dot(self.l[i-1], self.w[i-1]) + self.b[i]) # tanh 函数
        self.l[-1] = np.exp(np.dot(self.l[-2], self.w[-2]) + self.b[-1])
        self.probs = self.l[-1] / np.sum(self.l[-1], axis=1, keepdims=True)

    # 后向传播
    def backward(self, y):
        self.l_delta[-1] = np.copy(self.probs)
        self.l_delta[-1][range(self.n_samples), y] -= 1
        for i in range(self.size-2, 0, -1):
            self.l_delta[i] = np.dot(self.l_delta[i+1], self.w[i].T) * (1 - np.power(self.l[i], 2)) # tanh 函数的导数

    # 更新权值、偏置
    def update(self):
        self.b[-1] -= self.epsilon * np.sum(self.l_delta[-1], axis=0, keepdims=True)
        for i in range(self.size-2, -1, -1):
            self.w[i] -= self.epsilon * (np.dot(self.l[i].T, self.l_delta[i+1]) + self.lamda * self.w[i])
            if i == 0: break
            self.b[i] -= self.epsilon * np.sum(self.l_delta[i], axis=0)

    # 计算损失
    def calculate_loss(self, y):
        loss = np.sum(-np.log(self.probs[range(self.n_samples), y]))
        loss += self.lamda/2 * np.sum([np.sum(np.square(wi)) for wi in self.w[:-1]]) # 可选
        loss *= 1/self.n_samples  # 可选
        return loss

    # 拟合
    def fit(self, X, y, n_iter=1000, print_loss=True):
        self.n_samples = X.shape[0] # 样本大小(样本数目)

        for i in range(n_iter):
            self.forward(X)
            self.backward(y)
            self.update()

            if not print_loss: continue
            if i%100 == 0: print(self.calculate_loss(y))

    # 预测
    def predict(self, x):
        self.forward(x)
        return np.argmax(self.probs, axis=1)

def plot_decision_boundary(clf, X, y):
    # Set min and max values and give it some padding
    x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
    y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
    h = 0.01
    # Generate a grid of points with distance h between them
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
    # Predict the function value for the whole gid
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    # Plot the contour and training examples
    plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)

def test1():
    X = np.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1]])
    #y = np.array([0,1,1,0]) # 两类
    y = np.array([0,1,2,3])  # 多类

    # bpnn = BPNN([3, 10, 8, 1])
    bpnn = BPNN([3, 10, 8, 4])
    bpnn.fit(X, y, n_iter=1000)

    print(‘训练结果:‘, bpnn.predict(X))

def test2():
    from sklearn.datasets import make_moons
    from sklearn.linear_model import LogisticRegressionCV

    X, y = make_moons(200, noise=0.20)
    plt.scatter(X[:,0], X[:,1], s=40, c=y, cmap=plt.cm.Spectral)
    plt.show()

    clf = LogisticRegressionCV()
    clf.fit(X, y)
    plot_decision_boundary(clf, X, y)
    plt.show()

    #nn = BPNN([2,5,4,2])
    nn = BPNN([2,4,2])
    nn.fit(X, y, n_iter=1000)
    plot_decision_boundary(nn, X, y)
    plt.show()

if __name__ == ‘__main__‘:
    #test1()
    test2()
时间: 2024-08-10 17:21:24

可以多分类的神经网络的相关文章

【HowTo ML】分类问题->神经网络入门

非线性分类器(Non-linear hypotheses) 为什么使用非线性分类器 我们举几个栗子: 假如我们有一个数据空间如左上角坐标系所示,那么我们要的模型需要如右边公式所示的预测函数. 假设有n个特征那么计算二次多项式就有O(n^2)的复杂度.n能有多大?我们来看下面这个栗子. 假设我们需要识别汽车,假如选取图像上两个点,那么就如左边坐标系所示,这没什么. 但实际上我们需要的数据空间时整张图片所有的像素.也就是假设图像是50?50那么我们就有2500个像素点.也就是需要2500个特征. 刚

数学建模----分类----BP神经网络

1.former's experience reference:[1]https://www.cnblogs.com/babyfei/p/7003299.html?utm_source=itdadao&utm_medium=referral 2myself %两层的分类前反馈神经网络 %newff函数 主要tez*样品格式 %train训练函数 %训练数据集 clear; file = "";%路径 input_data= file(:,);%n*m:表示数据集*指标 outp

TensorFlow-多分类单层神经网络softmax

#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Wed Aug 8 19:13:09 2018 @author: myhaspl """ import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets(&

Google发布机器学习平台Tensorflow游乐场~带你玩神经网络(转载)

Google发布机器学习平台Tensorflow游乐场-带你玩神经网络 原文地址:http://f.dataguru.cn/article-9324-1.html> 摘要: 昨天,Google发布了Tensorflow游乐场.Tensorflow是Google今年推出的机器学习开源平台.而有了Tensorflow游乐场,我们在浏览器中就可以训练自己的神经网络,还有酷酷的图像让我们更直观地了解神经网络的工作原理.今 ... 网络 工具 机器学习 神经网络 Tensorflow 昨天,Google发

Andrew Ng机器学习课程笔记(四)之神经网络

Andrew Ng机器学习课程笔记(四)之神经网络 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7365730.html 前言 学习了Andrew Ng课程,开始写了一些笔记,现在写完第5章了,先把这5章的内容放在博客中,后面的内容会陆续更新! 这篇博客主要记录Andrew Ng课程第四章和第五章的神经网络,主要介绍前向传播算法,反向传播算法,神经网络的多类分类,梯度校验,参数随机初始化,参数的更新等等 1.神经网络概述

神经网络学习笔记-入门篇*

1简单模型 vs 复杂模型 对于一个崭新的机器学习的任务,在模型选取和特征向量获取上通常我们会有两种选择方式:a. 简单模型 + 复杂特征项:b. 复杂模型 + 简单特征项.这两种方式各有各的优缺点: 1.1 简单模型 对于简单模型来说,其优点是容易理解.可解释性较强,但是为了达到相对好的预测效果,我们通常会考虑对原始特征进一步抽象,增加大量的特征项来弥补model在处理非线性问题上的缺陷: 假设我们现在有一个学习任务,首先我们抽取出100个不相关的特征属性,然后我们想要尝试使用逻辑回归(LR)

卷积神经网络CNNs的理解与体会

https://blog.csdn.net/shijing_0214/article/details/53143393 孔子说过,温故而知新,时隔俩月再重看CNNs,当时不太了解的地方,又有了新的理解与体会,特此记录下来.文章图片及部分素材均来自网络,侵权请告知. 卷积神经网络(Convolutinal Neural Networks)是非常强大的一种深度神经网络,它在图片的识别分类.NLP句子分类等方面已经获得了巨大的成功,也被广泛使用于工业界,例如谷歌将它用于图片搜索.亚马逊将它用于商品推荐

前馈全连接神经网络和函数逼近、时间序列预测、手写数字识别

https://www.cnblogs.com/conmajia/p/annt-feed-forward-fully-connected-neural-networks.html Andrew Kirillov 著Conmajia 译2019 年 1 月 12 日 原文发表于 CodeProject(2018 年 9 月 28 日). 中文版有小幅修改,已获作者本人授权. 本文介绍了如何使用 ANNT 神经网络库生成前馈全连接神经网络并应用到问题求解. 全文约 12,000 字,建议阅读时间 3

人工智能的神经网络到底是什么?

我们都知道人工智能是在1956年第一次被提出,经过了六七十年的发展,经历了兴起热潮再到衰落,虽然在理论上有些进展,但是并没有什么大的突破,所有的研究都基于1936年数学家图灵制作的现代计算机原型.所以仍然与我们所以知道人工智能有较大的差距. 如果把人工智能按智能水平分类,可以分为三个层次,即弱人工智能强人工智能,和超人工智能.那什么是弱人工智能机器人呢?比如阿尔法狗这类擅长单方面的人工智能,强人工则是那些与人类相当的人工智能,超人工顾名思义就是指全方位超越我们人类的人工智能. 理论上人工智能也分