@jsmlt/jsmlt
Version:
JavaScript Machine Learning
329 lines (247 loc) • 14.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = exports.BinaryPerceptron = void 0;
var _base = require("../base");
var Arrays = _interopRequireWildcard(require("../../arrays"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
/**
* Perceptron learner for binary classification problem.
*/
var BinaryPerceptron =
/*#__PURE__*/
function (_Classifier) {
_inherits(BinaryPerceptron, _Classifier);
function BinaryPerceptron() {
_classCallCheck(this, BinaryPerceptron);
return _possibleConstructorReturn(this, _getPrototypeOf(BinaryPerceptron).apply(this, arguments));
}
_createClass(BinaryPerceptron, [{
key: "getClassIndexSign",
/**
* Get the signed value of the class index. Returns -1 for class index 0, 1 for class index 1.
*
* @param {number} classIndex - Class index
* @return {number} Sign corresponding to class index
*/
value: function getClassIndexSign(classIndex) {
return classIndex * 2 - 1;
}
/**
* Get the class index corresponding to a sign.
*
* @param {number} sign - Sign
* @return {number} Class index corresponding to sign
*/
}, {
key: "getSignClassIndex",
value: function getSignClassIndex(sign) {
return (sign + 1) / 2;
}
/**
* @see {Classifier#train}
*/
}, {
key: "train",
value: function train(X, y) {
// Weights increment to check for convergence
this.weightsIncrement = Infinity; // Initialize weights vector to zero. Here, the number of weights equals one plus the number of
// features, where the first weight (w0) is the weight used for the bias.
this.weights = Arrays.zeros(1 + X[0].length); // Store historic errors
var epochNumErrors = []; // Iteration index
var epoch = 0; // A single iteration of this loop corresponds to a single iteration of training all data
// points in the data set
while (true) {
var _this$trainIteration = this.trainIteration(X, y),
_this$trainIteration2 = _slicedToArray(_this$trainIteration, 2),
numErrors = _this$trainIteration2[0],
weightsIncrement = _this$trainIteration2[1];
epochNumErrors.push(numErrors);
if (weightsIncrement.reduce(function (r, a) {
return r + Math.abs(a);
}, 0) < 0.0001 || epoch > 100) {
break;
}
epoch += 1;
}
}
/**
* Train the classifier for a single iteration on the stored training data.
*
* @param {Array.<Array.<number>>} X - Features per data point
* @param {Array.<mixed>} y Class labels per data point
*/
}, {
key: "trainIteration",
value: function trainIteration(X, y) {
// Initialize the weights increment vector, which is used to increment the weights in each
// iteration after the calculations are done.
var weightsIncrement = Arrays.zeros(this.weights.length); // Initialize number of misclassified points
var numErrors = 0; // Shuffle data points
var _Arrays$shuffle = Arrays.shuffle(X, y),
_Arrays$shuffle2 = _slicedToArray(_Arrays$shuffle, 2),
XUse = _Arrays$shuffle2[0],
yUse = _Arrays$shuffle2[1]; // Loop over all datapoints
for (var i = 0; i < XUse.length; i += 1) {
// Transform binary class index to class sign (0 becomes -1 and 1 remains 1)
var classSign = this.getClassIndexSign(yUse[i]); // Copy features vector so it is not changed in the datapoint
var augmentedFeatures = XUse[i].slice(); // Add feature with value 1 at the beginning of the feature vector to correpond with the
// bias weight
augmentedFeatures.unshift(1); // Calculate output
var output = Arrays.dot(augmentedFeatures, this.weights); // Check whether the point was correctly classified
if (classSign * output <= 0) {
// Increase the number of errors
numErrors += 1; // Update the weights change to be used at the end of this epoch
weightsIncrement = Arrays.sum(weightsIncrement, Arrays.scale(augmentedFeatures, classSign));
}
} // Take average of all weight increments
this.weightsIncrement = Arrays.scale(weightsIncrement, 0.01 / XUse.length);
this.weights = Arrays.sum(this.weights, this.weightsIncrement);
return [numErrors, weightsIncrement];
}
/**
* Check whether training has convergence when using iterative training using trainIteration.
*
* @return {boolean} Whether the algorithm has converged
*/
}, {
key: "checkConvergence",
value: function checkConvergence() {
return Arrays.internalSum(Arrays.abs(this.weightsIncrement)) < 0.0001;
}
/**
* Make a prediction for a data set.
*
* @param {Array.Array.<number>} features - Features for each data point
* @param {Object} [optionsUser] User-defined options
* @param {string} [optionsUser.output = 'classLabels'] Output for predictions. Either
* "classLabels" (default, output predicted class label), "raw" (dot product of weights vector
* with augmented features vector) or "normalized" (dot product from "raw" but with unit-length
* weights)
* @return {Array.<number>} Predictions. Output dependent on options.output, defaults to class
* labels
*/
}, {
key: "predict",
value: function predict(features) {
var optionsUser = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
// Options
var optionsDefault = {
output: 'classLabels' // 'classLabels', 'normalized' or 'raw'
};
var options = _objectSpread({}, optionsDefault, {}, optionsUser); // Predictions
var predictions = []; // Normalization factor for normalized output
var weightsMagnitude = Math.sqrt(Arrays.dot(this.weights, this.weights)); // Loop over all datapoints
for (var i = 0; i < features.length; i += 1) {
// Copy features vector so it is not changed in the datapoint
var augmentedFeatures = features[i].slice(); // Add feature with value 1 at the beginning of the feature vector to correpond with the
// bias weight
augmentedFeatures.unshift(1); // Calculate output
var output = Arrays.dot(augmentedFeatures, this.weights); // Store prediction
if (options.output === 'raw') {// Raw output: do nothing
} else if (options.output === 'normalized') {
// Normalized output
output /= weightsMagnitude;
} else {
// Class label output
output = this.getSignClassIndex(output > 0 ? 1 : -1);
}
predictions.push(output);
}
return predictions;
}
}]);
return BinaryPerceptron;
}(_base.Classifier);
/**
* Perceptron learner for 2 or more classes. Uses 1-vs-all classification.
*/
exports.BinaryPerceptron = BinaryPerceptron;
var Perceptron =
/*#__PURE__*/
function (_OneVsAllClassifier) {
_inherits(Perceptron, _OneVsAllClassifier);
/**
* Constructor. Initialize class members and store user-defined options.
*
* @param {Object} [optionsUser] User-defined options
* @param {trackAccuracy} [optionsUser.trackAccuracy = false] Whether to track accuracy during the
* training process. This will let the perceptron keep track of the error rate on the test set
* in each training iteration
*/
function Perceptron() {
var _this;
var optionsUser = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, Perceptron);
_this = _possibleConstructorReturn(this, _getPrototypeOf(Perceptron).call(this)); // Parse options
var optionsDefault = {
// Whether the number of misclassified samples should be tracked at each iteration
trackAccuracy: false
};
var options = _objectSpread({}, optionsDefault, {}, optionsUser); // Set options
_this.trackAccuracy = options.trackAccuracy; // Accuracy tracking
if (_this.trackAccuracy) {
_this.addListener('iterationCompleted', function () {
return _this.calculateIntermediateAccuracy();
});
}
return _this;
}
/**
* @see {@link OneVsAll#createClassifier}
*/
_createClass(Perceptron, [{
key: "createClassifier",
value: function createClassifier(classIndex) {
return new BinaryPerceptron();
}
/**
* @see {@link Classifier#train}
*/
}, {
key: "train",
value: function train(X, y) {
this.createClassifiers(y);
if (this.trackAccuracy) {
this.numErrors = [];
this.trainIterative();
} else {
this.trainBatch(X, y);
}
}
/**
* Callback for calculating the accuracy of the classifier on the training set in intermediate
* steps of training
*/
}, {
key: "calculateIntermediateAccuracy",
value: function calculateIntermediateAccuracy() {
var _this2 = this;
// Track number of errors
var predictions = this.predict(this.training.features);
this.numErrors.push(predictions.reduce(function (r, x, i) {
return r + (x !== _this2.training.labels[i]);
}, 0));
}
}]);
return Perceptron;
}(_base.OneVsAllClassifier);
exports["default"] = Perceptron;