增加打印参数

pull/10/head
lidapeng 5 years ago
parent 2907b78ff8
commit eb92d8c8ef

@ -0,0 +1,12 @@
package org.wlld.config;
/**
* @author lidapeng
* @description
* @date 8:55 2020/3/21
*/
public class RZ {
public static final int NOT_RZ = 0;
public static final int L1 = 1;
public static final int L2 = 2;
}

@ -3,6 +3,7 @@ package org.wlld.imageRecognition;
import org.wlld.MatrixTools.Matrix; import org.wlld.MatrixTools.Matrix;
import org.wlld.MatrixTools.MatrixOperation; import org.wlld.MatrixTools.MatrixOperation;
import org.wlld.config.Classifier; import org.wlld.config.Classifier;
import org.wlld.config.RZ;
import org.wlld.config.StudyPattern; import org.wlld.config.StudyPattern;
import org.wlld.function.ReLu; import org.wlld.function.ReLu;
import org.wlld.function.Sigmod; import org.wlld.function.Sigmod;
@ -54,6 +55,17 @@ public class TempleConfig {
private ActiveFunction activeFunction = new Tanh(); private ActiveFunction activeFunction = new Tanh();
private double studyPoint = 0; private double studyPoint = 0;
private double matrixWidth = 1;//期望矩阵间隔 private double matrixWidth = 1;//期望矩阵间隔
private int rzType = RZ.NOT_RZ;//正则化类型,默认不进行正则化
private double lParam = 0;//正则参数
private int hiddenNerveNub = 9;//隐层神经元个数
public void setRzType(int rzType) {
this.rzType = rzType;
}
public void setlParam(double lParam) {
this.lParam = lParam;
}
public void setMatrixWidth(double matrixWidth) { public void setMatrixWidth(double matrixWidth) {
this.matrixWidth = matrixWidth; this.matrixWidth = matrixWidth;
@ -120,7 +132,7 @@ public class TempleConfig {
return convolutionNerveManagerB; return convolutionNerveManagerB;
} }
public void finishStudy() throws Exception {//结束 public void finishStudy() throws Exception {//结束学习
switch (classifier) { switch (classifier) {
case Classifier.LVQ: case Classifier.LVQ:
lvq.start(); lvq.start();
@ -267,8 +279,9 @@ public class TempleConfig {
private void initNerveManager(boolean initPower, int sensoryNerveNub private void initNerveManager(boolean initPower, int sensoryNerveNub
, int deep, double studyPoint) throws Exception { , int deep, double studyPoint) throws Exception {
nerveManager = new NerveManager(sensoryNerveNub, 6, nerveManager = new NerveManager(sensoryNerveNub, hiddenNerveNub,
classificationNub, deep, activeFunction, false, isAccurate, studyPoint); classificationNub, deep, activeFunction,
false, isAccurate, studyPoint, rzType, lParam);
nerveManager.init(initPower, false, isShowLog); nerveManager.init(initPower, false, isShowLog);
} }
@ -320,7 +333,8 @@ public class TempleConfig {
private NerveManager initNerveManager(Map<Integer, Matrix> matrixMap, boolean initPower, int deep) throws Exception { private NerveManager initNerveManager(Map<Integer, Matrix> matrixMap, boolean initPower, int deep) throws Exception {
//初始化卷积神经网络 //初始化卷积神经网络
NerveManager convolutionNerveManager = new NerveManager(1, 1, NerveManager convolutionNerveManager = new NerveManager(1, 1,
1, deep - 1, new ReLu(), true, isAccurate, studyPoint); 1, deep - 1, new ReLu(),
true, isAccurate, studyPoint, rzType, lParam);
convolutionNerveManager.setMatrixMap(matrixMap);//给卷积网络管理器注入期望矩阵 convolutionNerveManager.setMatrixMap(matrixMap);//给卷积网络管理器注入期望矩阵
convolutionNerveManager.init(initPower, true, isShowLog); convolutionNerveManager.init(initPower, true, isShowLog);
return convolutionNerveManager; return convolutionNerveManager;

@ -31,6 +31,8 @@ public class NerveManager {
private boolean isDynamic;//是否是动态神经网络 private boolean isDynamic;//是否是动态神经网络
private List<Double> studyList = new ArrayList<>(); private List<Double> studyList = new ArrayList<>();
private boolean isAccurate;//是否保留精度 private boolean isAccurate;//是否保留精度
private int rzType;//正则化类型,默认不进行正则化
private double lParam;//正则参数
public List<Double> getStudyList() {//查看每一次的学习率 public List<Double> getStudyList() {//查看每一次的学习率
return studyList; return studyList;
@ -194,11 +196,13 @@ public class NerveManager {
* @param activeFunction * @param activeFunction
* @param isDynamic * @param isDynamic
* @param isAccurate * @param isAccurate
* @param rzType
* @param lParam
* @throws Exception * @throws Exception
*/ */
public NerveManager(int sensoryNerveNub, int hiddenNerveNub, int outNerveNub public NerveManager(int sensoryNerveNub, int hiddenNerveNub, int outNerveNub
, int hiddenDepth, ActiveFunction activeFunction, boolean isDynamic, boolean isAccurate, , int hiddenDepth, ActiveFunction activeFunction, boolean isDynamic, boolean isAccurate,
double studyPoint) throws Exception { double studyPoint, int rzType, double lParam) throws Exception {
if (sensoryNerveNub > 0 && hiddenNerveNub > 0 && outNerveNub > 0 && hiddenDepth > 0 && activeFunction != null) { if (sensoryNerveNub > 0 && hiddenNerveNub > 0 && outNerveNub > 0 && hiddenDepth > 0 && activeFunction != null) {
this.hiddenNerveNub = hiddenNerveNub; this.hiddenNerveNub = hiddenNerveNub;
this.sensoryNerveNub = sensoryNerveNub; this.sensoryNerveNub = sensoryNerveNub;
@ -207,6 +211,8 @@ public class NerveManager {
this.activeFunction = activeFunction; this.activeFunction = activeFunction;
this.isDynamic = isDynamic; this.isDynamic = isDynamic;
this.isAccurate = isAccurate; this.isAccurate = isAccurate;
this.rzType = rzType;
this.lParam = lParam;
if (studyPoint > 0 && studyPoint < 1) { if (studyPoint > 0 && studyPoint < 1) {
this.studyPoint = studyPoint; this.studyPoint = studyPoint;
} }
@ -259,7 +265,7 @@ public class NerveManager {
//初始化输出神经元 //初始化输出神经元
for (int i = 1; i < outNerveNub + 1; i++) { for (int i = 1; i < outNerveNub + 1; i++) {
OutNerve outNerve = new OutNerve(i, hiddenNerveNub, 0, studyPoint, initPower, OutNerve outNerve = new OutNerve(i, hiddenNerveNub, 0, studyPoint, initPower,
activeFunction, isMatrix, isAccurate, isShowLog); activeFunction, isMatrix, isAccurate, isShowLog, rzType, lParam);
if (isMatrix) {//是卷积层神经网络 if (isMatrix) {//是卷积层神经网络
outNerve.setMatrixMap(matrixMap); outNerve.setMatrixMap(matrixMap);
} }
@ -306,7 +312,7 @@ public class NerveManager {
downNub = hiddenNerveNub; downNub = hiddenNerveNub;
} }
HiddenNerve hiddenNerve = new HiddenNerve(j, i + 1, upNub, downNub, studyPoint, initPower, activeFunction, isMatrix HiddenNerve hiddenNerve = new HiddenNerve(j, i + 1, upNub, downNub, studyPoint, initPower, activeFunction, isMatrix
, isAccurate); , isAccurate, rzType, lParam);
hiddenNerveList.add(hiddenNerve); hiddenNerveList.add(hiddenNerve);
} }
depthNerves.add(hiddenNerveList); depthNerves.add(hiddenNerveList);

@ -17,8 +17,10 @@ public class HiddenNerve extends Nerve {
private int depth;//所处深度 private int depth;//所处深度
public HiddenNerve(int id, int depth, int upNub, int downNub, double studyPoint, public HiddenNerve(int id, int depth, int upNub, int downNub, double studyPoint,
boolean init, ActiveFunction activeFunction, boolean isDynamic, boolean isAccurate) throws Exception {//隐层神经元 boolean init, ActiveFunction activeFunction, boolean isDynamic,
super(id, upNub, "HiddenNerve", downNub, studyPoint, init, activeFunction, isDynamic, isAccurate); boolean isAccurate, int rzType, double lParam) throws Exception {//隐层神经元
super(id, upNub, "HiddenNerve", downNub, studyPoint,
init, activeFunction, isDynamic, isAccurate, rzType, lParam);
this.depth = depth; this.depth = depth;
} }

@ -3,6 +3,7 @@ package org.wlld.nerveEntity;
import org.wlld.MatrixTools.Matrix; import org.wlld.MatrixTools.Matrix;
import org.wlld.MatrixTools.MatrixOperation; import org.wlld.MatrixTools.MatrixOperation;
import org.wlld.config.RZ;
import org.wlld.i.ActiveFunction; import org.wlld.i.ActiveFunction;
import org.wlld.i.OutBack; import org.wlld.i.OutBack;
import org.wlld.tools.ArithUtil; import org.wlld.tools.ArithUtil;
@ -35,6 +36,8 @@ public abstract class Nerve {
private int backNub = 0;//当前节点被反向传播的次数 private int backNub = 0;//当前节点被反向传播的次数
protected ActiveFunction activeFunction; protected ActiveFunction activeFunction;
private boolean isAccurate = false;//是否保留精度 private boolean isAccurate = false;//是否保留精度
private int rzType;//正则化类型,默认不进行正则化
private double lParam;//正则参数
public Map<Integer, Double> getDendrites() { public Map<Integer, Double> getDendrites() {
return dendrites; return dendrites;
@ -66,7 +69,7 @@ public abstract class Nerve {
protected Nerve(int id, int upNub, String name, int downNub, protected Nerve(int id, int upNub, String name, int downNub,
double studyPoint, boolean init, ActiveFunction activeFunction double studyPoint, boolean init, ActiveFunction activeFunction
, boolean isDynamic, boolean isAccurate) throws Exception {//该神经元在同层神经元中的编号 , boolean isDynamic, boolean isAccurate, int rzType, double lParam) throws Exception {//该神经元在同层神经元中的编号
this.id = id; this.id = id;
this.upNub = upNub; this.upNub = upNub;
this.name = name; this.name = name;
@ -74,8 +77,9 @@ public abstract class Nerve {
this.studyPoint = studyPoint; this.studyPoint = studyPoint;
this.activeFunction = activeFunction; this.activeFunction = activeFunction;
this.isAccurate = isAccurate; this.isAccurate = isAccurate;
this.rzType = rzType;
this.lParam = lParam;
initPower(init, isDynamic);//生成随机权重 initPower(init, isDynamic);//生成随机权重
} }
protected void setStudyPoint(double studyPoint) { protected void setStudyPoint(double studyPoint) {
@ -221,15 +225,32 @@ public abstract class Nerve {
backSendMessage(eventId); backSendMessage(eventId);
} }
private double regularization(double w, double param) {//正则化类型
double re = 0.0;
if (rzType != RZ.NOT_RZ) {
if (rzType == RZ.L2) {
re = ArithUtil.mul(param, -w);
} else if (rzType == RZ.L1) {
if (w > 0) {
re = -param;
} else if (w < 0) {
re = param;
}
}
}
return re;
}
private void updateW(double h, long eventId) {//h是学习率 * 当前g梯度 private void updateW(double h, long eventId) {//h是学习率 * 当前g梯度
List<Double> list = features.get(eventId); List<Double> list = features.get(eventId);
double stop = ArithUtil.sub(1, ArithUtil.div(ArithUtil.mul(studyPoint, 0.015), dendrites.size())); double param = ArithUtil.div(ArithUtil.mul(studyPoint, lParam), dendrites.size());
for (Map.Entry<Integer, Double> entry : dendrites.entrySet()) { for (Map.Entry<Integer, Double> entry : dendrites.entrySet()) {
int key = entry.getKey();//上层隐层神经元的编号 int key = entry.getKey();//上层隐层神经元的编号
double w = entry.getValue();//接收到编号为KEY的上层隐层神经元的权重 double w = entry.getValue();//接收到编号为KEY的上层隐层神经元的权重
double bn = list.get(key - 1);//接收到编号为KEY的上层隐层神经元的输入 double bn = list.get(key - 1);//接收到编号为KEY的上层隐层神经元的输入
double wp = ArithUtil.mul(bn, h);//编号为KEY的上层隐层神经元权重的变化值 double wp = ArithUtil.mul(bn, h);//编号为KEY的上层隐层神经元权重的变化值
w = ArithUtil.mul(w, stop); double regular = regularization(w, param);//正则化抑制权重s
w = ArithUtil.add(w, regular);
w = ArithUtil.add(w, wp);//修正后的编号为KEY的上层隐层神经元权重 w = ArithUtil.add(w, wp);//修正后的编号为KEY的上层隐层神经元权重
double dm = ArithUtil.mul(w, gradient);//返回给相对应的神经元 double dm = ArithUtil.mul(w, gradient);//返回给相对应的神经元
// System.out.println("allG==" + allG + ",dm==" + dm); // System.out.println("allG==" + allG + ",dm==" + dm);

@ -23,8 +23,9 @@ public class OutNerve extends Nerve {
public OutNerve(int id, int upNub, int downNub, double studyPoint, boolean init, public OutNerve(int id, int upNub, int downNub, double studyPoint, boolean init,
ActiveFunction activeFunction, boolean isDynamic, boolean isAccurate ActiveFunction activeFunction, boolean isDynamic, boolean isAccurate
, boolean isShowLog) throws Exception { , boolean isShowLog, int rzType, double lParam) throws Exception {
super(id, upNub, "OutNerve", downNub, studyPoint, init, activeFunction, isDynamic, isAccurate); super(id, upNub, "OutNerve", downNub, studyPoint, init,
activeFunction, isDynamic, isAccurate, rzType, lParam);
this.isShowLog = isShowLog; this.isShowLog = isShowLog;
} }

@ -15,7 +15,8 @@ import java.util.Map;
public class SensoryNerve extends Nerve { public class SensoryNerve extends Nerve {
public SensoryNerve(int id, int upNub) throws Exception { public SensoryNerve(int id, int upNub) throws Exception {
super(id, upNub, "SensoryNerve", 0, 0.1, false, null, false, false); super(id, upNub, "SensoryNerve", 0, 0.1, false,
null, false, false, 0, 0);
} }
/** /**

@ -1,6 +1,7 @@
package org.wlld; package org.wlld;
import org.wlld.MatrixTools.Matrix; import org.wlld.MatrixTools.Matrix;
import org.wlld.config.RZ;
import org.wlld.function.Sigmod; import org.wlld.function.Sigmod;
import org.wlld.i.OutBack; import org.wlld.i.OutBack;
import org.wlld.nerveCenter.NerveManager; import org.wlld.nerveCenter.NerveManager;
@ -32,7 +33,8 @@ public class NerveDemo1 {
* @param activeFunction * @param activeFunction
* @param isDynamic * @param isDynamic
*/ */
NerveManager nerveManager = new NerveManager(2, 6, 1, 4, new Sigmod(), false, true, 0); NerveManager nerveManager = new NerveManager(2, 6, 1, 4, new Sigmod(),
false, true, 0, RZ.NOT_RZ, 0);
nerveManager.init(true, false, false); nerveManager.init(true, false, false);
@ -108,7 +110,8 @@ public class NerveDemo1 {
public static void test3() throws Exception { public static void test3() throws Exception {
NerveManager nerveManager = new NerveManager(3, 6, 3 NerveManager nerveManager = new NerveManager(3, 6, 3
, 3, new Sigmod(), false, true, 0); , 3, new Sigmod(),
false, true, 0, RZ.NOT_RZ, 0);
nerveManager.init(true, false, false);//初始化 nerveManager.init(true, false, false);//初始化
List<Map<Integer, Double>> data = new ArrayList<>();//正样本 List<Map<Integer, Double>> data = new ArrayList<>();//正样本
List<Map<Integer, Double>> dataB = new ArrayList<>();//负样本 List<Map<Integer, Double>> dataB = new ArrayList<>();//负样本

Loading…
Cancel
Save