clustering-tfjs
Version:
High-performance TypeScript clustering algorithms (K-Means, Spectral, Agglomerative) with TensorFlow.js acceleration and scikit-learn compatibility
130 lines (129 loc) • 4.05 kB
JavaScript
;
/**
* TensorFlow.js backend manager
*
* Manages a singleton instance of TensorFlow.js with support for
* multiple backends and environments.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.initializeBackend = initializeBackend;
exports.getTensorFlow = getTensorFlow;
exports.isInitialized = isInitialized;
exports.resetBackend = resetBackend;
// Singleton storage
let tfInstance = null;
let initializationPromise = null;
/**
* Initialize the TensorFlow.js backend
*/
async function initializeBackend(config = {}) {
// Return existing instance if already initialized
if (tfInstance) {
return tfInstance;
}
// Return existing initialization promise if in progress
if (initializationPromise) {
return initializationPromise;
}
// Start initialization
initializationPromise = loadBackend(config);
try {
tfInstance = await initializationPromise;
return tfInstance;
}
catch (error) {
// Reset on error to allow retry
initializationPromise = null;
throw error;
}
}
/**
* Get the current TensorFlow instance
* @throws Error if not initialized
*/
function getTensorFlow() {
if (!tfInstance) {
throw new Error('TensorFlow.js not initialized. Please call Clustering.init() first.');
}
return tfInstance;
}
/**
* Check if TensorFlow is initialized
*/
function isInitialized() {
return tfInstance !== null;
}
/**
* Reset the backend (mainly for testing)
*/
function resetBackend() {
tfInstance = null;
initializationPromise = null;
}
/**
* Load the appropriate backend based on environment and config
*/
async function loadBackend(config) {
// Detect environment
const isNode = typeof window === 'undefined' &&
typeof process !== 'undefined' &&
process.versions &&
process.versions.node;
let tf;
if (isNode) {
// Node.js environment
const loader = await Promise.resolve().then(() => __importStar(require('./tf-loader.node')));
tf = await loader.loadTensorFlow();
}
else {
// Browser environment
const loader = await Promise.resolve().then(() => __importStar(require('./tf-loader.browser')));
tf = await loader.loadTensorFlow();
}
// Set custom flags if provided
if (config.flags) {
Object.entries(config.flags).forEach(([flag, value]) => {
tf.env().setFlags({ [flag]: value });
});
}
// Set specific backend if requested
if (config.backend) {
await tf.setBackend(config.backend);
}
// Wait for backend to be ready
await tf.ready();
return tf;
}