自适应线性神经网络Adaptive linear network, 是神经网络的入门级别网络。

相对于感知器,

  1. 采用了f(z)=z的激活函数,属于连续函数。
  2. 代价函数为LMS函数,最小均方算法,Least mean square。

实现上,采用随机梯度下降,由于更新的随机性,运行多次结果是不同的。

 '''
Adaline classifier created on 2019.9.14
author: vince
'''
import pandas
import math
import numpy
import logging
import random
import matplotlib.pyplot as plt from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score '''
Adaline classifier Attributes
w: ld-array = weights after training
l: list = number of misclassification during each iteration
'''
class Adaline:
def __init__(self, eta = 0.001, iter_num = 500, batch_size = 1):
'''
eta: float = learning rate (between 0.0 and 1.0).
iter_num: int = iteration over the training dataset.
batch_size: int = gradient descent batch number,
if batch_size == 1, used SGD;
if batch_size == 0, use BGD;
else MBGD;
''' self.eta = eta;
self.iter_num = iter_num;
self.batch_size = batch_size; def train(self, X, Y):
'''
train training data.
X:{array-like}, shape=[n_samples, n_features] = Training vectors,
where n_samples is the number of training samples and
n_features is the number of features.
Y:{array-like}, share=[n_samples] = traget values.
'''
self.w = numpy.zeros(1 + X.shape[1]);
self.l = numpy.zeros(self.iter_num);
for iter_index in range(self.iter_num):
for rand_time in range(X.shape[0]):
sample_index = random.randint(0, X.shape[0] - 1);
if (self.activation(X[sample_index]) == Y[sample_index]):
continue;
output = self.net_input(X[sample_index]);
errors = Y[sample_index] - output;
self.w[0] += self.eta * errors;
self.w[1:] += self.eta * numpy.dot(errors, X[sample_index]);
break;
for sample_index in range(X.shape[0]):
self.l[iter_index] += (Y[sample_index] - self.net_input(X[sample_index])) ** 2 * 0.5;
logging.info("iter %s: w0(%s), w1(%s), w2(%s), l(%s)" %
(iter_index, self.w[0], self.w[1], self.w[2], self.l[iter_index]));
if iter_index > 1 and math.fabs(self.l[iter_index - 1] - self.l[iter_index]) < 0.0001:
break; def activation(self, x):
return numpy.where(self.net_input(x) >= 0.0 , 1 , -1); def net_input(self, x):
return numpy.dot(x, self.w[1:]) + self.w[0]; def predict(self, x):
return self.activation(x); def main():
logging.basicConfig(level = logging.INFO,
format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt = '%a, %d %b %Y %H:%M:%S'); iris = load_iris(); features = iris.data[:99, [0, 2]];
# normalization
features_std = numpy.copy(features);
for i in range(features.shape[1]):
features_std[:, i] = (features_std[:, i] - features[:, i].mean()) / features[:, i].std(); labels = numpy.where(iris.target[:99] == 0, -1, 1); # 2/3 data from training, 1/3 data for testing
train_features, test_features, train_labels, test_labels = train_test_split(
features_std, labels, test_size = 0.33, random_state = 23323); logging.info("train set shape:%s" % (str(train_features.shape))); classifier = Adaline(); classifier.train(train_features, train_labels); test_predict = numpy.array([]);
for feature in test_features:
predict_label = classifier.predict(feature);
test_predict = numpy.append(test_predict, predict_label); score = accuracy_score(test_labels, test_predict);
logging.info("The accruacy score is: %s "% (str(score))); #plot
x_min, x_max = train_features[:, 0].min() - 1, train_features[:, 0].max() + 1;
y_min, y_max = train_features[:, 1].min() - 1, train_features[:, 1].max() + 1;
plt.xlim(x_min, x_max);
plt.ylim(y_min, y_max);
plt.xlabel("width");
plt.ylabel("heigt"); plt.scatter(train_features[:, 0], train_features[:, 1], c = train_labels, marker = 'o', s = 10); k = - classifier.w[1] / classifier.w[2];
d = - classifier.w[0] / classifier.w[2]; plt.plot([x_min, x_max], [k * x_min + d, k * x_max + d], "go-"); plt.show(); if __name__ == "__main__":
main();

自适应线性神经网络Adaline的更多相关文章

  1. python机器学习——自适应线性神经元

    上篇博客我们说了感知器,这篇博客主要记录自适应线性神经元的实现算法及一些其他的训练细节,自适应线性神经元(简称为Adaline)由Bernard Widrow和他的博士生Tedd Hoff提出,对感知 ...

  2. 神经网络_线性神经网络 2 (Nerual Network_Linear Nerual Network 2)

    1 LMS 学习规则 1.1 LMS学习规则定义 MSE=(1/Q)*Σe2k=(1/Q)*Σ(tk-ak)2,k=1,2,...,Q 式中:Q是训练样本:t(k)是神经元的期望输出:a(k)是神经元 ...

  3. 神经网络_线性神经网络 1 (Nerual Network_Linear Nerual Network 1)

    2019-04-08 16:59:23 1 学习规则(Learning Rule) 1.1 赫布学习规则(Hebb Learning Rule) 1949年,Hebb提出了关于神经网络学习机理的“突触 ...

  4. 单层感知机_线性神经网络_BP神经网络

    单层感知机 单层感知机基础总结很详细的博客 关于单层感知机的视频 最终y=t,说明经过训练预测值和真实值一致.下面图是sign函数 根据感知机规则实现的上述题目的代码 import numpy as ...

  5. 使用MindSpore的线性神经网络拟合非线性函数

    技术背景 在前面的几篇博客中,我们分别介绍了MindSpore的CPU版本在Docker下的安装与配置方案.MindSpore的线性函数拟合以及MindSpore后来新推出的GPU版本的Docker编 ...

  6. MATLAB——线性神经网络

     这个函数默认使用最小二乘,所以不需要训练 % example5_1.m x=-:; y=*x-; % 直线方程为 randn(); % 设置种子,便于重复执行 y=y+randn(,length(y ...

  7. 神经网络_线性神经网络 3 (Nerual Network_Linear Nerual Network 3)

    1 LMS 学习规则_解方程组 1.1 LMS学习规则举例 X1=[0 0 1]T,t1=0:X2=[1 0 1]T,t2=0:X3=[0 1 1]T,t3=0:X1=[1 1 1]T,t1=1. 设 ...

  8. (转)神经网络和深度学习简史(第一部分):从感知机到BP算法

    深度|神经网络和深度学习简史(第一部分):从感知机到BP算法 2016-01-23 机器之心 来自Andrey Kurenkov 作者:Andrey Kurenkov 机器之心编译出品 参与:chen ...

  9. 机器学习 —— 基础整理(六)线性判别函数:感知器、松弛算法、Ho-Kashyap算法

    这篇总结继续复习分类问题.本文简单整理了以下内容: (一)线性判别函数与广义线性判别函数 (二)感知器 (三)松弛算法 (四)Ho-Kashyap算法 闲话:本篇是本系列[机器学习基础整理]在time ...

随机推荐

  1. phpstudy渗透到服务器

    0x00 目标站点www.test.ichunqiu 0x01 尝试登陆系统 -尝试弱密码登陆 结果:forbidden!!! -尝试万能账号密码登陆 1‘ or 1=1--+ 和 1‘ or 1=1 ...

  2. JUC常用同步工具类——CountDownLatch,CyclicBarrier,Semaphore

    在 JUC 下包含了一些常用的同步工具类,今天就来详细介绍一下,CountDownLatch,CyclicBarrier,Semaphore 的使用方法以及它们之间的区别. 一.CountDownLa ...

  3. 基于abp框架的数据库种子数据初始化

    目录 基于abp框架的数据库种子数据初始化 1.背景 2.参照 3.解决方案 3.1 初始化数据 3.2 依赖注入方法容器里获取数据库上下文 3.3 封装创建初始化数据列表方法 3.4 数据库中没有的 ...

  4. Python 获取MySql某个表所有字段名

    在使用python导出数据库中数据的时候,往往除了插入的数据以外,还有表字段等信息需要导出,查阅了资料后发现了2种方法 第一种:在mysql自带的表里查询,这个表保存了每张表的字段信息,可以用pymy ...

  5. 初探Linux

    这是一个小小新手根据自己对Linux的理解而写下的笔记,记录的是大体的学习内容.记录的笔记不全面,甚至没有整体的概念,但也希望能够给部分人一些入门的帮助,实机基于CentOS 7. 导语:学习一件新事 ...

  6. LeetCode#15 | Three Sum 三数之和

    一.题目 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意:答案中不可以包含 ...

  7. 对时间进行操作 加减 XXX天

    date:要操作的时间目标 days: 要加减的天数时间 addDate: function(date, days) { if (date == '') { return '' }; if (days ...

  8. 记一次修改mysql密码

    因为马大哈的原因,没有记录自己服务器mysql的密码,试来试去试不出来只好选择重置密码. 直接上命令: 首先 vim /etc/my.cnf 在my.cnf文件中,在[mysqld]的段中加上 ski ...

  9. watch 同步表单 记得$nextTick,否则不会同步更新到组件内

    watch 同步表单 记得$nextTick,否则不会同步更新到组件内 watch: { 'formData.aaa' (val) { this.$nextTick(() => { this.f ...

  10. 【分布式锁】03-使用Redisson实现RedLock原理

    前言 前面已经学习了Redission可重入锁以及公平锁的原理,接着看看Redission是如何来实现RedLock的. RedLock原理 RedLock是基于redis实现的分布式锁,它能够保证以 ...