/*----------------------------------------------------------------------
File : mlp.c
Contents: multilayer perceptron management
Author : Christian Borgelt
History : 2001.02.26 file created
2001.03.10 first version completed
2001.03.18 other weight update methods added
2001.03.19 tabulated logistic function added
2001.04.15 tangens hyperbolicus added as a #define option
2001.04.16 output scaling added
2001.05.20 extended functions added (attribute set/table)
2001.07.17 adapted to modified module scan
2001.09.11 some assertions added
2002.06.04 input scaling/normalization added
2002.06.18 sensitivity analysis functions added
2002.06.19 bug in function mlp_result fixed
2002.09.12 scaling bug concerning numeric targets fixed
2003.01.30 functionality of mlp_reg extended
2003.02.01 bug in mapping binary attributes fixed
2003.02.07 reinitialization added to function mlp_range
2003.03.11 weight parameter added to function mlp_reg
2003.04.01 bug in function mlp_createx fixed
2003.08.06 another bug in mapping binary attributes fixed
2003.10.24 bug in output range determination fixed
2004.02.23 check of parabola orientation added to _quick
2004.08.10 function mlp_deletex added
2004.08.11 adapted to new module attmap
2004.08.12 adapted to new modules parse and nstats
2007.01.10 usage of attribute maps changed
2007.02.14 adapted to modified module attset
2008.04.14 bug in function _quick fixed (parabola orient.)
----------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <assert.h>
#include "mlp.h"
#ifdef STORAGE
#include "storage.h"
#endif
/*----------------------------------------------------------------------
Preprocessor Definitions
----------------------------------------------------------------------*/
/* --- activation function and its derivative --- */
#ifdef MLP_TABFN /* if to use a tabulated function */
#define TABSIZE 1024 /* size of interpolation table */
#define TABMAX 16.0 /* maximum (absolute) argument value */
#define TABSCALE (TABSIZE/TABMAX)
#endif /* scale factor for table indices */
#ifndef MLP_TANH /* default: logistic function */
#define MACTFN(x) (1/(1 +exp(-(x))))
#ifdef MLP_TABFN /* tabulated function */
#define ACTFN(x) _logistic(x)
#else /* direct computation */
#define ACTFN(x) MACTFN(x)
#endif
#define DERIV(x) ((x)*(1-(x)))
#define ACTMIN 0.0 /* minimal and maximal value */
#define ACTMAX 1.0 /* of the activation function */
#define ACTMID 0.5 /* middle value */
#else /* alternative: tangens hyperbolicus */
#define MACTFN(x) (2/(1 +exp(-2*(x))) -1)
#ifdef MLP_TABFN /* tabulated function */
#define ACTFN(x) _tanh(x)
#else /* direct computation */
#define ACTFN(x) MACTFN(x)
#endif
#define DERIV(x) ((1+(x))*(1-(x)))
#define ACTMIN -1.0 /* minimal and maximal value */
#define ACTMAX 1.0 /* of the activation function */
#define ACTMID 0.0 /* middle value */
#endif
#define NAMELEN 255 /* maximum target name length */
/*----------------------------------------------------------------------
Type Definitions
----------------------------------------------------------------------*/
typedef void UPDATEFN (MLP* mlp); /* a weight update function */
/*----------------------------------------------------------------------
Global Variables
----------------------------------------------------------------------*/
#ifdef MLP_TABFN
static double _tab[TABSIZE+1]; /* tabulated activation function */
static int _init = 0; /* whether table is initialized */
#endif
/*----------------------------------------------------------------------
Activation Function
----------------------------------------------------------------------*/
#ifdef MLP_TABFN
#ifndef MLP_TANH
static double _logistic (double x)
{ /* --- table-based logistic function */
int i; /* table index */
double y = fabs(x); /* argument of tabulated function */
if (y >= TABMAX) /* if beyond the table range, */
return (x > 0) ? 1 : 0; /* return asymptotic values */
i = (int)(y *= TABSCALE); /* otherwise compute the table index */
y = _tab[i] + (y -i) * (_tab[i+1] -_tab[i]);
return (x < 0) ? 1-y : y; /* interpolate between table values */
} /* _logistic() */
#else /*--------------------------------------------------------------*/
static double _tanh (double x)
{ /* --- table-based tanh function */
int i; /* table index */
double y = fabs(x); /* argument of tabulated function */
if (y >= TABMAX) /* if beyond the table range, */
return (x > 0) ? 1 : -1; /* return asymptotic values */
i = (int)(y *= TABSCALE); /* otherwise compute the table index */
y = _tab[i] + (y -i) * (_tab[i+1] -_tab[i]);
return (x < 0) ? -y : y; /* interpolate between table values */
} /* _tanh() */
#endif /* #ifndef MLP_TANH */
#endif /* #ifdef MLP_TABFN */
/*----------------------------------------------------------------------
Weight Update Functions
----------------------------------------------------------------------*/
static void _standard (MLP *mlp)
{ /* --- standard backpropagation */
int k; /* loop variable */
double *w, *c, *g; /* to traverse the vectors */
double lrate = mlp->lrate; /* learning rate */
double moment = mlp->moment; /* momentum coefficient */
w = mlp->wgts; /* get the necessary vectors and */
g = mlp->grds; /* traverse the connection weights */
if (moment <= 0) /* if standard backpropagation */
for (k = mlp->wgtcnt; --k >= 0; ) {
w[k] -= lrate *g[k]; g[k] = 0; }
else { /* if backpropagation with momentum */
c = mlp->chgs; /* get the vector of old changes */
for (k = mlp->wgtcnt; --k >= 0; ) {
w[k] += c[k] = moment *c[k] - lrate *g[k]; g[k] = 0; }
} /* update the connection weights */
} /* _standard() */
/*--------------------------------------------------------------------*/
static void _adaptive (MLP *mlp)
{ /* --- super self-adaptive backprop. */
int k; /* loop variable */
double *w, *c, *g, *p; /* to traverse the vectors */
double t; /* temporary buffer */
w = mlp->wgts; c = mlp->chgs; /* get the necessary vectors and */
g = mlp->grds; p = mlp->bufs; /* traverse the connection weights */
for (k = mlp->wgtcnt; --k >= 0; ) {
if (g[k] > 0) t = p[k];
else if (g[k] < 0) t = -p[k];
else t = 0; /* check directions of the changes */
if (t > 0) { /* if gradients have the same sign, */
c[k] *= mlp->growth; /* increase the learning rate */
if (c[k] > mlp->maxchg) c[k] = mlp->maxchg;
p[k] = g[k]; } /* note the current gradient */
else if (t < 0) { /* if gradients have opposite signs, */
c[k] *= mlp->shrink; /* decrease the learning rate */
if (c[k] < mlp->minchg) c[k] = mlp->minchg;
p[k] = 0; } /* suppress a change in the next step */
else { /* if one gra
alvarocfc
- 粉丝: 135
- 资源: 1万+
最新资源
- 多微网优化模型matlab 采用粒子群算法分析两个微网的优化调度,得到蓄电池,发电机以及微网间功率传输,程序有参考资料
- 潮流计算程序matlab 牛拉法 采用matlab对9节点进行潮流计算,采用牛拉法,程序运行可靠
- 微网优化调度matlab 采用matlab+yalmip编制含分布式和储能的微网优化模型,程序采用15分钟为采集节点,利用cplex求解,程序考虑发电机的启停约束,程序运行可靠
- PMSM永磁同步电机仿真三电平SVPWM矢量控制matlab PMSM双环矢量控制传统三电平
- 路径规划人工势场法以及改进人工势场法matlab代码,包含了
- MobaXterm 是一款功能强大且实用的终端仿真器软件.docx
- 三菱FX3U底层源码,PLSR源码 总体功能和指令可能支持在RUN中下载程序,支持注释的写入和读取,支持脉冲输出与定位指令(包括PLSY PWM PLSR PLSV DRVI DRVA 等指令
- Oracle Database Gateways for Win32-11gR2
- python071基于RSA加密算法软件的研究设计
- 成熟量产低压无刷伺服驱动 方案 全套软硬件资料 源码 原理图 需要的直接拿 基于28035平台
- 欧姆龙PLC ST语言6轴伺服RS232C通讯板CP1W-C IF0 真实项目程序,ST语言写的FB块 PLC用是两台CP1H-X40DT-D配置4块RS232C通讯板CP1W-CIF01 触摸屏是N
- 欧姆龙CP1H与力士乐VFC-x610变频器通讯程序功能:原创程序,可直接用于现场程序 欧姆龙CP1H的CIF11通讯板,实现对力士乐VFC-x610变频器 设定频率,控制正反转,读取实际频率,读取
- 级联型电力电子变压器,高压直流MMC控制系统,级联数可选,调 制方式有移相载波,nlm及混合调制,拥有冒泡排序,递归排序等方法,可控制三相不平衡与环流
- 西门子PLC双轴定位算法电池焊接控制程序-S7-1200 +威纶通触摸屏 S7-1200PLC做的电池焊接程序,电池包里面有n*m行列个电池 程序设计灵活SCL语言+梯形图,采用了位置试教与定位路径规
- 变压器副边突然短路simulink仿真
- MATLAB代码:基于模型预测控制的楼宇负荷需求响应研究 关键词:楼宇负荷 空调 模型预测控制 需求响应 仿真平台:MATLAB+CVX平台 主要内容:代码主要做的是一个建筑楼宇的需求响应问题,首
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈