package com.lqyandpy.crf;
import java.util.*;
public class ANN {
public ArrayList<Node> Nodes=new ArrayList<Node>();//
public int NetworkSize;
public ArrayList<InputNode> getInputNodes(){//输入变量列表
ArrayList<InputNode> tempN=new ArrayList<InputNode>();
for(Node n:Nodes){
if(n.getNodeType()==3){
tempN.add((InputNode)n);
}
}
Collections.sort(tempN);
return tempN;
}
public ArrayList<OutputNode> getOutputNodes(){//输出单元列表
ArrayList<OutputNode> tempN=new ArrayList<OutputNode>();
for(Node n:Nodes){
if(n.getNodeType()==1){
tempN.add((OutputNode)n);
}
}
Collections.sort(tempN);
return tempN;
}
public ArrayList<HiddenNode> getHiddenNodes(){//隐含单元列表
ArrayList<HiddenNode> tempN=new ArrayList<HiddenNode>();
for(Node n:Nodes){
if(n.getNodeType()==2){
tempN.add((HiddenNode)n);
}
}
Collections.sort(tempN);
return tempN;
}
public double[] getOutput(double[] argD){//输入变量,求值
ArrayList<OutputNode> tempONS=this.getOutputNodes();
double[] tempR=new double[tempONS.size()];//准备输出
ArrayList<InputNode> tempINS=this.getInputNodes();//
//assert(argD.length==tempINS.size());
for(int i=0;i<argD.length;i++){
tempINS.get(i).Value=argD[i];
}//为输入节点赋值
int i=0;
for(OutputNode on:tempONS){
tempR[i]=on.getOutput();
i++;
}
return tempR;
}
public ArrayList<Node> getNodesFrom(int argI){
return this.getNodesFrom(this.getNode(argI));
}
public ArrayList<Node> getNodesFrom(Node argN){
ArrayList<Node> tempL=new ArrayList<Node>();
switch(argN.getNodeType()){
case(1):break;//顶层节点,没有从它出发的连接
case(2):
for(Node n:this.Nodes){
ArrayList<Link> tempLK=n.getLinks();
for(Link l:n.getLinks()){
if(l.From.equals(argN)){
tempL.add(n);
}
}
}
break;//隐层节点,有从它出发的连接
case(3):
for(Node n:this.Nodes){
ArrayList<Link> tempLK=n.getLinks();
for(Link l:n.getLinks()){
if(l.From.equals(argN)){
tempL.add(n);
}
}
}
break;//输入层
case(4):
for(Node n:this.Nodes){
ArrayList<Link> tempLK=n.getLinks();
for(Link l:n.getLinks()){
if(l.From.equals(argN)){
tempL.add(n);
}
}
}
break;//偏移
default:break;
}
return tempL;
}
public void InitAnn(double[][] argD,int[] argB,ActivationFunction argHF,ActivationFunction argOF){//传入一个方阵,网络结构和初始权值.从最高层开始编号,越往下编号越大
//argB bias节点编号
this.NetworkSize=argD[0].length;
for(int i=0;i<this.NetworkSize;i++){
switch(this.NodeType(i, argD[i],argB)){
case(1)://新建顶层节点
OutputNode tempON=new OutputNode();
tempON.id=i;
tempON.setActivateFunction(argOF);
Nodes.add(tempON);
System.out.println("新建顶层节点 "+tempON.id);
break;
case(2)://新建隐层节点
HiddenNode tempHN=new HiddenNode();
tempHN.id=i;
tempHN.setActivateFunction(argHF);
Nodes.add(tempHN);
System.out.println("新建隐层节点 "+tempHN.id);
break;
case(3)://新建输入节点
InputNode tempIN=new InputNode();
tempIN.id=i;
Nodes.add(tempIN);
System.out.println("新建输入节点 "+tempIN.id);
break;
case(4)://新建bias节点
BiasNode tempBN=new BiasNode();
tempBN.id=i;
Nodes.add(tempBN);
System.out.println("新建偏移节点 "+tempBN.id);
break;
default:break;
}
}
for(Node n:Nodes){
int tempID=n.getID();
double[] tempWV=argD[tempID];//与节点关联的权值向量
switch(n.getNodeType()){
case(1):
OutputNode tempON=(OutputNode)n;
for(int i=0;i<tempWV.length;i++){
if(!new Double(tempWV[i]).equals(Double.NaN)){
Link tempL=new Link();
tempL.Weight=tempWV[i];
tempL.To=tempON;
tempL.From=this.getNode(i);
tempON.Links.add(tempL);
}
}
break;
case(2):
HiddenNode tempHN=(HiddenNode)n;
for(int i=0;i<tempWV.length;i++){
if(i>n.getID()&&!new Double(tempWV[i]).equals(Double.NaN)){
Link tempL=new Link();
tempL.Weight=tempWV[i];
tempL.To=tempHN;
tempL.From=this.getNode(i);
tempHN.Links.add(tempL);
}
}
break;
case(3):
break;
case(4):
break;
default:break;
}
}
}
public Node getNode(int argID){
Node tempN=null;
for(Node n:Nodes){
if(n.getID()==argID){
tempN=n;
}
}
return tempN;
}
public double[][] ConstructInitMatrix(int[][] argM){//通用初始化器
/* 1,2,n,n,n,n
* 3,4,5,6,n,n
* 7,8,9,n,n,n
* 10,11,12,13,n,n
*/
int tempD=0;
for(int i=0;i<argM.length;i++)
for(int j=0;j<argM[0].length;j++){
if(argM[i][j]>tempD)
tempD=argM[i][j];
}
tempD++;
double[][] tempR=new double[tempD][tempD];
for(int i=0;i<tempD;i++)
for(int j=0;j<tempD;j++)
tempR[i][j]=Double.POSITIVE_INFINITY;
for(int i=0;i<tempD;i++){
for(int j=0;j<tempD;j++){
if(i==j){//没有自联结
tempR[i][j]=Double.NaN;
}else{
if(new Double(tempR[i][j]).equals(Double.POSITIVE_INFINITY)){//如果还没有赋值
int tempI=this.LayerCompare(i, j, argM);
if((Math.abs(tempI)>1)||(tempI==0)){//跨越层级的节点无连接,同一层节点之间无链接
tempR[i][j]=tempR[j][i]=Double.NaN;
}else{
double r=Math.random();
tempR[i][j]=tempR[j][i]=r*(-1.0d)+(1-r)*1.0d;
}
}
}
}
}
return tempR;
}
/*三层结构的专用初始化器*/
public double[][] ConstructInitMatrix(int argI,int[] argOL,int[] argHL,int[] argIL){
double[][] tempR=new double[argI][argI];
for(int i=0;i<argI;i++)
for(int j=0;j<argI;j++)
tempR[i][j]=Double.POSITIVE_INFINITY;
for(int i=0;i<argI;i++){
for(int j=0;j<argI;j++){
if(i==j){//没有自联结
tempR[i][j]=Double.NaN;
}else{
if(new Double(tempR[i][j]).equals(Double.POSITIVE_INFINITY)){//如果还没有赋值
int tempI=this.LayerCompare(i, j, argOL, argHL, argIL);
if((Math.abs(tempI)>1)||(tempI==0)){//跨越层级的节点无连接,同一层节点之间无链接
tempR[i][j]=tempR[j][i]=Double.NaN;
}else{
tempR[i][j]=tempR[j][i]=Math.random();
}
}
}
}
}
return tempR;
}
private int LayerCompare(int argI,int argJ,int[][] argM){
return this.getIndexLayer(argI, argM)-this.getIndexLayer(argJ, argM);
}
private int getIndexLayer(int argI,int[][] argM){
int tempI=-1;
for(int i=0;i<argM.length;i++){
boolean found=false;
for(int j=0;j<argM[0].length;j++){
if(argM[i][j]==argI){
tempI=i;
break;
}
}
if(found){break;}
}
return tempI;
}
private int LayerCompare(int argI,int argJ,int[] argOL,int[] argHL,int[] argIL){//返回节点i和节点j所在层次的差值
return this.getIndexLayer(argI, argOL, argHL, argIL)-this.getIndexLayer(argJ, argOL, argHL, argIL);
}
private int getIndexLayer(int argI,int[] argOL,int[] argHL,int[] argIL){
int tempI=-1;
if(this.InArray(argI, argOL)){
tempI=1;
}else if(this.InArray(argI, argHL)){
tempI=2;
}else if(this.InArray(argI, argIL)){
tempI=3;
}
return tempI;
}
private boolean InArray(int argI,int[] argA){
boolean tempB=false;
for(int i:argA){
if(argI==i){
tempB=true;
break;
}
}
return tempB;
}
public double[][] getWeightMatrix(){
double[][] tempR=new double[this.NetworkSize][this.NetworkSize];
for(int i=0;i<tempR.length;i++)
for(int j=0;j<tempR[0].length;j++){
tempR[i][j]=Double.NaN;
}
for(Node n:this.Nodes){
for(Link l:n.getLinks()){
tempR[n.getID()][l.From.getID()]=tempR[l.From.getID()][n.getID()]=l.Weight;
}
}
return tempR;
}
private int NodeType(i
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ANN-BP.zip (31个子文件)
CRF
.project 379B
src
com
lqyandpy
crf
ANN.java 9KB
IdentityFunction.java 326B
OutputNode.java 2KB
TanhFunction.java 547B
Test.java 4KB
Node.java 547B
SignFunction.java 594B
InputNode.java 2KB
Trainer.java 4KB
BiasNode.java 1KB
ActivationFunction.java 154B
Link.java 151B
ConstantFunction.java 317B
HiddenNode.java 2KB
.settings
org.eclipse.jdt.core.prefs 598B
.classpath 3KB
bin
com
lqyandpy
crf
Link.class 363B
OutputNode.class 2KB
Trainer.class 5KB
HiddenNode.class 2KB
ActivationFunction.class 181B
InputNode.class 2KB
IdentityFunction.class 528B
TanhFunction.class 685B
Node.class 684B
SignFunction.class 614B
Test.class 2KB
ConstantFunction.class 528B
BiasNode.class 2KB
ANN.class 10KB
共 31 条
- 1
资源评论
- zzf8610112013-11-03I was really confused by the code. It should be better if more comment.
- xianspace20062014-04-08It's really good, thanks for sharing.
hello123401
- 粉丝: 7
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Java的在线宠物用品交易网站设计与实现源码
- 毕业设计-仿生六足机器人的制作全教程源码+电子元器件+程序代码+线路组件图+安装教程+搭建视频教程
- 基于ROS和webots的xrobot机械臂仿真初探C++源码
- 基于ROS的点焊机器人仿真与控制python源码+文档说明+使用说明+详细注释
- 基于vue实现的细粒度交通时空大数据分析系统+源代码+文档说明
- 安卓大作业-基于Electron的交通时空大数据分析挖掘系统客户端(Android)+源代码+文档说明+界面截图
- 基于Java的朱氏集团客户关系管理系统设计源码
- 基于C++的作业提交与批改系统设计源码
- 基于Vue2的移动端电影资讯网站设计源码
- 高分课程设计作业-基于QT的模仿宝石迷阵游戏C++源码+文档说明+界面截图
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功