121 lines · 3.8 KB · Text
RawDownload
//---------------------------------------------------------------------// name: wekinator-basic.ck// desc: basic example using the built-in Wekinator object in ChucK,//       based on Rebecca Fiebrink's Wekinator framework;//       This example adds two groups of 3-2 input-output//       observations, trains, and predicts output based on new input.//       default task: regression//       default model: MLPs (one hidden layer with same # of nodes as input)//// version: need chuck version 1.5.0.0 or higher// sorting: part of ChAI (ChucK for AI)//// uncomment for Wekinator API:// Wekinator.help();//// authors: Yikai Li//          Ge Wang (https://ccrma.stanford.edu/~ge/)//          Rebecca Fiebrink (original Wekinator framework)// date: Winter 2023//--------------------------------------------------------------------- // instantiate a global Wekinator, so other files can accessglobal Wekinator wek; // clear training observationswek.clear(); // group count1 => int groupNum; cherr <= "-----------" <= IO.newline();// add a group of dataaddGroup( [0.1, 0.1, 0.1], [48.0, 60.0], 20 );addGroup( [0.4, 0.4, 0.4], [5.0, 8.0], 20 );addGroup( [0.8, 0.0, 0.4], [30.0, 30.0], 20 );cherr <= "-----------" <= IO.newline(); // (optional) set model type; MLP is default// AI.MLP => wek.modelType;// (optional) set task type; regression is defaultAI.Regression => wek.taskType;// print<<< "modelType:", wek.modelTypeName(),    "| taskType:", wek.taskTypeName() >>>; // (optional) set properties to your liking// <<< "changing Wekinator MLP properties...", "" >>>;// wek.setProperty( AI.MLP, "hiddenLayers", 1 );// note: 0 nodesPerHiddenLayer means "same as input layer"// wek.setProperty( AI.MLP, "nodesPerHiddenLayer", 0 );// wek.setProperty( AI.MLP, "learningRate", 0.01 );// wek.setProperty( AI.MLP, "epochs", 100 );  // print model properties<<< "Wekinator MLP hiddenLayers:", wek.getPropertyInt( AI.MLP, "hiddenLayers" ) >>>;<<< "Wekinator MLP nodesPerHiddenLayer:", wek.getPropertyInt( AI.MLP, "nodesPerHiddenLayer" ) >>>;<<< "Wekinator MLP learningRate", wek.getPropertyFloat( AI.MLP, "learningRate" ) >>>;<<< "Wekinator MLP epochs:", wek.getPropertyInt( AI.MLP, "epochs" ) >>>;// (optional) save the observations for loading later// wek.exportObs( me.dir() + "currentData.arff" ); // printcherr <= "-----------" <= IO.newline();cherr <= "training..." <= IO.newline();cherr <= "-----------" <= IO.newline();// train using current training setwek.train(); // new inputfloat x[];// to hold predicted outputfloat y[2]; cherr <= "given new input, predict output..." <= IO.newline();// predict and printpredict( [0.101, 0.1, 0.1], y );predict( [0.401, 0.4, 0.4], y );predict( [0.801, 0.0, 0.4], y ); // test runrepeat( 20 ){    // generate random input    [Math.random2f(0,1), Math.random2f(0,1), Math.random2f(0,1)] @=> x;    // predict and print    predict( x, y );    // advance time (just for effect)    50::ms => now;}  // add a group of training observationsfun void addGroup( float inputs[], float outputs[], int N ){    // print    cherr <= "adding group " <= groupNum++;    // collect data    repeat( N )    {        // input and output (could be set in another file)        wek.input( inputs ); wek.output( outputs );        // add current input and output (typically these are different)        wek.add();        // print        cherr <= ".";        // advance time (delay on purpose to visualize the process)        (300.0/N)::ms => now;    }    // print    cherr <= IO.newline();} // predict and printfun void predict( float inputs[], float outputs[] ){    // predict output based on input; 3 inputs -> 2 outputs    wek.predict(inputs, outputs);    // print    cherr <= "(" <= inputs[0] <= ","<= inputs[1] <= ","<= inputs[2] <= ") -> ("          <= outputs[0] <= ", " <= outputs[1] <= ")" <= IO.newline();}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t