Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function () {
  2.   var main = null;
  3.   var modules = {
  4.       "require": {
  5.           factory: undefined,
  6.           dependencies: [],
  7.           exports: function (args, callback) { return require(args, callback); },
  8.           resolved: true
  9.       }
  10.   };
  11.   function define(id, dependencies, factory) {
  12.       return main = modules[id] = {
  13.           dependencies: dependencies,
  14.           factory: factory,
  15.           exports: {},
  16.           resolved: false
  17.       };
  18.   }
  19.   function resolve(definition) {
  20.       if (definition.resolved === true)
  21.           return;
  22.       definition.resolved = true;
  23.       var dependencies = definition.dependencies.map(function (id) {
  24.           return (id === "exports")
  25.               ? definition.exports
  26.               : (function () {
  27.                   if(modules[id] !== undefined) {
  28.                     resolve(modules[id]);
  29.                     return modules[id].exports;
  30.                   } else return require(id)
  31.               })();
  32.       });
  33.       definition.factory.apply(null, dependencies);
  34.   }
  35.   function collect() {
  36.       Object.keys(modules).map(function (key) { return modules[key]; }).forEach(resolve);
  37.       return (main !== null)
  38.         ? main.exports
  39.         : undefined
  40.   }
  41.  
  42.   define("src/matrix", ["require", "exports"], function (require, exports) {
  43.       "use strict";
  44.       exports.__esModule = true;
  45.       var Matrix = (function () {
  46.           function Matrix(inputs, outputs) {
  47.               this.inputs = inputs;
  48.               this.outputs = outputs;
  49.               this.data = new Float64Array(this.inputs * this.outputs);
  50.           }
  51.           Matrix.prototype.get = function (i, o) {
  52.               return this.data[i + (o * this.inputs)];
  53.           };
  54.           Matrix.prototype.set = function (i, o, value) {
  55.               this.data[i + (o * this.inputs)] = value;
  56.           };
  57.           return Matrix;
  58.       }());
  59.       exports.Matrix = Matrix;
  60.   });
  61.   define("src/tensor", ["require", "exports"], function (require, exports) {
  62.       "use strict";
  63.       exports.__esModule = true;
  64.       var select = function (type) {
  65.           switch (type) {
  66.               case "identity": return {
  67.                   activate: function (x) { return x; },
  68.                   derive: function (x) { return 1; }
  69.               };
  70.               case "tanh": return {
  71.                   activate: function (x) { return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x)); },
  72.                   derive: function (x) { return (1 - (x * x)); }
  73.               };
  74.               case "binary-step": return {
  75.                   activate: function (x) { return (x >= 0) ? 1 : 0; },
  76.                   derive: function (x) { return (x >= 0) ? 1 : 0; }
  77.               };
  78.               case "relu": return {
  79.                   activate: function (x) { return (x >= 0) ? x : 0; },
  80.                   derive: function (x) { return (x >= 0) ? 1 : 0; }
  81.               };
  82.               default: throw Error("unknown activation");
  83.           }
  84.       };
  85.       var Tensor = (function () {
  86.           function Tensor(units, activation, bias) {
  87.               if (activation === void 0) { activation = "identity"; }
  88.               if (bias === void 0) { bias = 1.0; }
  89.               this.data = new Float64Array(units + 1);
  90.               this.data[this.data.length - 1] = bias;
  91.               this.activation = select(activation);
  92.           }
  93.           return Tensor;
  94.       }());
  95.       exports.Tensor = Tensor;
  96.   });
  97.   define("src/network", ["require", "exports", "src/matrix"], function (require, exports, matrix_1) {
  98.       "use strict";
  99.       exports.__esModule = true;
  100.       var Network = (function () {
  101.           function Network(tensors) {
  102.               this.tensors = tensors;
  103.               this.output = new Array(this.tensors[this.tensors.length - 1].data.length - 1);
  104.               this.matrices = new Array(this.tensors.length - 1);
  105.               for (var i = 0; i < this.tensors.length - 1; i++) {
  106.                   this.matrices[i] = new matrix_1.Matrix(this.tensors[i + 0].data.length, this.tensors[i + 1].data.length - 1);
  107.               }
  108.               this.kernels = new Array(this.matrices.length);
  109.               for (var i = 0; i < this.kernels.length; i++) {
  110.                   this.kernels[i] = {
  111.                       input: this.tensors[i + 0],
  112.                       output: this.tensors[i + 1],
  113.                       matrix: this.matrices[i]
  114.                   };
  115.               }
  116.           }
  117.           Network.prototype.memory = function () {
  118.               var tensors = this.tensors.reduce(function (acc, t) { return acc + (t.data.byteLength); }, 0);
  119.               var matrices = this.matrices.reduce(function (acc, m) { return acc + (m.data.byteLength); }, 0);
  120.               return tensors + matrices;
  121.           };
  122.           Network.prototype.inputs = function () {
  123.               return (this.tensors[0].data.length - 1);
  124.           };
  125.           Network.prototype.outputs = function () {
  126.               return (this.tensors[this.tensors.length - 1].data.length - 1);
  127.           };
  128.           Network.prototype.forward = function (input) {
  129.               for (var i = 0; i < input.length; i++) {
  130.                   this.kernels[0].input.data[i] = input[i];
  131.               }
  132.               for (var k = 0; k < this.kernels.length; k++) {
  133.                   var kernel = this.kernels[k];
  134.                   for (var o = 0; o < kernel.matrix.outputs; o++) {
  135.                       var sum = 0;
  136.                       for (var i = 0; i < kernel.matrix.inputs; i++) {
  137.                           sum += kernel.matrix.get(i, o) * kernel.input.data[i];
  138.                       }
  139.                       kernel.output.data[o] = kernel.output.activation.activate(sum);
  140.                   }
  141.               }
  142.               for (var o = 0; o < this.output.length; o++) {
  143.                   this.output[o] = this.kernels[this.kernels.length - 1].output.data[o];
  144.               }
  145.               return this.output;
  146.           };
  147.           return Network;
  148.       }());
  149.       exports.Network = Network;
  150.   });
  151.   define("src/random", ["require", "exports"], function (require, exports) {
  152.       "use strict";
  153.       exports.__esModule = true;
  154.       var Random = (function () {
  155.           function Random(seed) {
  156.               this.seed = seed;
  157.               this.seed = this.seed === undefined ? 1 : this.seed;
  158.               this.a = 1103515245;
  159.               this.c = 12345;
  160.               this.m = Math.pow(2, 31);
  161.           }
  162.           Random.prototype.next = function () {
  163.               this.seed = (this.a * this.seed + this.c) % this.m;
  164.               return this.seed / this.m;
  165.           };
  166.           return Random;
  167.       }());
  168.       exports.Random = Random;
  169.   });
  170.   define("src/trainer", ["require", "exports", "src/matrix", "src/random"], function (require, exports, matrix_2, random_1) {
  171.       "use strict";
  172.       exports.__esModule = true;
  173.       var Trainer = (function () {
  174.           function Trainer(network, options) {
  175.               this.network = network;
  176.               this.options = options;
  177.               this.options = this.options || {};
  178.               this.options.seed = this.options.seed || 0;
  179.               this.options.step = this.options.step || 0.15;
  180.               this.options.momentum = this.options.momentum || 0.5;
  181.               this.random = new random_1.Random(this.options.seed);
  182.               this.deltas = new Array(this.network.matrices.length);
  183.               for (var i = 0; i < this.network.matrices.length; i++) {
  184.                   this.deltas[i] = new matrix_2.Matrix(this.network.matrices[i].inputs, this.network.matrices[i].outputs);
  185.               }
  186.               this.gradients = new Array(this.network.tensors.length);
  187.               for (var i = 0; i < this.network.tensors.length; i++) {
  188.                   this.gradients[i] = new Float64Array(this.network.tensors[i].data.length);
  189.               }
  190.               for (var m = 0; m < this.network.matrices.length; m++) {
  191.                   for (var o = 0; o < this.network.matrices[m].outputs; o++) {
  192.                       for (var i = 0; i < this.network.matrices[m].inputs; i++) {
  193.                           var rand = (this.random.next() - 0.5) * (1 / Math.sqrt(this.network.matrices[m].inputs));
  194.                           this.network.matrices[m].set(i, o, rand);
  195.                       }
  196.                   }
  197.               }
  198.               this.kernels = new Array(this.network.kernels.length);
  199.               for (var i = 0; i < this.network.kernels.length; i++) {
  200.                   this.kernels[i] = {
  201.                       matrix: {
  202.                           matrix: this.network.matrices[i],
  203.                           deltas: this.deltas[i]
  204.                       },
  205.                       input: {
  206.                           tensor: this.network.tensors[i + 0],
  207.                           grads: this.gradients[i + 0]
  208.                       },
  209.                       output: {
  210.                           tensor: this.network.tensors[i + 1],
  211.                           grads: this.gradients[i + 1]
  212.                       }
  213.                   };
  214.               }
  215.           }
  216.           Trainer.prototype.forward = function (input) {
  217.               return this.network.forward(input);
  218.           };
  219.           Trainer.prototype.error = function (input, expect) {
  220.               var actual = this.network.forward(input);
  221.               return Math.sqrt(actual.reduce(function (acc, value, index) {
  222.                   var delta = (expect[index] - value);
  223.                   return (acc + (delta * delta));
  224.               }, 0) / actual.length);
  225.           };
  226.           Trainer.prototype.backward = function (input, expect) {
  227.               var actual = this.network.forward(input);
  228.               var kernel = this.kernels[this.kernels.length - 1];
  229.               for (var o = 0; o < kernel.matrix.matrix.outputs; o++) {
  230.                   var delta = (expect[o] - kernel.output.tensor.data[o]);
  231.                   kernel.output.grads[o] = (delta * kernel.output.tensor.activation.derive(kernel.output.tensor.data[o]));
  232.               }
  233.               for (var k = this.kernels.length - 1; k > -1; k--) {
  234.                   var kernel_1 = this.kernels[k];
  235.                   for (var i = 0; i < kernel_1.matrix.matrix.inputs; i++) {
  236.                       var delta = 0;
  237.                       for (var o = 0; o < kernel_1.matrix.matrix.outputs; o++) {
  238.                           delta += kernel_1.matrix.matrix.get(i, o) * kernel_1.output.grads[o];
  239.                       }
  240.                       kernel_1.input.grads[i] = (delta * kernel_1.input.tensor.activation.derive(kernel_1.input.tensor.data[i]));
  241.                   }
  242.               }
  243.               for (var k = this.kernels.length - 1; k > -1; k--) {
  244.                   var kernel_2 = this.kernels[k];
  245.                   for (var i = 0; i < kernel_2.matrix.matrix.inputs; i++) {
  246.                       for (var o = 0; o < kernel_2.matrix.matrix.outputs; o++) {
  247.                           var old_delta = kernel_2.matrix.deltas.get(i, o);
  248.                           var new_delta = (this.options.step * kernel_2.input.tensor.data[i] * kernel_2.output.grads[o]) + (this.options.momentum * old_delta);
  249.                           var new_weight = kernel_2.matrix.matrix.get(i, o) + new_delta;
  250.                           kernel_2.matrix.matrix.set(i, o, new_weight);
  251.                           kernel_2.matrix.deltas.set(i, o, new_delta);
  252.                       }
  253.                   }
  254.               }
  255.               return Math.sqrt(actual.reduce(function (acc, value, index) {
  256.                   var delta = (expect[index] - value);
  257.                   return (acc + (delta * delta));
  258.               }, 0) / actual.length);
  259.           };
  260.           return Trainer;
  261.       }());
  262.       exports.Trainer = Trainer;
  263.   });
  264.   define("src/index", ["require", "exports", "src/network", "src/tensor", "src/trainer"], function (require, exports, network_1, tensor_1, trainer_1) {
  265.       "use strict";
  266.       exports.__esModule = true;
  267.       exports.Network = network_1.Network;
  268.       exports.Tensor = tensor_1.Tensor;
  269.       exports.Trainer = trainer_1.Trainer;
  270.   });
  271.   define("visual/script/ready", ["require", "exports"], function (require, exports) {
  272.       "use strict";
  273.       exports.__esModule = true;
  274.       var isready = false;
  275.       var callbacks = [];
  276.       window.addEventListener("load", function () {
  277.           isready = true;
  278.           while (callbacks.length > 0)
  279.               callbacks.shift()();
  280.       });
  281.       exports.ready = function (func) {
  282.           if (isready) {
  283.               func();
  284.           }
  285.           else {
  286.               callbacks.push(func);
  287.           }
  288.       };
  289.   });
  290.   define("visual/script/loop", ["require", "exports", "visual/script/ready"], function (require, exports, ready_1) {
  291.       "use strict";
  292.       exports.__esModule = true;
  293.       var callbacks = [];
  294.       var step = function () {
  295.           window.requestAnimationFrame(function () {
  296.               callbacks.forEach(function (callback) { return callback(); });
  297.               step();
  298.           });
  299.       };
  300.       step();
  301.       exports.loop = function (func) { return ready_1.ready(function () { return callbacks.push(func); }); };
  302.   });
  303.   define("visual/script/device", ["require", "exports"], function (require, exports) {
  304.       "use strict";
  305.       exports.__esModule = true;
  306.       var Device = (function () {
  307.           function Device(selector, options) {
  308.               this.selector = selector;
  309.               this.options = options;
  310.               this.canvas = document.querySelector(selector);
  311.               this.canvas.width = this.options.resolutionX;
  312.               this.canvas.height = this.options.resolutionY;
  313.               this.canvas.style.width = this.options.width + "px";
  314.               this.canvas.style.height = this.options.height + "px";
  315.               this.context = this.canvas.getContext("2d");
  316.               for (var y = 0; y < this.resY(); y++) {
  317.                   for (var x = 0; x < this.resX(); x++) {
  318.                       this.set(x, y, 0);
  319.                   }
  320.               }
  321.           }
  322.           Device.prototype.resX = function () {
  323.               return this.options.resolutionX;
  324.           };
  325.           Device.prototype.resY = function () {
  326.               return this.options.resolutionY;
  327.           };
  328.           Device.prototype.get = function (x, y) {
  329.               var imageData = this.context.getImageData(x, y, 1, 1);
  330.               return imageData.data[0] / 256;
  331.           };
  332.           Device.prototype.set = function (x, y, value) {
  333.               var imageData = this.context.getImageData(x, y, 1, 1);
  334.               value = Math.floor(value * 256);
  335.               imageData.data[0] = value;
  336.               imageData.data[1] = value;
  337.               imageData.data[2] = value;
  338.               imageData.data[3] = 255;
  339.               this.context.putImageData(imageData, x, y);
  340.           };
  341.           Device.prototype.clear = function (value) {
  342.               for (var y = 0; y < this.resY(); y++) {
  343.                   for (var x = 0; x < this.resX(); x++) {
  344.                       this.set(x, y, value);
  345.                   }
  346.               }
  347.           };
  348.           return Device;
  349.       }());
  350.       exports.Device = Device;
  351.   });
  352.   define("visual/script/index", ["require", "exports", "src/index", "visual/script/ready", "visual/script/loop", "visual/script/device"], function (require, exports, neuron, ready_2, loop_1, device_1) {
  353.       "use strict";
  354.       exports.__esModule = true;
  355.       ready_2.ready(function () {
  356.           var info = {
  357.               iteration: document.querySelector("#iteration"),
  358.               error: document.querySelector("#error"),
  359.               src: document.querySelector("#src")
  360.           };
  361.           var src = new device_1.Device("#src", {
  362.               resolutionX: 32,
  363.               resolutionY: 32,
  364.               width: 256,
  365.               height: 256
  366.           });
  367.           var dst = new device_1.Device("#dst", {
  368.               resolutionX: 32,
  369.               resolutionY: 32,
  370.               width: 256,
  371.               height: 256
  372.           });
  373.           info.src.addEventListener("click", function () {
  374.               src.clear(0);
  375.               for (var i = 0; i < 4; i++) {
  376.                   var ox = Math.floor(Math.random() * src.resX());
  377.                   var oy = Math.floor(Math.random() * src.resY());
  378.                   for (var y = 0; y < 2; y++) {
  379.                       for (var x = 0; x < 2; x++) {
  380.                           src.set(ox + x, oy + y, 1);
  381.                       }
  382.                   }
  383.               }
  384.           });
  385.           var network = new neuron.Trainer(new neuron.Network([
  386.               new neuron.Tensor(2),
  387.               new neuron.Tensor(8, "tanh"),
  388.               new neuron.Tensor(8, "tanh"),
  389.               new neuron.Tensor(8, "tanh"),
  390.               new neuron.Tensor(1, "tanh"),
  391.           ]), {
  392.               step: 0.015
  393.           });
  394.           for (var i = 0; i < 10; i++) {
  395.               var ox = Math.floor(Math.random() * src.resX());
  396.               var oy = Math.floor(Math.random() * src.resY());
  397.               for (var y = 0; y < 8; y++) {
  398.                   for (var x = 0; x < 8; x++) {
  399.                       src.set(ox + x, oy + y, 1);
  400.                   }
  401.               }
  402.           }
  403.           var iteration = 0;
  404.           loop_1.loop(function () {
  405.               var error = 0;
  406.               for (var i = 0; i < 1; i++) {
  407.                   error = 0;
  408.                   for (var y = 0; y < src.resY(); y++) {
  409.                       for (var x = 0; x < src.resX(); x++) {
  410.                           error += network.backward([x / src.resX(), y / src.resY()], [src.get(x, y)]);
  411.                       }
  412.                   }
  413.               }
  414.               for (var y = 0; y < src.resX(); y++) {
  415.                   for (var x = 0; x < src.resY(); x++) {
  416.                       dst.set(x, y, network.forward([x / 32, y / 32])[0]);
  417.                   }
  418.               }
  419.               info.error.innerHTML = (error / (src.resX() * src.resY())).toString();
  420.               info.iteration.innerHTML = iteration.toString();
  421.               iteration += 1;
  422.           });
  423.       });
  424.   });
  425.  
  426.   return collect();
  427. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement