@astermind/astermind-premium
Version:
Astermind Premium - Premium ML Toolkit
119 lines • 4.75 kB
JavaScript
// deep-kernel-elm.ts — Deep Kernel ELM
// Multi-layer kernel transformations with hierarchical kernel learning
import { KernelELM } from '@astermind/astermind-elm';
import { requireLicense } from '../core/license.js';
/**
* Deep Kernel ELM with multi-layer kernel transformations
* Features:
* - Hierarchical kernel learning
* - Deep feature extraction
* - Multi-layer kernel transformations
* - Complex non-linear pattern learning
*/
export class DeepKernelELM {
constructor(options) {
this.layers = [];
this.trained = false;
requireLicense(); // Premium feature - requires valid license
this.categories = options.categories;
this.options = {
categories: options.categories,
numLayers: options.numLayers ?? 3,
kernelType: options.kernelType ?? 'rbf',
hiddenUnitsPerLayer: options.hiddenUnitsPerLayer ?? 256,
gamma: options.gamma ?? 1.0,
degree: options.degree ?? 2,
coef0: options.coef0 ?? 0,
activation: options.activation ?? 'relu',
maxLen: options.maxLen ?? 100,
useTokenizer: options.useTokenizer ?? true,
};
// Initialize layers
for (let i = 0; i < this.options.numLayers; i++) {
const kelm = new KernelELM({
useTokenizer: i === 0 && this.options.useTokenizer ? true : undefined,
categories: i === this.options.numLayers - 1 ? this.options.categories : [],
maxLen: this.options.maxLen,
kernel: this.options.kernelType,
gamma: this.options.gamma,
degree: this.options.degree,
coef0: this.options.coef0,
});
this.layers.push(kelm);
}
}
/**
* Train deep kernel ELM
*/
train(X, y) {
// Prepare labels
const labelIndices = y.map(label => typeof label === 'number'
? label
: this.options.categories.indexOf(label));
// Forward pass through layers
let currentFeatures = X;
for (let i = 0; i < this.layers.length; i++) {
const layer = this.layers[i];
if (i === this.layers.length - 1) {
// Final layer: train with labels
layer.setCategories?.(this.options.categories);
layer.trainFromData?.(currentFeatures, labelIndices);
}
else {
// Intermediate layers: train autoencoder-style
layer.trainFromData?.(currentFeatures, currentFeatures.map((_, idx) => idx));
}
// Extract features from this layer
currentFeatures = this._extractLayerFeatures(currentFeatures, layer);
}
this.trained = true;
}
/**
* Extract features from a layer
*/
_extractLayerFeatures(X, layer) {
const features = [];
for (const x of X) {
// Get kernel features (simplified - in practice, you'd extract actual kernel features)
const pred = layer.predictLogitsFromVector?.(x) || [];
features.push(pred.length > 0 ? pred : x); // Use prediction as features or fallback to input
}
return features;
}
/**
* Predict with deep kernel
*/
predict(X, topK = 3, returnLayerFeatures = false) {
if (!this.trained) {
throw new Error('Model must be trained before prediction');
}
const XArray = Array.isArray(X[0]) ? X : [X];
const allResults = [];
for (const x of XArray) {
// Forward pass through layers
let currentFeatures = x;
const layerFeatures = [];
for (let i = 0; i < this.layers.length - 1; i++) {
const layer = this.layers[i];
const features = layer.predictLogitsFromVector?.(currentFeatures) || currentFeatures;
layerFeatures.push(features);
currentFeatures = features;
}
// Final layer prediction
const finalLayer = this.layers[this.layers.length - 1];
const preds = finalLayer.predictFromVector?.([currentFeatures], topK) || [];
for (const pred of preds.slice(0, topK)) {
const result = {
label: pred.label || this.options.categories[pred.index || 0],
prob: pred.prob || 0,
};
if (returnLayerFeatures) {
result.layerFeatures = layerFeatures.map(f => [...f]);
}
allResults.push(result);
}
}
return allResults;
}
}
//# sourceMappingURL=deep-kernel-elm.js.map