mirror of
https://gitee.com/dromara/easyAi.git
synced 2024-12-01 19:28:29 +08:00
提交被忽略的文件
This commit is contained in:
parent
72da7aa144
commit
0ee8c5903c
@ -0,0 +1,4 @@
|
||||
#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.
|
||||
#Wed Sep 11 13:56:34 CST 2024
|
||||
@default-central-https\://repo.maven.apache.org/maven2/.lastUpdated=1726034194286
|
||||
https\://repo.maven.apache.org/maven2/.error=Could not transfer artifact com.alibaba\:fastjson\:pom\:1.2.51 from/to central (https\://repo.maven.apache.org/maven2)\: Connect to repo.maven.apache.org\:443 [repo.maven.apache.org/146.75.112.215, repo.maven.apache.org/2a04\:4e42\:1a\:0\:0\:0\:0\:215] failed\: No route to host (connect failed)
|
139
src/main/java/org/dromara/config/TfConfig.java
Normal file
139
src/main/java/org/dromara/config/TfConfig.java
Normal file
@ -0,0 +1,139 @@
|
||||
package org.dromara.config;
|
||||
|
||||
public class TfConfig {
|
||||
private int maxLength = 25;//最大语句长度
|
||||
private int multiNumber = 8;//多头数量
|
||||
private int featureDimension = 50;//词向量维度
|
||||
private int allDepth = 1;//深度
|
||||
private double studyPoint = 0.01;
|
||||
private int typeNumber;
|
||||
private boolean showLog = true;
|
||||
private int times = 10;//循环增强次数
|
||||
private int regularModel = RZ.NOT_RZ;//正则模式
|
||||
private double regular = 0;//正则系数
|
||||
private String splitWord;//词向量默认隔断符,无隔断则会逐字隔断
|
||||
private boolean selfTimeCode = false;//使用对称三角函数位置编码
|
||||
private int coreNumber = 1;//是否使用多核并行计算进行提速
|
||||
private boolean outAllPro = false;//是否输出全概率,注意,若输出全概率只能用来分类概率,否则将消耗大量内存
|
||||
private double timePunValue = 0.7;//时间惩罚系数
|
||||
|
||||
public double getTimePunValue() {
|
||||
return timePunValue;
|
||||
}
|
||||
|
||||
public void setTimePunValue(double timePunValue) {
|
||||
this.timePunValue = timePunValue;
|
||||
}
|
||||
|
||||
public boolean isOutAllPro() {
|
||||
return outAllPro;
|
||||
}
|
||||
|
||||
public void setOutAllPro(boolean outAllPro) {
|
||||
this.outAllPro = outAllPro;
|
||||
}
|
||||
|
||||
public int getCoreNumber() {
|
||||
return coreNumber;
|
||||
}
|
||||
|
||||
public void setCoreNumber(int coreNumber) {
|
||||
this.coreNumber = coreNumber;
|
||||
}
|
||||
|
||||
public boolean isSelfTimeCode() {
|
||||
return selfTimeCode;
|
||||
}
|
||||
|
||||
public void setSelfTimeCode(boolean selfTimeCode) {
|
||||
this.selfTimeCode = selfTimeCode;
|
||||
}
|
||||
|
||||
public String getSplitWord() {
|
||||
return splitWord;
|
||||
}
|
||||
|
||||
public void setSplitWord(String splitWord) {
|
||||
this.splitWord = splitWord;
|
||||
}
|
||||
|
||||
public int getRegularModel() {
|
||||
return regularModel;
|
||||
}
|
||||
|
||||
public void setRegularModel(int regularModel) {
|
||||
this.regularModel = regularModel;
|
||||
}
|
||||
|
||||
public double getRegular() {
|
||||
return regular;
|
||||
}
|
||||
|
||||
public void setRegular(double regular) {
|
||||
this.regular = regular;
|
||||
}
|
||||
|
||||
public int getTimes() {
|
||||
return times;
|
||||
}
|
||||
|
||||
public void setTimes(int times) {
|
||||
this.times = times;
|
||||
}
|
||||
|
||||
public int getMaxLength() {
|
||||
return maxLength;
|
||||
}
|
||||
|
||||
public void setMaxLength(int maxLength) {
|
||||
this.maxLength = maxLength;
|
||||
}
|
||||
|
||||
public int getMultiNumber() {
|
||||
return multiNumber;
|
||||
}
|
||||
|
||||
public void setMultiNumber(int multiNumber) {
|
||||
this.multiNumber = multiNumber;
|
||||
}
|
||||
|
||||
public int getFeatureDimension() {
|
||||
return featureDimension;
|
||||
}
|
||||
|
||||
public void setFeatureDimension(int featureDimension) {
|
||||
this.featureDimension = featureDimension;
|
||||
}
|
||||
|
||||
public int getAllDepth() {
|
||||
return allDepth;
|
||||
}
|
||||
|
||||
public void setAllDepth(int allDepth) {
|
||||
this.allDepth = allDepth;
|
||||
}
|
||||
|
||||
public double getStudyPoint() {
|
||||
return studyPoint;
|
||||
}
|
||||
|
||||
public void setStudyPoint(double studyPoint) {
|
||||
this.studyPoint = studyPoint;
|
||||
}
|
||||
|
||||
public int getTypeNumber() {
|
||||
return typeNumber;
|
||||
}
|
||||
|
||||
public void setTypeNumber(int typeNumber) {
|
||||
this.typeNumber = typeNumber;
|
||||
}
|
||||
|
||||
public boolean isShowLog() {
|
||||
return showLog;
|
||||
}
|
||||
|
||||
public void setShowLog(boolean showLog) {
|
||||
this.showLog = showLog;
|
||||
}
|
||||
}
|
25
src/main/java/org/dromara/entity/CreatorModel.java
Normal file
25
src/main/java/org/dromara/entity/CreatorModel.java
Normal file
@ -0,0 +1,25 @@
|
||||
package org.dromara.entity;
|
||||
|
||||
|
||||
import org.dromara.rnnJumpNerveCenter.ModelParameter;
|
||||
|
||||
public class CreatorModel {
|
||||
private ModelParameter semanticsModel;
|
||||
private ModelParameter customModel;
|
||||
|
||||
public ModelParameter getSemanticsModel() {
|
||||
return semanticsModel;
|
||||
}
|
||||
|
||||
public void setSemanticsModel(ModelParameter semanticsModel) {
|
||||
this.semanticsModel = semanticsModel;
|
||||
}
|
||||
|
||||
public ModelParameter getCustomModel() {
|
||||
return customModel;
|
||||
}
|
||||
|
||||
public void setCustomModel(ModelParameter customModel) {
|
||||
this.customModel = customModel;
|
||||
}
|
||||
}
|
60
src/main/java/org/dromara/entity/Model.java
Normal file
60
src/main/java/org/dromara/entity/Model.java
Normal file
@ -0,0 +1,60 @@
|
||||
package org.dromara.entity;
|
||||
|
||||
|
||||
import org.dromara.nerveCenter.ModelParameter;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @param
|
||||
* @DATA
|
||||
* @Author LiDaPeng
|
||||
* @Description 食物模型
|
||||
*/
|
||||
public class Model {
|
||||
private ModelParameter modelR;
|
||||
private ModelParameter modelG;
|
||||
private ModelParameter modelB;
|
||||
private ModelParameter modelFood;
|
||||
private Map<Integer, Integer> mapping;
|
||||
|
||||
public Map<Integer, Integer> getMapping() {
|
||||
return mapping;
|
||||
}
|
||||
|
||||
public void setMapping(Map<Integer, Integer> mapping) {
|
||||
this.mapping = mapping;
|
||||
}
|
||||
|
||||
public ModelParameter getModelR() {
|
||||
return modelR;
|
||||
}
|
||||
|
||||
public void setModelR(ModelParameter modelR) {
|
||||
this.modelR = modelR;
|
||||
}
|
||||
|
||||
public ModelParameter getModelG() {
|
||||
return modelG;
|
||||
}
|
||||
|
||||
public void setModelG(ModelParameter modelG) {
|
||||
this.modelG = modelG;
|
||||
}
|
||||
|
||||
public ModelParameter getModelB() {
|
||||
return modelB;
|
||||
}
|
||||
|
||||
public void setModelB(ModelParameter modelB) {
|
||||
this.modelB = modelB;
|
||||
}
|
||||
|
||||
public ModelParameter getModelFood() {
|
||||
return modelFood;
|
||||
}
|
||||
|
||||
public void setModelFood(ModelParameter modelFood) {
|
||||
this.modelFood = modelFood;
|
||||
}
|
||||
}
|
46
src/main/java/org/dromara/entity/SemanticsBack.java
Normal file
46
src/main/java/org/dromara/entity/SemanticsBack.java
Normal file
@ -0,0 +1,46 @@
|
||||
package org.dromara.entity;
|
||||
|
||||
|
||||
import org.dromara.matrixTools.Matrix;
|
||||
import org.dromara.i.OutBack;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SemanticsBack implements OutBack {
|
||||
private Matrix matrix;
|
||||
private String word;
|
||||
|
||||
public String getWord() {
|
||||
return word;
|
||||
}
|
||||
|
||||
public Matrix getMatrix() {
|
||||
return matrix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getBack(double out, int id, long eventId) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getSoftMaxBack(long eventId, List<Double> softMax) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void backWord(String word, long eventId) {
|
||||
this.word = word;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getBackMatrix(Matrix matrix, int id, long eventId) {
|
||||
this.matrix = matrix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getWordVector(int id, double w) {
|
||||
|
||||
}
|
||||
}
|
64
src/main/java/org/dromara/entity/WordMatrix.java
Normal file
64
src/main/java/org/dromara/entity/WordMatrix.java
Normal file
@ -0,0 +1,64 @@
|
||||
package org.dromara.entity;
|
||||
|
||||
import org.dromara.matrixTools.Matrix;
|
||||
import org.dromara.i.OutBack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @param
|
||||
* @DATA
|
||||
* @Author LiDaPeng
|
||||
* @Description
|
||||
*/
|
||||
public class WordMatrix implements OutBack {
|
||||
private final double[] vector;
|
||||
|
||||
public WordMatrix(int size) {
|
||||
vector = new double[size];
|
||||
}
|
||||
|
||||
public Matrix getVector() throws Exception {
|
||||
Matrix matrix = new Matrix(1, vector.length);
|
||||
for (int i = 0; i < vector.length; i++) {
|
||||
matrix.setNub(0, i, vector[i]);
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
|
||||
public List<Double> getList() {
|
||||
List<Double> list = new ArrayList<>();
|
||||
for (double v : vector) {
|
||||
list.add(v);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getBack(double out, int id, long eventId) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getSoftMaxBack(long eventId, List<Double> softMax) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void backWord(String word, long eventId) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void getBackMatrix(Matrix matrix, int id, long eventId) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getWordVector(int id, double w) {
|
||||
vector[id - 1] = w;
|
||||
}
|
||||
}
|
27
src/main/java/org/dromara/function/TanHX.java
Normal file
27
src/main/java/org/dromara/function/TanHX.java
Normal file
@ -0,0 +1,27 @@
|
||||
package org.dromara.function;
|
||||
|
||||
|
||||
import org.dromara.i.ActiveFunction;
|
||||
|
||||
/**
|
||||
* @param
|
||||
* @DATA
|
||||
* @Author LiDaPeng
|
||||
* @Description
|
||||
*/
|
||||
public class TanHX implements ActiveFunction {
|
||||
@Override
|
||||
public double function(double x) {
|
||||
x = x * 0.038;
|
||||
double x1 = Math.exp(x);
|
||||
double x2 = Math.exp(-x);
|
||||
double son = x1 - x2;// ArithUtil.sub(x1, x2);
|
||||
double mother = x1 + x2;// ArithUtil.add(x1, x2);
|
||||
return son / mother;//ArithUtil.div(son, mother);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double functionG(double out) {
|
||||
return 1 - Math.pow(out, 2);
|
||||
}
|
||||
}
|
20
src/main/java/org/dromara/function/Tanh.java
Normal file
20
src/main/java/org/dromara/function/Tanh.java
Normal file
@ -0,0 +1,20 @@
|
||||
package org.dromara.function;
|
||||
|
||||
|
||||
import org.dromara.i.ActiveFunction;
|
||||
|
||||
public class Tanh implements ActiveFunction {
|
||||
@Override
|
||||
public double function(double x) {
|
||||
double x1 = Math.exp(x);
|
||||
double x2 = Math.exp(-x);
|
||||
double son = x1 - x2;// ArithUtil.sub(x1, x2);
|
||||
double mother = x1 + x2;// ArithUtil.add(x1, x2);
|
||||
return son / mother;//ArithUtil.div(son, mother);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double functionG(double out) {
|
||||
return 1 - Math.pow(out, 2);
|
||||
}
|
||||
}
|
70
src/main/java/org/dromara/gameRobot/DynamicState.java
Normal file
70
src/main/java/org/dromara/gameRobot/DynamicState.java
Normal file
@ -0,0 +1,70 @@
|
||||
package org.dromara.gameRobot;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author lidapeng
|
||||
* @description 状态
|
||||
* @date 10:27 上午 2022/9/12
|
||||
*/
|
||||
public class DynamicState {
|
||||
private int[] stateId;//状态id
|
||||
private int bestActionId = 1;//该状态的最优动作
|
||||
private double value = 0;//该状态价值
|
||||
private int number = 0;//该状态被执行了几次 被执行的时候需要修改
|
||||
private boolean isFinish = false;//是否是终结态
|
||||
private Map<Integer, List<DynamicState>> sonStatesMap = new HashMap<>();//动作-子状态集合 被执行的时候需要修改
|
||||
private Map<Integer, Integer> profitMap = new HashMap<>();//该状态的收益集合,主键是收益,值是次数 被执行的时候需要修改
|
||||
|
||||
public void add() {
|
||||
number++;
|
||||
}
|
||||
|
||||
public int getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public int getBestActionId() {
|
||||
return bestActionId;
|
||||
}
|
||||
|
||||
public void setBestActionId(int bestActionId) {
|
||||
this.bestActionId = bestActionId;
|
||||
}
|
||||
|
||||
public Map<Integer, List<DynamicState>> getSonStatesMap() {
|
||||
return sonStatesMap;
|
||||
}
|
||||
|
||||
public boolean isFinish() {
|
||||
return isFinish;
|
||||
}
|
||||
|
||||
public void setFinish(boolean finish) {
|
||||
isFinish = finish;
|
||||
}
|
||||
|
||||
public Map<Integer, Integer> getProfitMap() {
|
||||
return profitMap;
|
||||
}
|
||||
|
||||
public DynamicState(int[] stateId) {//设置状态id
|
||||
this.stateId = stateId;
|
||||
}
|
||||
|
||||
public int[] getStateId() {
|
||||
return stateId;
|
||||
}
|
||||
|
||||
public void setStateId(int[] stateId) {
|
||||
this.stateId = stateId;
|
||||
}
|
||||
|
||||
public double getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(double value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
22
src/main/java/org/dromara/gameRobot/ValueFunction.java
Normal file
22
src/main/java/org/dromara/gameRobot/ValueFunction.java
Normal file
@ -0,0 +1,22 @@
|
||||
package org.dromara.gameRobot;
|
||||
|
||||
public class ValueFunction {//价值函数
|
||||
private int[] stateId;//状态id
|
||||
private double value;//价值
|
||||
|
||||
public int[] getStateId() {
|
||||
return stateId;
|
||||
}
|
||||
|
||||
public void setStateId(int[] stateId) {
|
||||
this.stateId = stateId;
|
||||
}
|
||||
|
||||
public double getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(double value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
102
src/main/java/org/dromara/naturalLanguage/Talk.java
Normal file
102
src/main/java/org/dromara/naturalLanguage/Talk.java
Normal file
@ -0,0 +1,102 @@
|
||||
package org.dromara.naturalLanguage;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author lidapeng
|
||||
* @description 语句分类
|
||||
* @date 4:14 下午 2020/2/23
|
||||
*/
|
||||
public class Talk {
|
||||
private final List<WorldBody> allWorld;//所有词集合
|
||||
private final WordTemple wordTemple;
|
||||
|
||||
public Talk(WordTemple wordTemple) {
|
||||
this.wordTemple = wordTemple;
|
||||
allWorld = wordTemple.getAllWorld();//所有词集合
|
||||
}
|
||||
|
||||
public List<List<String>> getSplitWord(String sentence) {//单纯进行拆词
|
||||
List<Sentence> sentences = splitSentence(sentence);
|
||||
List<List<String>> words = new ArrayList<>();
|
||||
for (Sentence sentence1 : sentences) {
|
||||
words.add(sentence1.getKeyWords());
|
||||
}
|
||||
return words;
|
||||
}
|
||||
|
||||
private List<Sentence> splitSentence(String sentence) {
|
||||
String[] sens = sentence.replace(" ", "").split(",|。|?|!|;|、|:");
|
||||
//拆词
|
||||
List<Sentence> sentences = new ArrayList<>();
|
||||
for (String mySentence : sens) {
|
||||
List<Sentence> sentenceList = catchSentence(mySentence);
|
||||
int key = 0;
|
||||
int nub = 0;
|
||||
for (int j = 0; j < sentenceList.size(); j++) {
|
||||
Sentence sentence1 = sentenceList.get(j);
|
||||
restructure(sentence1);
|
||||
int size = sentence1.getKeyWords().size();
|
||||
if (size > nub) {
|
||||
key = j;
|
||||
nub = size;
|
||||
}
|
||||
}
|
||||
//System.out.println(sentenceList.get(key).getKeyWords());
|
||||
sentences.add(sentenceList.get(key));
|
||||
}
|
||||
return sentences;
|
||||
}
|
||||
|
||||
private List<Sentence> catchSentence(String sentence) {//把句子拆开
|
||||
int len = sentence.length();
|
||||
List<Sentence> sentences = new ArrayList<>();
|
||||
if (len > 1) {
|
||||
for (int j = 0; j < len - 1; j++) {
|
||||
Sentence sentenceWords = new Sentence();
|
||||
for (int i = j; i < len; i++) {
|
||||
String word = sentence.substring(j, i + 1);
|
||||
sentenceWords.setWord(word);
|
||||
}
|
||||
sentences.add(sentenceWords);
|
||||
}
|
||||
} else {
|
||||
Sentence sentenceWords = new Sentence();
|
||||
sentenceWords.setWord(sentence);
|
||||
sentences.add(sentenceWords);
|
||||
}
|
||||
return sentences;
|
||||
}
|
||||
|
||||
private void restructure(Sentence words) {//对句子里面的Word进行词频统计
|
||||
List<WorldBody> listWord = allWorld;
|
||||
List<Word> waitWorld = words.getWaitWords();
|
||||
for (Word word : waitWorld) {
|
||||
String myWord = word.getWord();
|
||||
WorldBody body = getBody(myWord, listWord);
|
||||
if (body == null) {//已经无法查找到对应的词汇了
|
||||
word.setWordFrequency(1);
|
||||
break;
|
||||
}
|
||||
listWord = body.getWorldBodies();//这个body报了一次空指针
|
||||
word.setWordFrequency(body.getWordFrequency());
|
||||
}
|
||||
Tokenizer tokenizer = new Tokenizer(wordTemple);
|
||||
tokenizer.radiation(words);
|
||||
|
||||
}
|
||||
|
||||
private WorldBody getBody(String word, List<WorldBody> worlds) {
|
||||
//TODO 这里有个BUG 当myBody出现空的时候断词已经找不到了
|
||||
WorldBody myBody = null;
|
||||
for (WorldBody body : worlds) {
|
||||
if (body.getWordName().hashCode() == word.hashCode() && body.getWordName().equals(word)) {
|
||||
myBody = body;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return myBody;
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package org.dromara.naturalLanguage.languageCreator;
|
||||
|
||||
import org.dromara.gameRobot.Action;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class WordLeft extends Action {
|
||||
private final List<String> keyWords;
|
||||
private final List<String> finishWords;//终结态词集合
|
||||
|
||||
public WordLeft(List<String> keyWords, List<String> finishWords) {
|
||||
this.keyWords = keyWords;
|
||||
this.finishWords = finishWords;
|
||||
}
|
||||
|
||||
private boolean isFinish(String myWord) {
|
||||
boolean isHere = false;
|
||||
for (String finishWord : finishWords) {
|
||||
if (finishWord.hashCode() == myWord.hashCode() && finishWord.equals(myWord)) {
|
||||
isHere = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return isHere;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getActionId() {
|
||||
return super.getActionId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setActionId(int actionId) {
|
||||
super.setActionId(actionId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<int[]> action(int[] stateId) {
|
||||
int id = stateId[0];
|
||||
String myWord = keyWords.get(id - 1);
|
||||
int len = myWord.length() + 1;
|
||||
int size = keyWords.size();
|
||||
List<int[]> list = new ArrayList<>();
|
||||
for (int i = 0; i < size; i++) {
|
||||
String word = keyWords.get(i);
|
||||
if (word.length() == len && word.substring(1).equals(myWord)) {
|
||||
list.add(new int[]{i + 1});
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getProfit(int[] stateId) {
|
||||
int allProfit = 0;
|
||||
List<int[]> states = action(stateId);
|
||||
if (states.isEmpty()) {
|
||||
allProfit = -10;
|
||||
} else {
|
||||
for (int i = 0; i < states.size(); i++) {
|
||||
int nextID = states.get(i)[0];
|
||||
if (nextID > 0) {
|
||||
String myWord = keyWords.get(nextID - 1);//查看是否为终结态
|
||||
if (isFinish(myWord)) {//是终结态
|
||||
allProfit = allProfit + 10;
|
||||
}
|
||||
} else {
|
||||
allProfit = allProfit - 10;
|
||||
}
|
||||
}
|
||||
allProfit = allProfit / states.size();
|
||||
}
|
||||
return allProfit;
|
||||
}
|
||||
}
|
44
src/main/java/org/dromara/nerveCenter/ModelParameter.java
Normal file
44
src/main/java/org/dromara/nerveCenter/ModelParameter.java
Normal file
@ -0,0 +1,44 @@
|
||||
package org.dromara.nerveCenter;
|
||||
|
||||
|
||||
import org.dromara.nerveEntity.DymNerveStudy;
|
||||
import org.dromara.nerveEntity.NerveStudy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author lidapeng
|
||||
* @description 学习结果
|
||||
* @date 3:33 下午 2020/1/8
|
||||
*/
|
||||
public class ModelParameter {
|
||||
//神经远模型参数
|
||||
private List<List<NerveStudy>> depthNerves = new ArrayList<>();//隐层神经元
|
||||
private List<NerveStudy> outNerves = new ArrayList<>();//输出神经元
|
||||
private List<List<DymNerveStudy>> dymNerveStudies = new ArrayList<>();//动态神经元隐层
|
||||
|
||||
public List<List<NerveStudy>> getDepthNerves() {
|
||||
return depthNerves;
|
||||
}
|
||||
|
||||
public void setDepthNerves(List<List<NerveStudy>> depthNerves) {
|
||||
this.depthNerves = depthNerves;
|
||||
}
|
||||
|
||||
public List<NerveStudy> getOutNerves() {
|
||||
return outNerves;
|
||||
}
|
||||
|
||||
public void setOutNerves(List<NerveStudy> outNerves) {
|
||||
this.outNerves = outNerves;
|
||||
}
|
||||
|
||||
public List<List<DymNerveStudy>> getDymNerveStudies() {
|
||||
return dymNerveStudies;
|
||||
}
|
||||
|
||||
public void setDymNerveStudies(List<List<DymNerveStudy>> dymNerveStudies) {
|
||||
this.dymNerveStudies = dymNerveStudies;
|
||||
}
|
||||
}
|
73
src/main/java/org/dromara/nerveEntity/HiddenNerve.java
Normal file
73
src/main/java/org/dromara/nerveEntity/HiddenNerve.java
Normal file
@ -0,0 +1,73 @@
|
||||
package org.dromara.nerveEntity;
|
||||
|
||||
import org.dromara.matrixTools.Matrix;
|
||||
import org.dromara.matrixTools.MatrixOperation;
|
||||
import org.dromara.i.ActiveFunction;
|
||||
import org.dromara.i.OutBack;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author lidapeng
|
||||
* 隐层神经元
|
||||
* @date 9:30 上午 2019/12/21
|
||||
*/
|
||||
public class HiddenNerve extends Nerve {
|
||||
private final boolean isConvFinish;//卷积最后一层
|
||||
private final MatrixOperation matrixOperation = new MatrixOperation();
|
||||
|
||||
public HiddenNerve(int id, int depth, int upNub, int downNub, double studyPoint,
|
||||
boolean init, ActiveFunction activeFunction, boolean isDynamic, int rzType, double lParam
|
||||
, int step, int kernLen, int matrixX, int matrixY, boolean isConvFinish, int coreNumber) throws Exception {//隐层神经元
|
||||
super(id, upNub, "HiddenNerve", downNub, studyPoint,
|
||||
init, activeFunction, isDynamic, rzType, lParam, step, kernLen, depth, matrixX, matrixY
|
||||
, coreNumber);
|
||||
this.isConvFinish = isConvFinish;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void input(long eventId, double parameter, boolean isKernelStudy, Map<Integer, Double> E
|
||||
, OutBack outBack) throws Exception {//接收上一层的输入
|
||||
boolean allReady = insertParameter(eventId, parameter);
|
||||
if (allReady) {//参数齐了,开始计算 sigma - threshold
|
||||
double sigma = calculation(eventId);
|
||||
double out = activeFunction.function(sigma);//激活函数输出数值
|
||||
if (isKernelStudy) {
|
||||
outNub = out;
|
||||
} else {
|
||||
destoryParameter(eventId);
|
||||
}
|
||||
sendMessage(eventId, out, isKernelStudy, E, outBack);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void inputMatrixFeature(long eventId, List<Double> parameters, boolean isStudy, Map<Integer, Double> E, OutBack imageBack) throws Exception {
|
||||
boolean allReady = insertParameters(eventId, parameters);
|
||||
if (allReady) {//参数齐了,开始计算 sigma - threshold
|
||||
double sigma = calculation(eventId);
|
||||
double out = activeFunction.function(sigma);//激活函数输出数值
|
||||
if (isStudy) {
|
||||
outNub = out;
|
||||
} else {
|
||||
destoryParameter(eventId);
|
||||
}
|
||||
sendMessage(eventId, out, isStudy, E, imageBack);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void inputMatrix(long eventId, Matrix matrix, boolean isStudy
|
||||
, Map<Integer, Double> E, OutBack outBack, boolean needMatrix) throws Exception {
|
||||
Matrix myMatrix = conv(matrix);//处理过的矩阵
|
||||
if (isConvFinish) {
|
||||
if (!isStudy && needMatrix) {
|
||||
outBack.getBackMatrix(myMatrix, getId(), eventId);
|
||||
}
|
||||
sendMatrixList(eventId, matrixOperation.matrixToList(myMatrix), isStudy, E, outBack);
|
||||
} else {
|
||||
sendMatrix(eventId, myMatrix, isStudy, E, outBack, needMatrix);
|
||||
}
|
||||
}
|
||||
}
|
30
src/main/java/org/dromara/nerveEntity/NerveStudy.java
Normal file
30
src/main/java/org/dromara/nerveEntity/NerveStudy.java
Normal file
@ -0,0 +1,30 @@
|
||||
package org.dromara.nerveEntity;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author lidapeng
|
||||
* @description
|
||||
* @date 3:36 下午 2020/1/8
|
||||
*/
|
||||
public class NerveStudy {
|
||||
private Map<String, Double> dendrites = new HashMap<>();//上一层权重(需要取出)
|
||||
private double threshold;//此神经元的阈值需要取出
|
||||
|
||||
public Map<String, Double> getDendrites() {
|
||||
return dendrites;
|
||||
}
|
||||
|
||||
public void setDendrites(Map<String, Double> dendrites) {
|
||||
this.dendrites = dendrites;
|
||||
}
|
||||
|
||||
public double getThreshold() {
|
||||
return threshold;
|
||||
}
|
||||
|
||||
public void setThreshold(double threshold) {
|
||||
this.threshold = threshold;
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package org.dromara.rnnJumpNerveCenter;
|
||||
|
||||
|
||||
|
||||
import org.dromara.rnnJumpNerveEntity.NerveStudy;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class RnnOutNerveStudy {
|
||||
private int depth;//深度
|
||||
private List<NerveStudy> nerveStudies;
|
||||
|
||||
public int getDepth() {
|
||||
return depth;
|
||||
}
|
||||
|
||||
public void setDepth(int depth) {
|
||||
this.depth = depth;
|
||||
}
|
||||
|
||||
public List<NerveStudy> getNerveStudies() {
|
||||
return nerveStudies;
|
||||
}
|
||||
|
||||
public void setNerveStudies(List<NerveStudy> nerveStudies) {
|
||||
this.nerveStudies = nerveStudies;
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package org.dromara.rnnJumpNerveEntity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class RnnOutNerveBody {
|
||||
private int depth;//深度
|
||||
private List<Nerve> outNerves;//输出神经元集合
|
||||
|
||||
public int getDepth() {
|
||||
return depth;
|
||||
}
|
||||
|
||||
public void setDepth(int depth) {
|
||||
this.depth = depth;
|
||||
}
|
||||
|
||||
public List<Nerve> getOutNerves() {
|
||||
return outNerves;
|
||||
}
|
||||
|
||||
public void setOutNerves(List<Nerve> outNerves) {
|
||||
this.outNerves = outNerves;
|
||||
}
|
||||
}
|
462
src/main/java/org/dromara/rnnNerveCenter/NerveManager.java
Normal file
462
src/main/java/org/dromara/rnnNerveCenter/NerveManager.java
Normal file
@ -0,0 +1,462 @@
|
||||
package org.dromara.rnnNerveCenter;
|
||||
|
||||
import org.dromara.matrixTools.Matrix;
|
||||
import org.dromara.i.ActiveFunction;
|
||||
import org.dromara.rnnNerveEntity.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 神经网络管理工具
|
||||
* 创建神经网络
|
||||
*
|
||||
* @author lidapeng
|
||||
* @date 11:05 上午 2019/12/21
|
||||
*/
|
||||
public class NerveManager {
|
||||
private final int hiddenNerveNub;//隐层神经元个数
|
||||
private final int sensoryNerveNub;//输入神经元个数
|
||||
private final int outNerveNub;//输出神经元个数
|
||||
private final int hiddenDepth;//隐层深度
|
||||
private final List<SensoryNerve> sensoryNerves = new ArrayList<>();//感知神经元
|
||||
private final List<List<Nerve>> depthNerves = new ArrayList<>();//隐层神经元
|
||||
private final List<Nerve> outNerves = new ArrayList<>();//输出神经元
|
||||
private final List<RnnOutNerveBody> rnnOutNerveBodies = new ArrayList<>();//rnn输出层神经元
|
||||
private boolean initPower;
|
||||
private double studyPoint = 0.1;//学习率
|
||||
private final ActiveFunction activeFunction;
|
||||
private Map<Integer, Matrix> matrixMap = new HashMap<>();//主键与期望矩阵的映射
|
||||
private final boolean isDynamic;//是否是动态神经网络
|
||||
private boolean isRnn = false;//是否为rnn网络
|
||||
private List<Double> studyList = new ArrayList<>();
|
||||
private final int rzType;//正则化类型,默认不进行正则化
|
||||
private final double lParam;//正则参数
|
||||
private boolean isSoftMax = true;
|
||||
|
||||
public void setSoftMax(boolean softMax) {
|
||||
isSoftMax = softMax;
|
||||
}
|
||||
|
||||
public List<Double> getStudyList() {//查看每一次的学习率
|
||||
return studyList;
|
||||
}
|
||||
|
||||
public void setStudyList(List<Double> studyList) {//设置每一层的学习率
|
||||
this.studyList = studyList;
|
||||
}
|
||||
|
||||
public void setMatrixMap(Map<Integer, Matrix> matrixMap) {
|
||||
this.matrixMap = matrixMap;
|
||||
}
|
||||
|
||||
private Map<String, Double> conversion(Map<Integer, Double> map) {
|
||||
Map<String, Double> cMap = new HashMap<>();
|
||||
for (Map.Entry<Integer, Double> entry : map.entrySet()) {
|
||||
cMap.put(String.valueOf(entry.getKey()), entry.getValue());
|
||||
}
|
||||
return cMap;
|
||||
}
|
||||
|
||||
private Map<Integer, Double> unConversion(Map<String, Double> map) {
|
||||
Map<Integer, Double> cMap = new HashMap<>();
|
||||
for (Map.Entry<String, Double> entry : map.entrySet()) {
|
||||
cMap.put(Integer.parseInt(entry.getKey()), entry.getValue());
|
||||
}
|
||||
return cMap;
|
||||
}
|
||||
|
||||
private ModelParameter getDymModelParameter() throws Exception {//获取动态神经元参数
|
||||
ModelParameter modelParameter = new ModelParameter();
|
||||
List<DymNerveStudy> dymNerveStudies = new ArrayList<>();//动态神经元隐层
|
||||
DymNerveStudy dymOutNerveStudy = new DymNerveStudy();//动态神经元输出层
|
||||
modelParameter.setDymNerveStudies(dymNerveStudies);
|
||||
modelParameter.setDymOutNerveStudy(dymOutNerveStudy);
|
||||
for (List<Nerve> nerve : depthNerves) {
|
||||
Nerve depthNerve = nerve.get(0);//隐层神经元
|
||||
DymNerveStudy deepNerveStudy = new DymNerveStudy();//动态神经元输出层
|
||||
List<Double> list = deepNerveStudy.getList();
|
||||
Matrix matrix = depthNerve.getNerveMatrix();
|
||||
insertWList(matrix, list);
|
||||
dymNerveStudies.add(deepNerveStudy);
|
||||
}
|
||||
Nerve outNerve = outNerves.get(0);
|
||||
Matrix matrix = outNerve.getNerveMatrix();
|
||||
List<Double> list = dymOutNerveStudy.getList();
|
||||
insertWList(matrix, list);
|
||||
return modelParameter;
|
||||
}
|
||||
|
||||
private void insertWList(Matrix matrix, List<Double> list) throws Exception {//
|
||||
for (int i = 0; i < matrix.getX(); i++) {
|
||||
for (int j = 0; j < matrix.getY(); j++) {
|
||||
list.add(matrix.getNumber(i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ModelParameter getModelParameter() throws Exception {
|
||||
if (isRnn) {
|
||||
return getRnnModelParameter();
|
||||
} else if (isDynamic) {
|
||||
return getDymModelParameter();
|
||||
} else {
|
||||
return getStaticModelParameter();
|
||||
}
|
||||
}
|
||||
|
||||
private ModelParameter getRnnModelParameter() {//获取rnn当前模型参数
|
||||
ModelParameter modelParameter = new ModelParameter();
|
||||
List<List<NerveStudy>> studyDepthNerves = new ArrayList<>();//隐层神经元模型
|
||||
List<RnnOutNerveStudy> rnnOutNerveStudies = new ArrayList<>();
|
||||
modelParameter.setDepthNerves(studyDepthNerves);
|
||||
modelParameter.setRnnOutNerveStudies(rnnOutNerveStudies);
|
||||
//隐层神经元
|
||||
getHiddenNerveModel(studyDepthNerves);
|
||||
for (RnnOutNerveBody rnnOutNerveBody : rnnOutNerveBodies) {
|
||||
List<NerveStudy> nerveStudies = new ArrayList<>();
|
||||
RnnOutNerveStudy rnnOutNerveStudy = new RnnOutNerveStudy();
|
||||
rnnOutNerveStudies.add(rnnOutNerveStudy);
|
||||
rnnOutNerveStudy.setDepth(rnnOutNerveBody.getDepth());
|
||||
rnnOutNerveStudy.setNerveStudies(nerveStudies);
|
||||
List<Nerve> outNerveList = rnnOutNerveBody.getOutNerves();
|
||||
getRnnOutNerveModel(nerveStudies, outNerveList);
|
||||
}
|
||||
return modelParameter;
|
||||
}
|
||||
|
||||
private void getRnnOutNerveModel(List<NerveStudy> nerveStudies, List<Nerve> outNerveList) {
|
||||
for (Nerve nerve : outNerveList) {
|
||||
NerveStudy nerveStudy = new NerveStudy();
|
||||
nerveStudy.setThreshold(nerve.getThreshold());
|
||||
nerveStudy.setDendrites(conversion(nerve.getDendrites()));
|
||||
nerveStudies.add(nerveStudy);
|
||||
}
|
||||
}
|
||||
|
||||
private void getHiddenNerveModel(List<List<NerveStudy>> studyDepthNerves) {
|
||||
for (List<Nerve> depthNerve : depthNerves) {
|
||||
//创建一层深度的隐层神经元模型
|
||||
List<NerveStudy> deepNerve = new ArrayList<>();
|
||||
for (Nerve nerve : depthNerve) {
|
||||
//遍历某一层深度的所有隐层神经元
|
||||
NerveStudy nerveStudy = new NerveStudy();
|
||||
nerveStudy.setThreshold(nerve.getThreshold());
|
||||
nerveStudy.setDendrites(conversion(nerve.getDendrites()));
|
||||
deepNerve.add(nerveStudy);
|
||||
}
|
||||
studyDepthNerves.add(deepNerve);
|
||||
}
|
||||
}
|
||||
|
||||
private ModelParameter getStaticModelParameter() {//获取当前模型参数
|
||||
ModelParameter modelParameter = new ModelParameter();
|
||||
List<List<NerveStudy>> studyDepthNerves = new ArrayList<>();//隐层神经元模型
|
||||
List<NerveStudy> outStudyNerves = new ArrayList<>();//输出神经元
|
||||
//隐层神经元
|
||||
getHiddenNerveModel(studyDepthNerves);
|
||||
getRnnOutNerveModel(outStudyNerves, outNerves);
|
||||
modelParameter.setDepthNerves(studyDepthNerves);
|
||||
modelParameter.setOutNerves(outStudyNerves);
|
||||
return modelParameter;
|
||||
}
|
||||
|
||||
public void insertModelParameter(ModelParameter modelParameter) throws Exception {
|
||||
if (isRnn) {
|
||||
insertRnnModelParameter(modelParameter);
|
||||
} else if (isDynamic) {
|
||||
insertConvolutionModelParameter(modelParameter);//动态神经元注入
|
||||
} else {
|
||||
insertBpModelParameter(modelParameter);//全连接层注入参数
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//注入卷积层模型参数
|
||||
private void insertConvolutionModelParameter(ModelParameter modelParameter) throws Exception {
|
||||
List<DymNerveStudy> dymNerveStudyList = modelParameter.getDymNerveStudies();
|
||||
DymNerveStudy dymOutNerveStudy = modelParameter.getDymOutNerveStudy();
|
||||
for (int i = 0; i < depthNerves.size(); i++) {
|
||||
Nerve depthNerve = depthNerves.get(i).get(0);
|
||||
DymNerveStudy dymNerveStudy = dymNerveStudyList.get(i);
|
||||
List<Double> list = dymNerveStudy.getList();
|
||||
Matrix nerveMatrix = depthNerve.getNerveMatrix();
|
||||
insertMatrix(nerveMatrix, list);
|
||||
}
|
||||
Nerve outNerve = outNerves.get(0);
|
||||
Matrix outNerveMatrix = outNerve.getNerveMatrix();
|
||||
List<Double> list = dymOutNerveStudy.getList();
|
||||
insertMatrix(outNerveMatrix, list);
|
||||
}
|
||||
|
||||
private void insertMatrix(Matrix matrix, List<Double> list) throws Exception {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
matrix.setNub(i, 0, list.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
private void insertRnnModelParameter(ModelParameter modelParameter) {
|
||||
List<List<NerveStudy>> depthStudyNerves = modelParameter.getDepthNerves();//隐层神经元
|
||||
List<RnnOutNerveStudy> rnnOutNerveStudies = modelParameter.getRnnOutNerveStudies();
|
||||
//隐层神经元参数注入
|
||||
for (int i = 0; i < depthNerves.size(); i++) {
|
||||
List<NerveStudy> depth = depthStudyNerves.get(i);//对应的学习结果
|
||||
List<Nerve> depthNerve = depthNerves.get(i);//深度隐层神经元
|
||||
for (int j = 0; j < depthNerve.size(); j++) {//遍历当前深度神经元
|
||||
Nerve nerve = depthNerve.get(j);
|
||||
NerveStudy nerveStudy = depth.get(j);
|
||||
//学习结果
|
||||
Map<Integer, Double> studyDendrites = unConversion(nerveStudy.getDendrites());
|
||||
//神经元参数注入
|
||||
Map<Integer, Double> dendrites = nerve.getDendrites();
|
||||
nerve.setThreshold(nerveStudy.getThreshold());//注入隐层阈值
|
||||
for (Map.Entry<Integer, Double> entry : dendrites.entrySet()) {
|
||||
int key = entry.getKey();
|
||||
dendrites.put(key, studyDendrites.get(key));//注入隐层权重
|
||||
}
|
||||
}
|
||||
}
|
||||
for (RnnOutNerveStudy rnnOutNerveStudy : rnnOutNerveStudies) {
|
||||
RnnOutNerveBody rnnOutNerveBody = getRnnOutNerveBody(rnnOutNerveStudy.getDepth());
|
||||
List<NerveStudy> outStudyNerves = rnnOutNerveStudy.getNerveStudies();
|
||||
List<Nerve> outNerveBody = rnnOutNerveBody.getOutNerves();
|
||||
//输出神经元参数注入
|
||||
for (int i = 0; i < outNerveBody.size(); i++) {
|
||||
Nerve outNerve = outNerveBody.get(i);
|
||||
NerveStudy nerveStudy = outStudyNerves.get(i);
|
||||
outNerve.setThreshold(nerveStudy.getThreshold());
|
||||
Map<Integer, Double> dendrites = outNerve.getDendrites();
|
||||
Map<Integer, Double> studyDendrites = unConversion(nerveStudy.getDendrites());
|
||||
for (Map.Entry<Integer, Double> outEntry : dendrites.entrySet()) {
|
||||
int key = outEntry.getKey();
|
||||
dendrites.put(key, studyDendrites.get(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private RnnOutNerveBody getRnnOutNerveBody(int depth) {
|
||||
RnnOutNerveBody myRnnOutNerveBody = null;
|
||||
for (RnnOutNerveBody rnnOutNerveBody : rnnOutNerveBodies) {
|
||||
if (rnnOutNerveBody.getDepth() == depth) {
|
||||
myRnnOutNerveBody = rnnOutNerveBody;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return myRnnOutNerveBody;
|
||||
}
|
||||
|
||||
//注入全连接模型参数
|
||||
private void insertBpModelParameter(ModelParameter modelParameter) {
|
||||
List<List<NerveStudy>> depthStudyNerves = modelParameter.getDepthNerves();//隐层神经元
|
||||
List<NerveStudy> outStudyNerves = modelParameter.getOutNerves();//输出神经元
|
||||
//隐层神经元参数注入
|
||||
for (int i = 0; i < depthNerves.size(); i++) {
|
||||
List<NerveStudy> depth = depthStudyNerves.get(i);//对应的学习结果
|
||||
List<Nerve> depthNerve = depthNerves.get(i);//深度隐层神经元
|
||||
for (int j = 0; j < depthNerve.size(); j++) {//遍历当前深度神经元
|
||||
Nerve nerve = depthNerve.get(j);
|
||||
NerveStudy nerveStudy = depth.get(j);
|
||||
//学习结果
|
||||
Map<Integer, Double> studyDendrites = unConversion(nerveStudy.getDendrites());
|
||||
//神经元参数注入
|
||||
Map<Integer, Double> dendrites = nerve.getDendrites();
|
||||
nerve.setThreshold(nerveStudy.getThreshold());//注入隐层阈值
|
||||
for (Map.Entry<Integer, Double> entry : dendrites.entrySet()) {
|
||||
int key = entry.getKey();
|
||||
dendrites.put(key, studyDendrites.get(key));//注入隐层权重
|
||||
}
|
||||
}
|
||||
}
|
||||
//输出神经元参数注入
|
||||
for (int i = 0; i < outNerves.size(); i++) {
|
||||
Nerve outNerve = outNerves.get(i);
|
||||
NerveStudy nerveStudy = outStudyNerves.get(i);
|
||||
outNerve.setThreshold(nerveStudy.getThreshold());
|
||||
Map<Integer, Double> dendrites = outNerve.getDendrites();
|
||||
Map<Integer, Double> studyDendrites = unConversion(nerveStudy.getDendrites());
|
||||
for (Map.Entry<Integer, Double> outEntry : dendrites.entrySet()) {
|
||||
int key = outEntry.getKey();
|
||||
dendrites.put(key, studyDendrites.get(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化神经元参数
|
||||
*
|
||||
* @param sensoryNerveNub 输入神经元个数
|
||||
* @param hiddenNerveNub 隐层神经元个数
|
||||
* @param outNerveNub 输出神经元个数
|
||||
* @param hiddenDepth 隐层深度
|
||||
* @param activeFunction 激活函数
|
||||
* @param isDynamic 是否是动态神经元
|
||||
* @param rzType 正则函数
|
||||
* @param lParam 正则系数
|
||||
* @throws Exception 如果参数错误则抛异常
|
||||
*/
|
||||
public NerveManager(int sensoryNerveNub, int hiddenNerveNub, int outNerveNub
|
||||
, int hiddenDepth, ActiveFunction activeFunction, boolean isDynamic,
|
||||
double studyPoint, int rzType, double lParam) throws Exception {
|
||||
if (sensoryNerveNub > 0 && hiddenNerveNub > 0 && outNerveNub > 0 && hiddenDepth > 0 && activeFunction != null) {
|
||||
this.hiddenNerveNub = hiddenNerveNub;
|
||||
this.sensoryNerveNub = sensoryNerveNub;
|
||||
this.outNerveNub = outNerveNub;
|
||||
this.hiddenDepth = hiddenDepth;
|
||||
this.activeFunction = activeFunction;
|
||||
this.isDynamic = isDynamic;
|
||||
this.rzType = rzType;
|
||||
this.lParam = lParam;
|
||||
if (studyPoint > 0 && studyPoint < 1) {
|
||||
this.studyPoint = studyPoint;
|
||||
}
|
||||
} else {
|
||||
throw new Exception("param is null");
|
||||
}
|
||||
}
|
||||
|
||||
public List<SensoryNerve> getSensoryNerves() {//获取感知神经元集合
|
||||
return sensoryNerves;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*
|
||||
* @param initPower 是否是第一次注入
|
||||
* @param isMatrix 参数是否是一个矩阵
|
||||
* @param isShowLog 是否打印学习参数
|
||||
* @param isSoftMax 最后一层是否用softMax激活
|
||||
* @param step 卷积步长
|
||||
* @param kernLen 卷积核长
|
||||
*/
|
||||
public void init(boolean initPower, boolean isMatrix, boolean isShowLog, boolean isSoftMax
|
||||
, int step, int kernLen) throws Exception {//进行神经网络的初始化构建
|
||||
this.initPower = initPower;
|
||||
initDepthNerve(isMatrix, step, kernLen);//初始化深度隐层神经元
|
||||
List<Nerve> nerveList = depthNerves.get(0);//第一层隐层神经元
|
||||
//最后一层隐层神经元啊
|
||||
List<Nerve> lastNerveList = depthNerves.get(depthNerves.size() - 1);
|
||||
//初始化输出神经元
|
||||
List<OutNerve> outNerveList = new ArrayList<>();
|
||||
for (int i = 1; i < outNerveNub + 1; i++) {
|
||||
OutNerve outNerve = new OutNerve(i, hiddenNerveNub, 0, studyPoint, initPower,
|
||||
activeFunction, isMatrix, isShowLog, rzType, lParam, isSoftMax, step, kernLen);
|
||||
if (isMatrix) {//是卷积层神经网络
|
||||
outNerve.setMatrixMap(matrixMap);
|
||||
}
|
||||
//输出层神经元连接最后一层隐层神经元
|
||||
outNerve.connectFather(lastNerveList);
|
||||
outNerves.add(outNerve);
|
||||
outNerveList.add(outNerve);
|
||||
}
|
||||
//生成softMax层
|
||||
createSoftMax(isShowLog, isSoftMax, outNerveList, outNerves);
|
||||
//最后一层隐层神经元 与输出神经元进行连接
|
||||
for (Nerve nerve : lastNerveList) {
|
||||
nerve.connect(outNerves);
|
||||
}
|
||||
|
||||
//初始化感知神经元
|
||||
for (int i = 1; i < sensoryNerveNub + 1; i++) {
|
||||
SensoryNerve sensoryNerve = new SensoryNerve(i, 0);
|
||||
//感知神经元与第一层隐层神经元进行连接
|
||||
sensoryNerve.connect(nerveList);
|
||||
sensoryNerves.add(sensoryNerve);
|
||||
}
|
||||
}
|
||||
|
||||
private void createSoftMax(boolean isShowLog, boolean isSoftMax, List<OutNerve> outNerveList, List<Nerve> outNerves) throws Exception {
|
||||
if (isSoftMax) {
|
||||
List<Nerve> softMaxList = new ArrayList<>();
|
||||
SoftMax softMax = new SoftMax(outNerveNub, false, outNerveList, isShowLog);
|
||||
softMaxList.add(softMax);
|
||||
for (Nerve nerve : outNerves) {
|
||||
nerve.connect(softMaxList);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void createRnnOutNerve(boolean initPower, boolean isShowLog, List<Nerve> nerveList, int depth) throws Exception {
|
||||
RnnOutNerveBody rnnOutNerveBody = new RnnOutNerveBody();
|
||||
List<Nerve> rnnOutNerves = new ArrayList<>();
|
||||
List<OutNerve> outNerveList = new ArrayList<>();
|
||||
rnnOutNerveBody.setDepth(depth);
|
||||
rnnOutNerveBody.setOutNerves(rnnOutNerves);
|
||||
for (int i = 1; i < outNerveNub + 1; i++) {
|
||||
OutNerve outNerve = new OutNerve(i, hiddenNerveNub, 0, studyPoint, initPower,
|
||||
activeFunction, false, isShowLog, rzType, lParam, isSoftMax, 0, 0);
|
||||
outNerve.connectFather(nerveList);//每一层的输出神经元 链接每一层的隐层神经元
|
||||
rnnOutNerves.add(outNerve);
|
||||
outNerveList.add(outNerve);
|
||||
}
|
||||
createSoftMax(isShowLog, isSoftMax, outNerveList, rnnOutNerves);
|
||||
for (Nerve nerve : nerveList) {
|
||||
nerve.connectOut(rnnOutNerves);
|
||||
}
|
||||
rnnOutNerveBodies.add(rnnOutNerveBody);
|
||||
}
|
||||
|
||||
public void initRnn(boolean initPower, boolean isShowLog) throws Exception {
|
||||
isRnn = true;
|
||||
this.initPower = initPower;
|
||||
initDepthNerve(false, 0, 0);//初始化深度隐层神经元
|
||||
for (int i = 0; i < depthNerves.size(); i++) {
|
||||
createRnnOutNerve(initPower, isShowLog, depthNerves.get(i), i + 1);
|
||||
}
|
||||
List<Nerve> nerveList = depthNerves.get(0);//第一层隐层神经元
|
||||
//初始化感知神经元
|
||||
for (int i = 1; i < sensoryNerveNub + 1; i++) {
|
||||
SensoryNerve sensoryNerve = new SensoryNerve(i, 0);
|
||||
//感知神经元与第一层隐层神经元进行连接
|
||||
sensoryNerve.connect(nerveList);
|
||||
sensoryNerves.add(sensoryNerve);
|
||||
}
|
||||
}
|
||||
|
||||
private void initDepthNerve(boolean isMatrix, int step, int kernLen) throws Exception {//初始化隐层神经元1
|
||||
for (int i = 0; i < hiddenDepth; i++) {//遍历深度
|
||||
List<Nerve> hiddenNerveList = new ArrayList<>();
|
||||
double studyPoint = this.studyPoint;
|
||||
if (studyList.contains(i)) {//加载每一层的学习率
|
||||
studyPoint = studyList.get(i);
|
||||
}
|
||||
if (studyPoint <= 0 || studyPoint > 1) {
|
||||
throw new Exception("studyPoint Values range from 0 to 1");
|
||||
}
|
||||
for (int j = 1; j < hiddenNerveNub + 1; j++) {//遍历同级
|
||||
int upNub;
|
||||
int downNub;
|
||||
if (i == 0) {
|
||||
upNub = sensoryNerveNub;
|
||||
} else {
|
||||
upNub = hiddenNerveNub;
|
||||
}
|
||||
if (i == hiddenDepth - 1) {//最后一层隐层神经元z
|
||||
downNub = outNerveNub;
|
||||
} else {
|
||||
downNub = hiddenNerveNub;
|
||||
}
|
||||
HiddenNerve hiddenNerve = new HiddenNerve(j, i + 1, upNub, downNub, studyPoint, initPower, activeFunction, isMatrix
|
||||
, rzType, lParam, step, kernLen, outNerveNub);
|
||||
hiddenNerveList.add(hiddenNerve);
|
||||
}
|
||||
depthNerves.add(hiddenNerveList);
|
||||
}
|
||||
initHiddenNerve();
|
||||
}
|
||||
|
||||
private void initHiddenNerve() {//初始化隐层神经元2
|
||||
for (int i = 0; i < hiddenDepth - 1; i++) {//遍历深度
|
||||
List<Nerve> hiddenNerveList = depthNerves.get(i);//当前遍历隐层神经元
|
||||
List<Nerve> nextHiddenNerveList = depthNerves.get(i + 1);//当前遍历的下一层神经元
|
||||
for (Nerve hiddenNerve : hiddenNerveList) {
|
||||
hiddenNerve.connect(nextHiddenNerveList);
|
||||
}
|
||||
for (Nerve nextHiddenNerve : nextHiddenNerveList) {
|
||||
nextHiddenNerve.connectFather(hiddenNerveList);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
106
src/main/java/org/dromara/tools/GMClustering.java
Normal file
106
src/main/java/org/dromara/tools/GMClustering.java
Normal file
@ -0,0 +1,106 @@
|
||||
package org.dromara.tools;
|
||||
|
||||
import org.dromara.matrixTools.Matrix;
|
||||
import org.dromara.entity.RGBNorm;
|
||||
|
||||
|
||||
/**
|
||||
* @param
|
||||
* @DATA
|
||||
* @Author LiDaPeng
|
||||
* @Description
|
||||
*/
|
||||
public class GMClustering extends MeanClustering {
|
||||
private double regionSize;//单区域面积
|
||||
|
||||
public double getRegionSize() {
|
||||
return regionSize;
|
||||
}
|
||||
|
||||
public void setRegionSize(double regionSize) {
|
||||
this.regionSize = regionSize;
|
||||
}
|
||||
|
||||
public GMClustering(int speciesQuantity, int maxTimes) throws Exception {
|
||||
super(speciesQuantity, maxTimes);
|
||||
}
|
||||
|
||||
public int getProbabilityDensity(double[] feature) throws Exception {//获取簇id
|
||||
double maxPower = 0;
|
||||
int id = 0;
|
||||
int index = 0;
|
||||
for (RGBNorm rgbNorm : matrices) {
|
||||
double power = rgbNorm.getGMProbability(feature);
|
||||
if (power > maxPower) {
|
||||
maxPower = power;
|
||||
id = index;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() throws Exception {
|
||||
super.start();
|
||||
for (RGBNorm rgbNorm : matrices) {//高斯系数初始化
|
||||
rgbNorm.gm();
|
||||
}
|
||||
for (int i = 0; i < 50; i++) {
|
||||
gmClustering();
|
||||
}
|
||||
}
|
||||
|
||||
public void insertParameter(Matrix matrix) throws Exception {
|
||||
int y = matrix.getY();
|
||||
int size = y / speciesQuantity;
|
||||
for (int i = 0; i <= y - size; i += size) {
|
||||
double[] feature = new double[size];
|
||||
RGBNorm rgbNorm = new RGBNorm();
|
||||
matrices.add(rgbNorm);
|
||||
for (int j = i; j < i + size; j++) {
|
||||
feature[j - i] = matrix.getNumber(0, j);
|
||||
}
|
||||
rgbNorm.insertFeature(feature);
|
||||
}
|
||||
}
|
||||
|
||||
private void clear() {
|
||||
for (RGBNorm rgbNorm : matrices) {//高斯系数初始化
|
||||
rgbNorm.clearRGB();
|
||||
}
|
||||
}
|
||||
|
||||
private void gmClustering() throws Exception {//进行gm聚类
|
||||
clear();
|
||||
for (double[] rgb : matrixList) {//遍历当前集合
|
||||
double allProbability = 0;//全概率
|
||||
double[] pro = new double[speciesQuantity];
|
||||
for (int i = 0; i < speciesQuantity; i++) {
|
||||
RGBNorm rgbNorm = matrices.get(i);
|
||||
double probability = rgbNorm.getGMProbability(rgb);
|
||||
//System.out.println("pro===" + probability);
|
||||
allProbability = allProbability + probability;
|
||||
pro[i] = probability;
|
||||
}
|
||||
//求每个簇的后验概率
|
||||
for (int i = 0; i < speciesQuantity; i++) {
|
||||
pro[i] = pro[i] / allProbability;
|
||||
}
|
||||
//判断概率最大的簇
|
||||
int index = 0;
|
||||
double max = 0;
|
||||
for (int i = 0; i < speciesQuantity; i++) {
|
||||
if (pro[i] > max) {
|
||||
max = pro[i];
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
//注入特征
|
||||
matrices.get(index).setGmFeature(rgb, pro[index]);
|
||||
}
|
||||
for (RGBNorm rgbNorm : matrices) {//高斯系数初始化
|
||||
rgbNorm.gm();
|
||||
}
|
||||
}
|
||||
}
|
146
src/main/java/org/dromara/tools/LVQ.java
Normal file
146
src/main/java/org/dromara/tools/LVQ.java
Normal file
@ -0,0 +1,146 @@
|
||||
package org.dromara.tools;
|
||||
|
||||
import org.dromara.matrixTools.Matrix;
|
||||
import org.dromara.matrixTools.MatrixOperation;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* @author lidapeng
|
||||
* @description LVQ 向量量化
|
||||
* @date 5:36 下午 2020/2/4
|
||||
*/
|
||||
public class LVQ {
|
||||
private int typeNub;//原型聚类个数,即分类个数(需要模型返回)
|
||||
private MatrixBody[] model;//原型向量(需要模型返回)
|
||||
private final List<MatrixBody> matrixList = new ArrayList<>();
|
||||
private final double studyPoint;//量化学习率
|
||||
private int length;//向量长度(需要返回)
|
||||
private boolean isReady = false;
|
||||
private final int lvqNub;
|
||||
private final MatrixOperation matrixOperation = new MatrixOperation();
|
||||
|
||||
public void setTypeNub(int typeNub) {
|
||||
this.typeNub = typeNub;
|
||||
}
|
||||
|
||||
public void setModel(MatrixBody[] model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public void setLength(int length) {
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
public void setReady(boolean ready) {
|
||||
isReady = ready;
|
||||
}
|
||||
|
||||
public boolean isReady() {
|
||||
return isReady;
|
||||
}
|
||||
|
||||
public int getTypeNub() {
|
||||
return typeNub;
|
||||
}
|
||||
|
||||
public int getLength() {
|
||||
return length;
|
||||
}
|
||||
|
||||
public LVQ(int typeNub, int lvqNub, double studyPoint) {
|
||||
this.typeNub = typeNub;
|
||||
this.lvqNub = lvqNub;
|
||||
this.studyPoint = studyPoint;
|
||||
model = new MatrixBody[typeNub];
|
||||
}
|
||||
|
||||
public MatrixBody[] getModel() throws Exception {
|
||||
if (!isReady) {
|
||||
throw new Exception("not study");
|
||||
}
|
||||
return model;
|
||||
}
|
||||
|
||||
public void insertMatrixBody(MatrixBody matrixBody) throws Exception {
|
||||
if (matrixBody.getMatrix().isVector() && matrixBody.getMatrix().isRowVector()) {
|
||||
Matrix matrix = matrixBody.getMatrix();
|
||||
if (matrixList.size() == 0) {
|
||||
matrixList.add(matrixBody);
|
||||
length = matrix.getY();
|
||||
} else {
|
||||
if (length == matrix.getY()) {
|
||||
matrixList.add(matrixBody);
|
||||
} else {
|
||||
throw new Exception("vector length is different");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new Exception("this matrix is not vector or rowVector");
|
||||
}
|
||||
}
|
||||
|
||||
private void study() throws Exception {
|
||||
for (MatrixBody matrixBody : matrixList) {
|
||||
Matrix matrix = matrixBody.getMatrix();//特征向量
|
||||
long type = matrixBody.getId();//类别
|
||||
double distEnd = 0;
|
||||
int id = 0;
|
||||
for (int i = 0; i < typeNub; i++) {
|
||||
MatrixBody modelBody = model[i];
|
||||
Matrix modelMatrix = modelBody.getMatrix();
|
||||
//修正矩阵与原矩阵的范数差
|
||||
double dist = vectorEqual(modelMatrix, matrix);
|
||||
if (distEnd == 0 || dist < distEnd) {
|
||||
id = modelBody.getId();
|
||||
distEnd = dist;
|
||||
}
|
||||
}
|
||||
MatrixBody modelBody = model[id];
|
||||
Matrix modelMatrix = modelBody.getMatrix();
|
||||
boolean isRight = id == type;
|
||||
Matrix matrix1 = op(matrix, modelMatrix, isRight);
|
||||
modelBody.setMatrix(matrix1);
|
||||
}
|
||||
}
|
||||
|
||||
//比较两个向量之间的范数差
|
||||
public double vectorEqual(Matrix matrix1, Matrix matrix2) throws Exception {
|
||||
Matrix matrix = matrixOperation.sub(matrix1, matrix2);
|
||||
return matrixOperation.getNorm(matrix);
|
||||
}
|
||||
|
||||
private Matrix op(Matrix matrix, Matrix modelMatrix, boolean isRight) throws Exception {
|
||||
Matrix matrix1 = matrixOperation.sub(matrix, modelMatrix);
|
||||
matrixOperation.mathMul(matrix1, studyPoint);
|
||||
Matrix matrix2;
|
||||
if (isRight) {
|
||||
matrix2 = matrixOperation.add(modelMatrix, matrix1);
|
||||
} else {
|
||||
matrix2 = matrixOperation.sub(modelMatrix, matrix1);
|
||||
}
|
||||
return matrix2;
|
||||
}
|
||||
|
||||
public void start() throws Exception {//开始向量量化聚类
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < typeNub; i++) {
|
||||
MatrixBody matrixBody = new MatrixBody();
|
||||
Matrix matrix = new Matrix(1, length);
|
||||
matrixBody.setMatrix(matrix);
|
||||
matrixBody.setId(i);
|
||||
for (int j = 0; j < length; j++) {
|
||||
matrix.setNub(0, j, random.nextDouble());
|
||||
}
|
||||
model[i] = matrixBody;
|
||||
}
|
||||
//初始化完成
|
||||
for (int i = 0; i < lvqNub; i++) {
|
||||
//System.out.println("================================");
|
||||
study();
|
||||
}
|
||||
isReady = true;
|
||||
}
|
||||
}
|
124
src/main/java/org/dromara/tools/RgbRegression.java
Normal file
124
src/main/java/org/dromara/tools/RgbRegression.java
Normal file
@ -0,0 +1,124 @@
|
||||
package org.dromara.tools;
|
||||
|
||||
|
||||
import org.dromara.matrixTools.Matrix;
|
||||
import org.dromara.matrixTools.MatrixOperation;
|
||||
|
||||
/**
|
||||
* @param
|
||||
* @DATA
|
||||
* @Author LiDaPeng
|
||||
* @Description rgb回归 Y = r *wr + g * wg + b* wb
|
||||
*/
|
||||
public class RgbRegression extends MatrixOperation {
|
||||
private double wr;
|
||||
private double wg;
|
||||
private double b;
|
||||
private Matrix RG;//rg矩阵
|
||||
private Matrix B;//b矩阵
|
||||
private Matrix RGB;//rgb矩阵
|
||||
private int xIndex = 0;//记录插入数量
|
||||
private boolean isRegression = false;//是否进行了回归
|
||||
private int regionNub;
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
public Matrix getRGB() {
|
||||
return RGB;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public Matrix getRGMatrix() {
|
||||
return RG;
|
||||
}
|
||||
|
||||
public Matrix getBMatrix() {
|
||||
return B;
|
||||
}
|
||||
|
||||
public void clear(int size) {
|
||||
RG = new Matrix(size, 3);
|
||||
RGB = new Matrix(size, 3);
|
||||
B = new Matrix(size, 1);
|
||||
xIndex = 0;
|
||||
regionNub = size;
|
||||
}
|
||||
|
||||
public int getRegionNub() {
|
||||
return regionNub;
|
||||
}
|
||||
|
||||
public void setRegionNub(int regionNub) {
|
||||
this.regionNub = regionNub;
|
||||
}
|
||||
|
||||
public double getWr() {
|
||||
return wr;
|
||||
}
|
||||
|
||||
public double getWg() {
|
||||
return wg;
|
||||
}
|
||||
|
||||
public double getB() {
|
||||
return b;
|
||||
}
|
||||
|
||||
public RgbRegression(int size) {//初始化rgb矩阵
|
||||
RG = new Matrix(size, 3);
|
||||
RGB = new Matrix(size, 3);
|
||||
B = new Matrix(size, 1);
|
||||
regionNub = size;
|
||||
xIndex = 0;
|
||||
}
|
||||
|
||||
public void insertRGB(double[] rgb) throws Exception {//rgb插入矩阵
|
||||
if (rgb.length == 3) {
|
||||
RGB.setNub(xIndex, 0, rgb[0]);
|
||||
RGB.setNub(xIndex, 1, rgb[1]);
|
||||
RGB.setNub(xIndex, 2, rgb[2]);
|
||||
|
||||
RG.setNub(xIndex, 0, rgb[0]);
|
||||
RG.setNub(xIndex, 1, rgb[1]);
|
||||
RG.setNub(xIndex, 2, 1.0);
|
||||
|
||||
B.setNub(xIndex, 0, rgb[2]);
|
||||
xIndex++;
|
||||
} else {
|
||||
throw new Exception("rgb length is not equals three");
|
||||
}
|
||||
}
|
||||
|
||||
public boolean regression() throws Exception {//开始进行回归
|
||||
if (xIndex > 0) {
|
||||
Matrix ws = getLinearRegression(RG, B);
|
||||
if (ws.getX() == 1 && ws.getY() == 1) {//矩阵奇异
|
||||
isRegression = false;
|
||||
} else {
|
||||
wr = ws.getNumber(0, 0);
|
||||
wg = ws.getNumber(1, 0);
|
||||
b = ws.getNumber(2, 0);
|
||||
isRegression = true;
|
||||
}
|
||||
return isRegression;
|
||||
// System.out.println("wr==" + wr + ",wg==" + wg + ",b==" + b);
|
||||
} else {
|
||||
throw new Exception("regression matrix size is zero");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package org.dromara.transFormer.model;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CodecBlockModel {
|
||||
private MultiSelfAttentionModel multiSelfAttentionModel;//注意力层model
|
||||
private LayNormModel attentionLayNormModel;//残差1层model
|
||||
private List<double[][]> fistNervesModel;//FNN层第一层model
|
||||
private List<double[][]> secondNervesModel;//FNN层第二层model
|
||||
private LayNormModel lineLayNormModel;//残差层最后2层model
|
||||
|
||||
public MultiSelfAttentionModel getMultiSelfAttentionModel() {
|
||||
return multiSelfAttentionModel;
|
||||
}
|
||||
|
||||
public void setMultiSelfAttentionModel(MultiSelfAttentionModel multiSelfAttentionModel) {
|
||||
this.multiSelfAttentionModel = multiSelfAttentionModel;
|
||||
}
|
||||
|
||||
public LayNormModel getAttentionLayNormModel() {
|
||||
return attentionLayNormModel;
|
||||
}
|
||||
|
||||
public void setAttentionLayNormModel(LayNormModel attentionLayNormModel) {
|
||||
this.attentionLayNormModel = attentionLayNormModel;
|
||||
}
|
||||
|
||||
public List<double[][]> getFistNervesModel() {
|
||||
return fistNervesModel;
|
||||
}
|
||||
|
||||
public void setFistNervesModel(List<double[][]> fistNervesModel) {
|
||||
this.fistNervesModel = fistNervesModel;
|
||||
}
|
||||
|
||||
public List<double[][]> getSecondNervesModel() {
|
||||
return secondNervesModel;
|
||||
}
|
||||
|
||||
public void setSecondNervesModel(List<double[][]> secondNervesModel) {
|
||||
this.secondNervesModel = secondNervesModel;
|
||||
}
|
||||
|
||||
public LayNormModel getLineLayNormModel() {
|
||||
return lineLayNormModel;
|
||||
}
|
||||
|
||||
public void setLineLayNormModel(LayNormModel lineLayNormModel) {
|
||||
this.lineLayNormModel = lineLayNormModel;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package org.dromara.transFormer.model;
|
||||
|
||||
public class FirstDecoderModel {
|
||||
private MultiSelfAttentionModel multiSelfAttentionModel;//注意力层model
|
||||
private LayNormModel attentionLayNormModel;//残差1层model
|
||||
|
||||
public MultiSelfAttentionModel getMultiSelfAttentionModel() {
|
||||
return multiSelfAttentionModel;
|
||||
}
|
||||
|
||||
public void setMultiSelfAttentionModel(MultiSelfAttentionModel multiSelfAttentionModel) {
|
||||
this.multiSelfAttentionModel = multiSelfAttentionModel;
|
||||
}
|
||||
|
||||
public LayNormModel getAttentionLayNormModel() {
|
||||
return attentionLayNormModel;
|
||||
}
|
||||
|
||||
public void setAttentionLayNormModel(LayNormModel attentionLayNormModel) {
|
||||
this.attentionLayNormModel = attentionLayNormModel;
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package org.dromara.transFormer.model;
|
||||
|
||||
|
||||
public class LayNormModel {
|
||||
private double[][] bTa;//模型需要保存
|
||||
private double[][] power;//模型需要保存
|
||||
|
||||
public double[][] getbTa() {
|
||||
return bTa;
|
||||
}
|
||||
|
||||
public void setbTa(double[][] bTa) {
|
||||
this.bTa = bTa;
|
||||
}
|
||||
|
||||
public double[][] getPower() {
|
||||
return power;
|
||||
}
|
||||
|
||||
public void setPower(double[][] power) {
|
||||
this.power = power;
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package org.dromara.transFormer.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class LineBlockModel {
|
||||
private List<double[][]> hiddenNervesModel;//隐层model
|
||||
private List<double[][]> outNervesModel;//输出层model
|
||||
|
||||
public List<double[][]> getHiddenNervesModel() {
|
||||
return hiddenNervesModel;
|
||||
}
|
||||
|
||||
public void setHiddenNervesModel(List<double[][]> hiddenNervesModel) {
|
||||
this.hiddenNervesModel = hiddenNervesModel;
|
||||
}
|
||||
|
||||
public List<double[][]> getOutNervesModel() {
|
||||
return outNervesModel;
|
||||
}
|
||||
|
||||
public void setOutNervesModel(List<double[][]> outNervesModel) {
|
||||
this.outNervesModel = outNervesModel;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package org.dromara.transFormer.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MultiSelfAttentionModel {
|
||||
private double[][] powerModel;
|
||||
private List<QKVModel> qkvModelList;
|
||||
private int depth;
|
||||
|
||||
public int getDepth() {
|
||||
return depth;
|
||||
}
|
||||
|
||||
public void setDepth(int depth) {
|
||||
this.depth = depth;
|
||||
}
|
||||
|
||||
public double[][] getPowerModel() {
|
||||
return powerModel;
|
||||
}
|
||||
|
||||
public void setPowerModel(double[][] powerModel) {
|
||||
this.powerModel = powerModel;
|
||||
}
|
||||
|
||||
public List<QKVModel> getQkvModelList() {
|
||||
return qkvModelList;
|
||||
}
|
||||
|
||||
public void setQkvModelList(List<QKVModel> qkvModelList) {
|
||||
this.qkvModelList = qkvModelList;
|
||||
}
|
||||
}
|
40
src/main/java/org/dromara/transFormer/model/QKVModel.java
Normal file
40
src/main/java/org/dromara/transFormer/model/QKVModel.java
Normal file
@ -0,0 +1,40 @@
|
||||
package org.dromara.transFormer.model;
|
||||
|
||||
public class QKVModel {
|
||||
private double[][] Q;
|
||||
private double[][] K;
|
||||
private double[][] V;
|
||||
private int selfID;
|
||||
|
||||
public int getSelfID() {
|
||||
return selfID;
|
||||
}
|
||||
|
||||
public void setSelfID(int selfID) {
|
||||
this.selfID = selfID;
|
||||
}
|
||||
|
||||
public double[][] getQ() {
|
||||
return Q;
|
||||
}
|
||||
|
||||
public void setQ(double[][] q) {
|
||||
Q = q;
|
||||
}
|
||||
|
||||
public double[][] getK() {
|
||||
return K;
|
||||
}
|
||||
|
||||
public void setK(double[][] k) {
|
||||
K = k;
|
||||
}
|
||||
|
||||
public double[][] getV() {
|
||||
return V;
|
||||
}
|
||||
|
||||
public void setV(double[][] v) {
|
||||
V = v;
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package org.dromara.transFormer.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TransFormerModel {
|
||||
private List<CodecBlockModel> encoderBlockModels;//编码器模块
|
||||
private List<CodecBlockModel> decoderBlockModels;//解码器模块
|
||||
private FirstDecoderModel firstDecoderBlockModel;//第一个解码器模块
|
||||
private LineBlockModel lineBlockModel;//线性分类层
|
||||
|
||||
public List<CodecBlockModel> getEncoderBlockModels() {
|
||||
return encoderBlockModels;
|
||||
}
|
||||
|
||||
public void setEncoderBlockModels(List<CodecBlockModel> encoderBlockModels) {
|
||||
this.encoderBlockModels = encoderBlockModels;
|
||||
}
|
||||
|
||||
public List<CodecBlockModel> getDecoderBlockModels() {
|
||||
return decoderBlockModels;
|
||||
}
|
||||
|
||||
public void setDecoderBlockModels(List<CodecBlockModel> decoderBlockModels) {
|
||||
this.decoderBlockModels = decoderBlockModels;
|
||||
}
|
||||
|
||||
public FirstDecoderModel getFirstDecoderBlockModel() {
|
||||
return firstDecoderBlockModel;
|
||||
}
|
||||
|
||||
public void setFirstDecoderBlockModel(FirstDecoderModel firstDecoderBlockModel) {
|
||||
this.firstDecoderBlockModel = firstDecoderBlockModel;
|
||||
}
|
||||
|
||||
public LineBlockModel getLineBlockModel() {
|
||||
return lineBlockModel;
|
||||
}
|
||||
|
||||
public void setLineBlockModel(LineBlockModel lineBlockModel) {
|
||||
this.lineBlockModel = lineBlockModel;
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package org.dromara.transFormer.nerve;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author lidapeng
|
||||
* @description 动态神经元模型参数
|
||||
* @date 8:14 上午 2020/1/18
|
||||
*/
|
||||
public class DymNerveStudy {
|
||||
private List<Double> list = new ArrayList<>();
|
||||
|
||||
public List<Double> getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public void setList(List<Double> list) {
|
||||
this.list = list;
|
||||
}
|
||||
}
|
51
src/main/java/org/dromara/transFormer/nerve/HiddenNerve.java
Normal file
51
src/main/java/org/dromara/transFormer/nerve/HiddenNerve.java
Normal file
@ -0,0 +1,51 @@
|
||||
package org.dromara.transFormer.nerve;
|
||||
|
||||
|
||||
import org.dromara.matrixTools.Matrix;
|
||||
import org.dromara.i.ActiveFunction;
|
||||
import org.dromara.i.OutBack;
|
||||
import org.dromara.transFormer.LineBlock;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author lidapeng
|
||||
* 隐层神经元
|
||||
* @date 9:30 上午 2019/12/21
|
||||
*/
|
||||
public class HiddenNerve extends Nerve {
|
||||
|
||||
public HiddenNerve(int id, int depth, double studyPoint, ActiveFunction activeFunction, int sensoryNerveNub,
|
||||
int outNerveNub, LineBlock lineBlock, int regularModel, double regular
|
||||
, int coreNumber) throws Exception {//隐层神经元
|
||||
super(id, "HiddenNerve", studyPoint, activeFunction, sensoryNerveNub, 0,
|
||||
outNerveNub, lineBlock, regularModel, regular, coreNumber);
|
||||
this.depth = depth;
|
||||
}
|
||||
|
||||
public void receiveErrorMatrix(Matrix g, long eventId, Matrix allError) throws Exception {//
|
||||
updatePower(eventId, g, allError);
|
||||
}
|
||||
|
||||
public void receive(Matrix feature, long eventId, boolean isStudy, OutBack outBack,
|
||||
List<Integer> E, Matrix encoderFeature, boolean outAllPro) throws Exception {//接收上一个残差层传过来得参数
|
||||
Matrix out = opMatrix(feature, isStudy);
|
||||
sendMessage(eventId, out, isStudy, feature, outBack, E, encoderFeature, outAllPro);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void input(long eventId, Matrix parameter, boolean isStudy, Matrix allFeature, OutBack outBack,
|
||||
List<Integer> E, Matrix encoderFeature, boolean outAllPro) throws Exception {//第二层收到参数
|
||||
boolean allReady = insertMatrixParameter(eventId, parameter);
|
||||
if (allReady) {//参数齐了,开始计算
|
||||
Matrix out = opMatrix(reMatrixFeatures.get(eventId), isStudy);
|
||||
reMatrixFeatures.remove(eventId);
|
||||
beforeLayNorm.addNormFromNerve(eventId, isStudy, out, allFeature, outBack, E, encoderFeature, outAllPro);
|
||||
}
|
||||
}
|
||||
|
||||
public void postMessage(long eventId, Matrix feature, boolean isStudy, OutBack outBack, List<Integer> E, boolean outAllPro) throws Exception {
|
||||
Matrix out = opMatrix(feature, isStudy);
|
||||
sendOutMessage(eventId, out, isStudy, outBack, E, outAllPro);
|
||||
}
|
||||
}
|
38
src/main/java/org/dromara/transFormer/nerve/OutNerve.java
Normal file
38
src/main/java/org/dromara/transFormer/nerve/OutNerve.java
Normal file
@ -0,0 +1,38 @@
|
||||
package org.dromara.transFormer.nerve;
|
||||
|
||||
|
||||
import org.dromara.matrixTools.Matrix;
|
||||
import org.dromara.i.OutBack;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author lidapeng
|
||||
* 输出神经元
|
||||
* @date 11:25 上午 2019/12/21
|
||||
*/
|
||||
public class OutNerve extends Nerve {
|
||||
private final SoftMax softMax;
|
||||
|
||||
public OutNerve(int id, double studyPoint, int sensoryNerveNub, int hiddenNerveNub, int outNerveNub,
|
||||
SoftMax softMax, int regularModel, double regular, int coreNumber) throws Exception {
|
||||
super(id, "OutNerve", studyPoint, null, sensoryNerveNub,
|
||||
hiddenNerveNub, outNerveNub, null, regularModel, regular, coreNumber);
|
||||
this.softMax = softMax;
|
||||
}
|
||||
|
||||
void getGBySoftMax(Matrix g, long eventId) throws Exception {//接收softMax层回传梯度
|
||||
updatePower(eventId, g, null);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void toOut(long eventId, Matrix parameter, boolean isStudy, OutBack outBack, List<Integer> E, boolean outAllPro) throws Exception {
|
||||
boolean allReady = insertMatrixParameter(eventId, parameter);
|
||||
if (allReady) {
|
||||
Matrix out = opMatrix(reMatrixFeatures.get(eventId), isStudy);
|
||||
reMatrixFeatures.remove(eventId);
|
||||
softMax.toOut(eventId, out, isStudy, outBack, E, outAllPro);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package org.dromara.transFormer.nerve;
|
||||
|
||||
|
||||
import org.dromara.matrixTools.Matrix;
|
||||
import org.dromara.i.OutBack;
|
||||
import org.dromara.transFormer.CodecBlock;
|
||||
import org.dromara.transFormer.FirstDecoderBlock;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 感知神经元输入层
|
||||
*
|
||||
* @author lidapeng
|
||||
* @date 9:29 上午 2019/12/21
|
||||
*/
|
||||
public class SensoryNerve {
|
||||
private final CodecBlock firstEncoderBlock;
|
||||
private final FirstDecoderBlock firstDecoderBlock;
|
||||
|
||||
public SensoryNerve(CodecBlock firstEncoderBlock, FirstDecoderBlock firstDecoderBlock) throws Exception {
|
||||
this.firstEncoderBlock = firstEncoderBlock;
|
||||
this.firstDecoderBlock = firstDecoderBlock;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param eventId 唯一的事件id
|
||||
* @param encoderParameter 编码器输入特征
|
||||
* @param decoderParameter 解码器输入特征
|
||||
* @param isStudy 是否是学习 (学习状态没有输出)
|
||||
* @param E 标注
|
||||
* @param outBack 回调结果
|
||||
*/
|
||||
public void postMessage(long eventId, Matrix encoderParameter, Matrix decoderParameter, boolean isStudy, List<Integer> E
|
||||
, OutBack outBack, boolean outAllPro) throws Exception {//感知神经元输入
|
||||
firstEncoderBlock.sendInputMatrix(eventId, encoderParameter, isStudy, outBack, E, null, outAllPro);
|
||||
firstDecoderBlock.sendInputMatrix(eventId, decoderParameter, isStudy, outBack, E, outAllPro);
|
||||
}
|
||||
|
||||
}
|
15
src/main/java/org/dromara/voice/VoiceTest.java
Normal file
15
src/main/java/org/dromara/voice/VoiceTest.java
Normal file
@ -0,0 +1,15 @@
|
||||
package org.dromara.voice;
|
||||
|
||||
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class VoiceTest {
|
||||
public static void main(String[] args) throws UnsupportedAudioFileException, IOException {
|
||||
File file = new File("D:\\sondTest\\wo.wav");
|
||||
WaveFile wav = new WaveFile(file);
|
||||
int amplitudeExample = wav.getSampleInt(140); // 140th amplitude value.
|
||||
System.out.println("帧数:"+wav.getFramesCount());
|
||||
System.out.println("140帧的幅度:"+amplitudeExample);
|
||||
}
|
||||
}
|
24
src/main/java/org/dromara/yolo/YoloSample.java
Normal file
24
src/main/java/org/dromara/yolo/YoloSample.java
Normal file
@ -0,0 +1,24 @@
|
||||
package org.dromara.yolo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class YoloSample {
|
||||
private String locationURL;//图片本地url
|
||||
private List<YoloBody> yoloBodies;
|
||||
|
||||
public String getLocationURL() {
|
||||
return locationURL;
|
||||
}
|
||||
|
||||
public void setLocationURL(String locationURL) {
|
||||
this.locationURL = locationURL;
|
||||
}
|
||||
|
||||
public List<YoloBody> getYoloBodies() {
|
||||
return yoloBodies;
|
||||
}
|
||||
|
||||
public void setYoloBodies(List<YoloBody> yoloBodies) {
|
||||
this.yoloBodies = yoloBodies;
|
||||
}
|
||||
}
|
54
src/main/java/org/dromara/yolo/YoloTypeBack.java
Normal file
54
src/main/java/org/dromara/yolo/YoloTypeBack.java
Normal file
@ -0,0 +1,54 @@
|
||||
package org.dromara.yolo;
|
||||
|
||||
import org.dromara.matrixTools.Matrix;
|
||||
import org.dromara.i.OutBack;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class YoloTypeBack implements OutBack {
|
||||
private double out = -1;
|
||||
private int id = 0;
|
||||
|
||||
|
||||
public double getOut() {
|
||||
return out;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
out = -1;
|
||||
id = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getBack(double out, int id, long eventId) {
|
||||
if (out > this.out) {
|
||||
this.out = out;
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getSoftMaxBack(long eventId, List<Double> softMax) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void backWord(String word, long eventId) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getBackMatrix(Matrix matrix, int id, long eventId) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getWordVector(int id, double w) {
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user