|
|
@ -1,9 +1,7 @@
|
|
|
|
package org.wlld.nerveEntity;
|
|
|
|
package org.wlld.nerveEntity;
|
|
|
|
|
|
|
|
|
|
|
|
import org.wlld.MatrixTools.Matrix;
|
|
|
|
|
|
|
|
import org.wlld.MatrixTools.MatrixOperation;
|
|
|
|
|
|
|
|
import org.wlld.i.ActiveFunction;
|
|
|
|
import org.wlld.i.ActiveFunction;
|
|
|
|
import org.wlld.test.Ma;
|
|
|
|
|
|
|
|
import org.wlld.tools.ArithUtil;
|
|
|
|
import org.wlld.tools.ArithUtil;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.*;
|
|
|
@ -50,21 +48,14 @@ 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 isMatrix) {//该神经元在同层神经元中的编号
|
|
|
|
|
|
|
|
this.id = id;
|
|
|
|
this.id = id;
|
|
|
|
this.upNub = upNub;
|
|
|
|
this.upNub = upNub;
|
|
|
|
this.name = name;
|
|
|
|
this.name = name;
|
|
|
|
this.downNub = downNub;
|
|
|
|
this.downNub = downNub;
|
|
|
|
this.studyPoint = studyPoint;
|
|
|
|
this.studyPoint = studyPoint;
|
|
|
|
this.activeFunction = activeFunction;
|
|
|
|
this.activeFunction = activeFunction;
|
|
|
|
if (init) {
|
|
|
|
initPower(init);//生成随机权重
|
|
|
|
if (isMatrix) {
|
|
|
|
|
|
|
|
initKernel();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
initPower();//生成随机权重
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void sendMessage(long enevtId, double parameter, boolean isStudy, Map<Integer, Double> E) throws Exception {
|
|
|
|
public void sendMessage(long enevtId, double parameter, boolean isStudy, Map<Integer, Double> E) throws Exception {
|
|
|
@ -77,18 +68,6 @@ public abstract class Nerve {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//正向传播矩阵
|
|
|
|
|
|
|
|
public void sendMatrixMessage(long enevtId, Matrix parameter, boolean isStudy,
|
|
|
|
|
|
|
|
double E) throws Exception {
|
|
|
|
|
|
|
|
if (son.size() > 0) {
|
|
|
|
|
|
|
|
for (Nerve nerve : son) {
|
|
|
|
|
|
|
|
nerve.inputMatrix(enevtId, parameter, isStudy, E);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
throw new Exception("this layer is lastIndex");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void backSendMessage(long eventId) throws Exception {//反向传播
|
|
|
|
private void backSendMessage(long eventId) throws Exception {//反向传播
|
|
|
|
if (fathor.size() > 0) {
|
|
|
|
if (fathor.size() > 0) {
|
|
|
|
for (int i = 0; i < fathor.size(); i++) {
|
|
|
|
for (int i = 0; i < fathor.size(); i++) {
|
|
|
@ -102,12 +81,6 @@ public abstract class Nerve {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//接收矩阵参数
|
|
|
|
|
|
|
|
protected void inputMatrix(long eventId, Matrix parameter, boolean isStudy
|
|
|
|
|
|
|
|
, double E) throws Exception {//输入
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void backGetMessage(double parameter, long eventId) throws Exception {//反向传播
|
|
|
|
private void backGetMessage(double parameter, long eventId) throws Exception {//反向传播
|
|
|
|
backNub++;
|
|
|
|
backNub++;
|
|
|
|
sigmaW = ArithUtil.add(sigmaW, parameter);
|
|
|
|
sigmaW = ArithUtil.add(sigmaW, parameter);
|
|
|
@ -135,6 +108,7 @@ public abstract class Nerve {
|
|
|
|
double wp = ArithUtil.mul(bn, h);//编号为KEY的上层隐层神经元权重的变化值
|
|
|
|
double wp = ArithUtil.mul(bn, h);//编号为KEY的上层隐层神经元权重的变化值
|
|
|
|
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);
|
|
|
|
wg.put(key, dm);//保存上一层权重与梯度的积
|
|
|
|
wg.put(key, dm);//保存上一层权重与梯度的积
|
|
|
|
dendrites.put(key, w);//保存修正结果
|
|
|
|
dendrites.put(key, w);//保存修正结果
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -157,14 +131,6 @@ public abstract class Nerve {
|
|
|
|
return allReady;
|
|
|
|
return allReady;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected void initFeatures(long eventId) {//初始化九个参数和
|
|
|
|
|
|
|
|
List<Double> list = new ArrayList<>();
|
|
|
|
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
|
|
|
|
list.add(0.0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
features.put(eventId, list);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void destoryParameter(long eventId) {//销毁参数
|
|
|
|
protected void destoryParameter(long eventId) {//销毁参数
|
|
|
|
features.remove(eventId);
|
|
|
|
features.remove(eventId);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -183,73 +149,23 @@ public abstract class Nerve {
|
|
|
|
return ArithUtil.sub(sigma, threshold);
|
|
|
|
return ArithUtil.sub(sigma, threshold);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//进行卷积运算
|
|
|
|
private void initPower(boolean init) {//初始化权重及阈值
|
|
|
|
protected Matrix convolution(Matrix matrix, long eventId, boolean isStudy) throws Exception {
|
|
|
|
|
|
|
|
Matrix kernel = getKernel();
|
|
|
|
|
|
|
|
int xn = matrix.getX();
|
|
|
|
|
|
|
|
int yn = matrix.getY();
|
|
|
|
|
|
|
|
int x = xn / 3;//求导后矩阵的行数
|
|
|
|
|
|
|
|
int y = yn / 3;//求导后矩阵的列数
|
|
|
|
|
|
|
|
Matrix myMatrix = new Matrix(x, y);//最终合成矩阵
|
|
|
|
|
|
|
|
for (int i = 0; i < xn - 3; i += 3) {//遍历行
|
|
|
|
|
|
|
|
for (int j = 0; j < yn - 3; j += 3) {//遍历每行的列
|
|
|
|
|
|
|
|
//进行卷积运算
|
|
|
|
|
|
|
|
double dm = MatrixOperation.convolution(matrix, kernel, i, j);
|
|
|
|
|
|
|
|
if (isStudy) {//如果是学习的话,拿到分块矩阵,对对应权重的值进行求和
|
|
|
|
|
|
|
|
Matrix matrix1 = matrix.getSonOfMatrix(i, j, 3, 3);
|
|
|
|
|
|
|
|
sigma(matrix1, eventId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dm > 0) {//存在边缘
|
|
|
|
|
|
|
|
myMatrix.setNub(i, j, dm);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return myMatrix;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//对每一项进行求和,只有在学习的时候执行它
|
|
|
|
|
|
|
|
private void sigma(Matrix matrix, long eventId) throws Exception {
|
|
|
|
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
List<Double> list = features.get(eventId);
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
|
|
|
|
for (int j = 0; j < 3; j++) {
|
|
|
|
|
|
|
|
list.set(n, ArithUtil.add(list.get(n), matrix.getNumber(i, j)));
|
|
|
|
|
|
|
|
n++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Matrix getKernel() throws Exception {
|
|
|
|
|
|
|
|
Matrix kernel = new Matrix(3, 3);
|
|
|
|
|
|
|
|
for (int i = 1; i < 10; i++) {
|
|
|
|
|
|
|
|
double w = dendrites.get(i);
|
|
|
|
|
|
|
|
int t = i - 1;
|
|
|
|
|
|
|
|
//将权重填入卷积核当中
|
|
|
|
|
|
|
|
int x = t / 3;
|
|
|
|
|
|
|
|
int y = t % 3;
|
|
|
|
|
|
|
|
kernel.setNub(x, y, w);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return kernel;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void initPower() {//初始化权重及阈值
|
|
|
|
|
|
|
|
if (upNub > 0) {
|
|
|
|
if (upNub > 0) {
|
|
|
|
Random random = new Random();
|
|
|
|
Random random = new Random();
|
|
|
|
for (int i = 1; i < upNub + 1; i++) {
|
|
|
|
for (int i = 1; i < upNub + 1; i++) {
|
|
|
|
dendrites.put(i, random.nextDouble());//random.nextDouble()
|
|
|
|
double nub = 0;
|
|
|
|
|
|
|
|
if (init) {
|
|
|
|
|
|
|
|
nub = random.nextDouble();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//生成随机阈值
|
|
|
|
dendrites.put(i, nub);//random.nextDouble()
|
|
|
|
threshold = random.nextDouble();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//生成随机阈值
|
|
|
|
|
|
|
|
double nub = 0;
|
|
|
|
|
|
|
|
if (init) {
|
|
|
|
|
|
|
|
nub = random.nextDouble();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
threshold = nub;
|
|
|
|
public void initKernel() {//对卷积核进行初始化
|
|
|
|
|
|
|
|
Random random = new Random();
|
|
|
|
|
|
|
|
for (int i = 1; i < 10; i++) {
|
|
|
|
|
|
|
|
dendrites.put(i, random.nextDouble());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//生成随机阈值
|
|
|
|
|
|
|
|
threshold = random.nextDouble();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int getId() {
|
|
|
|
public int getId() {
|
|
|
|