/** * Returns an array of a given size with each element filled with a single value */ export function values(size: number, value: number): Float32Array { return new Float32Array(size).fill(value); }
/** * * @returns {{rows: *, columns: *, weights: Array}} */ toJSON() { return { rows: this.rows, columns: this.columns, weights: this.weights.slice(0), }; }
function inputYXOffset(mode) { const gpu = new GPU({ mode }); const kernel = gpu.createKernel(function(a) { return a[this.thread.x][this.thread.y]; }) .setOutput([8, 2]); const a = new Float32Array(16); a.set([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]); const result = kernel(input(a, [2, 8])); assert.deepEqual(result.map(function(v) { return Array.from(v); }), [[1,3,5,7,9,11,13,15],[2,4,6,8,10,12,14,16]]); gpu.destroy(); }
function fourSquareMatrix(value) { const result = new Matrix(4, 4); result.weights.forEach((_, i) => { result.weights[i] = value; }); return result; }
function singlePrecisionKernel(mode) { const lst = new Float32Array([1, 2, 3, 4, 5, 6, 7, 8]); const gpu = new GPU({ mode }); const kernel = gpu.createKernel(function(lst) { return lst[this.thread.x]; }, { precision: 'single', output: [lst.length] }); assert.deepEqual(kernel(lst), lst); gpu.destroy(); }
predictTargetIndex(input, target) { const output = this.runIndex(input); // set gradients into log probabilities const logProbabilities = output; // interpret output as log probabilities const probabilities = softmax(output); // compute the softmax probabilities // write gradients into log probabilities logProbabilities.deltas = probabilities.weights.slice(0); logProbabilities.deltas[target] -= 1; // accumulate base 2 log prob and do smoothing return -Math.log2(probabilities.weights[target]); }
function inputZYX(mode) { const gpu = new GPU({ mode }); const kernel = gpu.createKernel(function(a) { return a[this.thread.z][this.thread.y][this.thread.x]; }) .setOutput([2, 4, 4]); const a = new Float32Array(32); a.set([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32]); const result = kernel(input(a, [2, 4, 4])); assert.deepEqual(result.map(function(v) { return v.map(function(v) { return Array.from(v); }); }), [[[1,2],[3,4],[5,6],[7,8]],[[9,10],[11,12],[13,14],[15,16]],[[17,18],[19,20],[21,22],[23,24]],[[25,26],[27,28],[29,30],[31,32]]]); gpu.destroy(); }
constructor(rows, columns, std) { super(rows, columns); this.rows = rows; this.columns = columns; this.std = std; for (let i = 0, max = this.weights.length; i < max; i++) { this.weights[i] = randomFloat(-std, std); } }
function inputX(mode) { const gpu = new GPU({ mode }); const kernel = gpu.createKernel(function(a) { return a[this.thread.x]; }) .setOutput([9]); const a = new Float32Array(9); a.set([1,2,3,4,5,6,7,8,9]); const result = kernel(input(a, [3, 3])); assert.deepEqual(Array.from(result), [1,2,3,4,5,6,7,8,9]); gpu.destroy(); }
// fill matrix with random gaussian numbers fillRandN(mu, std) { for (let i = 0, max = this.weights.length; i < max; i++) { this.weights[i] = randomN(mu, std); } }
function inputYX(mode) { const gpu = new GPU({ mode }); const kernel = gpu.createKernel(function(a) { return a[this.thread.y][this.thread.x]; }) .setOutput([3, 3]); const a = new Float32Array(9); a.set([1,2,3,4,5,6,7,8,9]); const result = kernel(input(a, [3, 3])); assert.deepEqual(result.map(function(v) { return Array.from(v); }), [[1,2,3],[4,5,6],[7,8,9]]); gpu.destroy(); }