⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.java

📁 java 实现的神经网络 解决4色问题 含有随机数生成初始partten
💻 JAVA
字号:
package Hopfield;

import java.util.Random;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Random generator = new Random();
		int n = 10;
		int s = 12;
		Net net_1 = new Net(n, s, 800, 800, 100, 10,.01);
		int [][] c = new int[s][2];
		for(int i = 0; i<s; i++){
			c[i][0] = 0;
			c[i][1] = 0;
		}
		
		//int[] c0 = new int[2], c1 = new int[2], c2 = new int[2], c3 = new int[2], c4 = new int[2], c5 = new int[2], c6 = new int[2], c7 = new int[2], c8 = new int[2];
		c[0][0] = 1;
		c[0][1] = 5;
		c[1][0] = 2;
		c[1][1] = 4;
		c[2][0] = 2;
		c[2][1] = 3;
		c[3][0] = 3;
		c[3][1] = 5;
		c[4][0] = 3;
		c[4][1] = 6;
		c[5][0] = 5;
		c[5][1] = 6;
		c[6][0] = 4;
		c[6][1] = 5;
		c[7][0] = 4;
		c[7][1] = 5;
		c[8][0] = 5;
		c[8][1] = 9;
		c[9][0] = 6;
		c[9][1] = 9;
		c[10][0]=  8;
		c[10][1]=  9;
		c[11][0]=  5;
		c[11][1]=  7;
		/*c[12][0]= 5;
		c[12][1]= 10;
		c[13][0]= 6;
		c[13][1]= 13;
		c[14][0]= 6;
		c[14][1]= 9;
		c[15][0]= 6;
		c[15][1]= 14;
		c[16][0]= 6;
		c[16][1]= 11;
		c[17][0]= 7;
		c[17][1]= 11;
		c[18][0]= 8;
		c[18][1]= 12;
		c[19][0]= 9;
		c[19][1]= 13;
		c[20][0]= 9;
		c[20][1]= 14;
		c[21][0]= 10;
		c[21][1]= 11;
		c[22][0]= 11;
		c[22][1]= 12;
		c[23][0]= 11;
		c[23][1]= 15;
		c[24][0]= 11;
		c[24][1]= 14;
		c[25][0]= 12;
		c[25][1]= 15;
		c[26][0]= 12;
		c[26][1]= 16;
		c[27][0]= 13;
		c[27][1]= 14;
		c[28][0]= 13;
		c[28][1]= 16;
		c[29][0]= 14;
		c[29][1]= 16;
		c[30][0]= 15;
		c[30][1]= 16;*/
		//c[31][0] = 1;
		//c[31][0] = 5;
		net_1.setCourse(0, c[0]);
		net_1.setCourse(1, c[1]);
		net_1.setCourse(2, c[2]);
		net_1.setCourse(3, c[3]);
		net_1.setCourse(4, c[4]);
		net_1.setCourse(5, c[5]);
		net_1.setCourse(6, c[6]);
		net_1.setCourse(7, c[7]);
		net_1.setCourse(8, c[8]);
		net_1.setCourse(9, c[9]);
		net_1.setCourse(10, c[10]);
		net_1.setCourse(11, c[11]);
		/*net_1.setCourse(12, c[12]);
		net_1.setCourse(13, c[13]);
		net_1.setCourse(14, c[14]);
		net_1.setCourse(15, c[15]);
		net_1.setCourse(16, c[16]);
		net_1.setCourse(17, c[17]);
		net_1.setCourse(18, c[18]);
		net_1.setCourse(19, c[19]);
		net_1.setCourse(20, c[20]);
		net_1.setCourse(21, c[21]);
		net_1.setCourse(22, c[22]);
		net_1.setCourse(23, c[23]);
		net_1.setCourse(24, c[24]);
		net_1.setCourse(25, c[25]);
		net_1.setCourse(26, c[26]);
		net_1.setCourse(27, c[27]);
		net_1.setCourse(28, c[28]);
		net_1.setCourse(29, c[29]);
		net_1.setCourse(30, c[30]);*/
		//net_1.setCourse(31, c[31]);
		net_1.generateAMartix();
		net_1.setTime(0, 0);
		// initial the neural state matrix
		for (int i = 1; i < n; i++) {
			for(int j = 0; j<n; j++){
//				double u_temp = generator.nextInt(2)-1;
//				net_1.LayerU[i][j] = u_temp;
//				if(u_temp>0){
//					net_1.setTime(i, j);
//				}else{
//					net_1.erTime(i, j);
//				}
				if(i == j){
					net_1.erTime(i, j);
				}else{
					net_1.erTime(i, j);
				}
				//net_1.erTime(i, j);
			}
		}
		//double e_temp = net_1.calculateE();
		//System.out.println(e_temp);	

//		for (int k = 0; k < 7; k++) {
//			int[] time_temp = net_1.getTiem(k);
//			for (int j = 0; j < time_temp.length; j++) {
//				if (k == 0) {
//					System.out.print("0 ");
//				} else {
//					System.out.print(time_temp[j] + " ");
//				}
//
//			}
//			System.out.println();
//		}
		// random select a vector
		while(true){
			double e = 0;
			for (int i = 0; i < 50000-1; i++) {
				for(int j = 1; j<n ; j++){
					for(int k = 0; k<n; k++){
						//calculate u
						double temp_u = net_1.calculateU(j, k);
						//System.out.println(temp_u);
						net_1.LayerU[j][k] += temp_u;
						
						//System.out.println(temp_u);
						//double u = 1/(1+Math.exp(-uu));
						//double temp_su= net_1.generateOutput(temp_u);
						//>0.5, temp_v = 1; else  = 0;
						int temp_v = net_1.neuralModel(net_1.LayerU[j][k]);
						if(temp_v == 1){
							net_1.setTime(j, k);
						}else{
							net_1.erTime(j, k);
						}
						
					}
				}
//				for(int j = 0; j<n; j++){
//					for(int k = 0; k<n; k++){
//						System.out.println(net_1.LayerU[j][k]+" ");
//					}
//					System.out.println();
//				}
				//double e_temp1 = net_1.calculateE();
				//if(i<20)
					//System.out.println(e_temp1);	
				//if(i==20)
					//System.out.println();
				//if(i>49980)
					//System.out.println(e_temp1);	
				//e = e_temp1;
			}
//			for(int i = 0; i<n; i++){
//			System.out.print(i+" ");
//		}
//		System.out.println();//		for(int i = 0; i<n; i++){
//			System.out.print(i+" ");
//			}
//			System.out.println();//		for(int i = 0; i<n; i++){
//			System.out.print(i+" ");
//			}
//			System.out.println();//		for(int i = 0; i<n; i++){
//			System.out.print(i+" ");
//			}
//			System.out.println();//		for(int i = 0; i<n; i++){
//			System.out.print(i+" ");
//			}
//			System.out.println();//		for(int i = 0; i<n; i++){
//			System.out.print(i+" ");
//			}
//			System.out.println();
			if(e == 0){break;}
			break;
		}
		
//		System.out.println("The final state :");
//		for(int j = 0; j<n; j++){
//		for(int k = 0; k<n; k++){
//		System.out.println(net_1.LayerU[j][k]+" ");
//	        }
//	    System.out.println();
//       }
		for (int i = 0; i < n; i++) {
			int[] time_temp = net_1.getTiem(i);
			System.out.print(i+" ");
			for (int j = 0; j < time_temp.length; j++) {
				if (i == 0) {
					System.out.print("0 ");   //course 0 is not used.
				} else {
					System.out.print(time_temp[j] + " ");
				}

			}
			System.out.println();
		}
		System.out.println("The weight matrix : ");
		int[][] temp_weight = net_1.getWeight();
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				System.out.print(temp_weight[i][j] + " ");
			}
			System.out.println();
		}

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -