Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function () {
- var main = null;
- var modules = {
- "require": {
- factory: undefined,
- dependencies: [],
- exports: function (args, callback) { return require(args, callback); },
- resolved: true
- }
- };
- function define(id, dependencies, factory) {
- return main = modules[id] = {
- dependencies: dependencies,
- factory: factory,
- exports: {},
- resolved: false
- };
- }
- function resolve(definition) {
- if (definition.resolved === true)
- return;
- definition.resolved = true;
- var dependencies = definition.dependencies.map(function (id) {
- return (id === "exports")
- ? definition.exports
- : (function () {
- if(modules[id] !== undefined) {
- resolve(modules[id]);
- return modules[id].exports;
- } else return require(id)
- })();
- });
- definition.factory.apply(null, dependencies);
- }
- function collect() {
- Object.keys(modules).map(function (key) { return modules[key]; }).forEach(resolve);
- return (main !== null)
- ? main.exports
- : undefined
- }
- define("src/matrix", ["require", "exports"], function (require, exports) {
- "use strict";
- exports.__esModule = true;
- var Matrix = (function () {
- function Matrix(inputs, outputs) {
- this.inputs = inputs;
- this.outputs = outputs;
- this.data = new Float64Array(this.inputs * this.outputs);
- }
- Matrix.prototype.get = function (i, o) {
- return this.data[i + (o * this.inputs)];
- };
- Matrix.prototype.set = function (i, o, value) {
- this.data[i + (o * this.inputs)] = value;
- };
- return Matrix;
- }());
- exports.Matrix = Matrix;
- });
- define("src/tensor", ["require", "exports"], function (require, exports) {
- "use strict";
- exports.__esModule = true;
- var select = function (type) {
- switch (type) {
- case "identity": return {
- activate: function (x) { return x; },
- derive: function (x) { return 1; }
- };
- case "tanh": return {
- activate: function (x) { return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x)); },
- derive: function (x) { return (1 - (x * x)); }
- };
- case "binary-step": return {
- activate: function (x) { return (x >= 0) ? 1 : 0; },
- derive: function (x) { return (x >= 0) ? 1 : 0; }
- };
- case "relu": return {
- activate: function (x) { return (x >= 0) ? x : 0; },
- derive: function (x) { return (x >= 0) ? 1 : 0; }
- };
- default: throw Error("unknown activation");
- }
- };
- var Tensor = (function () {
- function Tensor(units, activation, bias) {
- if (activation === void 0) { activation = "identity"; }
- if (bias === void 0) { bias = 1.0; }
- this.data = new Float64Array(units + 1);
- this.data[this.data.length - 1] = bias;
- this.activation = select(activation);
- }
- return Tensor;
- }());
- exports.Tensor = Tensor;
- });
- define("src/network", ["require", "exports", "src/matrix"], function (require, exports, matrix_1) {
- "use strict";
- exports.__esModule = true;
- var Network = (function () {
- function Network(tensors) {
- this.tensors = tensors;
- this.output = new Array(this.tensors[this.tensors.length - 1].data.length - 1);
- this.matrices = new Array(this.tensors.length - 1);
- for (var i = 0; i < this.tensors.length - 1; i++) {
- this.matrices[i] = new matrix_1.Matrix(this.tensors[i + 0].data.length, this.tensors[i + 1].data.length - 1);
- }
- this.kernels = new Array(this.matrices.length);
- for (var i = 0; i < this.kernels.length; i++) {
- this.kernels[i] = {
- input: this.tensors[i + 0],
- output: this.tensors[i + 1],
- matrix: this.matrices[i]
- };
- }
- }
- Network.prototype.memory = function () {
- var tensors = this.tensors.reduce(function (acc, t) { return acc + (t.data.byteLength); }, 0);
- var matrices = this.matrices.reduce(function (acc, m) { return acc + (m.data.byteLength); }, 0);
- return tensors + matrices;
- };
- Network.prototype.inputs = function () {
- return (this.tensors[0].data.length - 1);
- };
- Network.prototype.outputs = function () {
- return (this.tensors[this.tensors.length - 1].data.length - 1);
- };
- Network.prototype.forward = function (input) {
- for (var i = 0; i < input.length; i++) {
- this.kernels[0].input.data[i] = input[i];
- }
- for (var k = 0; k < this.kernels.length; k++) {
- var kernel = this.kernels[k];
- for (var o = 0; o < kernel.matrix.outputs; o++) {
- var sum = 0;
- for (var i = 0; i < kernel.matrix.inputs; i++) {
- sum += kernel.matrix.get(i, o) * kernel.input.data[i];
- }
- kernel.output.data[o] = kernel.output.activation.activate(sum);
- }
- }
- for (var o = 0; o < this.output.length; o++) {
- this.output[o] = this.kernels[this.kernels.length - 1].output.data[o];
- }
- return this.output;
- };
- return Network;
- }());
- exports.Network = Network;
- });
- define("src/random", ["require", "exports"], function (require, exports) {
- "use strict";
- exports.__esModule = true;
- var Random = (function () {
- function Random(seed) {
- this.seed = seed;
- this.seed = this.seed === undefined ? 1 : this.seed;
- this.a = 1103515245;
- this.c = 12345;
- this.m = Math.pow(2, 31);
- }
- Random.prototype.next = function () {
- this.seed = (this.a * this.seed + this.c) % this.m;
- return this.seed / this.m;
- };
- return Random;
- }());
- exports.Random = Random;
- });
- define("src/trainer", ["require", "exports", "src/matrix", "src/random"], function (require, exports, matrix_2, random_1) {
- "use strict";
- exports.__esModule = true;
- var Trainer = (function () {
- function Trainer(network, options) {
- this.network = network;
- this.options = options;
- this.options = this.options || {};
- this.options.seed = this.options.seed || 0;
- this.options.step = this.options.step || 0.15;
- this.options.momentum = this.options.momentum || 0.5;
- this.random = new random_1.Random(this.options.seed);
- this.deltas = new Array(this.network.matrices.length);
- for (var i = 0; i < this.network.matrices.length; i++) {
- this.deltas[i] = new matrix_2.Matrix(this.network.matrices[i].inputs, this.network.matrices[i].outputs);
- }
- this.gradients = new Array(this.network.tensors.length);
- for (var i = 0; i < this.network.tensors.length; i++) {
- this.gradients[i] = new Float64Array(this.network.tensors[i].data.length);
- }
- for (var m = 0; m < this.network.matrices.length; m++) {
- for (var o = 0; o < this.network.matrices[m].outputs; o++) {
- for (var i = 0; i < this.network.matrices[m].inputs; i++) {
- var rand = (this.random.next() - 0.5) * (1 / Math.sqrt(this.network.matrices[m].inputs));
- this.network.matrices[m].set(i, o, rand);
- }
- }
- }
- this.kernels = new Array(this.network.kernels.length);
- for (var i = 0; i < this.network.kernels.length; i++) {
- this.kernels[i] = {
- matrix: {
- matrix: this.network.matrices[i],
- deltas: this.deltas[i]
- },
- input: {
- tensor: this.network.tensors[i + 0],
- grads: this.gradients[i + 0]
- },
- output: {
- tensor: this.network.tensors[i + 1],
- grads: this.gradients[i + 1]
- }
- };
- }
- }
- Trainer.prototype.forward = function (input) {
- return this.network.forward(input);
- };
- Trainer.prototype.error = function (input, expect) {
- var actual = this.network.forward(input);
- return Math.sqrt(actual.reduce(function (acc, value, index) {
- var delta = (expect[index] - value);
- return (acc + (delta * delta));
- }, 0) / actual.length);
- };
- Trainer.prototype.backward = function (input, expect) {
- var actual = this.network.forward(input);
- var kernel = this.kernels[this.kernels.length - 1];
- for (var o = 0; o < kernel.matrix.matrix.outputs; o++) {
- var delta = (expect[o] - kernel.output.tensor.data[o]);
- kernel.output.grads[o] = (delta * kernel.output.tensor.activation.derive(kernel.output.tensor.data[o]));
- }
- for (var k = this.kernels.length - 1; k > -1; k--) {
- var kernel_1 = this.kernels[k];
- for (var i = 0; i < kernel_1.matrix.matrix.inputs; i++) {
- var delta = 0;
- for (var o = 0; o < kernel_1.matrix.matrix.outputs; o++) {
- delta += kernel_1.matrix.matrix.get(i, o) * kernel_1.output.grads[o];
- }
- kernel_1.input.grads[i] = (delta * kernel_1.input.tensor.activation.derive(kernel_1.input.tensor.data[i]));
- }
- }
- for (var k = this.kernels.length - 1; k > -1; k--) {
- var kernel_2 = this.kernels[k];
- for (var i = 0; i < kernel_2.matrix.matrix.inputs; i++) {
- for (var o = 0; o < kernel_2.matrix.matrix.outputs; o++) {
- var old_delta = kernel_2.matrix.deltas.get(i, o);
- var new_delta = (this.options.step * kernel_2.input.tensor.data[i] * kernel_2.output.grads[o]) + (this.options.momentum * old_delta);
- var new_weight = kernel_2.matrix.matrix.get(i, o) + new_delta;
- kernel_2.matrix.matrix.set(i, o, new_weight);
- kernel_2.matrix.deltas.set(i, o, new_delta);
- }
- }
- }
- return Math.sqrt(actual.reduce(function (acc, value, index) {
- var delta = (expect[index] - value);
- return (acc + (delta * delta));
- }, 0) / actual.length);
- };
- return Trainer;
- }());
- exports.Trainer = Trainer;
- });
- define("src/index", ["require", "exports", "src/network", "src/tensor", "src/trainer"], function (require, exports, network_1, tensor_1, trainer_1) {
- "use strict";
- exports.__esModule = true;
- exports.Network = network_1.Network;
- exports.Tensor = tensor_1.Tensor;
- exports.Trainer = trainer_1.Trainer;
- });
- define("visual/script/ready", ["require", "exports"], function (require, exports) {
- "use strict";
- exports.__esModule = true;
- var isready = false;
- var callbacks = [];
- window.addEventListener("load", function () {
- isready = true;
- while (callbacks.length > 0)
- callbacks.shift()();
- });
- exports.ready = function (func) {
- if (isready) {
- func();
- }
- else {
- callbacks.push(func);
- }
- };
- });
- define("visual/script/loop", ["require", "exports", "visual/script/ready"], function (require, exports, ready_1) {
- "use strict";
- exports.__esModule = true;
- var callbacks = [];
- var step = function () {
- window.requestAnimationFrame(function () {
- callbacks.forEach(function (callback) { return callback(); });
- step();
- });
- };
- step();
- exports.loop = function (func) { return ready_1.ready(function () { return callbacks.push(func); }); };
- });
- define("visual/script/device", ["require", "exports"], function (require, exports) {
- "use strict";
- exports.__esModule = true;
- var Device = (function () {
- function Device(selector, options) {
- this.selector = selector;
- this.options = options;
- this.canvas = document.querySelector(selector);
- this.canvas.width = this.options.resolutionX;
- this.canvas.height = this.options.resolutionY;
- this.canvas.style.width = this.options.width + "px";
- this.canvas.style.height = this.options.height + "px";
- this.context = this.canvas.getContext("2d");
- for (var y = 0; y < this.resY(); y++) {
- for (var x = 0; x < this.resX(); x++) {
- this.set(x, y, 0);
- }
- }
- }
- Device.prototype.resX = function () {
- return this.options.resolutionX;
- };
- Device.prototype.resY = function () {
- return this.options.resolutionY;
- };
- Device.prototype.get = function (x, y) {
- var imageData = this.context.getImageData(x, y, 1, 1);
- return imageData.data[0] / 256;
- };
- Device.prototype.set = function (x, y, value) {
- var imageData = this.context.getImageData(x, y, 1, 1);
- value = Math.floor(value * 256);
- imageData.data[0] = value;
- imageData.data[1] = value;
- imageData.data[2] = value;
- imageData.data[3] = 255;
- this.context.putImageData(imageData, x, y);
- };
- Device.prototype.clear = function (value) {
- for (var y = 0; y < this.resY(); y++) {
- for (var x = 0; x < this.resX(); x++) {
- this.set(x, y, value);
- }
- }
- };
- return Device;
- }());
- exports.Device = Device;
- });
- 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) {
- "use strict";
- exports.__esModule = true;
- ready_2.ready(function () {
- var info = {
- iteration: document.querySelector("#iteration"),
- error: document.querySelector("#error"),
- src: document.querySelector("#src")
- };
- var src = new device_1.Device("#src", {
- resolutionX: 32,
- resolutionY: 32,
- width: 256,
- height: 256
- });
- var dst = new device_1.Device("#dst", {
- resolutionX: 32,
- resolutionY: 32,
- width: 256,
- height: 256
- });
- info.src.addEventListener("click", function () {
- src.clear(0);
- for (var i = 0; i < 4; i++) {
- var ox = Math.floor(Math.random() * src.resX());
- var oy = Math.floor(Math.random() * src.resY());
- for (var y = 0; y < 2; y++) {
- for (var x = 0; x < 2; x++) {
- src.set(ox + x, oy + y, 1);
- }
- }
- }
- });
- var network = new neuron.Trainer(new neuron.Network([
- new neuron.Tensor(2),
- new neuron.Tensor(8, "tanh"),
- new neuron.Tensor(8, "tanh"),
- new neuron.Tensor(8, "tanh"),
- new neuron.Tensor(1, "tanh"),
- ]), {
- step: 0.015
- });
- for (var i = 0; i < 10; i++) {
- var ox = Math.floor(Math.random() * src.resX());
- var oy = Math.floor(Math.random() * src.resY());
- for (var y = 0; y < 8; y++) {
- for (var x = 0; x < 8; x++) {
- src.set(ox + x, oy + y, 1);
- }
- }
- }
- var iteration = 0;
- loop_1.loop(function () {
- var error = 0;
- for (var i = 0; i < 1; i++) {
- error = 0;
- for (var y = 0; y < src.resY(); y++) {
- for (var x = 0; x < src.resX(); x++) {
- error += network.backward([x / src.resX(), y / src.resY()], [src.get(x, y)]);
- }
- }
- }
- for (var y = 0; y < src.resX(); y++) {
- for (var x = 0; x < src.resY(); x++) {
- dst.set(x, y, network.forward([x / 32, y / 32])[0]);
- }
- }
- info.error.innerHTML = (error / (src.resX() * src.resY())).toString();
- info.iteration.innerHTML = iteration.toString();
- iteration += 1;
- });
- });
- });
- return collect();
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement