Comparing version 0.3.7 to 0.3.8
import { NDArrayMath } from '../math/math'; | ||
import { NDArray } from '../math/ndarray'; | ||
import * as util from '../util'; | ||
export interface InputProvider { | ||
@@ -27,4 +26,4 @@ getNextCopy(math: NDArrayMath): NDArray; | ||
getInputProvider(inputId: number): { | ||
getNextCopy(math: NDArrayMath): NDArray<util.DType>; | ||
disposeCopy(math: NDArrayMath, copy: NDArray<util.DType>): void; | ||
getNextCopy(math: NDArrayMath): NDArray<"float32" | "int32" | "bool">; | ||
disposeCopy(math: NDArrayMath, copy: NDArray<"float32" | "int32" | "bool">): void; | ||
}; | ||
@@ -34,5 +33,5 @@ } | ||
getInputProvider(inputId: number): { | ||
getNextCopy(math: NDArrayMath): NDArray<util.DType>; | ||
disposeCopy(math: NDArrayMath, copy: NDArray<util.DType>): void; | ||
getNextCopy(math: NDArrayMath): NDArray<"float32" | "int32" | "bool">; | ||
disposeCopy(math: NDArrayMath, copy: NDArray<"float32" | "int32" | "bool">): void; | ||
}; | ||
} |
@@ -156,6 +156,4 @@ "use strict"; | ||
var feedEntry = _this.inferenceFeedEntries[j]; | ||
ndarrayFeedEntries.push({ | ||
tensor: feedEntry.tensor, | ||
data: track(feedEntry.data.getNextCopy(_this.math)) | ||
}); | ||
var nextCopy = feedEntry.data.getNextCopy(_this.math); | ||
ndarrayFeedEntries.push({ tensor: feedEntry.tensor, data: track(nextCopy) }); | ||
} | ||
@@ -162,0 +160,0 @@ feeds.push(ndarrayFeedEntries); |
@@ -29,4 +29,5 @@ "use strict"; | ||
var x = inferenceArrays.get(this.xTensor); | ||
var clone = math.clone(x); | ||
math.scope(function (keep) { | ||
inferenceArrays.set(_this.yTensor, keep(x.reshape(_this.yTensor.shape))); | ||
inferenceArrays.set(_this.yTensor, keep(clone.reshape(_this.yTensor.shape))); | ||
}); | ||
@@ -37,4 +38,5 @@ }; | ||
var dy = gradientArrays.get(this.yTensor); | ||
var clone = math.clone(dy); | ||
math.scope(function () { | ||
gradientArrays.add(_this.xTensor, dy.reshape(_this.xTensor.shape)); | ||
gradientArrays.add(_this.xTensor, clone.reshape(_this.xTensor.shape)); | ||
}); | ||
@@ -41,0 +43,0 @@ }; |
@@ -69,4 +69,8 @@ "use strict"; | ||
}); | ||
var oldAccB1 = _this.accB1; | ||
var oldAccB2 = _this.accB2; | ||
_this.accB1 = keep(math.multiply(_this.accB1, _this.b1)); | ||
_this.accB2 = keep(math.multiply(_this.accB2, _this.b2)); | ||
oldAccB1.dispose(); | ||
oldAccB2.dispose(); | ||
}); | ||
@@ -73,0 +77,0 @@ this.variableGradients.dispose(); |
@@ -17,4 +17,5 @@ import { NDArrayMath } from '../../math/math'; | ||
private eps; | ||
private accB1; | ||
private b1; | ||
private b2; | ||
} |
@@ -25,4 +25,6 @@ "use strict"; | ||
_this.weightedInfNorm = new tensor_array_map_1.TensorArrayMap(); | ||
_this.eps = ndarray_1.Scalar.new(1e-8); | ||
_this.b1 = ndarray_1.Scalar.new(_this.beta1); | ||
_this.b2 = ndarray_1.Scalar.new(_this.beta2); | ||
_this.accB1 = ndarray_1.Scalar.new(_this.beta1); | ||
return _this; | ||
@@ -56,3 +58,3 @@ } | ||
var newWeightedInfNorm = math.add(math.relu(math.subtract(ut0, ut1)), ut1); | ||
var variable = math.scaledArrayAdd(_this.one, oldVariable, math.divide(_this.c, math.subtract(_this.one, _this.b1)), math.divide(newFirstMoment, newWeightedInfNorm)); | ||
var variable = math.scaledArrayAdd(_this.one, oldVariable, math.divide(_this.c, math.subtract(_this.one, _this.accB1)), math.divide(newFirstMoment, math.add(_this.eps, newWeightedInfNorm))); | ||
activationArrayMap.set(node.output, keep(variable)); | ||
@@ -67,2 +69,5 @@ node.data = variable; | ||
}); | ||
var oldAccB1 = _this.accB1; | ||
_this.accB1 = keep(math.multiply(_this.accB1, _this.b1)); | ||
oldAccB1.dispose(); | ||
}); | ||
@@ -77,2 +82,3 @@ this.variableGradients.dispose(); | ||
this.eps.dispose(); | ||
this.accB1.dispose(); | ||
this.b1.dispose(); | ||
@@ -79,0 +85,0 @@ this.b2.dispose(); |
@@ -1,2 +0,1 @@ | ||
import * as util from '../util'; | ||
import { ConvInfo } from './conv_util'; | ||
@@ -40,3 +39,2 @@ import { MatrixOrientation, NDArrayMath, SumTypes } from './math'; | ||
protected squareInternal<T extends NDArray>(x: T): T; | ||
protected logSumExpInternal(input: NDArray, axes: number[]): NDArray; | ||
protected reluInternal<T extends NDArray>(input: T): T; | ||
@@ -62,6 +60,7 @@ protected eluInternal<T extends NDArray>(ndarray: T): T; | ||
protected conv2dDerBiasInternal(dY: Array3D): Array1D; | ||
protected tileInternal<D extends keyof DataTypes, T extends NDArray<D>>(a: T, reps: number[]): T; | ||
protected transposeInternal<D extends keyof DataTypes, T extends NDArray<D>>(a: T, perm: number[]): T; | ||
private pool(x, convInfo, poolType); | ||
protected maxPoolInternal(x: Array3D, convInfo: ConvInfo): Array3D; | ||
maxPoolPositions(x: Array3D, convInfo: ConvInfo): Array3D<util.DType>; | ||
maxPoolPositions(x: Array3D, convInfo: ConvInfo): Array3D<"float32" | "int32" | "bool">; | ||
protected maxPoolBackpropInternal(dy: Array3D, x: Array3D, convInfo: ConvInfo): Array3D; | ||
@@ -68,0 +67,0 @@ protected minPoolInternal(x: Array3D, convInfo: ConvInfo): Array3D; |
@@ -275,3 +275,3 @@ "use strict"; | ||
NDArrayMathCPU.prototype.argMinInternal = function (input, axes) { | ||
axis_util.assertAxesAreInnerMostDims('argMax', axes, input.rank); | ||
axis_util.assertAxesAreInnerMostDims('argMin', axes, input.rank); | ||
var _a = axis_util.computeOutAndReduceShapes(input.shape, axes), outShape = _a[0], reduceShape = _a[1]; | ||
@@ -453,12 +453,2 @@ var result = ndarray_1.NDArray.zeros(outShape, 'int32'); | ||
}; | ||
NDArrayMathCPU.prototype.logSumExpInternal = function (input, axes) { | ||
axis_util.assertAxesAreInnerMostDims('logSumExp', axes, input.rank); | ||
var xMax = this.max(input, axes, true); | ||
var a = this.subtract(input, xMax); | ||
var b = this.exp(a); | ||
var c = this.sum(b, axes); | ||
var d = this.log(c); | ||
var result = this.add(xMax.reshape(d.shape), d); | ||
return result; | ||
}; | ||
NDArrayMathCPU.prototype.reluInternal = function (input) { | ||
@@ -741,2 +731,34 @@ var res = ndarray_1.NDArray.zeros(input.shape, input.dtype); | ||
}; | ||
NDArrayMathCPU.prototype.tileInternal = function (a, reps) { | ||
var newShape = new Array(a.rank); | ||
for (var i = 0; i < newShape.length; i++) { | ||
newShape[i] = a.shape[i] * reps[i]; | ||
} | ||
var dtype; | ||
if (a.dtype === 'float32') { | ||
dtype = Float32Array; | ||
} | ||
else if (a.dtype === 'int32') { | ||
dtype = Int32Array; | ||
} | ||
else if (a.dtype === 'bool') { | ||
dtype = Uint8Array; | ||
} | ||
else { | ||
throw new Error("Dtype " + a.dtype + " not supported for tile"); | ||
} | ||
var resultValues = new dtype(util.sizeFromShape(newShape)); | ||
var result = ndarray_1.NDArray.make(newShape, { values: resultValues }, a.dtype); | ||
var values = a.getValues(); | ||
for (var i = 0; i < result.size; ++i) { | ||
var newLoc = result.indexToLoc(i); | ||
var originalLoc = new Array(a.rank); | ||
for (var i_1 = 0; i_1 < originalLoc.length; i_1++) { | ||
originalLoc[i_1] = newLoc[i_1] % a.shape[i_1]; | ||
} | ||
var originalIndex = a.locToIndex(originalLoc); | ||
resultValues[i] = values[originalIndex]; | ||
} | ||
return result; | ||
}; | ||
NDArrayMathCPU.prototype.transposeInternal = function (a, perm) { | ||
@@ -753,4 +775,4 @@ var newShape = new Array(a.rank); | ||
var newLoc = new Array(loc.length); | ||
for (var i_1 = 0; i_1 < newLoc.length; i_1++) { | ||
newLoc[i_1] = loc[perm[i_1]]; | ||
for (var i_2 = 0; i_2 < newLoc.length; i_2++) { | ||
newLoc[i_2] = loc[perm[i_2]]; | ||
} | ||
@@ -757,0 +779,0 @@ var newIndex = result.locToIndex(newLoc); |
@@ -30,3 +30,6 @@ import { ConvInfo } from './conv_util'; | ||
protected batchNormalization3DInternal(x: Array3D, mean: Array3D | Array1D, variance: Array3D | Array1D, varianceEpsilon: number | null, scale?: Array3D | Array1D, offset?: Array3D | Array1D): Array3D; | ||
protected tileInternal<D extends keyof DataTypes, T extends NDArray<D>>(a: T, reps: number[]): T; | ||
protected transposeInternal<D extends keyof DataTypes, T extends NDArray<D>>(a: T, perm: number[]): T; | ||
private reduce<D>(a, reduceType, dtype); | ||
private argReduce(a, reduceType, bestIndicesA?); | ||
protected sumInternal<T extends keyof DataTypes>(a: NDArray<T>, axes: number[]): NDArray<SumTypes[T]>; | ||
@@ -45,3 +48,2 @@ protected argMinInternal(a: NDArray, axes: number[]): NDArray<'int32'>; | ||
protected subtractInternal<T extends NDArray>(a: T, b: T): T; | ||
protected logSumExpInternal(a: NDArray, axes: number[]): NDArray; | ||
protected ceilInternal<T extends NDArray>(a: T): T; | ||
@@ -48,0 +50,0 @@ protected floorInternal<T extends NDArray>(a: T): T; |
@@ -13,5 +13,8 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var util = require("../util"); | ||
var axis_util = require("./axis_util"); | ||
var math_1 = require("./math"); | ||
var ndarray = require("./ndarray"); | ||
var ndarray_1 = require("./ndarray"); | ||
var reduce_util = require("./reduce_util"); | ||
var addscaledmat_gpu_1 = require("./webgl/addscaledmat_gpu"); | ||
@@ -30,5 +33,3 @@ var argminmax_gpu_1 = require("./webgl/argminmax_gpu"); | ||
var gpgpu_util = require("./webgl/gpgpu_util"); | ||
var logsumexp_gpu_1 = require("./webgl/logsumexp_gpu"); | ||
var max_pool_backprop_gpu_1 = require("./webgl/max_pool_backprop_gpu"); | ||
var minmax_gpu_1 = require("./webgl/minmax_gpu"); | ||
var mulmat_gpu_1 = require("./webgl/mulmat_gpu"); | ||
@@ -38,6 +39,7 @@ var multinomial_gpu_1 = require("./webgl/multinomial_gpu"); | ||
var pool_gpu_1 = require("./webgl/pool_gpu"); | ||
var reducesum_gpu_1 = require("./webgl/reducesum_gpu"); | ||
var reduce_gpu_1 = require("./webgl/reduce_gpu"); | ||
var resize_bilinear_gpu_1 = require("./webgl/resize_bilinear_gpu"); | ||
var slice_gpu_1 = require("./webgl/slice_gpu"); | ||
var texture_manager_1 = require("./webgl/texture_manager"); | ||
var tile_gpu_1 = require("./webgl/tile_gpu"); | ||
var transpose_gpu_1 = require("./webgl/transpose_gpu"); | ||
@@ -168,2 +170,6 @@ var unary_op = require("./webgl/unaryop_gpu"); | ||
}; | ||
NDArrayMathGPU.prototype.tileInternal = function (a, reps) { | ||
var program = new tile_gpu_1.TileProgram(a.shape, reps); | ||
return this.compileAndRun(program, [a]); | ||
}; | ||
NDArrayMathGPU.prototype.transposeInternal = function (a, perm) { | ||
@@ -173,16 +179,60 @@ var program = new transpose_gpu_1.TransposeProgram(a.shape, perm); | ||
}; | ||
NDArrayMathGPU.prototype.reduce = function (a, reduceType, dtype) { | ||
var batchSize = a.shape[0]; | ||
var inSize = a.shape[1]; | ||
var windowSize = reduce_util.computeOptimalWindowSize(inSize); | ||
var reduceInfo = { windowSize: windowSize, inSize: inSize, batchSize: batchSize }; | ||
var program = new reduce_gpu_1.ReduceProgram(reduceInfo, reduceType); | ||
var _a = program.outputShape, rows = _a[0], cols = _a[1]; | ||
var output = this.makeOutputArray(program.outputShape, dtype).as2D(rows, cols); | ||
this.compileAndRun(program, [a], output); | ||
if (output.shape[1] === 1) { | ||
return output; | ||
} | ||
return this.reduce(output, reduceType, dtype); | ||
}; | ||
NDArrayMathGPU.prototype.argReduce = function (a, reduceType, bestIndicesA) { | ||
if (bestIndicesA === void 0) { bestIndicesA = null; } | ||
var batchSize = a.shape[0]; | ||
var inSize = a.shape[1]; | ||
if (bestIndicesA != null) { | ||
batchSize = bestIndicesA.shape[0]; | ||
inSize = bestIndicesA.shape[1]; | ||
} | ||
var windowSize = reduce_util.computeOptimalWindowSize(inSize); | ||
var reduceInfo = { windowSize: windowSize, inSize: inSize, batchSize: batchSize }; | ||
var program = new argminmax_gpu_1.ArgMinMaxProgram(reduceInfo, reduceType, bestIndicesA == null); | ||
var _a = program.outputShape, rows = _a[0], cols = _a[1]; | ||
var output = this.makeOutputArray(program.outputShape, 'int32').as2D(rows, cols); | ||
var inputs = [a]; | ||
if (bestIndicesA != null) { | ||
inputs.push(bestIndicesA); | ||
} | ||
this.compileAndRun(program, inputs, output); | ||
if (output.shape[1] === 1) { | ||
return output; | ||
} | ||
return this.argReduce(a, reduceType, output); | ||
}; | ||
NDArrayMathGPU.prototype.sumInternal = function (a, axes) { | ||
var program = new reducesum_gpu_1.ReduceSumProgram(a.shape, axes); | ||
var output = this.makeOutputArray(program.outputShape, math_1.SumTypesMap[a.dtype]); | ||
return this.compileAndRun(program, [a], output); | ||
axis_util.assertAxesAreInnerMostDims('sum', axes, a.rank); | ||
var _a = axis_util.computeOutAndReduceShapes(a.shape, axes), outShape = _a[0], reduceShape = _a[1]; | ||
var inSize = util.sizeFromShape(reduceShape); | ||
var a2D = a.as2D(-1, inSize); | ||
var outputDType = math_1.SumTypesMap[a.dtype]; | ||
return this.reduce(a2D, 'sum', outputDType).reshape(outShape); | ||
}; | ||
NDArrayMathGPU.prototype.argMinInternal = function (a, axes) { | ||
var program = new argminmax_gpu_1.ArgMinMaxProgram(a.shape, axes, 'min'); | ||
var output = this.makeOutputArray(program.outputShape, 'int32'); | ||
return this.compileAndRun(program, [a], output); | ||
axis_util.assertAxesAreInnerMostDims('argMin', axes, a.rank); | ||
var _a = axis_util.computeOutAndReduceShapes(a.shape, axes), outShape = _a[0], reduceShape = _a[1]; | ||
var inSize = util.sizeFromShape(reduceShape); | ||
var a2D = a.as2D(-1, inSize); | ||
return this.argReduce(a2D, 'min').reshape(outShape); | ||
}; | ||
NDArrayMathGPU.prototype.argMaxInternal = function (a, axes) { | ||
var program = new argminmax_gpu_1.ArgMinMaxProgram(a.shape, axes, 'max'); | ||
var output = this.makeOutputArray(program.outputShape, 'int32'); | ||
return this.compileAndRun(program, [a], output); | ||
axis_util.assertAxesAreInnerMostDims('argMax', axes, a.rank); | ||
var _a = axis_util.computeOutAndReduceShapes(a.shape, axes), outShape = _a[0], reduceShape = _a[1]; | ||
var inSize = util.sizeFromShape(reduceShape); | ||
var a2D = a.as2D(-1, inSize); | ||
return this.argReduce(a2D, 'max').reshape(outShape); | ||
}; | ||
@@ -198,8 +248,14 @@ NDArrayMathGPU.prototype.equalInternal = function (x, y) { | ||
NDArrayMathGPU.prototype.minInternal = function (a, axes) { | ||
var program = new minmax_gpu_1.MinMaxProgram(a.shape, axes, 'min'); | ||
return this.compileAndRun(program, [a]); | ||
axis_util.assertAxesAreInnerMostDims('min', axes, a.rank); | ||
var _a = axis_util.computeOutAndReduceShapes(a.shape, axes), outShape = _a[0], reduceShape = _a[1]; | ||
var inSize = util.sizeFromShape(reduceShape); | ||
var a2D = a.as2D(-1, inSize); | ||
return this.reduce(a2D, 'min', a2D.dtype).reshape(outShape); | ||
}; | ||
NDArrayMathGPU.prototype.maxInternal = function (a, axes) { | ||
var program = new minmax_gpu_1.MinMaxProgram(a.shape, axes, 'max'); | ||
return this.compileAndRun(program, [a]); | ||
axis_util.assertAxesAreInnerMostDims('max', axes, a.rank); | ||
var _a = axis_util.computeOutAndReduceShapes(a.shape, axes), outShape = _a[0], reduceShape = _a[1]; | ||
var inSize = util.sizeFromShape(reduceShape); | ||
var a2D = a.as2D(-1, inSize); | ||
return this.reduce(a2D, 'max', a2D.dtype).reshape(outShape); | ||
}; | ||
@@ -219,6 +275,2 @@ NDArrayMathGPU.prototype.divideInternal = function (a, b) { | ||
}; | ||
NDArrayMathGPU.prototype.logSumExpInternal = function (a, axes) { | ||
var program = new logsumexp_gpu_1.LogSumExpProgram(a.shape, axes); | ||
return this.compileAndRun(program, [a]); | ||
}; | ||
NDArrayMathGPU.prototype.ceilInternal = function (a) { | ||
@@ -225,0 +277,0 @@ var program = new unaryop_gpu_1.UnaryOpProgram(a.shape, unary_op.CEIL); |
@@ -67,3 +67,2 @@ import { ConvInfo } from './conv_util'; | ||
logSumExp(input: NDArray, axis?: number | number[], keepDims?: boolean): NDArray; | ||
protected abstract logSumExpInternal(ndarray: NDArray, axes: number[]): NDArray; | ||
sum<T extends keyof DataTypes>(input: NDArray<T>, axis?: number | number[], keepDims?: boolean): NDArray<SumTypes[T]>; | ||
@@ -94,2 +93,4 @@ protected abstract sumInternal<T extends keyof DataTypes>(ndarray: NDArray<T>, axes: number[]): NDArray<SumTypes[T]>; | ||
switchDim<T extends NDArray>(a: T, newDim: number[]): T; | ||
tile<D extends keyof DataTypes, T extends NDArray<D>>(a: T, reps: number[]): T; | ||
protected abstract tileInternal<D extends keyof DataTypes, T extends NDArray<D>>(a: T, reps: number[]): T; | ||
transpose<D extends keyof DataTypes, T extends NDArray<D>>(a: T, perm?: number[]): T; | ||
@@ -96,0 +97,0 @@ protected abstract transposeInternal<D extends keyof DataTypes, T extends NDArray<D>>(a: T, perm: number[]): T; |
@@ -146,3 +146,3 @@ "use strict"; | ||
util.assert(a.rank === 2 && b.rank === 2, "Error in matMul: inputs must be rank 2, got ranks " + a.rank + | ||
("and " + b.rank + ".")); | ||
(" and " + b.rank + ".")); | ||
util.assert(innerShapeA === innerShapeB, "Error in matMul: inner shapes (" + innerShapeA + ") and (" + | ||
@@ -273,9 +273,9 @@ (innerShapeB + ") of NDArrays with shapes " + a.shape + " and ") + | ||
var axes = axis_util.parseAxisParam(axis, input.shape); | ||
var permutedAxes = axis_util.getPermutedAxes(axes, input.rank); | ||
return this.executeOp('logSumExp', function () { | ||
if (permutedAxes != null) { | ||
input = _this.transpose(input, permutedAxes); | ||
axes = axis_util.getInnerMostAxes(axes.length, input.rank); | ||
} | ||
var res = _this.logSumExpInternal(input, axes); | ||
var xMax = _this.max(input, axes, true); | ||
var a = _this.subtract(input, xMax); | ||
var b = _this.exp(a); | ||
var c = _this.sum(b, axes); | ||
var d = _this.log(c); | ||
var res = _this.add(xMax.reshape(d.shape), d); | ||
if (keepDims) { | ||
@@ -292,3 +292,4 @@ var newShape = axis_util.expandShapeToKeepDim(res.shape, axes); | ||
if (keepDims === void 0) { keepDims = false; } | ||
var axes = axis_util.parseAxisParam(axis, input.shape); | ||
var origAxes = axis_util.parseAxisParam(axis, input.shape); | ||
var axes = origAxes; | ||
var permutedAxes = axis_util.getPermutedAxes(axes, input.rank); | ||
@@ -302,3 +303,3 @@ return this.executeOp('sum', function () { | ||
if (keepDims) { | ||
var newShape = axis_util.expandShapeToKeepDim(res.shape, axes); | ||
var newShape = axis_util.expandShapeToKeepDim(res.shape, origAxes); | ||
return res.reshape(newShape); | ||
@@ -381,3 +382,4 @@ } | ||
if (keepDims === void 0) { keepDims = false; } | ||
var axes = axis_util.parseAxisParam(axis, input.shape); | ||
var origAxes = axis_util.parseAxisParam(axis, input.shape); | ||
var axes = origAxes; | ||
var permutedAxes = axis_util.getPermutedAxes(axes, input.rank); | ||
@@ -391,3 +393,3 @@ return this.executeOp('min', function () { | ||
if (keepDims) { | ||
var newShape = axis_util.expandShapeToKeepDim(res.shape, axes); | ||
var newShape = axis_util.expandShapeToKeepDim(res.shape, origAxes); | ||
return res.reshape(newShape); | ||
@@ -402,3 +404,4 @@ } | ||
if (keepDims === void 0) { keepDims = false; } | ||
var axes = axis_util.parseAxisParam(axis, input.shape); | ||
var origAxes = axis_util.parseAxisParam(axis, input.shape); | ||
var axes = origAxes; | ||
var permutedAxes = axis_util.getPermutedAxes(axes, input.rank); | ||
@@ -412,3 +415,3 @@ return this.executeOp('max', function () { | ||
if (keepDims) { | ||
var newShape = axis_util.expandShapeToKeepDim(res.shape, axes); | ||
var newShape = axis_util.expandShapeToKeepDim(res.shape, origAxes); | ||
return res.reshape(newShape); | ||
@@ -440,2 +443,8 @@ } | ||
}; | ||
NDArrayMath.prototype.tile = function (a, reps) { | ||
var _this = this; | ||
util.assert(a.rank === reps.length, "Error in transpose: rank of input " + a.rank + " " + | ||
("must match length of reps " + reps + ".")); | ||
return this.executeOp('tile', function () { return _this.tileInternal(a, reps); }); | ||
}; | ||
NDArrayMath.prototype.transpose = function (a, perm) { | ||
@@ -442,0 +451,0 @@ var _this = this; |
@@ -1,2 +0,1 @@ | ||
import * as util from '../util'; | ||
import { GPGPUContext } from './webgl/gpgpu_context'; | ||
@@ -22,2 +21,3 @@ import { TextureType } from './webgl/tex_util'; | ||
textureType?: TextureType; | ||
isDisposed?: boolean; | ||
} | ||
@@ -61,2 +61,3 @@ export declare function initializeGPU(gpgpu: GPGPUContext, textureManager: TextureManager): void; | ||
getTextureShapeRC(preferredShapeRC?: [number, number]): [number, number]; | ||
private throwIfDisposed(); | ||
dispose(): void; | ||
@@ -74,6 +75,6 @@ private disposeTexture(); | ||
static new<T extends keyof DataTypes = keyof DataTypes>(value: number | boolean, dtype?: T): Scalar<T>; | ||
static ZERO: Scalar<util.DType>; | ||
static ONE: Scalar<util.DType>; | ||
static TWO: Scalar<util.DType>; | ||
static NEG_ONE: Scalar<util.DType>; | ||
static ZERO: Scalar<"float32" | "int32" | "bool">; | ||
static ONE: Scalar<"float32" | "int32" | "bool">; | ||
static TWO: Scalar<"float32" | "int32" | "bool">; | ||
static NEG_ONE: Scalar<"float32" | "int32" | "bool">; | ||
get(): number; | ||
@@ -80,0 +81,0 @@ val(): Promise<number>; |
@@ -109,2 +109,5 @@ "use strict"; | ||
NDArray.make = function (shape, data, dtype) { | ||
if (data.isDisposed) { | ||
throw new Error("Cannot make new NDArray from disposed NDArrayData."); | ||
} | ||
switch (shape.length) { | ||
@@ -138,2 +141,3 @@ case 0: | ||
NDArray.prototype.reshape = function (newShape) { | ||
this.throwIfDisposed(); | ||
newShape = util.inferFromImplicitShape(newShape, this.size); | ||
@@ -169,2 +173,3 @@ if (util.arraysEqual(this.shape, newShape)) { | ||
NDArray.prototype.asType = function (dtype) { | ||
this.throwIfDisposed(); | ||
var newData = this.getData(); | ||
@@ -178,2 +183,3 @@ if (newData.values != null) { | ||
get: function () { | ||
this.throwIfDisposed(); | ||
return this.shape.length; | ||
@@ -189,2 +195,3 @@ }, | ||
} | ||
this.throwIfDisposed(); | ||
var index = locs[locs.length - 1]; | ||
@@ -201,2 +208,3 @@ for (var i = 0; i < locs.length - 1; ++i) { | ||
} | ||
this.throwIfDisposed(); | ||
this.set.apply(this, [this.get.apply(this, locs) + value].concat(locs)); | ||
@@ -209,2 +217,3 @@ }; | ||
} | ||
this.throwIfDisposed(); | ||
var index = locs[locs.length - 1]; | ||
@@ -224,3 +233,5 @@ for (var i = 0; i < locs.length - 1; ++i) { | ||
switch (_a.label) { | ||
case 0: return [4, this.data()]; | ||
case 0: | ||
this.throwIfDisposed(); | ||
return [4, this.data()]; | ||
case 1: | ||
@@ -250,2 +261,3 @@ _a.sent(); | ||
NDArray.prototype.fill = function (value) { | ||
this.throwIfDisposed(); | ||
this.getValues().fill(value); | ||
@@ -268,2 +280,3 @@ }; | ||
case 0: | ||
this.throwIfDisposed(); | ||
if (this.ndarrayData.values != null) { | ||
@@ -294,2 +307,3 @@ return [2, this.ndarrayData.values]; | ||
NDArray.prototype.dataSync = function () { | ||
this.throwIfDisposed(); | ||
if (this.ndarrayData.values == null) { | ||
@@ -311,2 +325,3 @@ throwIfGPUNotInitialized(); | ||
throwIfGPUNotInitialized(); | ||
this.throwIfDisposed(); | ||
this.ndarrayData.textureShapeRC = | ||
@@ -321,2 +336,3 @@ webgl_util.getTextureShapeFromLogicalShape(exports.GPGPU.gl, this.shape, preferredTexShape); | ||
NDArray.prototype.getTexture = function (preferredShapeRC) { | ||
this.throwIfDisposed(); | ||
if (this.ndarrayData.texture == null) { | ||
@@ -328,2 +344,3 @@ this.uploadToGPU(preferredShapeRC); | ||
NDArray.prototype.getTextureShapeRC = function (preferredShapeRC) { | ||
this.throwIfDisposed(); | ||
if (this.ndarrayData.textureShapeRC == null) { | ||
@@ -334,5 +351,11 @@ this.uploadToGPU(preferredShapeRC); | ||
}; | ||
NDArray.prototype.throwIfDisposed = function () { | ||
if (this.ndarrayData.isDisposed) { | ||
throw new Error("NDArray is disposed."); | ||
} | ||
}; | ||
NDArray.prototype.dispose = function () { | ||
this.ndarrayData.values = null; | ||
this.shape = null; | ||
this.ndarrayData.isDisposed = true; | ||
if (this.ndarrayData.texture != null) { | ||
@@ -350,5 +373,7 @@ this.disposeTexture(); | ||
NDArray.prototype.inGPU = function () { | ||
this.throwIfDisposed(); | ||
return this.ndarrayData.texture != null; | ||
}; | ||
NDArray.prototype.equals = function (t) { | ||
this.throwIfDisposed(); | ||
return this.dtype === t.dtype && util.arraysEqual(this.shape, t.shape) && | ||
@@ -355,0 +380,0 @@ util.arraysEqual(this.getValues(), t.getValues()); |
@@ -0,3 +1,3 @@ | ||
import { ReduceInfo } from '../reduce_util'; | ||
import { GPGPUProgram } from './gpgpu_math'; | ||
export declare function getArgMinMaxSnippet(op: 'min' | 'max', texName: string, size: number): string; | ||
export declare class ArgMinMaxProgram implements GPGPUProgram { | ||
@@ -7,4 +7,3 @@ variableNames: string[]; | ||
userCode: string; | ||
numBatchDims: number; | ||
constructor(shape: number[], axes: number[], opType: 'min' | 'max'); | ||
constructor(reduceInfo: ReduceInfo, op: 'max' | 'min', firstPass: boolean); | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var util = require("../../util"); | ||
var axis_util = require("../axis_util"); | ||
function getArgMinMaxSnippet(op, texName, size) { | ||
var compOp = (op === 'min') ? '<' : '>'; | ||
return "\n float getArgMinMax" + texName + "() {\n int bestIndex = 0;\n float bestValue = get" + texName + "Flat(0);\n\n for (int i = 0; i < " + size + "; i++) {\n float candidate = get" + texName + "Flat(i);\n if (isNaN(candidate)) {\n return candidate;\n }\n if (candidate " + compOp + " bestValue) {\n bestValue = candidate;\n bestIndex = i;\n }\n }\n return float(bestIndex);\n }\n "; | ||
} | ||
exports.getArgMinMaxSnippet = getArgMinMaxSnippet; | ||
var ArgMinMaxProgram = (function () { | ||
function ArgMinMaxProgram(shape, axes, opType) { | ||
function ArgMinMaxProgram(reduceInfo, op, firstPass) { | ||
this.variableNames = ['A']; | ||
var _a = axis_util.computeOutAndReduceShapes(shape, axes), outShape = _a[0], reduceShape = _a[1]; | ||
this.outputShape = outShape; | ||
this.numBatchDims = outShape.length; | ||
var size = util.sizeFromShape(reduceShape); | ||
var aSnippet = getArgMinMaxSnippet(opType, 'A', size); | ||
this.userCode = "\n " + aSnippet + "\n\n void main() {\n setOutput(getArgMinMaxA());\n }\n "; | ||
var windowSize = reduceInfo.windowSize; | ||
var batchSize = reduceInfo.batchSize; | ||
var inSize = reduceInfo.inSize; | ||
var outSize = Math.ceil(inSize / windowSize); | ||
if (!firstPass) { | ||
this.variableNames.push('bestIndicesA'); | ||
} | ||
this.outputShape = [batchSize, outSize]; | ||
var compOp = (op === 'max') ? '>' : '<'; | ||
var indexSnippet = firstPass ? | ||
'inOffset + i;' : | ||
'round(getBestIndicesA(batch, inOffset + i));'; | ||
this.userCode = "\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n int outIdx = coords[1];\n int inOffset = outIdx * " + windowSize + ";\n\n int bestIndex = 0;\n float bestValue = getA(batch, inOffset);\n\n for (int i = 0; i < " + windowSize + "; i++) {\n int inIdx = " + indexSnippet + ";\n float candidate = getA(batch, inIdx);\n if (isNaN(candidate)) {\n setOutput(candidate);\n return;\n }\n if (candidate " + compOp + " bestValue) {\n bestValue = candidate;\n bestIndex = inIdx;\n }\n }\n setOutput(float(bestIndex));\n }\n "; | ||
} | ||
@@ -20,0 +20,0 @@ return ArgMinMaxProgram; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var concat_util = require("../concat_util"); | ||
var shader_compiler_1 = require("./shader_compiler"); | ||
var ConcatProgram = (function () { | ||
@@ -11,3 +12,3 @@ function ConcatProgram(aShape, bShape, axis) { | ||
this.outputShape = concat_util.computeOutShape(aShape, bShape, axis); | ||
var dType = getDataType(aShape.length); | ||
var dType = shader_compiler_1.getCoordsDataType(aShape.length); | ||
var unpackSnippet = getUnpack(aShape.length); | ||
@@ -53,19 +54,2 @@ var sampleCoords = getSampleCoords(aShape.length); | ||
} | ||
function getDataType(rank) { | ||
if (rank === 1) { | ||
return 'int'; | ||
} | ||
else if (rank === 2) { | ||
return 'ivec2'; | ||
} | ||
else if (rank === 3) { | ||
return 'ivec3'; | ||
} | ||
else if (rank === 4) { | ||
return 'ivec4'; | ||
} | ||
else { | ||
throw Error("Concat for rank " + rank + " is not yet supported"); | ||
} | ||
} | ||
//# sourceMappingURL=concat_gpu.js.map |
@@ -9,3 +9,2 @@ import { NDArray } from '../ndarray'; | ||
supportsBroadcasting?: boolean; | ||
numBatchDims?: number; | ||
} | ||
@@ -12,0 +11,0 @@ export interface GPGPUBinary { |
@@ -27,3 +27,3 @@ "use strict"; | ||
}; | ||
var source = shader_compiler.makeShader(inputInfos, outShapeInfo, userCode, program.supportsBroadcasting === true, program.numBatchDims); | ||
var source = shader_compiler.makeShader(inputInfos, outShapeInfo, userCode, program.supportsBroadcasting === true); | ||
var webGLProgram = gpgpu.createProgram(source); | ||
@@ -57,3 +57,3 @@ var uniformLocations = {}; | ||
exports.compileProgram = compileProgram; | ||
function validateBinaryAndProgram(shapeInfos, inputs, numBatchDims) { | ||
function validateBinaryAndProgram(shapeInfos, inputs) { | ||
if (shapeInfos.length !== inputs.length) { | ||
@@ -68,7 +68,7 @@ throw Error("Binary was compiled with " + shapeInfos.length + " inputs, but " + | ||
var texShapeB = inputs[i].getTextureShapeRC(); | ||
if (!numBatchDims && !util.arraysEqual(shapeA, shapeB)) { | ||
if (!util.arraysEqual(shapeA, shapeB)) { | ||
throw Error("Binary was compiled with different shapes than " + | ||
("the current args. Shapes " + shapeA + " and " + shapeB + " must match")); | ||
} | ||
if (!numBatchDims && !util.arraysEqual(texShapeA, texShapeB)) { | ||
if (!util.arraysEqual(texShapeA, texShapeB)) { | ||
throw Error("Binary was compiled with different texture shapes than the" + | ||
@@ -80,4 +80,4 @@ (" current args. Shape " + texShapeA + " and " + texShapeB + " must match")); | ||
function runProgram(binary, inputs, output, customSetup) { | ||
validateBinaryAndProgram(binary.inShapeInfos, inputs, binary.program.numBatchDims); | ||
validateBinaryAndProgram([binary.outShapeInfo], [output], binary.program.numBatchDims); | ||
validateBinaryAndProgram(binary.inShapeInfos, inputs); | ||
validateBinaryAndProgram([binary.outShapeInfo], [output]); | ||
var outTex = output.getTexture(); | ||
@@ -84,0 +84,0 @@ var outTexShape = output.getTextureShapeRC(); |
@@ -7,3 +7,2 @@ import { GPGPUContext } from './gpgpu_context'; | ||
userCode: string; | ||
numBatchDims: number; | ||
seedLoc: WebGLUniformLocation; | ||
@@ -10,0 +9,0 @@ constructor(batchSize: number, numOutcomes: number, numSamples: number); |
@@ -7,4 +7,3 @@ "use strict"; | ||
this.outputShape = [batchSize, numSamples]; | ||
this.numBatchDims = 1; | ||
this.userCode = "\n uniform float seed;\n\n void main() {\n float r = random(seed);\n float cdf = 0.0;\n\n for (int i = 0; i < " + (numOutcomes - 1) + "; i++) {\n cdf += getProbs(i);\n\n if (r < cdf) {\n setOutput(float(i));\n return;\n }\n }\n\n // If no other event happened, last event happened.\n setOutput(float(" + (numOutcomes - 1) + "));\n }\n "; | ||
this.userCode = "\n uniform float seed;\n\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n\n float r = random(seed);\n float cdf = 0.0;\n\n for (int i = 0; i < " + (numOutcomes - 1) + "; i++) {\n cdf += getProbs(batch, i);\n\n if (r < cdf) {\n setOutput(float(i));\n return;\n }\n }\n\n // If no other event happened, last event happened.\n setOutput(float(" + (numOutcomes - 1) + "));\n }\n "; | ||
} | ||
@@ -11,0 +10,0 @@ MultinomialProgram.prototype.getCustomSetupFunc = function (seed) { |
@@ -11,2 +11,3 @@ import { TextureType } from './tex_util'; | ||
}; | ||
export declare function makeShader(inputsInfo: InputInfo[], outputShape: ShapeInfo, userCode: string, broadcast: boolean, numBatchDims: number): string; | ||
export declare function makeShader(inputsInfo: InputInfo[], outputShape: ShapeInfo, userCode: string, broadcast: boolean): string; | ||
export declare function getCoordsDataType(rank: number): string; |
@@ -8,23 +8,7 @@ "use strict"; | ||
var tex_util_1 = require("./tex_util"); | ||
function updateBatchedShapeInfo(info, numBatchDims) { | ||
info.logicalShape = info.logicalShape.slice(numBatchDims); | ||
} | ||
function makeShader(inputsInfo, outputShape, userCode, broadcast, numBatchDims) { | ||
var batchSnippet = ''; | ||
if (numBatchDims) { | ||
if (inputsInfo.length !== 1) { | ||
throw new Error("Batching for 2 or more inputs is not yet supported. " + | ||
("Got " + inputsInfo.length + " inputs.")); | ||
} | ||
updateBatchedShapeInfo(inputsInfo[0].shapeInfo, numBatchDims); | ||
updateBatchedShapeInfo(outputShape, numBatchDims); | ||
var inputSize = util.sizeFromShape(inputsInfo[0].shapeInfo.logicalShape); | ||
var outputSize = util.sizeFromShape(outputShape.logicalShape); | ||
batchSnippet = getBatchSnippet(inputSize, outputSize, outputShape.texShape); | ||
} | ||
function makeShader(inputsInfo, outputShape, userCode, broadcast) { | ||
var sampleSnippet = getSampleSnippet(); | ||
var setOutputSnippet = getSetOutputSnippet(); | ||
var inputPrefixSnippet = inputsInfo.map(function (x) { return "uniform sampler2D " + x.name + ";"; }).join('\n'); | ||
var inputSamplingSnippet = inputsInfo | ||
.map(function (x) { return getInputSamplingSnippet(x, outputShape, broadcast, numBatchDims); }) | ||
var inputSamplingSnippet = inputsInfo.map(function (x) { return getInputSamplingSnippet(x, outputShape, broadcast); }) | ||
.join('\n'); | ||
@@ -34,4 +18,4 @@ var outTexShape = outputShape.texShape; | ||
var source = [ | ||
SHADER_PREFIX, batchSnippet, sampleSnippet, setOutputSnippet, | ||
inputPrefixSnippet, outputSamplingSnippet, inputSamplingSnippet, userCode | ||
SHADER_PREFIX, sampleSnippet, setOutputSnippet, inputPrefixSnippet, | ||
outputSamplingSnippet, inputSamplingSnippet, userCode | ||
].join('\n'); | ||
@@ -51,5 +35,5 @@ return source; | ||
} | ||
function getInputSamplingSnippet(inInfo, outShapeInfo, broadcast, numBatchDims) { | ||
function getInputSamplingSnippet(inInfo, outShapeInfo, broadcast) { | ||
var shape = inInfo.shapeInfo.logicalShape; | ||
var res = getSamplerFlat(inInfo, numBatchDims); | ||
var res = getSamplerFlat(inInfo); | ||
switch (shape.length) { | ||
@@ -81,5 +65,2 @@ case 0: | ||
} | ||
function getBatchSnippet(inputSize, outputSize, texShape) { | ||
return "\n int getBatchOffset() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2(" + texShape[0] + ", " + texShape[1] + "));\n int index = resTexRC.x * " + texShape[1] + " + resTexRC.y;\n int b = index / " + outputSize + ";\n return b * " + inputSize + ";\n }\n "; | ||
} | ||
function getOutputSamplingSnippet(outShape, outTexShape) { | ||
@@ -224,3 +205,3 @@ switch (outShape.length) { | ||
} | ||
function getSamplerFlat(inputInfo, numBatchDims) { | ||
function getSamplerFlat(inputInfo) { | ||
var texName = inputInfo.name; | ||
@@ -231,16 +212,12 @@ var texShape = inputInfo.shapeInfo.texShape; | ||
var tNumC = texShape[1]; | ||
var batchSnippet = ''; | ||
if (numBatchDims) { | ||
batchSnippet = 'index += getBatchOffset();'; | ||
} | ||
if (tNumC === 1 && tNumR === 1) { | ||
return "\n float " + funcName + "(int index) {\n " + batchSnippet + "\n return sample(" + texName + ", halfCR);\n }\n "; | ||
return "\n float " + funcName + "(int index) {\n return sample(" + texName + ", halfCR);\n }\n "; | ||
} | ||
if (tNumC === 1) { | ||
return "\n float " + funcName + "(int index) {\n " + batchSnippet + "\n vec2 uv = vec2(0.5, (float(index) + 0.5) / " + tNumR + ".0);\n return sample(" + texName + ", uv);\n }\n "; | ||
return "\n float " + funcName + "(int index) {\n vec2 uv = vec2(0.5, (float(index) + 0.5) / " + tNumR + ".0);\n return sample(" + texName + ", uv);\n }\n "; | ||
} | ||
if (tNumR === 1) { | ||
return "\n float " + funcName + "(int index) {\n " + batchSnippet + "\n vec2 uv = vec2((float(index) + 0.5) / " + tNumC + ".0, 0.5);\n return sample(" + texName + ", uv);\n }\n "; | ||
return "\n float " + funcName + "(int index) {\n vec2 uv = vec2((float(index) + 0.5) / " + tNumC + ".0, 0.5);\n return sample(" + texName + ", uv);\n }\n "; | ||
} | ||
return "\n float " + funcName + "(int index) {\n " + batchSnippet + "\n vec2 uv = UVfrom1D(" + tNumR + ", " + tNumC + ", index);\n return sample(" + texName + ", uv);\n }\n "; | ||
return "\n float " + funcName + "(int index) {\n vec2 uv = UVfrom1D(" + tNumR + ", " + tNumC + ", index);\n return sample(" + texName + ", uv);\n }\n "; | ||
} | ||
@@ -318,2 +295,20 @@ function getBroadcastOutputCoordsSampler(inputInfo, outShapeInfo, texFuncSnippet, funcName) { | ||
} | ||
function getCoordsDataType(rank) { | ||
if (rank === 1) { | ||
return 'int'; | ||
} | ||
else if (rank === 2) { | ||
return 'ivec2'; | ||
} | ||
else if (rank === 3) { | ||
return 'ivec3'; | ||
} | ||
else if (rank === 4) { | ||
return 'ivec4'; | ||
} | ||
else { | ||
throw Error("GPU for rank " + rank + " is not yet supported"); | ||
} | ||
} | ||
exports.getCoordsDataType = getCoordsDataType; | ||
//# sourceMappingURL=shader_compiler.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var shader_compiler_1 = require("./shader_compiler"); | ||
var SliceProgram = (function () { | ||
@@ -8,3 +9,3 @@ function SliceProgram(destSize) { | ||
this.rank = destSize.length; | ||
var dtype = getDataType(this.rank); | ||
var dtype = shader_compiler_1.getCoordsDataType(this.rank); | ||
var sourceCoords = getCoords(this.rank); | ||
@@ -63,19 +64,2 @@ this.userCode = "\n uniform " + dtype + " start;\n\n void main() {\n " + dtype + " sourceLoc = start + getOutputCoords();\n setOutput(getSource(" + sourceCoords + "));\n }\n "; | ||
} | ||
function getDataType(rank) { | ||
if (rank === 1) { | ||
return 'int'; | ||
} | ||
else if (rank === 2) { | ||
return 'ivec2'; | ||
} | ||
else if (rank === 3) { | ||
return 'ivec3'; | ||
} | ||
else if (rank === 4) { | ||
return 'ivec4'; | ||
} | ||
else { | ||
throw Error("Slicing for rank " + rank + " is not yet supported"); | ||
} | ||
} | ||
//# sourceMappingURL=slice_gpu.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var shader_compiler_1 = require("./shader_compiler"); | ||
var TransposeProgram = (function () { | ||
@@ -12,3 +13,3 @@ function TransposeProgram(aShape, newDim) { | ||
this.rank = outputShape.length; | ||
var dtype = getDataType(this.rank); | ||
var dtype = shader_compiler_1.getCoordsDataType(this.rank); | ||
var switched = getSwitchedCoords(newDim); | ||
@@ -32,19 +33,2 @@ this.userCode = "\n void main() {\n " + dtype + " resRC = getOutputCoords();\n setOutput(getA(" + switched + "));\n }\n "; | ||
} | ||
function getDataType(rank) { | ||
if (rank === 1) { | ||
return 'int'; | ||
} | ||
else if (rank === 2) { | ||
return 'ivec2'; | ||
} | ||
else if (rank === 3) { | ||
return 'ivec3'; | ||
} | ||
else if (rank === 4) { | ||
return 'ivec4'; | ||
} | ||
else { | ||
throw Error("Transpose for rank " + rank + " is not yet supported"); | ||
} | ||
} | ||
//# sourceMappingURL=transpose_gpu.js.map |
@@ -1,2 +0,2 @@ | ||
declare const version = "0.3.7"; | ||
declare const version = "0.3.8"; | ||
export { version }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var version = '0.3.7'; | ||
var version = '0.3.8'; | ||
exports.version = version; | ||
//# sourceMappingURL=version.js.map |
@@ -18,4 +18,6 @@ /** | ||
// tslint:disable-next-line:max-line-length | ||
import {Array1D, Array3D, Array4D, CheckpointLoader, Model, NDArray, NDArrayMath} from '../../src'; | ||
import {Array1D, Array3D, Array4D, CheckpointLoader, initializeGPU, Model, NDArray, NDArrayMath, NDArrayMathCPU, NDArrayMathGPU} from 'deeplearn'; | ||
import {IMAGENET_CLASSES} from './imagenet_classes'; | ||
const GOOGLE_CLOUD_STORAGE_DIR = | ||
@@ -29,3 +31,11 @@ 'https://storage.googleapis.com/learnjs-data/checkpoint_zoo/'; | ||
constructor(private math: NDArrayMath) {} | ||
constructor(private math: NDArrayMath) { | ||
// TODO(nsthorat): This awful hack is because we need to share the global | ||
// GPGPU between deeplearn loaded from standalone as well as the internal | ||
// deeplearn that gets compiled as part of this model. Remove this once we | ||
// decouple NDArray from storage mechanism. | ||
initializeGPU( | ||
(this.math as NDArrayMathGPU).getGPGPUContext(), | ||
(this.math as NDArrayMathGPU).getTextureManager()); | ||
} | ||
@@ -135,2 +145,23 @@ /** | ||
/** | ||
* Get the topK classes for pre-softmax logits. Returns a map of className | ||
* to softmax normalized probability. | ||
* | ||
* @param logits Pre-softmax logits array. | ||
* @param topK How many top classes to return. | ||
*/ | ||
async getTopKClasses(logits: Array1D, topK: number): | ||
Promise<{[className: string]: number}> { | ||
const predictions = this.math.softmax(logits); | ||
const topk = new NDArrayMathCPU().topK(predictions, topK); | ||
const topkIndices = await topk.indices.data(); | ||
const topkValues = await topk.values.data(); | ||
const topClassesToProbability: {[className: string]: number} = {}; | ||
for (let i = 0; i < topkIndices.length; i++) { | ||
topClassesToProbability[IMAGENET_CLASSES[topkIndices[i]]] = topkValues[i]; | ||
} | ||
return topClassesToProbability; | ||
} | ||
dispose() { | ||
@@ -137,0 +168,0 @@ this.preprocessOffset.dispose(); |
{ | ||
"name": "deeplearn", | ||
"version": "0.3.7", | ||
"version": "0.3.8", | ||
"description": "Hardware-accelerated JavaScript library for machine intelligence", | ||
@@ -15,6 +15,5 @@ "private": false, | ||
"devDependencies": { | ||
"@types/polymer": "~1.1.31", | ||
"@types/jasmine": "~2.5.53", | ||
"@types/polymer": "~1.1.31", | ||
"@types/seedrandom": "~2.4.27", | ||
"bower": "~1.8.0", | ||
"browserify": "~14.4.0", | ||
@@ -31,3 +30,2 @@ "cross-spawn": "~5.1.0", | ||
"opn": "~5.1.0", | ||
"polymer-bundler": "~3.0.1", | ||
"tone-piano": "0.0.5", | ||
@@ -43,3 +41,3 @@ "tsify": "~3.0.1", | ||
"scripts": { | ||
"prep": "npm install && bower install && mkdirp dist", | ||
"prep": "npm install && mkdirp dist && cd demos/ && npm run prep && cd ..", | ||
"build": "tsc", | ||
@@ -46,0 +44,0 @@ "test": "karma start", |
@@ -25,4 +25,4 @@ <a id="travis-badge" href="https://travis-ci.org/PAIR-code/deeplearnjs" alt="Build Status"> | ||
#### TypeScript / ES6 JavaScript | ||
See the [TypeScript starter project](./starter/typescript/) and the | ||
[ES6 starter project](./starter/es6/) to get you quickly started. They contain a | ||
See the [TypeScript starter project](https://github.com/PAIR-code/deeplearnjs/tree/master/starter/typescript/) and the | ||
[ES6 starter project](https://github.com/PAIR-code/deeplearnjs/tree/master/starter/es6/) to get you quickly started. They contain a | ||
short example that sums an array with a scalar (broadcasted): | ||
@@ -174,3 +174,1 @@ | ||
</p> | ||
This is not an official Google product. |
@@ -22,2 +22,3 @@ { | ||
"starter/", | ||
"models/", | ||
"node_modules/", | ||
@@ -24,0 +25,0 @@ "dist/" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
1493755
22
209
25436
173