Debian Codegraph @

examples/ai/mlp/model-save.ck

raw
1 //---------------------------------------------------------------------
2 // name: model-save.ck
3 // desc: training a basic artificial neural network and save the
4 // parameters to a model file, which can later be loaded
5 // using MLP.load()
6 //
7 // version: need chuck version 1.5.0.0 or higher
8 // sorting: part of ChAI (ChucK for AI)
9 //
10 // uncomment for MLP API:
11 // MLP.help();
12 //
13 // author: Yikai Li
14 // Ge Wang (https://ccrma.stanford.edu/~ge/)
15 // date: Winter 2023
16 //---------------------------------------------------------------------
17
18 // instantiate a multilayer perception
19 MLP mlp;
20
21 // nodes per layer: input, [hidden layer(s)], output
22 [3, 5, 2] @=> int nodesPerLayer[];
23 // initialize with the units per layer with default activation function
24 // default: AI.Sigmoid for all layers except output layer;
25 // default: AI.Linear for output layer
26 mlp.init( nodesPerLayer );
27
28 // (ALTERNATE #1) initialize with activation function
29 // mlp.init( nodesPerLayer, AI.Sigmoid );
30
31 // (ALTERNATE #2) specify activation function per layer
32 // [AI.ReLU, AI.Tanh, AI.Softmax] @=> int activationPerLayer[];
33 // (ALTERNATE #2) initialize with activation function per layer
34 // mlp.init( nodesPerLayer, activationPerLayer );
35
36 // input observations
37 [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]] @=> float X[][];
38 // output observations
39 [[0.1, 0.2], [0.3, 0.4]] @=> float Y[][];
40 // learning rate
41 0.001 => float learningRate;
42 // number of epochs
43 100 => int epochs;
44 // train the network
45 mlp.train( X, Y, learningRate, epochs );
46 // ALTERNATE: train with default learning_rate=0.001, epochs=100
47 // mlp.train(X, Y);
48
49 // file name
50 "model2.txt" => string filename;
51 // save the network
52 mlp.save( me.dir() + filename );
53 // print
54 <<< "saved model to file:", filename >>>;