From 65f2b3f8e87cc38a8fa5abc424cfd06b2258d0cb Mon Sep 17 00:00:00 2001 From: Thras <thrasyvoulos.karydis@cba.mit.edu> Date: Mon, 5 Feb 2018 10:58:30 -0500 Subject: [PATCH] first pass on ML bottom up --- files.html | 12 ++++ modules/index.html | 3 + modules/ml/fully-connected | 111 ++++++++++++++++++++++++++++++++++ modules/ml/nonlinearity | 118 +++++++++++++++++++++++++++++++++++++ 4 files changed, 244 insertions(+) create mode 100644 modules/ml/fully-connected create mode 100644 modules/ml/nonlinearity diff --git a/files.html b/files.html index b9ad79c..ce2ccd0 100644 --- a/files.html +++ b/files.html @@ -10,10 +10,19 @@ </script> <i> .git</i><br> <a href='./.gitignore'>.gitignore</a><br> +<i> .idea</i><br> +<i> inspectionProfiles</i><br> + <a href='./.idea/inspectionProfiles/Project_Default.xml'>Project_Default.xml</a><br> + <a href='./.idea/jsLibraryMappings.xml'>jsLibraryMappings.xml</a><br> + <a href='./.idea/mods.iml'>mods.iml</a><br> + <a href='./.idea/modules.xml'>modules.xml</a><br> + <a href='./.idea/vcs.xml'>vcs.xml</a><br> + <a href='./.idea/workspace.xml'>workspace.xml</a><br> <a href='./README.md'>README.md</a><br> <a href='./files.html'>files.html</a><br> <a href='./index.html'>index.html</a><br> <i> js</i><br> + <a href='./js/convnet-min.js'>convnet-min.js</a><br> <a href='./js/deviceserver.js'>deviceserver.js</a><br> <a href='./js/echoserver.js'>echoserver.js</a><br> <a href='./js/files.js'>files.js</a><br> @@ -85,6 +94,9 @@ <a href='./modules/math/scalar'>scalar</a><br> <i> mesh</i><br> <a href='./modules/mesh/slice'>slice</a><br> +<i> ml</i><br> + <a href='./modules/ml/fully-connected'>fully-connected</a><br> + <a href='./modules/ml/nonlinearity'>nonlinearity</a><br> <i> object</i><br> <a href='./modules/object/download'>download</a><br> <a href='./modules/object/set'>set</a><br> diff --git a/modules/index.html b/modules/index.html index 1dbe4ae..f0f0ed4 100644 --- a/modules/index.html +++ b/modules/index.html @@ -67,6 +67,9 @@ <a href="javascript:handler('modules/math/scalar')">scalar</a><br> <i>mesh</i><br> <a href="javascript:handler('modules/mesh/slice')">slice</a><br> +<i>ml</i><br> + <a href="javascript:handler('modules/ml/fully-connected')">fully-connected</a><br> + <a href="javascript:handler('modules/ml/nonlinearity')">nonlinearity</a><br> <i>object</i><br> <a href="javascript:handler('modules/object/download')">download</a><br> <a href="javascript:handler('modules/object/set')">set</a><br> diff --git a/modules/ml/fully-connected b/modules/ml/fully-connected new file mode 100644 index 0000000..629de28 --- /dev/null +++ b/modules/ml/fully-connected @@ -0,0 +1,111 @@ +// +// ML: Fully-Connected module +// +// Thras Karydis +// (c) Massachusetts Institute of Technology 2018 +// +// This work may be reproduced, modified, distributed, performed, and +// displayed for any purpose, but must acknowledge the mods +// project. Copyright is retained and must be preserved. The work is +// provided as is; no warranty is provided, and users accept all +// liability. +// +// closure +// +(function(){ +// +// module globals +// + var mod = {w: []} +// +// name +// + var name = 'Fully-Connected' +// +// initialization +// + var init = function() { + mod.x = [0.1, 0.7] + mod.w = [0.5, 0.5] + mod.xtext.value = mod.x + mod.wtext.value = mod.w + outputs.y.event() + } +// +// inputs +// + var inputs = { + x:{type:'vector1d', + event:function(evt){ + mod.x = evt.detail + mod.xtext.value = mod.x + outputs.y.event()}}} +// +// outputs +// + var outputs = { + y:{type:'number', + event:function(){ + var x = mod.x + var w = mod.w + var y = linearSum(x, w) + mod.ytext.value = y + mods.output(mod,'y',y)}}} +// +// interface +// + var interface = function(div){ + mod.div = div + div.appendChild(document.createTextNode('x: ')) + var input = document.createElement('input') + input.type = 'text' + input.size = 6 + input.addEventListener('input',function(evt){ + mod.x = parseFloat(mod.xtext.value) + outputs.y.event() + }) + div.appendChild(input) + mod.xtext = input + div.appendChild(document.createElement('br')) + div.appendChild(document.createTextNode('w: ')) + var input = document.createElement('input') + input.type = 'text' + input.size = 6 + input.addEventListener('input',function(evt){ + mod.w = parseFloat(mod.wtext.value) + outputs.y.event() + }) + div.appendChild(input) + mod.wtext = input + div.appendChild(document.createTextNode('y: ')) + var input = document.createElement('input') + input.type = 'text' + input.size = 6 + div.appendChild(input) + mod.ytext = input + } +// +// local functions +// + + function linearSum(arr1, arr2) { + var sum = 0; + for (var i = 0; i < arr1.length; i++) { + sum += arr1[i] * arr2[i]; + } + return sum + } + + + +// +// return values +// + return ({ + name:name, + init:init, + inputs:inputs, + outputs:outputs, + interface:interface + }) +}()) diff --git a/modules/ml/nonlinearity b/modules/ml/nonlinearity new file mode 100644 index 0000000..cf30e14 --- /dev/null +++ b/modules/ml/nonlinearity @@ -0,0 +1,118 @@ +// +// ML: Nonlinearity module +// +// Thras Karydis +// (c) Massachusetts Institute of Technology 2018 +// +// This work may be reproduced, modified, distributed, performed, and +// displayed for any purpose, but must acknowledge the mods +// project. Copyright is retained and must be preserved. The work is +// provided as is; no warranty is provided, and users accept all +// liability. +// +// closure +// +(function(){ +// +// module globals +// + var mod = {functions:{ + tanh: tanh, + relu: relu + }} +// +// name +// + var name = 'Nonlinearity' +// +// initialization +// + var init = function() { + mod.x = 123 + mod.xtext.value = mod.x + expression = '3*x+0.5' + mod.etext.value = expression + outputs.y.event() + } +// +// inputs +// + var inputs = { + x:{type:'number', + event:function(evt){ + mod.x = evt.detail + mod.xtext.value = mod.x + outputs.y.event()}}} +// +// outputs +// + var outputs = { + y:{type:'number', + event:function(){ + var x = mod.x + var y = tanh(x) //TODO:change + mod.ytext.value = y + mods.output(mod,'y',y)}}} +// +// interface +// + var interface = function(div){ + mod.div = div + div.appendChild(document.createTextNode('x: ')) + var input = document.createElement('input') + input.type = 'text' + input.size = 6 + input.addEventListener('input',function(evt){ + mod.x = parseFloat(mod.xtext.value) + outputs.y.event() + }) + div.appendChild(input) + mod.xtext = input + div.appendChild(document.createElement('br')) + div.appendChild(document.createTextNode('expression: ')) + var input = document.createElement('SELECT') + input.addEventListener('input',function(evt){ + outputs.y.event() + }) + input.setAttribute("id", "func-select"); + //select menu for functions + for (var property in mod.functions) { + console.log(property) + var z = document.createElement("option"); + z.setAttribute("value", property); + var t = document.createTextNode(property); + z.appendChild(t); + input.appendChild(z); + } + div.appendChild(input) + mod.func = input + div.appendChild(document.createElement('br')) + div.appendChild(document.createTextNode('y: ')) + var input = document.createElement('input') + input.type = 'text' + input.size = 6 + div.appendChild(input) + mod.ytext = input + + } +// +// local functions +// + function tanh(x) { + var y = Math.exp(2 * x); + return (y - 1) / (y + 1); + } + function relu(x){ + return Math.max(0, x); + } +// +// return values +// + return ({ + name:name, + init:init, + inputs:inputs, + outputs:outputs, + interface:interface + }) +}()) -- GitLab