package ann;
public class Node implements java.io.Serializable,Cloneable {
public double activation;
public double threshold;
public double weights[];
public double detweightslast[];
public double detthresholdlast;
public double error;
public int numOfweights;
public double amultinum;
public ArrayList arWeight=new ArrayList();
public Node() {
activation = 0;
error = 0;
threshold = 0;
amultinum = 1;
}
public Node(int numOfweights0) {
amultinum = 1;
numOfweights = numOfweights0;
weights = new double[numOfweights];
detweightslast = new double[numOfweights];
detthresholdlast = 0;
error = 0;
int i;
for (i = 0; i < numOfweights; i++) {
weights[i] = (2 * Math.random() - 1) * amultinum;
detweightslast[i] = 0;
}
threshold = (2 * Math.random() - 1) * amultinum;
}
public Node(double act, double thr, int numOfweights0) {
amultinum = 1;
numOfweights = numOfweights0;
activation = act;
threshold = thr;
weights = new double[numOfweights];
detweightslast = new double[numOfweights];
}
public void setWeight(ArrayList weight){
weights = new double[weight.size()];
for(int i=0;i<weight.size();i++){
weights[i] = ((Double)weight.get(i)).doubleValue();
}
}
}
package ann;
import ann.*;
public class Nnetwork implements java.io.Serializable,Cloneable{
public int n_input;
public int n_output;
public int n_layer;
public Layer hidelayer[];
public Layer input, output;
boolean iftrain[];
public double LH=-10;
public double LO=-10;
public double desiredOutputs[];
public double a=0.2;
public int connecttype;
public double total_error, total_error_one_circle_all;
public double error_compared_to_tolerance;
double total_error_one_circle[];
public int trainnum;
public Nnetwork() {
}
public Nnetwork(int inp, int hide[], int outp, int hidenum, int connecttype0) {
connecttype = connecttype0;
int i, j;
n_input = inp;
n_output = outp;
total_error_one_circle = new double[outp];
desiredOutputs = new double[outp];
output = new Layer(n_output);
for (i = 0; i < n_output; i++) {
output.nodes[i] = new Node(0);
}
n_layer = hidenum;
hidelayer = new Layer[n_layer];
for (i = n_layer - 1; i >= 0; i--) {
hidelayer[i] = new Layer(hide[i]);
for (j = 0; j < hidelayer[i].N_NODES; j++) {
if (i == n_layer - 1) {
hidelayer[i].nodes[j] = new Node(outp);
}
else {
hidelayer[i].nodes[j] = new Node(hidelayer[i + 1].N_NODES);
}
}
}
input = new Layer(n_input);
for (i = 0; i < n_input; i++) {
input.nodes[i] = new Node(hidelayer[0].N_NODES);
}
}
void FirstTimeSettings() {
for (int i = 0; i < n_layer; i++) {
int j;
for (j = 0; j < hidelayer[i].N_NODES; j++) {
hidelayer[i].nodes[j].threshold = (2 * Math.random() - 1) *
hidelayer[i].nodes[j].amultinum;
}
}
for (int i = 0; i < n_output; i++) {
output.nodes[i].threshold = (2 * Math.random() - 1) *
output.nodes[i].amultinum;
}
}
void BeforeTraining(double inp[], double outp[]) {
int i;
for (i = 0; i < n_input; i++) {
input.nodes[i].activation = inp[i];
}
for (i = 0; i < n_output; i++) {
desiredOutputs[i] = outp[i];
}
}
public void Calc_Activation(double result[]) {
int i, j, ci;
for (i = 0; i < n_layer; i++) {
if (i == 0) {
for (j = 0; j < hidelayer[i].N_NODES; j++) {
hidelayer[i].nodes[j].activation = 0;
for (ci = 0; ci < n_input; ci++) {
hidelayer[i].nodes[j].activation += input.nodes[ci].activation *input.nodes[ci].weights[j];
}
hidelayer[i].nodes[j].activation += hidelayer[i].nodes[j].threshold;
hidelayer[i].nodes[j].activation = activefun(hidelayer[i].nodes[j].activation);
}
}
else {
for (j = 0; j < hidelayer[i].N_NODES; j++) {
hidelayer[i].nodes[j].activation = 0;
for (ci = 0; ci < hidelayer[i - 1].N_NODES; ci++) {
hidelayer[i].nodes[j].activation += hidelayer[i -1].nodes[ci].activation * hidelayer[i - 1].nodes[ci].weights[j];
}
hidelayer[i].nodes[j].activation += hidelayer[i].nodes[j].threshold;
hidelayer[i].nodes[j].activation = activefun(hidelayer[i].nodes[j].activation);
}
}
}
for (j = 0; j < output.N_NODES; j++) {
output.nodes[j].activation = 0;
for (ci = 0; ci < hidelayer[n_layer - 1].N_NODES; ci++) {
output.nodes[j].activation += hidelayer[n_layer -1].nodes[ci].activation * hidelayer[n_layer -1].nodes[ci].weights[j];
}
output.nodes[j].activation += output.nodes[j].threshold;
output.nodes[j].activation = activefun(output.nodes[j].activation);
}
for (i = 0; i < n_output; i++) {
result[i] = output.nodes[i].activation;
}
}
void Calc_error_output() {
for (int x = 0; x < n_output; x++)
//output.nodes[x].error = output.nodes[x].activation * (1 - output.nodes[x].activation ) * (desiredOutputs[x] - output.nodes[x].activation );
{
output.nodes[x].error += (output.nodes[x].activation - desiredOutputs[x]);
output.nodes[x].error *= difactivefun(output.nodes[x].activation);
}
}
void Calc_error_hidden() {
int j, i;
for (j = 0; j < hidelayer[n_layer - 1].N_NODES; j++) {
for (int x = 0; x < n_output; x++) {
hidelayer[n_layer - 1].nodes[j].error += hidelayer[n_layer -
1].nodes[j].weights[x] * output.nodes[x].error;
}
hidelayer[n_layer -
1].nodes[j].error *=
difactivefun(hidelayer[n_layer - 1].nodes[j].activation);
}
for (i = n_layer - 2; i >= 0; i--) {
for (j = 0; j < hidelayer[i].N_NODES; j++) {
for (int x = 0; x < hidelayer[i + 1].N_NODES; x++) {
hidelayer[i].nodes[j].error += hidelayer[i].nodes[j].weights[x] *
hidelayer[i + 1].nodes[x].error;
}
hidelayer[i].nodes[j].error *=
difactivefun(hidelayer[i].nodes[j].activation);
}
}
}
void Calc_new_Thresholds() {
int i;
// computing the thresholds for next itration for hidden layer
for (i = 0; i < n_layer; i++) {
for (int x = 0; x < hidelayer[i].N_NODES; x++) {
double det = a * hidelayer[i].nodes[x].detthresholdlast +
hidelayer[i].nodes[x].error * LH;
hidelayer[i].nodes[x].detthresholdlast = det;
hidelayer[i].nodes[x].threshold += det;
}
}
for (int y = 0; y < output.N_NODES; y++) {
double det = a * output.nodes[y].detthresholdlast +
output.nodes[y].error * LO;
output.nodes[y].detthresholdlast = det;
output.nodes[y].threshold += det;
}
}
void Calc_new_weights_in_hidden() {
int i, j;
double temp = 0.0f;
for (j = 0; j < hidelayer[n_layer - 1].N_NODES; j++) {
temp = hidelayer[n_layer - 1].nodes[j].activation * LO;
for (int y = 0; y < n_output; y++) {
double det = a * hidelayer[n_layer - 1].nodes[j].detweightslast[y] +
temp * output.nodes[y].error;
hidelayer[n_layer - 1].nodes[j].detweightslast[y] = det;
hidelayer[n_layer - 1].nodes[j].weights[y] += det;
}
}
for (i = 0; i < n_layer - 1; i++) {
for (j = 0; j < hidelayer[i].N_NODES; j++) {
temp = hidelayer[i].nodes[j].activation * LH;
for (int y = 0; y < hidelayer[i + 1].N_NODES; y++) {
double det = a * hidelayer[i].nodes[j].detweightslast[y] +
temp * hidelayer[i + 1].nodes[y].error;
hidelayer[i].nodes[j].detweightslast[y] = det;
hidelayer[i].nodes[j].weights[y] += det;
}
}
}
}
void Calc_new_weights_in_input() {
int j;
double temp = 0.0f;
for (j = 0; j < input.N_NODES; j++) {
temp = input.nodes[j].activation * LH;
for (int y = 0; y < hidelayer[0].N_NODES; y++) {
double det = a * input.nodes[j].detweightslast[y] +
temp * hidelayer[0].nodes[y].error;
input.nodes[j].detweightslast[y] = det;
input.nodes[j].weights[y] += det;
}
}
}
double Calc_total_error_in_pattern() {
double temp = 0.0;
for (int x = 0; x < n_output; x++) {
[x].activation - desiredOutputs[x]);
continue;
temp += Math.pow((output.nodes[x].activation - desiredOutputs[x]), 2);
total_error_one_circle[x] += Math.pow((output.nodes[x].activation - desiredOutputs[x]), 2);
}
total_error = temp;
total_error_one_circle_all += total_error;
return temp;
}
void reset_error() {
for (int i = 0; i < n_input; i++) {
input.nodes[i].error = 0;
}
for (int i = 0; i < n_output; i++) {
output.nodes[i].error = 0;
}
for (int i = 0; i < n_layer; i++) {
for (int j = 0; j < hidelayer[i].N_NODES; j++) {
hidelayer[i].nodes[j].error = 0;
}
}
}
void reset_total_error() {
total_error_one_circle_all = 0;
for (int x = 0; x < n_output; x++) {
total_error_one_circle[x] = 0;
}
}
void Training_for_one_pattern(double result[]) {
Calc_Activation(result);
Calc_error_output();
Calc_error_hidden();
Calc_new_Thresholds();
Calc_new_weights_in_hidden();
Calc_new_weights_in_input();
}
public void Training(double inputs[][], double outputs[][], int num, boolean ifresort) {
clearlastdetweight();
iftrain = new boolean[num];
setiftrain(inputs, outputs, num, iftrain);
int neworder[] = new int[num];
sortrandom(neworder, num, ifresort);
reset_total_error();
for (int k = 0; k < num; k++) {
int i = neworder[k];
if (iftrain[i]) {
// System.out.println("k="+k+",i="+i+",iftrain[i]="+iftrain[i]);
reset_error();
BeforeTraining(inputs[i], outputs[i]);
double tmp[] = new double[output.N_NODES];
Calc_Activation(tmp);
Calc_error_output();
Calc_total_error_in_pattern();
// System.out.println(k+" "+tmp[0]+" "+outputs[i][0]+" "+output.nodes[0].activation +" "+ desiredOutputs[0]+" "+total_error_one_circle_all);
Calc_error_hidden();
Calc_new_Thresholds();
Calc_new_weights_in_hidden();
Calc_new_weights_in_input();
}
}
}
void TrainingAll(double inputs[][], double outputs[][], int num,boolean ifresort) {
clearlastdetweight();
iftrain = new boolean[num];
setiftrain(inputs, outputs, num, iftrain);
int neworder[] = new int[num];
sortrandom(neworder, num, ifresort);
reset_total_error();
reset_error();
for (int k = 0; k < num; k++) {
int i = neworder[k];
if (iftrain[i]) {
BeforeTraining(inputs[i], outputs[i]);
double tmp[] = new double[output.N_NODES];
Calc_Activation(tmp);
Calc_error_output();
Calc_total_error_in_pattern();
}
}
Calc_error_hidden();
Calc_new_Thresholds();
Calc_new_weights_in_hidden();
Calc_new_weights_in_input();
}
void clearlastdetweight() {
for (int i = 0; i < n_input; i++) {
input.nodes[i].detthresholdlast = 0;
for (int j = 0; j < input.nodes[i].numOfweights; j++) {
input.nodes[i].detweightslast[j] = 0;
}
}
for (int i = 0; i < n_output; i++) {
output.nodes[i].detthresholdlast = 0;
for (int j = 0; j < output.nodes[i].numOfweights; j++) {
output.nodes[i].detweightslast[j] = 0;
}
}
for (int k = 0; k < n_layer; k++) {
for (int i = 0; i < hidelayer[k].N_NODES; i++) {
hidelayer[k].nodes[i].detthresholdlast = 0;
for (int j = 0; j < hidelayer[k].nodes[i].numOfweights; j++) {
hidelayer[k].nodes[i].detweightslast[j] = 0;
}
}
}
}
void sortrandom(int neworder[], int num, boolean ifresort) {
for (int i = 0; i < num; i++) {
neworder[i] = i;
}
if (ifresort) {
for (int i = 0; i < num; i++) {
int pos = (int) (Math.random() * (num - i)) + i;
int tmp = neworder[pos];
neworder[pos] = neworder[i];
neworder[i] = tmp;
}
}
}
int setiftrain(double inputs[][], double outputs[][], int num,boolean iftrain[]) {
for (int i = 0; i < num; i++) {
iftrain[i] = true;
if (outputs[i][0] <= 0) {
iftrain[i] = false;
}
}
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
if (i != j) {
boolean ifsame = true;
for (int k = 0; k < n_input; k++) {
if (inputs[i][k] != inputs[j][k]) {
ifsame = false;
break;
}
}
if (ifsame) {
iftrain[i] = false;
}
}
if (iftrain[i] == false) {
break;
}
}
}
trainnum = 0;
for (int i = 0; i < num; i++) {
if (iftrain[i]) {
trainnum++;
}
}
return trainnum;
}
double sigmoid(double x) {
return 1 / (1 + Math.exp( -x));
}
double difsigmoid(double x) {
return x - x * x;
}
double tanh(double x) {
return (1 - Math.exp( -x)) / (1 + Math.exp( -x));
}
double diftanh(double x) {
return (1 - x * x) / 2;
}
double activefun(double x) {
if (connecttype == 0) {
return sigmoid(x);
}
else if (connecttype == 1) {
return tanh(x);
}
else {
return 0;
}
}
double difactivefun(double x) {
if (connecttype == 0) {
return difsigmoid(x);
}
else if (connecttype == 1) {
return diftanh(x);
}
else {
return 0;
}
}
}
package ann;
public class Layer implements java.io.Serializable,Cloneable{
public int N_NODES;
public Node nodes[];
public Layer() {
}
public Layer(int n)
{
N_NODES=n;
nodes=new Node[N_NODES];
}
}
分享到:
相关推荐
BP神经网络算法java实现.docx
BP神经网络算法的java实现
java实现的bp神经网络算法,代码超级简单,里面有实际数据例子,一看就懂!
用于BP神经网络算法,可以用于工程预测。
BP神经网络的Java语言实现(代码)。非常好用!!
BP算法的JAVA实现,BP神经网络的数学原理及其算法实现,实验使用IRIS数据集,BP神经网络,BP即Back Propagation的缩写,也就是反向传播的意思,顾名思义,将什么反向传播?文中将会解答。不仅如此,关于隐层的含义...
使用java语言编写的BP神经网络算法,实现了BP算法的功能
蚁群算法和BP神经网络的Java实现
JAVA 写的BP神经网络算法,实现了sin三角函数的模拟和数字识别的模拟,有源代码和文档说明
使用java语言描述bp神经网络算法,该算法比较容易让人理解。
利用成熟的 BP 神经网络算法对数据库中的照片进行学习, 然后实现对视频中的人脸进行性别识别,最后成功再电脑屏幕显示出来。 详细介绍参考:https://blog.csdn.net/sheziqiong/article/details/125458562
Java实现BP神经网络算法并演示绘制曲线,顺便写了一个基于神经网络的数字识别的小程序
这是俺上神经网络课时做的BP算法实现,支持多输入,多层网络,多输出,闲在那怪可惜的。共享源程序给大家批评批评。
采用三层前馈BP神经网络实现标准人脸YALE数据库的识别,编程语言为C系列语言。 二、BP神经网络的结构和学习算法 实验中建议采用如下最简单的三层BP神经网络,输入层为 ,有n个神经元节点,输出层具有m个神经元,网络...
BP神经网络实现算法,简简单单一个java文件,以后可能用到
基于BP神经网络算法的手写识别程序,带图形用户界面,Java实现.zip