Debian Codegraph @

examples/ai/wekinator/wekinator-basic.ck

raw
1 //---------------------------------------------------------------------
2 // name: wekinator-basic.ck
3 // desc: basic example using the built-in Wekinator object in ChucK,
4 // based on Rebecca Fiebrink's Wekinator framework;
5 // This example adds two groups of 3-2 input-output
6 // observations, trains, and predicts output based on new input.
7 // default task: regression
8 // default model: MLPs (one hidden layer with same # of nodes as input)
9 //
10 // version: need chuck version 1.5.0.0 or higher
11 // sorting: part of ChAI (ChucK for AI)
12 //
13 // uncomment for Wekinator API:
14 // Wekinator.help();
15 //
16 // authors: Yikai Li
17 // Ge Wang (https://ccrma.stanford.edu/~ge/)
18 // Rebecca Fiebrink (original Wekinator framework)
19 // date: Winter 2023
20 //---------------------------------------------------------------------
21
22 // instantiate a global Wekinator, so other files can access
23 global Wekinator wek;
24
25 // clear training observations
26 wek.clear();
27
28 // group count
29 1 => int groupNum;
30
31 cherr <= "-----------" <= IO.newline();
32 // add a group of data
33 addGroup( [0.1, 0.1, 0.1], [48.0, 60.0], 20 );
34 addGroup( [0.4, 0.4, 0.4], [5.0, 8.0], 20 );
35 addGroup( [0.8, 0.0, 0.4], [30.0, 30.0], 20 );
36 cherr <= "-----------" <= IO.newline();
37
38 // (optional) set model type; MLP is default
39 // AI.MLP => wek.modelType;
40 // (optional) set task type; regression is default
41 AI.Regression => wek.taskType;
42 // print
43 <<< "modelType:", wek.modelTypeName(),
44 "| taskType:", wek.taskTypeName() >>>;
45
46 // (optional) set properties to your liking
47 // <<< "changing Wekinator MLP properties...", "" >>>;
48 // wek.setProperty( AI.MLP, "hiddenLayers", 1 );
49 // note: 0 nodesPerHiddenLayer means "same as input layer"
50 // wek.setProperty( AI.MLP, "nodesPerHiddenLayer", 0 );
51 // wek.setProperty( AI.MLP, "learningRate", 0.01 );
52 // wek.setProperty( AI.MLP, "epochs", 100 );
53
54 // print model properties
55 <<< "Wekinator MLP hiddenLayers:", wek.getPropertyInt( AI.MLP, "hiddenLayers" ) >>>;
56 <<< "Wekinator MLP nodesPerHiddenLayer:", wek.getPropertyInt( AI.MLP, "nodesPerHiddenLayer" ) >>>;
57 <<< "Wekinator MLP learningRate", wek.getPropertyFloat( AI.MLP, "learningRate" ) >>>;
58 <<< "Wekinator MLP epochs:", wek.getPropertyInt( AI.MLP, "epochs" ) >>>;
59 // (optional) save the observations for loading later
60 // wek.exportObs( me.dir() + "currentData.arff" );
61
62 // print
63 cherr <= "-----------" <= IO.newline();
64 cherr <= "training..." <= IO.newline();
65 cherr <= "-----------" <= IO.newline();
66 // train using current training set
67 wek.train();
68
69 // new input
70 float x[];
71 // to hold predicted output
72 float y[2];
73
74 cherr <= "given new input, predict output..." <= IO.newline();
75 // predict and print
76 predict( [0.101, 0.1, 0.1], y );
77 predict( [0.401, 0.4, 0.4], y );
78 predict( [0.801, 0.0, 0.4], y );
79
80 // test run
81 repeat( 20 )
82 {
83 // generate random input
84 [Math.random2f(0,1), Math.random2f(0,1), Math.random2f(0,1)] @=> x;
85 // predict and print
86 predict( x, y );
87 // advance time (just for effect)
88 50::ms => now;
89 }
90
91
92 // add a group of training observations
93 fun void addGroup( float inputs[], float outputs[], int N )
94 {
95 // print
96 cherr <= "adding group " <= groupNum++;
97 // collect data
98 repeat( N )
99 {
100 // input and output (could be set in another file)
101 wek.input( inputs ); wek.output( outputs );
102 // add current input and output (typically these are different)
103 wek.add();
104 // print
105 cherr <= ".";
106 // advance time (delay on purpose to visualize the process)
107 (300.0/N)::ms => now;
108 }
109 // print
110 cherr <= IO.newline();
111 }
112
113 // predict and print
114 fun void predict( float inputs[], float outputs[] )
115 {
116 // predict output based on input; 3 inputs -> 2 outputs
117 wek.predict(inputs, outputs);
118 // print
119 cherr <= "(" <= inputs[0] <= ","<= inputs[1] <= ","<= inputs[2] <= ") -> ("
120 <= outputs[0] <= ", " <= outputs[1] <= ")" <= IO.newline();
121 }