UNPKG

clustering-tfjs

Version:

High-performance TypeScript clustering algorithms (K-Means, Spectral, Agglomerative) with TensorFlow.js acceleration and scikit-learn compatibility

484 lines (483 loc) 18.8 kB
"use strict"; /** * Browser-specific TensorFlow.js adapter * * This module is used when building for browser environments. * It expects users to have loaded @tensorflow/tfjs separately. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.mean = exports.norm = exports.transpose = exports.outerProduct = exports.dot = exports.matMul = exports.softmax = exports.prelu = exports.leakyRelu = exports.selu = exports.relu = exports.elu = exports.tanh = exports.cosh = exports.sinh = exports.atan = exports.acos = exports.asin = exports.tan = exports.cos = exports.sin = exports.ceil = exports.floor = exports.round = exports.sign = exports.neg = exports.abs = exports.square = exports.sqrt = exports.pow = exports.div = exports.mul = exports.sub = exports.add = exports.linspace = exports.range = exports.fill = exports.onesLike = exports.zerosLike = exports.ones = exports.zeros = exports.scalar = exports.variable = exports.tensor6d = exports.tensor5d = exports.tensor4d = exports.tensor3d = exports.tensor2d = exports.tensor1d = exports.tensor = void 0; exports.Tensor = exports.scatterND = exports.topk = exports.variableGrads = exports.valueAndGrads = exports.valueAndGrad = exports.customGrad = exports.grads = exports.grad = exports.getBackend = exports.setBackend = exports.ready = exports.env = exports.backend = exports.memory = exports.keep = exports.dispose = exports.tidy = exports.unique = exports.diag = exports.eye = exports.where = exports.logicalNot = exports.logicalOr = exports.logicalAnd = exports.lessEqual = exports.less = exports.greaterEqual = exports.greater = exports.equal = exports.expandDims = exports.squeeze = exports.reshape = exports.cast = exports.reverse = exports.gather = exports.split = exports.unstack = exports.stack = exports.concat = exports.slice = exports.argMin = exports.argMax = exports.any = exports.all = exports.cumsum = exports.prod = exports.max = exports.min = exports.sum = void 0; exports.randomGamma = exports.multinomial = exports.randomUniform = exports.randomNormal = exports.booleanMaskAsync = exports.irfft = exports.rfft = exports.ifft = exports.fft = exports.imag = exports.real = exports.complex = exports.avgPool3d = exports.maxPool3d = exports.pool = exports.avgPool = exports.maxPool = exports.conv3dTranspose = exports.conv3d = exports.conv2dTranspose = exports.conv2d = exports.conv1d = exports.depthwiseConv2d = exports.separableConv2d = exports.localResponseNormalization = exports.batchNorm = exports.logicalXor = exports.notEqual = exports.pad = exports.print = exports.clone = exports.minimum = exports.maximum = exports.exp = exports.log = exports.sigmoid = exports.io = exports.util = exports.browser = exports.data = exports.train = exports.losses = exports.linalg = exports.image = void 0; // Function to get tf from global function getTf() { if (typeof window !== 'undefined' && window.tf) { return window.tf; } throw new Error('TensorFlow.js not found. Please load it before using this library.'); } // Re-export all tf functions, properly typed const tf = new Proxy({}, { get(_target, prop) { const tfInstance = getTf(); return tfInstance[prop]; } }); // Export commonly used functions for better tree-shaking const tensor = (...args) => tf.tensor(...args); exports.tensor = tensor; const tensor1d = (...args) => tf.tensor1d(...args); exports.tensor1d = tensor1d; const tensor2d = (...args) => tf.tensor2d(...args); exports.tensor2d = tensor2d; const tensor3d = (...args) => tf.tensor3d(...args); exports.tensor3d = tensor3d; const tensor4d = (...args) => tf.tensor4d(...args); exports.tensor4d = tensor4d; const tensor5d = (...args) => tf.tensor5d(...args); exports.tensor5d = tensor5d; const tensor6d = (...args) => tf.tensor6d(...args); exports.tensor6d = tensor6d; const variable = (...args) => tf.variable(...args); exports.variable = variable; const scalar = (...args) => tf.scalar(...args); exports.scalar = scalar; const zeros = (...args) => tf.zeros(...args); exports.zeros = zeros; const ones = (...args) => tf.ones(...args); exports.ones = ones; const zerosLike = (...args) => tf.zerosLike(...args); exports.zerosLike = zerosLike; const onesLike = (...args) => tf.onesLike(...args); exports.onesLike = onesLike; const fill = (...args) => tf.fill(...args); exports.fill = fill; const range = (...args) => tf.range(...args); exports.range = range; const linspace = (...args) => tf.linspace(...args); exports.linspace = linspace; // Math operations const add = (...args) => tf.add(...args); exports.add = add; const sub = (...args) => tf.sub(...args); exports.sub = sub; const mul = (...args) => tf.mul(...args); exports.mul = mul; const div = (...args) => tf.div(...args); exports.div = div; const pow = (...args) => tf.pow(...args); exports.pow = pow; const sqrt = (...args) => tf.sqrt(...args); exports.sqrt = sqrt; const square = (...args) => tf.square(...args); exports.square = square; const abs = (...args) => tf.abs(...args); exports.abs = abs; const neg = (...args) => tf.neg(...args); exports.neg = neg; const sign = (...args) => tf.sign(...args); exports.sign = sign; const round = (...args) => tf.round(...args); exports.round = round; const floor = (...args) => tf.floor(...args); exports.floor = floor; const ceil = (...args) => tf.ceil(...args); exports.ceil = ceil; const sin = (...args) => tf.sin(...args); exports.sin = sin; const cos = (...args) => tf.cos(...args); exports.cos = cos; const tan = (...args) => tf.tan(...args); exports.tan = tan; const asin = (...args) => tf.asin(...args); exports.asin = asin; const acos = (...args) => tf.acos(...args); exports.acos = acos; const atan = (...args) => tf.atan(...args); exports.atan = atan; const sinh = (...args) => tf.sinh(...args); exports.sinh = sinh; const cosh = (...args) => tf.cosh(...args); exports.cosh = cosh; const tanh = (...args) => tf.tanh(...args); exports.tanh = tanh; const elu = (...args) => tf.elu(...args); exports.elu = elu; const relu = (...args) => tf.relu(...args); exports.relu = relu; const selu = (...args) => tf.selu(...args); exports.selu = selu; const leakyRelu = (...args) => tf.leakyRelu(...args); exports.leakyRelu = leakyRelu; const prelu = (...args) => tf.prelu(...args); exports.prelu = prelu; const softmax = (...args) => tf.softmax(...args); exports.softmax = softmax; // Linear algebra const matMul = (...args) => tf.matMul(...args); exports.matMul = matMul; const dot = (...args) => tf.dot(...args); exports.dot = dot; const outerProduct = (...args) => tf.outerProduct(...args); exports.outerProduct = outerProduct; const transpose = (...args) => tf.transpose(...args); exports.transpose = transpose; const norm = (...args) => tf.norm(...args); exports.norm = norm; // Reduction const mean = (...args) => tf.mean(...args); exports.mean = mean; const sum = (...args) => tf.sum(...args); exports.sum = sum; const min = (...args) => tf.min(...args); exports.min = min; const max = (...args) => tf.max(...args); exports.max = max; const prod = (...args) => tf.prod(...args); exports.prod = prod; const cumsum = (...args) => tf.cumsum(...args); exports.cumsum = cumsum; const all = (...args) => tf.all(...args); exports.all = all; const any = (...args) => tf.any(...args); exports.any = any; const argMax = (...args) => tf.argMax(...args); exports.argMax = argMax; const argMin = (...args) => tf.argMin(...args); exports.argMin = argMin; // Manipulation const slice = (...args) => tf.slice(...args); exports.slice = slice; const concat = (...args) => tf.concat(...args); exports.concat = concat; const stack = (...args) => tf.stack(...args); exports.stack = stack; const unstack = (...args) => tf.unstack(...args); exports.unstack = unstack; const split = (...args) => tf.split(...args); exports.split = split; const gather = (...args) => tf.gather(...args); exports.gather = gather; const reverse = (...args) => tf.reverse(...args); exports.reverse = reverse; const cast = (...args) => tf.cast(...args); exports.cast = cast; const reshape = (...args) => tf.reshape(...args); exports.reshape = reshape; const squeeze = (...args) => tf.squeeze(...args); exports.squeeze = squeeze; const expandDims = (...args) => tf.expandDims(...args); exports.expandDims = expandDims; // Logical const equal = (...args) => tf.equal(...args); exports.equal = equal; const greater = (...args) => tf.greater(...args); exports.greater = greater; const greaterEqual = (...args) => tf.greaterEqual(...args); exports.greaterEqual = greaterEqual; const less = (...args) => tf.less(...args); exports.less = less; const lessEqual = (...args) => tf.lessEqual(...args); exports.lessEqual = lessEqual; const logicalAnd = (...args) => tf.logicalAnd(...args); exports.logicalAnd = logicalAnd; const logicalOr = (...args) => tf.logicalOr(...args); exports.logicalOr = logicalOr; const logicalNot = (...args) => tf.logicalNot(...args); exports.logicalNot = logicalNot; const where = (...args) => tf.where(...args); exports.where = where; // Special tensors const eye = (...args) => tf.eye(...args); exports.eye = eye; const diag = (...args) => tf.diag(...args); exports.diag = diag; const unique = (...args) => tf.unique(...args); exports.unique = unique; // Utility const tidy = (...args) => tf.tidy(...args); exports.tidy = tidy; const dispose = (...args) => tf.dispose(...args); exports.dispose = dispose; const keep = (...args) => tf.keep(...args); exports.keep = keep; const memory = () => tf.memory(); exports.memory = memory; const backend = () => tf.backend(); exports.backend = backend; const env = () => tf.env(); exports.env = env; const ready = () => tf.ready(); exports.ready = ready; const setBackend = (...args) => tf.setBackend(...args); exports.setBackend = setBackend; const getBackend = () => tf.getBackend(); exports.getBackend = getBackend; // Advanced const grad = (...args) => tf.grad(...args); exports.grad = grad; const grads = (...args) => tf.grads(...args); exports.grads = grads; const customGrad = (...args) => tf.customGrad(...args); exports.customGrad = customGrad; const valueAndGrad = (...args) => tf.valueAndGrad(...args); exports.valueAndGrad = valueAndGrad; const valueAndGrads = (...args) => tf.valueAndGrads(...args); exports.valueAndGrads = valueAndGrads; const variableGrads = (...args) => tf.variableGrads(...args); exports.variableGrads = variableGrads; // Scatter const topk = (...args) => tf.topk(...args); exports.topk = topk; const scatterND = (...args) => tf.scatterND(...args); exports.scatterND = scatterND; // Globals/Types - Access from runtime tf object const Tensor = () => getTf().Tensor; exports.Tensor = Tensor; // Namespaces - return functions to avoid immediate evaluation const image = () => getTf().image; exports.image = image; const linalg = () => getTf().linalg; exports.linalg = linalg; const losses = () => getTf().losses; exports.losses = losses; const train = () => getTf().train; exports.train = train; // data namespace is not in @tensorflow/tfjs-core, only in full tfjs // Return type is unknown since data namespace types aren't in core const data = () => { const tfInstance = getTf(); if ('data' in tfInstance) { return tfInstance.data; } throw new Error('TensorFlow.js data API not available. Please load @tensorflow/tfjs instead of @tensorflow/tfjs-core'); }; exports.data = data; const browser = () => getTf().browser; exports.browser = browser; const util = () => getTf().util; exports.util = util; const io = () => getTf().io; exports.io = io; // Additional functions that might be needed - export them const sigmoid = (...args) => tf.sigmoid(...args); exports.sigmoid = sigmoid; const log = (...args) => tf.log(...args); exports.log = log; const exp = (...args) => tf.exp(...args); exports.exp = exp; const maximum = (...args) => tf.maximum(...args); exports.maximum = maximum; const minimum = (...args) => tf.minimum(...args); exports.minimum = minimum; const clone = (...args) => tf.clone(...args); exports.clone = clone; const print = (...args) => tf.print(...args); exports.print = print; const pad = (...args) => tf.pad(...args); exports.pad = pad; const notEqual = (...args) => tf.notEqual(...args); exports.notEqual = notEqual; const logicalXor = (...args) => tf.logicalXor(...args); exports.logicalXor = logicalXor; const batchNorm = (...args) => tf.batchNorm(...args); exports.batchNorm = batchNorm; const localResponseNormalization = (...args) => tf.localResponseNormalization(...args); exports.localResponseNormalization = localResponseNormalization; const separableConv2d = (...args) => tf.separableConv2d(...args); exports.separableConv2d = separableConv2d; const depthwiseConv2d = (...args) => tf.depthwiseConv2d(...args); exports.depthwiseConv2d = depthwiseConv2d; const conv1d = (...args) => tf.conv1d(...args); exports.conv1d = conv1d; const conv2d = (...args) => tf.conv2d(...args); exports.conv2d = conv2d; const conv2dTranspose = (...args) => tf.conv2dTranspose(...args); exports.conv2dTranspose = conv2dTranspose; const conv3d = (...args) => tf.conv3d(...args); exports.conv3d = conv3d; const conv3dTranspose = (...args) => tf.conv3dTranspose(...args); exports.conv3dTranspose = conv3dTranspose; const maxPool = (...args) => tf.maxPool(...args); exports.maxPool = maxPool; const avgPool = (...args) => tf.avgPool(...args); exports.avgPool = avgPool; const pool = (...args) => tf.pool(...args); exports.pool = pool; const maxPool3d = (...args) => tf.maxPool3d(...args); exports.maxPool3d = maxPool3d; const avgPool3d = (...args) => tf.avgPool3d(...args); exports.avgPool3d = avgPool3d; const complex = (...args) => tf.complex(...args); exports.complex = complex; const real = (...args) => tf.real(...args); exports.real = real; const imag = (...args) => tf.imag(...args); exports.imag = imag; const fft = (...args) => tf.fft(...args); exports.fft = fft; const ifft = (...args) => tf.ifft(...args); exports.ifft = ifft; const rfft = (...args) => tf.rfft(...args); exports.rfft = rfft; const irfft = (...args) => tf.irfft(...args); exports.irfft = irfft; const booleanMaskAsync = (...args) => tf.booleanMaskAsync(...args); exports.booleanMaskAsync = booleanMaskAsync; const randomNormal = (...args) => tf.randomNormal(...args); exports.randomNormal = randomNormal; const randomUniform = (...args) => tf.randomUniform(...args); exports.randomUniform = randomUniform; const multinomial = (...args) => tf.multinomial(...args); exports.multinomial = multinomial; const randomGamma = (...args) => tf.randomGamma(...args); exports.randomGamma = randomGamma; // Default export as namespace exports.default = { // Export all our typed functions tensor: exports.tensor, tensor1d: exports.tensor1d, tensor2d: exports.tensor2d, tensor3d: exports.tensor3d, tensor4d: exports.tensor4d, tensor5d: exports.tensor5d, tensor6d: exports.tensor6d, variable: exports.variable, scalar: exports.scalar, zeros: exports.zeros, ones: exports.ones, zerosLike: exports.zerosLike, onesLike: exports.onesLike, fill: exports.fill, range: exports.range, linspace: exports.linspace, add: exports.add, sub: exports.sub, mul: exports.mul, div: exports.div, pow: exports.pow, sqrt: exports.sqrt, square: exports.square, abs: exports.abs, neg: exports.neg, sign: exports.sign, round: exports.round, floor: exports.floor, ceil: exports.ceil, sin: exports.sin, cos: exports.cos, tan: exports.tan, asin: exports.asin, acos: exports.acos, atan: exports.atan, sinh: exports.sinh, cosh: exports.cosh, tanh: exports.tanh, elu: exports.elu, relu: exports.relu, selu: exports.selu, leakyRelu: exports.leakyRelu, prelu: exports.prelu, softmax: exports.softmax, matMul: exports.matMul, dot: exports.dot, outerProduct: exports.outerProduct, transpose: exports.transpose, norm: exports.norm, mean: exports.mean, sum: exports.sum, min: exports.min, max: exports.max, prod: exports.prod, cumsum: exports.cumsum, all: exports.all, any: exports.any, argMax: exports.argMax, argMin: exports.argMin, slice: exports.slice, concat: exports.concat, stack: exports.stack, unstack: exports.unstack, split: exports.split, gather: exports.gather, reverse: exports.reverse, cast: exports.cast, reshape: exports.reshape, squeeze: exports.squeeze, expandDims: exports.expandDims, equal: exports.equal, greater: exports.greater, greaterEqual: exports.greaterEqual, less: exports.less, lessEqual: exports.lessEqual, logicalAnd: exports.logicalAnd, logicalOr: exports.logicalOr, logicalNot: exports.logicalNot, where: exports.where, eye: exports.eye, diag: exports.diag, unique: exports.unique, tidy: exports.tidy, dispose: exports.dispose, keep: exports.keep, memory: exports.memory, backend: exports.backend, env: exports.env, ready: exports.ready, setBackend: exports.setBackend, getBackend: exports.getBackend, grad: exports.grad, grads: exports.grads, customGrad: exports.customGrad, valueAndGrad: exports.valueAndGrad, valueAndGrads: exports.valueAndGrads, variableGrads: exports.variableGrads, topk: exports.topk, scatterND: exports.scatterND, Tensor: exports.Tensor, image: exports.image, linalg: exports.linalg, losses: exports.losses, train: exports.train, data: exports.data, browser: exports.browser, util: exports.util, io: exports.io, // Additional sigmoid: exports.sigmoid, log: exports.log, exp: exports.exp, maximum: exports.maximum, minimum: exports.minimum, clone: exports.clone, print: exports.print, pad: exports.pad, notEqual: exports.notEqual, logicalXor: exports.logicalXor, batchNorm: exports.batchNorm, localResponseNormalization: exports.localResponseNormalization, separableConv2d: exports.separableConv2d, depthwiseConv2d: exports.depthwiseConv2d, conv1d: exports.conv1d, conv2d: exports.conv2d, conv2dTranspose: exports.conv2dTranspose, conv3d: exports.conv3d, conv3dTranspose: exports.conv3dTranspose, maxPool: exports.maxPool, avgPool: exports.avgPool, pool: exports.pool, maxPool3d: exports.maxPool3d, avgPool3d: exports.avgPool3d, complex: exports.complex, real: exports.real, imag: exports.imag, fft: exports.fft, ifft: exports.ifft, rfft: exports.rfft, irfft: exports.irfft, booleanMaskAsync: exports.booleanMaskAsync, randomNormal: exports.randomNormal, randomUniform: exports.randomUniform, multinomial: exports.multinomial, randomGamma: exports.randomGamma, };