Debian Codegraph @

examples/ai/mlp/mlp-manual.ck

raw
1 //---------------------------------------------------------------------
2 // name: mlp-manual.ck
3 // desc: training (step-by-step) a multilayer perception (MLP):
4 // (for a less manaul example, see mlp.ck)
5 //
6 // version: need chuck version 1.5.0.0 or higher
7 // sorting: part of ChAI (ChucK for AI)
8 //
9 // uncomment for MLP API:
10 // MLP.help();
11 //
12 // author: Yikai Li
13 // Ge Wang (https://ccrma.stanford.edu/~ge/)
14 // date: Winter 2023
15 //---------------------------------------------------------------------
16 // delay for each epoch (change for print speed)
17 // (or run with --silent for no print delay)
18 100::ms => dur T_PRINT;
19 // learning rate
20 0.01 => float learningRate;
21 // number of epochs
22 100 => int epochs;
23
24 // instantiate a multilayer perception (a basic neural network)
25 MLP mlp;
26
27 // neurons per layer: input, [hidden layer(s)], output
28 [3, 5, 5, 2] @=> int nodesPerLayer[];
29 // initialize with the # of neurons per layer
30 mlp.init( nodesPerLayer );
31
32 // input observations
33 [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]] @=> float X[][];
34 // output observations
35 [[0.1, 0.2], [0.3, 0.4]] @=> float Y[][];
36 // input and output array references
37 float x[], y[];
38
39
40 //---------------------------------------------------------------------
41 // train the neural network epoch by epoch
42 // this is a more "manual" alternative to simply calling MLP.train()
43 // intended for education and curiosity
44 //---------------------------------------------------------------------
45 for( int i; i < epochs; i++ )
46 {
47 // shuffle the observations
48 MLP.shuffle( X, Y );
49
50 // train over all observations
51 for( int j; j < X.size(); j++ )
52 {
53 // print weights and biases
54 chout <= IO.newline() <= "--- WEIGHTS AND BIASES |"
55 <= " EPOCH:" <= i+1 <= " OBSERVATION:" <= j+1 <= " ---" <= IO.newline();
56 visWeightsBiases();
57
58 // forward propagation
59 chout <= IO.newline() <= "--- FORWARD-PROPAGATION |"
60 <= " EPOCH:" <= i+1 <= " OBSERVATION:" <= j+1 <= " ---" <= IO.newline();
61 mlp.forward( X[j] );
62 visActivations();
63
64 // backpropagation
65 chout <= IO.newline() <= "--- BACKPROPAGATION |"
66 <= " EPOCH:" <= i+1 <= " OBSERVATION:" <= j+1 <= " ---" <= IO.newline();
67 mlp.backprop( Y[j], learningRate );
68 visGradients();
69 }
70
71 // print
72 chout <= IO.newline();
73 chout <= "********************************************" <= IO.newline();
74 chout <= "*** LEARNING-RATE:" <= learningRate <= " | EPOCH:" <= i+1 <= " DONE" <= IO.newline();
75 chout <= "********************************************" <= IO.newline();
76
77 // pause for print
78 T_PRINT => now;
79 }
80
81 // print weights and biases
82 chout <= IO.newline() <= "--- FINAL WEIGHTS AND BIASES |"
83 <= " EPOCH:" <= epochs <= " ---" <= IO.newline();
84 visWeightsBiases();
85 chout <= IO.newline();
86
87 // print
88 chout <= "********************************************" <= IO.newline();
89 chout <= "************* TRAINING DONE ****************" <= IO.newline();
90 chout <= "********************************************" <= IO.newline();
91 chout <= IO.newline();
92
93
94 //---------------------------------------------------------------------
95 // visualize activations
96 //---------------------------------------------------------------------
97 fun void visActivations()
98 {
99 // chout <= "[activations]" <= IO.newline();
100 for ( 0 => int k; k < nodesPerLayer.size(); k++ )
101 {
102 // activations
103 chout <= "layer " <= k <= " ACTIVATIONS" <= IO.newline();
104 float a[nodesPerLayer[k]];
105 mlp.getActivations(k, a);
106 for ( 0 => int l; l < nodesPerLayer[k]; l++ )
107 chout <= a[l] <= " ";
108 chout <= IO.newline();
109 }
110 }
111
112
113 //---------------------------------------------------------------------
114 // visualize weights and biases
115 //---------------------------------------------------------------------
116 fun void visWeightsBiases()
117 {
118 // chout <= "[weights]" <= IO.newline();
119 for ( 0 => int k; k < nodesPerLayer.size() - 1; k++ )
120 {
121 // weights
122 chout <= "layer " <= k <= " WEIGHTS" <= IO.newline();
123 float w[nodesPerLayer[k]][nodesPerLayer[k+1]];
124 mlp.getWeights(k, w);
125 for ( 0 => int l; l < nodesPerLayer[k]; l++ )
126 {
127 for ( 0 => int m; m < nodesPerLayer[k+1]; m++ )
128 chout <= w[l][m] <= " ";
129 chout <= IO.newline();
130 }
131 }
132
133 chout <= "---" <= IO.newline();
134 // chout <= "[biases]" <= IO.newline();
135 for ( 0 => int k; k < nodesPerLayer.size() - 1; k++ )
136 {
137 // biases
138 chout <= "layer " <= k <= " BIASES" <= IO.newline();
139 float b[nodesPerLayer[k+1]];
140 mlp.getBiases(k, b);
141 for ( 0 => int l; l < nodesPerLayer[k+1]; l++ )
142 chout <= b[l] <= " ";
143 chout <= IO.newline();
144 }
145 }
146
147
148 //---------------------------------------------------------------------
149 // visualize gradients
150 //---------------------------------------------------------------------
151 fun void visGradients()
152 {
153 // chout <= "[gradients]" <= IO.newline();
154 for ( 0 => int k; k < nodesPerLayer.size(); k++ )
155 {
156 // gradients
157 chout <= "layer " <= k <= " GRADIENTS" <= IO.newline();
158 float g[nodesPerLayer[k]];
159 mlp.getGradients(k, g);
160 for ( 0 => int l; l < nodesPerLayer[k]; l++ )
161 chout <= g[l] <= " ";
162 chout <= IO.newline();
163 }
164 }