Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constructor(inputs, outputs, options) {
super();
if (inputs === true) {
// reloading model
this.alpha = outputs.alpha;
this.inputs = outputs.inputs;
this.kernelType = outputs.kernelType;
this.kernelOptions = outputs.kernelOptions;
this.kernel = new Kernel(outputs.kernelType, outputs.kernelOptions);
} else {
inputs = Matrix.checkMatrix(inputs);
options = Object.assign({}, defaultOptions, options);
const kernelFunction = new Kernel(
options.kernelType,
options.kernelOptions
);
const K = kernelFunction.compute(inputs);
const n = inputs.rows;
K.add(Matrix.eye(n, n).mul(options.lambda));
this.alpha = solve(K, outputs);
this.inputs = inputs;
this.kernelType = options.kernelType;
this.kernelOptions = options.kernelOptions;
this.kernel = kernelFunction;
}
var selectedFunction = ACTIVATION_FUNCTIONS[options.activation];
var params = selectedFunction.activation.length;
var actFunction = params > 1 ? (val) => selectedFunction.activation(val, options.activationParam) : selectedFunction.activation;
var derFunction = params > 1 ? (val) => selectedFunction.derivate(val, options.activationParam) : selectedFunction.derivate;
this.activationFunction = function (i, j) {
this.set(i, j, actFunction(this.get(i, j)));
};
this.derivate = function (i, j) {
this.set(i, j, derFunction(this.get(i, j)));
};
if (options.model) {
// load model
this.W = Matrix.checkMatrix(options.W);
this.b = Matrix.checkMatrix(options.b);
} else {
// default constructor
this.W = Matrix.rand(this.inputSize, this.outputSize);
this.b = Matrix.zeros(1, this.outputSize);
this.W.apply(function (i, j) {
this.set(i, j, this.get(i, j) / Math.sqrt(options.inputSize));
});
}
}
var params = selectedFunction.activation.length;
var actFunction = params > 1 ? (val) => selectedFunction.activation(val, options.activationParam) : selectedFunction.activation;
var derFunction = params > 1 ? (val) => selectedFunction.derivate(val, options.activationParam) : selectedFunction.derivate;
this.activationFunction = function (i, j) {
this.set(i, j, actFunction(this.get(i, j)));
};
this.derivate = function (i, j) {
this.set(i, j, derFunction(this.get(i, j)));
};
if (options.model) {
// load model
this.W = Matrix.checkMatrix(options.W);
this.b = Matrix.checkMatrix(options.b);
} else {
// default constructor
this.W = Matrix.rand(this.inputSize, this.outputSize);
this.b = Matrix.zeros(1, this.outputSize);
this.W.apply(function (i, j) {
this.set(i, j, this.get(i, j) / Math.sqrt(options.inputSize));
});
}
}
train(features, labels) {
features = Matrix.checkMatrix(features);
this.dicts = dictOutputs(labels);
var inputSize = features.columns;
var outputSize = Object.keys(this.dicts.inputs).length;
if (!this.model) {
this.buildNetwork(inputSize, outputSize);
}
for (var i = 0; i < this.iterations; ++i) {
var probabilities = this.propagate(features);
this.backpropagation(features, labels, probabilities);
}
}
predict(features) {
features = Matrix.checkMatrix(features);
var outputs = new Array(features.rows);
var probabilities = this.propagate(features);
for (var i = 0; i < features.rows; ++i) {
outputs[i] = this.dicts.outputs[probabilities.maxRowIndex(i)[1]];
}
return outputs;
}
invert(dataset) {
dataset = Matrix.checkMatrix(dataset);
var inverse = dataset.mmul(this.U.transpose());
if (this.center) {
if (this.scale) {
inverse.mulRowVector(this.stdevs);
}
inverse.addRowVector(this.means);
}
return inverse;
}
constructor(dataset, options = {}) {
if (dataset === true) {
const model = options;
this.center = model.center;
this.scale = model.scale;
this.means = model.means;
this.stdevs = model.stdevs;
this.U = Matrix.checkMatrix(model.U);
this.S = model.S;
this.R = model.R;
this.excludedFeatures = model.excludedFeatures || [];
return;
}
dataset = new Matrix(dataset);
const {
isCovarianceMatrix = false,
method = 'SVD',
nCompNIPALS = 2,
center = true,
scale = false,
ignoreZeroVariance = false,
} = options;