Compare commits

..

No commits in common. "eeadfbdd384bd0d3a446ad65ca3f483e056498e2" and "43cd89faa5e7274875cb0100fff7af208dd84c46" have entirely different histories.

2 changed files with 84 additions and 402 deletions

75
tf.js
View File

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

411
tf.maxpat
View File

@ -10,7 +10,7 @@
}
,
"classnamespace" : "box",
"rect" : [ 44.0, 125.0, 1036.0, 622.0 ],
"rect" : [ 164.0, 165.0, 948.0, 575.0 ],
"bglocked" : 0,
"openinpresentation" : 0,
"default_fontsize" : 12.0,
@ -40,36 +40,26 @@
"assistshowspatchername" : 0,
"boxes" : [ {
"box" : {
"id" : "obj-44",
"maxclass" : "comment",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 858.0, 270.0, 150.0, 20.0 ],
"text" : "dump all training points"
}
}
, {
"box" : {
"id" : "obj-42",
"id" : "obj-40",
"linecount" : 2,
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 858.0, 296.0, 69.0, 22.0 ],
"text" : "dump_data"
"patching_rect" : [ 237.0, 453.0, 50.0, 36.0 ],
"text" : "9. 10. 11."
}
}
, {
"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)"
"id" : "obj-36",
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 768.0, 156.0, 111.0, 22.0 ],
"text" : "load weights kieran"
}
}
@ -80,287 +70,66 @@
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 858.0, 232.0, 65.0, 22.0 ],
"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"
"patching_rect" : [ 626.0, 156.0, 114.0, 22.0 ],
"text" : "save weights kieran"
}
}
, {
"box" : {
"data" : {
"weights" : [ {
"kieran" : [ {
"data" : {
"0" : -0.044006392359734,
"1" : 0.037017118185759,
"2" : -0.603930592536926,
"3" : 0.060808453708887,
"4" : 0.180193305015564,
"5" : 0.282816737890244,
"6" : 0.363093823194504,
"7" : 0.173229858279228
"0" : 0.927409112453461,
"1" : -0.10581861436367,
"2" : 0.110250025987625,
"3" : -1.001458287239075,
"4" : 0.689840376377106,
"5" : 0.396656483411789,
"6" : -1.090744495391846,
"7" : -0.033253565430641
}
,
"shape" : [ 2, 4 ]
}
, {
"data" : {
"0" : 0,
"1" : 0,
"2" : 0,
"3" : 0
"0" : -0.04672335088253,
"1" : 0.441919326782227,
"2" : -0.127905443310738,
"3" : 0.395313948392868
}
,
"shape" : [ 4 ]
}
, {
"data" : {
"0" : 0.224349781870842,
"1" : 0.860164880752563,
"2" : -0.442636489868164,
"3" : -0.214278265833855,
"4" : 0.491469591856003,
"5" : -0.450522541999817,
"6" : 0.438913941383362,
"7" : -0.085629880428314,
"8" : 0.372829973697662,
"9" : -0.778826951980591,
"10" : 0.724699020385742,
"11" : 0.034776996821165
"0" : 0.714273393154144,
"1" : 0.45893207192421,
"2" : 0.772015511989594,
"3" : -0.403766572475433,
"4" : 0.378416150808334,
"5" : 0.486913919448853,
"6" : -0.327280908823013,
"7" : -0.424903750419617,
"8" : -0.59527575969696,
"9" : -0.619619905948639,
"10" : -0.706632375717163,
"11" : -0.025489492341876
}
,
"shape" : [ 4, 3 ]
}
, {
"data" : {
"0" : 0,
"1" : 0,
"2" : 0
"0" : -0.584373593330383,
"1" : 0.079081602394581,
"2" : 0.754254102706909
}
,
"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",
@ -368,7 +137,7 @@
"numinlets" : 2,
"numoutlets" : 5,
"outlettype" : [ "dictionary", "", "", "", "" ],
"patching_rect" : [ 482.0, 161.0, 129.0, 22.0 ],
"patching_rect" : [ 768.0, 215.0, 133.0, 22.0 ],
"saved_object_attributes" : {
"embed" : 1,
"legacy" : 0,
@ -376,7 +145,7 @@
"parameter_mappable" : 0
}
,
"text" : "dict myDict @embed 1"
"text" : "dict weights @embed 1"
}
}
@ -394,12 +163,12 @@
, {
"box" : {
"id" : "obj-30",
"linecount" : 4,
"linecount" : 3,
"maxclass" : "comment",
"numinlets" : 1,
"numoutlets" : 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. Always Relu activation."
"patching_rect" : [ 484.0, 230.0, 150.0, 48.0 ],
"text" : "args are input shape, output shape, and size of the single hidden layer"
}
}
@ -443,7 +212,7 @@
"maxclass" : "comment",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 187.0, 25.0, 150.0, 20.0 ],
"patching_rect" : [ 128.0, 18.0, 150.0, 20.0 ],
"text" : "1. Add training points"
}
@ -456,7 +225,7 @@
"numoutlets" : 1,
"outlettype" : [ "bang" ],
"parameter_enable" : 0,
"patching_rect" : [ 204.0, 62.0, 24.0, 24.0 ]
"patching_rect" : [ 128.0, 49.0, 24.0, 24.0 ]
}
}
@ -467,7 +236,7 @@
"numinlets" : 1,
"numoutlets" : 4,
"outlettype" : [ "bang", "bang", "bang", "bang" ],
"patching_rect" : [ 204.0, 101.0, 52.0, 22.0 ],
"patching_rect" : [ 128.0, 118.0, 52.0, 22.0 ],
"text" : "t b b b b"
}
@ -479,8 +248,8 @@
"numinlets" : 1,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 204.0, 215.0, 112.0, 22.0 ],
"text" : "prepend data_point"
"patching_rect" : [ 171.0, 215.0, 107.0, 22.0 ],
"text" : "prepend dataPoint"
}
}
@ -491,7 +260,7 @@
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 266.0, 146.0, 55.0, 22.0 ],
"patching_rect" : [ 262.0, 163.0, 55.0, 22.0 ],
"text" : "3 3 5 6 7"
}
@ -503,7 +272,7 @@
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 266.0, 170.0, 55.0, 22.0 ],
"patching_rect" : [ 330.0, 163.0, 55.0, 22.0 ],
"text" : "4 4 7 8 9"
}
@ -515,7 +284,7 @@
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 204.0, 170.0, 55.0, 22.0 ],
"patching_rect" : [ 197.0, 163.0, 55.0, 22.0 ],
"text" : "2 2 3 4 5"
}
@ -527,7 +296,7 @@
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 204.0, 146.0, 55.0, 22.0 ],
"patching_rect" : [ 128.0, 163.0, 55.0, 22.0 ],
"text" : "1 1 1 2 3"
}
@ -539,7 +308,7 @@
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 560.0, 56.0, 65.0, 22.0 ],
"patching_rect" : [ 560.0, 74.0, 65.0, 22.0 ],
"text" : "predict 5 5"
}
@ -572,7 +341,7 @@
"numoutlets" : 1,
"offset" : [ 0.0, 0.0 ],
"outlettype" : [ "bang" ],
"patching_rect" : [ 560.0, 353.0, 400.0, 220.0 ],
"patching_rect" : [ 526.0, 284.0, 400.0, 220.0 ],
"viewvisibility" : 1
}
@ -601,8 +370,6 @@
"args" : [ 2, 3, 4 ],
"autostart" : 1,
"defer" : 0,
"node_bin_path" : "",
"npm_bin_path" : "",
"watch" : 1
}
,
@ -613,7 +380,7 @@
],
"lines" : [ {
"patchline" : {
"destination" : [ "obj-38", 0 ],
"destination" : [ "obj-40", 1 ],
"source" : [ "obj-1", 0 ]
}
@ -624,20 +391,6 @@
"source" : [ "obj-1", 1 ]
}
}
, {
"patchline" : {
"destination" : [ "obj-1", 0 ],
"source" : [ "obj-11", 0 ]
}
}
, {
"patchline" : {
"destination" : [ "obj-7", 0 ],
"source" : [ "obj-12", 0 ]
}
}
, {
"patchline" : {
@ -701,13 +454,6 @@
"source" : [ "obj-18", 2 ]
}
}
, {
"patchline" : {
"destination" : [ "obj-1", 0 ],
"source" : [ "obj-2", 0 ]
}
}
, {
"patchline" : {
@ -715,27 +461,6 @@
"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" : {
@ -746,15 +471,8 @@
}
, {
"patchline" : {
"destination" : [ "obj-12", 0 ],
"source" : [ "obj-38", 1 ]
}
}
, {
"patchline" : {
"destination" : [ "obj-40", 1 ],
"source" : [ "obj-38", 0 ]
"destination" : [ "obj-1", 0 ],
"source" : [ "obj-36", 0 ]
}
}
@ -764,13 +482,6 @@
"source" : [ "obj-4", 0 ]
}
}
, {
"patchline" : {
"destination" : [ "obj-1", 0 ],
"source" : [ "obj-42", 0 ]
}
}
, {
"patchline" : {