📄 bpn.h
字号:
/*
Copyright (C) 2001 Tsinghuaeolus
Authors : ChenJiang, YaoJinyi, CaiYunpeng, Lishi
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
If you make any changes or have any comments we would appreciate a
message to yjy01@mails.tsinghua.edu.cn.
*/
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef _BPN
#define _BPN
#include "types.h"
enum FuncType{purelin,logsig,tansig};
enum TrainType{adapt, batch};
typedef double REAL;
#define _BPN
class BPN;
class LAYER{
private:
int Units;
REAL* Output;
REAL* Error;
REAL** Weight;
FuncType functype;
REAL** dWeight; /*weight deltas */
REAL** last_dWeight; /* last weight deltas for momentum */
REAL Eta;
REAL Alpha;
public:
void SetLearningRate(REAL lr);
void SetMomentum(REAL mc);
void SetFuncType(FuncType functype);
friend class BPN;
};
class BPN{
private:
LAYER* InputLayer;
LAYER* OutputLayer;
int NUM_LAYERS;
int INPUT_SIZE;
int OUTPUT_SIZE;
REAL BIAS;
REAL Gain; /* - gain of sigmoid function */
REAL Error; /* - total net error */
REAL sigmoid(REAL Input);
REAL dsigmoid(REAL Out);
REAL purelinear(REAL Input);
REAL dpurelinear(REAL Out);
REAL tanh(REAL Input);
REAL dtanh(REAL Out);
REAL Input_HI, Input_LO;
REAL Input_MAX, Input_MIN;
REAL Target_HI, Target_LO;
REAL Target_MAX, Target_MIN;
int epoch;
int batch_period;
TrainType traintype;
public:
LAYER* Layers;
void Randomize();
void RandomWeights(REAL Low = -0.5,REAL High = 0.5);
int RandomInt(int Low, int High);
int RandomInt(int High);
BPN(int NUM_LAYERS = 3, int* LAYER_SIZE = NULL, FuncType* functype = NULL, bool BIASED = true);
void construct(int NUM_LAYERS, int* Units);
~BPN();
void destroy();
void SetInput(REAL* Input);
void GetOutput(REAL* Output);
void PropagateLayer(int Lower,int Upper);
void PropagateNet();
void ComputeOutputError(REAL* Desire);
void SetOutputError(REAL* Error);
void GetInputError(REAL* Error);
void BackpropagateLayer(int Lower,int Upper);
void BackpropagateNet();
void AdjustWeights();
void SimulateNet(REAL* Input, REAL* Output);
void SimulateNet(int num_samples, REAL* Input, REAL* Output);
void ForwardBack(REAL* Input, REAL* Desire);
void GeneratedWeights();
void Adapt(int num_samples, REAL* Input, REAL* Desire);
void BatchTrain(int num_samples, REAL* Input, REAL* Desire);
void TestNet(REAL* Input, REAL* Desire);
REAL TestNet(int num_samples, REAL* Input, REAL* Desire);
void TrainNet(int num_samples, REAL* Input, REAL* Desire);
bool SetFunctype(FuncType functype, int layer);
void SetTraintype(TrainType traintype);
void SetEpoch(int epoch);
void SetBatchPeriod(int period);
void SetBIAS(REAL BIAS);
void SetGain(REAL gain);
void SetLearningRate(REAL lr);
void SetMomentum(REAL mc);
bool SetInputRange(REAL min, REAL max);
bool SetTargetRange(REAL min, REAL max);
bool SetInsideRange(REAL input_min, REAL input_max, REAL target_min, REAL target_max);
void Normalize(int num_samples, REAL* Input, REAL* Desire);
void DeNormalize(int num_samples, REAL* Output);
void ResetdWeights();
bool SaveNet(char* filename);
bool RestoreNet(char* filename, int mode = 0);
bool RestoreNetFromString(char*string);
REAL GetError();
REAL Normalize_Input(REAL y);
REAL DeNormalize_Input(REAL y);
REAL Normalize_Target(REAL y);
REAL DeNormalize_Target(REAL y);
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -