| 1 |
//--------------------------------------------------------------------- |
| 2 |
// name: wekinator-customize.ck |
| 3 |
// desc: a more extensive example of working with the Wekinator object, |
| 4 |
// with many options including per-output-channel configurations. |
| 5 |
// |
| 6 |
// achievement trophy to Yikai Li for the adaptation from Wekinator |
| 7 |
// system and source, and redesigning the API with Ge into Wekinator |
| 8 |
// chuck object; Yikai generated over 6000 lines of C++ code in this |
| 9 |
// substantial adaption effort |
| 10 |
// |
| 11 |
// lifetime achievement award to Rebecca Fiebrink from making Wekinator, |
| 12 |
// and showing us a humanistic and playful way forward in an AI-drenched |
| 13 |
// world... |
| 14 |
// |
| 15 |
// version: need chuck version 1.5.0.0 or higher |
| 16 |
// sorting: part of ChAI (ChucK for AI) |
| 17 |
// |
| 18 |
// uncomment for Wekinator API: |
| 19 |
// Wekinator.help(); |
| 20 |
// |
| 21 |
// authors: Yikai Li |
| 22 |
// Ge Wang (https://ccrma.stanford.edu/~ge/) |
| 23 |
// Rebecca Fiebrink (original Wekinator framework) |
| 24 |
// date: Winter 2023 |
| 25 |
//--------------------------------------------------------------------- |
| 26 |
|
| 27 |
// Wekinator instance |
| 28 |
global Wekinator wek; |
| 29 |
|
| 30 |
<<< "---------------", "" >>>; |
| 31 |
// set number of expect inputs |
| 32 |
// (if different than exisxting, this will clear all current observations) |
| 33 |
wek.inputDims(2); |
| 34 |
<<< "# input channels:", wek.inputDims() >>>; |
| 35 |
// set number of expect outputs |
| 36 |
// (if different than exisxting, this will clear all current observations) |
| 37 |
wek.outputDims(3); |
| 38 |
<<< "# output channels:", wek.outputDims() >>>; |
| 39 |
// set the type of task for Wekinator to do |
| 40 |
// current options: AI.Classification, AI.Regression |
| 41 |
wek.taskType( AI.Regression ); |
| 42 |
// print including taskType name |
| 43 |
<<< "task ID:", wek.taskType(), "==", wek.taskTypeName() >>>; |
| 44 |
|
| 45 |
// set regression-related attributes |
| 46 |
wek.setProperty( AI.Regression, "min", 0 ); |
| 47 |
wek.setProperty( AI.Regression, "max", 1 ); |
| 48 |
// how to handle values outside [min,max]? 0 = none, 1 = soft, 2 = hard |
| 49 |
wek.setProperty( AI.Regression, "limit", false ); |
| 50 |
// set classification-related attributes |
| 51 |
wek.setProperty( AI.Classification, "classes", 5 ); |
| 52 |
// set (global) MLP attributes |
| 53 |
// NOTE: these will overwrite the corresponding output channel-specific properties |
| 54 |
wek.setProperty( AI.MLP, "hiddenLayers", 1 ); |
| 55 |
wek.setProperty( AI.MLP, "nodesPerHiddenLayer", 0 ); // 0 = same as number of inputs |
| 56 |
wek.setProperty( AI.MLP, "learningRate", 0.01 ); |
| 57 |
wek.setProperty( AI.MLP, "epochs", 100 ); |
| 58 |
// set (global) KNN attributes |
| 59 |
// this is the default unless a more local attribute is set for a given output channel |
| 60 |
wek.setProperty(AI.KNN, "k", 5); |
| 61 |
<<< "---------------", "" >>>; |
| 62 |
// how to get properties; NOTE that each numeric property is associatd with int or float type |
| 63 |
<<< "regression min:", wek.getPropertyFloat(AI.Regression, "min") >>>; |
| 64 |
<<< "regression max:", wek.getPropertyFloat(AI.Regression, "max") >>>; |
| 65 |
<<< "regression limit type:", wek.getPropertyInt(AI.Regression, "limit") >>>; |
| 66 |
<<< "MLP hiddenLayers:", wek.getPropertyInt(AI.MLP, "hiddenLayers") >>>; |
| 67 |
<<< "MLP nodesPerHiddenLayer:", wek.getPropertyInt(AI.MLP, "nodesPerHiddenLayer") >>>; |
| 68 |
<<< "MLP learningRate:", wek.getPropertyFloat(AI.MLP, "learningRate") >>>; |
| 69 |
<<< "MLP epochs:", wek.getPropertyInt(AI.MLP, "epochs") >>>; |
| 70 |
<<< "classification # classes:", wek.getPropertyInt(AI.Classification, "classes") >>>; |
| 71 |
<<< "KNN k:", wek.getPropertyInt(AI.KNN, "k") >>>; |
| 72 |
<<< "---------------", "" >>>; |
| 73 |
|
| 74 |
// set the type of underlying algorithm used |
| 75 |
// options: AI.Regression: AI.MLP, AI.LR (currently not implemented) |
| 76 |
// options: AI.Classification: AI.KNN, AI.SVM, AI.DT (current not implemented) |
| 77 |
wek.modelType(AI.MLP); |
| 78 |
// print including modelType name |
| 79 |
<<< "modelType ID:", wek.modelType(), "==", wek.modelTypeName() >>>; |
| 80 |
|
| 81 |
// set any output channel-specific attributes, which locally |
| 82 |
// overrides the global attributes set in wek.setProperty() |
| 83 |
wek.setOutputProperty(0, "taskType", AI.Regression); // AI.Classification, AI.Regression |
| 84 |
wek.setOutputProperty(0, "modelType", AI.MLP); // AI.Regression: AI.MLP, AI.LR; AI.Classification: AI.KNN, AI.SVM, AI.DT |
| 85 |
wek.setOutputProperty(0, AI.Regression, "min", 0); |
| 86 |
wek.setOutputProperty(0, AI.Regression, "max", 1); |
| 87 |
wek.setOutputProperty(0, AI.Regression, "limit", false); // true: limit to [min,max]; false: no limit |
| 88 |
wek.setOutputProperty(0, AI.MLP, "hiddenLayers", 1); |
| 89 |
wek.setOutputProperty(0, AI.MLP, "nodesPerHiddenLayer", 0); // 0 = same as number of inputs |
| 90 |
wek.setOutputProperty(0, AI.MLP, "learningRate", 0.01); |
| 91 |
wek.setOutputProperty(0, AI.MLP, "epochs", 100); |
| 92 |
wek.setOutputProperty(0, AI.Classification, "classes", 5); |
| 93 |
wek.setOutputProperty(0, AI.KNN, "k", 5); |
| 94 |
wek.setOutputProperty(0, "connectedInputs", [0, 1]); // sets which input to incorporate into the model |
| 95 |
|
| 96 |
<<< "---------------", "" >>>; |
| 97 |
// get output channel-specific attributes |
| 98 |
<<< "taskType | output channel 0:", wek.getOutputPropertyInt(0, "taskType") >>>; |
| 99 |
<<< "modelType | output channel 0:", wek.getOutputPropertyInt(0, "modelType") >>>; |
| 100 |
<<< "regression min | output channel 0:", wek.getOutputPropertyFloat(0, AI.Regression, "min") >>>; |
| 101 |
<<< "regression max | output channel 0:", wek.getOutputPropertyFloat(0, AI.Regression, "max") >>>; |
| 102 |
<<< "regression limit type | output channel 0:", wek.getOutputPropertyInt(0, AI.Regression, "limit") >>>; |
| 103 |
<<< "MLP hiddenLayers | output channel 0:", wek.getOutputPropertyInt(0, AI.MLP, "hiddenLayers") >>>; |
| 104 |
<<< "MLP nodesPerHiddenLayer | output channel 0:", wek.getOutputPropertyInt(0, AI.MLP, "nodesPerHiddenLayer") >>>; |
| 105 |
<<< "MLP learningRate | output channel 0:", wek.getOutputPropertyFloat(0, AI.MLP, "learningRate") >>>; |
| 106 |
<<< "MLP epochs | output channel 0:", wek.getOutputPropertyInt(0, AI.MLP, "epochs") >>>; |
| 107 |
<<< "classification # classes | output channel 0:", wek.getOutputPropertyInt(0, AI.Classification, "classes") >>>; |
| 108 |
<<< "KNN k | output channel 0:", wek.getOutputPropertyInt(0, AI.KNN, "k") >>>; |
| 109 |
<<< "---------------", "" >>>; |
| 110 |
|
| 111 |
int indices[1]; |
| 112 |
// get indices of input channels connected to a particular output |
| 113 |
wek.getOutputProperty(0, "connectedInputs", indices); |
| 114 |
// print |
| 115 |
cherr <= "connected inputs | output channel 0: "; |
| 116 |
for( int i; i < indices.size(); i++ ) cherr <= indices[i] <= " "; |
| 117 |
cherr <= IO.newline(); |
| 118 |
|
| 119 |
// set as the current input |
| 120 |
wek.input([0.5, 0.5]); |
| 121 |
|
| 122 |
// randomize output for each channel between that channels min and max |
| 123 |
wek.randomizeOutputs(); |
| 124 |
// set as the current output |
| 125 |
wek.output([0.5, 0.5, 0.5]); |
| 126 |
|
| 127 |
// add an observation from the current input to output |
| 128 |
wek.add(); |
| 129 |
// increments the training round number |
| 130 |
// a round consists of one or more added observations |
| 131 |
wek.nextRound(); |
| 132 |
// add an observation explicitly from input and output |
| 133 |
wek.add([1.0, 1.0], [1.0, 1.0, 1.0]); |
| 134 |
// add an observation explicitly from input and output, for channel 0 |
| 135 |
wek.add(0, [2.0, 2.0], [2.0, 2.0, 2.0]); |
| 136 |
|
| 137 |
// train on all the observations so far |
| 138 |
wek.train(); |
| 139 |
|
| 140 |
// array to hold predictions |
| 141 |
float outputs[1]; |
| 142 |
// predict output given new input |
| 143 |
wek.predict([3.0, 3.0], outputs); |
| 144 |
// print output |
| 145 |
cherr <= "prediction: "; |
| 146 |
for( int i; i < outputs.size(); i++ ) cherr <= outputs[i] <= ", "; |
| 147 |
cherr <= IO.newline(); |
| 148 |
|
| 149 |
// get the number of observations (globally) |
| 150 |
<<< "number of observations (total):", wek.numObs() >>>; |
| 151 |
// get the number of observations for a specific output channel |
| 152 |
<<< "number of observations for output channel 0:", wek.numObs(0) >>>; |
| 153 |
|
| 154 |
<<< "---------------", "" >>>; |
| 155 |
// array to hold observations |
| 156 |
// due to a current ChucK language limitation, need to pre-allocate array to obs size |
| 157 |
float obs[wek.numObs()][0]; |
| 158 |
// retrieve current observations |
| 159 |
wek.getObs(obs); |
| 160 |
// print what we got |
| 161 |
<<< "ID, time, recording round, Input-0, Input-1, Output-0, Output-1, Output-2", "" >>>; |
| 162 |
for( 0 => int i; i < obs.size(); i++ ) |
| 163 |
{ |
| 164 |
for( 0 => int j; j < obs[i].size(); j++ ) |
| 165 |
{ |
| 166 |
cherr <= obs[i][j] <= ", "; |
| 167 |
} |
| 168 |
cherr <= IO.newline(); |
| 169 |
} |
| 170 |
|
| 171 |
// array to hold observations |
| 172 |
// due to a current ChucK language limitation, need to pre-allocate array to obs size |
| 173 |
float obs1[wek.numObs(0)][0]; |
| 174 |
// retrieve current observations for a specific channel |
| 175 |
wek.getObs(0, obs1); |
| 176 |
// print what we got |
| 177 |
<<< "ID, time, recording round, Input-0, Input-1, Output-0, Output-1, Output-2", "" >>>; |
| 178 |
for( 0 => int i; i < obs1.size(); i++ ) |
| 179 |
{ |
| 180 |
for( 0 => int j; j < obs1[i].size(); j++ ) |
| 181 |
{ |
| 182 |
cherr <= obs1[i][j] <= ", "; |
| 183 |
} |
| 184 |
cherr <= IO.newline(); |
| 185 |
} |
| 186 |
|
| 187 |
// delete the last round of observations (since the last wek.nextRound()) |
| 188 |
wek.deleteLastRound(); |
| 189 |
|
| 190 |
// clear observations by ID in the given range lo to hi |
| 191 |
wek.clearObs( 0, 1 ); |
| 192 |
// clear observations by ID in the given range lo to hi, for a specific channel |
| 193 |
wek.clearObs( 0, 0, 1 ); |
| 194 |
|
| 195 |
// save everything for this Wekinator (observations, model parameters, configurations) |
| 196 |
wek.save("./tmp.txt"); |
| 197 |
// load Wekinator state from file |
| 198 |
wek.load("./tmp.txt"); |
| 199 |
|
| 200 |
// export observations only (no model or configurations) |
| 201 |
wek.exportObs("./tmp.arff"); |
| 202 |
// export observations only (no model or configurations), for a specific channel |
| 203 |
wek.exportObs(0, "./tmp0.arff"); |
| 204 |
// import observations (NOTE: this replaces existing observations, |
| 205 |
// reset the round number, and will be associated with all output channels) |
| 206 |
wek.importObs("./tmp.arff"); |
| 207 |
|
| 208 |
// clear all observations |
| 209 |
wek.clearAllObs(); |
| 210 |
// clear all observations on a given channel |
| 211 |
wek.clearAllObs(0); |
| 212 |
|
| 213 |
<<< "---------------", "" >>>; |
| 214 |
// set whether Wekinator.add() should be associated with output channels (all) |
| 215 |
wek.setAllRecordStatus( false ); |
| 216 |
// set whether Wekinator.add() should be associated with a specific output channel |
| 217 |
wek.setOutputRecordStatus( 0, true ); |
| 218 |
// print |
| 219 |
<<< "record status | output channel 0:", wek.getOutputRecordStatus( 0 ) >>>; |
| 220 |
|
| 221 |
// setting this to false will "mask" out all output channels; will predict 0s |
| 222 |
wek.setAllRunStatus( false ); |
| 223 |
// <<< wek.getAllRunStatus() >>>; |
| 224 |
// setting this to false will "mask" out a specific output channels |
| 225 |
// that particular output channel will predict 0s |
| 226 |
wek.setOutputRunStatus(0, true); |
| 227 |
<<< "run status | output channel 0:", wek.getOutputRunStatus(0) >>>; |
| 228 |
|
| 229 |
// clear all observations, round number, model (parameters and local properties) |
| 230 |
// NOTE: the global properties will remain unchanged |
| 231 |
wek.clear(); |
| 232 |
<<< "---------------", "" >>>; |