GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — DeepLearning/java/src/DeepLearning/dA.java at master · cdingding/DeepLearning Expand file tree/ Copy pathdA.javaMore file actions 187 lines (156 loc) · 5.5 KB / Copy pathdA.javaFile metadata and controls187 lines (156 loc) · 5.5 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 package DeepLearning; import java.util.Random; import static DeepLearning.utils.*; public class dA { public int n_visible; public int N; public int n_hidden; public double[][] W; public double[] hbias; public double[] vbias; public Random rng; public dA(int N, int n_visible, int n_hidden, double[][] W, double[] hbias, double[] vbias, Random rng) { this.N = N; this.n_visible = n_visible; this.n_hidden = n_hidden; if(rng == null) this.rng = new Random(1234); else this.rng = rng; if(W == null) { this.W = new double[this.n_hidden][this.n_visible]; double a = 1.0 / this.n_visible; for(int i=0; i<this.n_hidden; i++) { for(int j=0; j<this.n_visible; j++) { this.W[i][j] = uniform(-a, a, rng); } } } else { this.W = W; } if(hbias == null) { this.hbias = new double[this.n_hidden]; for(int i=0; i<this.n_hidden; i++) this.hbias[i] = 0; } else { this.hbias = hbias; } if(vbias == null) { this.vbias = new double[this.n_visible]; for(int i=0; i<this.n_visible; i++) this.vbias[i] = 0; } else { this.vbias = vbias; } } public void get_corrupted_input(int[] x, int[] tilde_x, double p) { for(int i=0; i<n_visible; i++) { if(x[i] == 0) { tilde_x[i] = 0; } else { tilde_x[i] = binomial(1, p, rng); } } } // Encode public void get_hidden_values(int[] x, double[] y) { for(int i=0; i<n_hidden; i++) { y[i] = 0; for(int j=0; j<n_visible; j++) { y[i] += W[i][j] * x[j]; } y[i] += hbias[i]; y[i] = sigmoid(y[i]); } } // Decode public void get_reconstructed_input(double[] y, double[] z) { for(int i=0; i<n_visible; i++) { z[i] = 0; for(int j=0; j<n_hidden; j++) { z[i] += W[j][i] * y[j]; } z[i] += vbias[i]; z[i] = sigmoid(z[i]); } } public void train(int[] x, double lr, double corruption_level) { int[] tilde_x = new int[n_visible]; double[] y = new double[n_hidden]; double[] z = new double[n_visible]; double[] L_vbias = new double[n_visible]; double[] L_hbias = new double[n_hidden]; double p = 1 - corruption_level; get_corrupted_input(x, tilde_x, p); get_hidden_values(tilde_x, y); get_reconstructed_input(y, z); // vbias for(int i=0; i<n_visible; i++) { L_vbias[i] = x[i] - z[i]; vbias[i] += lr * L_vbias[i] / N; } // hbias for(int i=0; i<n_hidden; i++) { L_hbias[i] = 0; for(int j=0; j<n_visible; j++) { L_hbias[i] += W[i][j] * L_vbias[j]; } L_hbias[i] *= y[i] * (1 - y[i]); hbias[i] += lr * L_hbias[i] / N; } // W for(int i=0; i<n_hidden; i++) { for(int j=0; j<n_visible; j++) { W[i][j] += lr * (L_hbias[i] * tilde_x[j] + L_vbias[j] * y[i]) / N; } } } public void reconstruct(int[] x, double[] z) { double[] y = new double[n_hidden]; get_hidden_values(x, y); get_reconstructed_input(y, z); } private static void test_dA() { Random rng = new Random(123); double learning_rate = 0.1; double corruption_level = 0.3; int training_epochs = 100; int train_N = 10; int test_N = 2; int n_visible = 20; int n_hidden = 5; int[][] train_X = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0} }; dA da = new dA(train_N, n_visible, n_hidden, null, null, null, rng); // train for(int epoch=0; epoch<training_epochs; epoch++) { for(int i=0; i<train_N; i++) { da.train(train_X[i], learning_rate, corruption_level); } } // test data int[][] test_X = { {1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0} }; double[][] reconstructed_X = new double[test_N][n_visible]; // test for(int i=0; i<test_N; i++) { da.reconstruct(test_X[i], reconstructed_X[i]); for(int j=0; j<n_visible; j++) { System.out.printf("%.5f ", reconstructed_X[i][j]); } System.out.println(); } } public static void main(String[] args) { test_dA(); } } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |