Compare commits

...

10 Commits

Author SHA1 Message Date
eeadfbdd38 Removed useless load function 2024-11-20 16:23:56 +01:00
trian-gles
f4ef5ffcbc dataPoint renamed to data_point 2024-07-02 09:18:48 +02:00
trian-gles
ebecddde75 dump_data and clear_data 2024-07-02 09:18:14 +02:00
Kieran McAuliffe
621422979a
training_done message emitted 2024-07-01 15:54:49 +02:00
trian-gles
5ec1afe4b4 native like dictionary loading behaviour 2024-07-01 00:03:51 +02:00
trian-gles
694d2f3988 dump weights 2024-06-28 09:39:01 +02:00
trian-gles
2e22f60c2e relu activation 2024-06-28 09:13:47 +02:00
trian-gles
77f8d3bf57 script start demonstrated 2024-06-28 09:11:21 +02:00
trian-gles
ea17b45f1c loading configures saved architecture 2024-06-28 09:11:01 +02:00
trian-gles
9e14de36fb removed print statement 2024-06-27 16:01:27 +02:00
2 changed files with 402 additions and 84 deletions

75
tf.js
View File

@ -7,16 +7,15 @@ const inputShape = parseInt(args[0]);
const outputShape = parseInt(args[1]); const outputShape = parseInt(args[1]);
const hiddenSize = parseInt(args[2]); const hiddenSize = parseInt(args[2]);
// Define a model for linear regression.
var model = tf.sequential(); var model = tf.sequential();
model.add(tf.layers.dense({units: hiddenSize, inputShape: [inputShape]})); model.add(tf.layers.dense({units: hiddenSize, inputShape: [inputShape], activation: 'relu'}));
model.add(tf.layers.dense({units: outputShape})); model.add(tf.layers.dense({units: outputShape}));
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
// Generate some synthetic data for training.
var xsArr = []; var xsArr = [];
@ -24,6 +23,7 @@ var ysArr = [];
maxApi.addHandler("train", (epochs) => { maxApi.addHandler("train", (epochs) => {
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
// aggregate data // aggregate data
const xs = tf.tensor2d(xsArr, [xsArr.length, inputShape]); const xs = tf.tensor2d(xsArr, [xsArr.length, inputShape]);
@ -31,14 +31,28 @@ maxApi.addHandler("train", (epochs) => {
// Train the model using the data. // Train the model using the data.
model.fit(xs, ys, {epochs}); model.fit(xs, ys, {epochs});
maxApi.outlet("training_done");
}); });
maxApi.addHandler("dataPoint", (...data) => { maxApi.addHandler("data_point", (...data) => {
data.map((item) => parseFloat(item)); data.map((item) => parseFloat(item));
xsArr.push(data.slice(0, inputShape)); xsArr.push(data.slice(0, inputShape));
ysArr.push(data.slice(inputShape)); ysArr.push(data.slice(inputShape));
}); });
maxApi.addHandler("clear_data", () => {
xsArr = [];
ysArr = [];
});
maxApi.addHandler("dump_data", () => {
for (let i=0; i<xsArr.length; i++) {
maxApi.outlet(xsArr[i].concat(ysArr[i]));
}
});
maxApi.addHandler("predict", (...data) => { maxApi.addHandler("predict", (...data) => {
data.map((item) => parseFloat(item)); data.map((item) => parseFloat(item));
model.predict(tf.tensor2d([data], [1, inputShape])).array().then((value) => { model.predict(tf.tensor2d([data], [1, inputShape])).array().then((value) => {
@ -60,28 +74,43 @@ async function getWeights() {
maxApi.addHandler("save", (dictId, key) => { maxApi.addHandler("save", (dictId, key) => {
maxApi.getDict(dictId).then((dict) => { maxApi.getDict(dictId).then((dict) => {
getWeights().then((weights) => { getJson().then((json) => {
console.log(weights); dict[key] = json
dict[key] = weights;
maxApi.setDict(dictId, dict); maxApi.setDict(dictId, dict);
}); });
}); });
}); });
maxApi.addHandler("load", (dictId, key) => { async function getJson() {
maxApi.getDict(dictId).then((dict) => { let json = {}
let data = dict[key]; json.weights = await getWeights();
let tensors = []; json.model = model.toJSON(null, false);
data.forEach(item => { return json;
let shape = item.shape; }
let vals = [];
for (const [key, value] of Object.entries(item.data)) {
vals.push(value);
} function loadWeights(dict){
tensors.push(tf.tensor(vals, shape)); tf.models.modelFromJSON(dict.model).then((m) => {
}); model = m
model.setWeights(tensors);
}); let data = dict.weights;
}); let tensors = [];
data.forEach(item => {
let shape = item.shape;
let vals = [];
for (const [key, value] of Object.entries(item.data)) {
vals.push(value);
}
tensors.push(tf.tensor(vals, shape));
});
model.setWeights(tensors);
});
}
maxApi.addHandler("set_weights", loadWeights);
maxApi.addHandler("dump_weights", () => {
getJson().then((json) => {maxApi.outlet("weights", json);});
})

411
tf.maxpat
View File

@ -10,7 +10,7 @@
} }
, ,
"classnamespace" : "box", "classnamespace" : "box",
"rect" : [ 164.0, 165.0, 948.0, 575.0 ], "rect" : [ 44.0, 125.0, 1036.0, 622.0 ],
"bglocked" : 0, "bglocked" : 0,
"openinpresentation" : 0, "openinpresentation" : 0,
"default_fontsize" : 12.0, "default_fontsize" : 12.0,
@ -40,26 +40,36 @@
"assistshowspatchername" : 0, "assistshowspatchername" : 0,
"boxes" : [ { "boxes" : [ {
"box" : { "box" : {
"id" : "obj-40", "id" : "obj-44",
"linecount" : 2, "maxclass" : "comment",
"maxclass" : "message", "numinlets" : 1,
"numinlets" : 2, "numoutlets" : 0,
"numoutlets" : 1, "patching_rect" : [ 858.0, 270.0, 150.0, 20.0 ],
"outlettype" : [ "" ], "text" : "dump all training points"
"patching_rect" : [ 237.0, 453.0, 50.0, 36.0 ],
"text" : "9. 10. 11."
} }
} }
, { , {
"box" : { "box" : {
"id" : "obj-36", "id" : "obj-42",
"maxclass" : "message", "maxclass" : "message",
"numinlets" : 2, "numinlets" : 2,
"numoutlets" : 1, "numoutlets" : 1,
"outlettype" : [ "" ], "outlettype" : [ "" ],
"patching_rect" : [ 768.0, 156.0, 111.0, 22.0 ], "patching_rect" : [ 858.0, 296.0, 69.0, 22.0 ],
"text" : "load weights kieran" "text" : "dump_data"
}
}
, {
"box" : {
"id" : "obj-39",
"linecount" : 3,
"maxclass" : "comment",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 854.0, 175.0, 150.0, 48.0 ],
"text" : "clear all cached training data points (does not clear the weights)"
} }
} }
@ -70,66 +80,287 @@
"numinlets" : 2, "numinlets" : 2,
"numoutlets" : 1, "numoutlets" : 1,
"outlettype" : [ "" ], "outlettype" : [ "" ],
"patching_rect" : [ 626.0, 156.0, 114.0, 22.0 ], "patching_rect" : [ 858.0, 232.0, 65.0, 22.0 ],
"text" : "save weights kieran" "text" : "clear_data"
}
}
, {
"box" : {
"id" : "obj-40",
"linecount" : 2,
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 199.0, 355.0, 50.0, 36.0 ],
"text" : "1 1 1 2 3"
}
}
, {
"box" : {
"id" : "obj-38",
"maxclass" : "newobj",
"numinlets" : 3,
"numoutlets" : 3,
"outlettype" : [ "", "", "" ],
"patching_rect" : [ 230.0, 325.0, 98.0, 22.0 ],
"text" : "route list weights"
}
}
, {
"box" : {
"id" : "obj-37",
"linecount" : 4,
"maxclass" : "comment",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 510.0, 97.0, 150.0, 62.0 ],
"text" : "Send a dictionary with weights and architecture prepended by \"set_weights\" to load"
}
}
, {
"box" : {
"id" : "obj-2",
"maxclass" : "newobj",
"numinlets" : 1,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 482.0, 192.0, 119.0, 22.0 ],
"text" : "prepend set_weights"
}
}
, {
"box" : {
"id" : "obj-31",
"maxclass" : "button",
"numinlets" : 1,
"numoutlets" : 1,
"outlettype" : [ "bang" ],
"parameter_enable" : 0,
"patching_rect" : [ 482.0, 130.0, 24.0, 24.0 ]
}
}
, {
"box" : {
"id" : "obj-27",
"linecount" : 3,
"maxclass" : "comment",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 676.0, 172.0, 150.0, 48.0 ],
"text" : "output a dictionary containing model architecture and weights"
}
}
, {
"box" : {
"id" : "obj-7",
"maxclass" : "dict.view",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 305.0, 382.0, 220.0, 214.0 ]
}
}
, {
"box" : {
"id" : "obj-21",
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 676.0, 225.0, 86.0, 22.0 ],
"text" : "dump_weights"
}
}
, {
"box" : {
"id" : "obj-12",
"maxclass" : "newobj",
"numinlets" : 2,
"numoutlets" : 5,
"outlettype" : [ "dictionary", "", "", "", "" ],
"patching_rect" : [ 305.0, 355.0, 61.0, 22.0 ],
"saved_object_attributes" : {
"embed" : 0,
"legacy" : 0,
"parameter_enable" : 0,
"parameter_mappable" : 0
}
,
"text" : "dict"
}
}
, {
"box" : {
"id" : "obj-11",
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 9.0, 185.0, 94.0, 22.0 ],
"text" : "script start 2 3 4"
}
}
, {
"box" : {
"id" : "obj-8",
"linecount" : 2,
"maxclass" : "comment",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 7.0, 134.0, 150.0, 34.0 ],
"text" : "0.5. Restart the main script"
} }
} }
, { , {
"box" : { "box" : {
"data" : { "data" : {
"kieran" : [ { "weights" : [ {
"data" : { "data" : {
"0" : 0.927409112453461, "0" : -0.044006392359734,
"1" : -0.10581861436367, "1" : 0.037017118185759,
"2" : 0.110250025987625, "2" : -0.603930592536926,
"3" : -1.001458287239075, "3" : 0.060808453708887,
"4" : 0.689840376377106, "4" : 0.180193305015564,
"5" : 0.396656483411789, "5" : 0.282816737890244,
"6" : -1.090744495391846, "6" : 0.363093823194504,
"7" : -0.033253565430641 "7" : 0.173229858279228
} }
, ,
"shape" : [ 2, 4 ] "shape" : [ 2, 4 ]
} }
, { , {
"data" : { "data" : {
"0" : -0.04672335088253, "0" : 0,
"1" : 0.441919326782227, "1" : 0,
"2" : -0.127905443310738, "2" : 0,
"3" : 0.395313948392868 "3" : 0
} }
, ,
"shape" : [ 4 ] "shape" : [ 4 ]
} }
, { , {
"data" : { "data" : {
"0" : 0.714273393154144, "0" : 0.224349781870842,
"1" : 0.45893207192421, "1" : 0.860164880752563,
"2" : 0.772015511989594, "2" : -0.442636489868164,
"3" : -0.403766572475433, "3" : -0.214278265833855,
"4" : 0.378416150808334, "4" : 0.491469591856003,
"5" : 0.486913919448853, "5" : -0.450522541999817,
"6" : -0.327280908823013, "6" : 0.438913941383362,
"7" : -0.424903750419617, "7" : -0.085629880428314,
"8" : -0.59527575969696, "8" : 0.372829973697662,
"9" : -0.619619905948639, "9" : -0.778826951980591,
"10" : -0.706632375717163, "10" : 0.724699020385742,
"11" : -0.025489492341876 "11" : 0.034776996821165
} }
, ,
"shape" : [ 4, 3 ] "shape" : [ 4, 3 ]
} }
, { , {
"data" : { "data" : {
"0" : -0.584373593330383, "0" : 0,
"1" : 0.079081602394581, "1" : 0,
"2" : 0.754254102706909 "2" : 0
} }
, ,
"shape" : [ 3 ] "shape" : [ 3 ]
} }
],
"model" : {
"class_name" : "Sequential",
"config" : {
"name" : "sequential_1",
"layers" : [ {
"class_name" : "Dense",
"config" : {
"units" : 4,
"activation" : "relu",
"use_bias" : 1,
"kernel_initializer" : {
"class_name" : "VarianceScaling",
"config" : {
"scale" : 1,
"mode" : "fan_avg",
"distribution" : "normal",
"seed" : null
}
}
,
"bias_initializer" : {
"class_name" : "Zeros",
"config" : {
}
}
,
"kernel_regularizer" : null,
"bias_regularizer" : null,
"activity_regularizer" : null,
"kernel_constraint" : null,
"bias_constraint" : null,
"name" : "dense_Dense1",
"trainable" : 1,
"batch_input_shape" : [ null, 2 ],
"dtype" : "float32"
}
}
, {
"class_name" : "Dense",
"config" : {
"units" : 3,
"activation" : "linear",
"use_bias" : 1,
"kernel_initializer" : {
"class_name" : "VarianceScaling",
"config" : {
"scale" : 1,
"mode" : "fan_avg",
"distribution" : "normal",
"seed" : null
}
}
,
"bias_initializer" : {
"class_name" : "Zeros",
"config" : {
}
}
,
"kernel_regularizer" : null,
"bias_regularizer" : null,
"activity_regularizer" : null,
"kernel_constraint" : null,
"bias_constraint" : null,
"name" : "dense_Dense2",
"trainable" : 1
}
}
] ]
}
,
"keras_version" : "tfjs-layers 4.20.0",
"backend" : "tensor_flow.js"
}
} }
, ,
"id" : "obj-33", "id" : "obj-33",
@ -137,7 +368,7 @@
"numinlets" : 2, "numinlets" : 2,
"numoutlets" : 5, "numoutlets" : 5,
"outlettype" : [ "dictionary", "", "", "", "" ], "outlettype" : [ "dictionary", "", "", "", "" ],
"patching_rect" : [ 768.0, 215.0, 133.0, 22.0 ], "patching_rect" : [ 482.0, 161.0, 129.0, 22.0 ],
"saved_object_attributes" : { "saved_object_attributes" : {
"embed" : 1, "embed" : 1,
"legacy" : 0, "legacy" : 0,
@ -145,7 +376,7 @@
"parameter_mappable" : 0 "parameter_mappable" : 0
} }
, ,
"text" : "dict weights @embed 1" "text" : "dict myDict @embed 1"
} }
} }
@ -163,12 +394,12 @@
, { , {
"box" : { "box" : {
"id" : "obj-30", "id" : "obj-30",
"linecount" : 3, "linecount" : 4,
"maxclass" : "comment", "maxclass" : "comment",
"numinlets" : 1, "numinlets" : 1,
"numoutlets" : 0, "numoutlets" : 0,
"patching_rect" : [ 484.0, 230.0, 150.0, 48.0 ], "patching_rect" : [ 491.0, 238.0, 150.0, 62.0 ],
"text" : "args are input shape, output shape, and size of the single hidden layer" "text" : "args are input shape, output shape, and size of the single hidden layer. Always Relu activation."
} }
} }
@ -212,7 +443,7 @@
"maxclass" : "comment", "maxclass" : "comment",
"numinlets" : 1, "numinlets" : 1,
"numoutlets" : 0, "numoutlets" : 0,
"patching_rect" : [ 128.0, 18.0, 150.0, 20.0 ], "patching_rect" : [ 187.0, 25.0, 150.0, 20.0 ],
"text" : "1. Add training points" "text" : "1. Add training points"
} }
@ -225,7 +456,7 @@
"numoutlets" : 1, "numoutlets" : 1,
"outlettype" : [ "bang" ], "outlettype" : [ "bang" ],
"parameter_enable" : 0, "parameter_enable" : 0,
"patching_rect" : [ 128.0, 49.0, 24.0, 24.0 ] "patching_rect" : [ 204.0, 62.0, 24.0, 24.0 ]
} }
} }
@ -236,7 +467,7 @@
"numinlets" : 1, "numinlets" : 1,
"numoutlets" : 4, "numoutlets" : 4,
"outlettype" : [ "bang", "bang", "bang", "bang" ], "outlettype" : [ "bang", "bang", "bang", "bang" ],
"patching_rect" : [ 128.0, 118.0, 52.0, 22.0 ], "patching_rect" : [ 204.0, 101.0, 52.0, 22.0 ],
"text" : "t b b b b" "text" : "t b b b b"
} }
@ -248,8 +479,8 @@
"numinlets" : 1, "numinlets" : 1,
"numoutlets" : 1, "numoutlets" : 1,
"outlettype" : [ "" ], "outlettype" : [ "" ],
"patching_rect" : [ 171.0, 215.0, 107.0, 22.0 ], "patching_rect" : [ 204.0, 215.0, 112.0, 22.0 ],
"text" : "prepend dataPoint" "text" : "prepend data_point"
} }
} }
@ -260,7 +491,7 @@
"numinlets" : 2, "numinlets" : 2,
"numoutlets" : 1, "numoutlets" : 1,
"outlettype" : [ "" ], "outlettype" : [ "" ],
"patching_rect" : [ 262.0, 163.0, 55.0, 22.0 ], "patching_rect" : [ 266.0, 146.0, 55.0, 22.0 ],
"text" : "3 3 5 6 7" "text" : "3 3 5 6 7"
} }
@ -272,7 +503,7 @@
"numinlets" : 2, "numinlets" : 2,
"numoutlets" : 1, "numoutlets" : 1,
"outlettype" : [ "" ], "outlettype" : [ "" ],
"patching_rect" : [ 330.0, 163.0, 55.0, 22.0 ], "patching_rect" : [ 266.0, 170.0, 55.0, 22.0 ],
"text" : "4 4 7 8 9" "text" : "4 4 7 8 9"
} }
@ -284,7 +515,7 @@
"numinlets" : 2, "numinlets" : 2,
"numoutlets" : 1, "numoutlets" : 1,
"outlettype" : [ "" ], "outlettype" : [ "" ],
"patching_rect" : [ 197.0, 163.0, 55.0, 22.0 ], "patching_rect" : [ 204.0, 170.0, 55.0, 22.0 ],
"text" : "2 2 3 4 5" "text" : "2 2 3 4 5"
} }
@ -296,7 +527,7 @@
"numinlets" : 2, "numinlets" : 2,
"numoutlets" : 1, "numoutlets" : 1,
"outlettype" : [ "" ], "outlettype" : [ "" ],
"patching_rect" : [ 128.0, 163.0, 55.0, 22.0 ], "patching_rect" : [ 204.0, 146.0, 55.0, 22.0 ],
"text" : "1 1 1 2 3" "text" : "1 1 1 2 3"
} }
@ -308,7 +539,7 @@
"numinlets" : 2, "numinlets" : 2,
"numoutlets" : 1, "numoutlets" : 1,
"outlettype" : [ "" ], "outlettype" : [ "" ],
"patching_rect" : [ 560.0, 74.0, 65.0, 22.0 ], "patching_rect" : [ 560.0, 56.0, 65.0, 22.0 ],
"text" : "predict 5 5" "text" : "predict 5 5"
} }
@ -341,7 +572,7 @@
"numoutlets" : 1, "numoutlets" : 1,
"offset" : [ 0.0, 0.0 ], "offset" : [ 0.0, 0.0 ],
"outlettype" : [ "bang" ], "outlettype" : [ "bang" ],
"patching_rect" : [ 526.0, 284.0, 400.0, 220.0 ], "patching_rect" : [ 560.0, 353.0, 400.0, 220.0 ],
"viewvisibility" : 1 "viewvisibility" : 1
} }
@ -370,6 +601,8 @@
"args" : [ 2, 3, 4 ], "args" : [ 2, 3, 4 ],
"autostart" : 1, "autostart" : 1,
"defer" : 0, "defer" : 0,
"node_bin_path" : "",
"npm_bin_path" : "",
"watch" : 1 "watch" : 1
} }
, ,
@ -380,7 +613,7 @@
], ],
"lines" : [ { "lines" : [ {
"patchline" : { "patchline" : {
"destination" : [ "obj-40", 1 ], "destination" : [ "obj-38", 0 ],
"source" : [ "obj-1", 0 ] "source" : [ "obj-1", 0 ]
} }
@ -391,6 +624,20 @@
"source" : [ "obj-1", 1 ] "source" : [ "obj-1", 1 ]
} }
}
, {
"patchline" : {
"destination" : [ "obj-1", 0 ],
"source" : [ "obj-11", 0 ]
}
}
, {
"patchline" : {
"destination" : [ "obj-7", 0 ],
"source" : [ "obj-12", 0 ]
}
} }
, { , {
"patchline" : { "patchline" : {
@ -454,6 +701,13 @@
"source" : [ "obj-18", 2 ] "source" : [ "obj-18", 2 ]
} }
}
, {
"patchline" : {
"destination" : [ "obj-1", 0 ],
"source" : [ "obj-2", 0 ]
}
} }
, { , {
"patchline" : { "patchline" : {
@ -461,6 +715,27 @@
"source" : [ "obj-20", 0 ] "source" : [ "obj-20", 0 ]
} }
}
, {
"patchline" : {
"destination" : [ "obj-1", 0 ],
"source" : [ "obj-21", 0 ]
}
}
, {
"patchline" : {
"destination" : [ "obj-33", 0 ],
"source" : [ "obj-31", 0 ]
}
}
, {
"patchline" : {
"destination" : [ "obj-2", 0 ],
"source" : [ "obj-33", 0 ]
}
} }
, { , {
"patchline" : { "patchline" : {
@ -471,8 +746,15 @@
} }
, { , {
"patchline" : { "patchline" : {
"destination" : [ "obj-1", 0 ], "destination" : [ "obj-12", 0 ],
"source" : [ "obj-36", 0 ] "source" : [ "obj-38", 1 ]
}
}
, {
"patchline" : {
"destination" : [ "obj-40", 1 ],
"source" : [ "obj-38", 0 ]
} }
} }
@ -482,6 +764,13 @@
"source" : [ "obj-4", 0 ] "source" : [ "obj-4", 0 ]
} }
}
, {
"patchline" : {
"destination" : [ "obj-1", 0 ],
"source" : [ "obj-42", 0 ]
}
} }
, { , {
"patchline" : { "patchline" : {