nlptoolkit-classification 1.0.8 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Attribute/Attribute.js +6 -16
- package/dist/Attribute/Attribute.js.map +1 -1
- package/dist/Attribute/AttributeType.js +22 -32
- package/dist/Attribute/AttributeType.js.map +1 -1
- package/dist/Attribute/BinaryAttribute.js +14 -24
- package/dist/Attribute/BinaryAttribute.js.map +1 -1
- package/dist/Attribute/ContinuousAttribute.js +46 -55
- package/dist/Attribute/ContinuousAttribute.js.map +1 -1
- package/dist/Attribute/DiscreteAttribute.js +38 -48
- package/dist/Attribute/DiscreteAttribute.js.map +1 -1
- package/dist/Attribute/DiscreteIndexedAttribute.js +47 -55
- package/dist/Attribute/DiscreteIndexedAttribute.js.map +1 -1
- package/dist/DataSet/DataDefinition.js +115 -123
- package/dist/DataSet/DataDefinition.js.map +1 -1
- package/dist/DataSet/DataSet.js +268 -245
- package/dist/DataSet/DataSet.js.map +1 -1
- package/dist/DistanceMetric/DistanceMetric.js +2 -12
- package/dist/DistanceMetric/EuclidianDistance.js +27 -37
- package/dist/DistanceMetric/EuclidianDistance.js.map +1 -1
- package/dist/DistanceMetric/MahalanobisDistance.js +27 -36
- package/dist/DistanceMetric/MahalanobisDistance.js.map +1 -1
- package/dist/Experiment/BootstrapRun.js +31 -40
- package/dist/Experiment/BootstrapRun.js.map +1 -1
- package/dist/Experiment/Experiment.js +46 -53
- package/dist/Experiment/Experiment.js.map +1 -1
- package/dist/Experiment/KFoldRun.js +45 -54
- package/dist/Experiment/KFoldRun.js.map +1 -1
- package/dist/Experiment/KFoldRunSeparateTest.js +48 -58
- package/dist/Experiment/KFoldRunSeparateTest.js.map +1 -1
- package/dist/Experiment/MultipleRun.js +2 -12
- package/dist/Experiment/MxKFoldRun.js +31 -40
- package/dist/Experiment/MxKFoldRun.js.map +1 -1
- package/dist/Experiment/MxKFoldRunSeparateTest.js +35 -44
- package/dist/Experiment/MxKFoldRunSeparateTest.js.map +1 -1
- package/dist/Experiment/SingleRun.js +2 -12
- package/dist/Experiment/SingleRunWithK.js +39 -48
- package/dist/Experiment/SingleRunWithK.js.map +1 -1
- package/dist/Experiment/StratifiedKFoldRun.js +26 -36
- package/dist/Experiment/StratifiedKFoldRun.js.map +1 -1
- package/dist/Experiment/StratifiedKFoldRunSeparateTest.js +30 -40
- package/dist/Experiment/StratifiedKFoldRunSeparateTest.js.map +1 -1
- package/dist/Experiment/StratifiedMxKFoldRun.js +29 -39
- package/dist/Experiment/StratifiedMxKFoldRun.js.map +1 -1
- package/dist/Experiment/StratifiedMxKFoldRunSeparateTest.js +34 -43
- package/dist/Experiment/StratifiedMxKFoldRunSeparateTest.js.map +1 -1
- package/dist/Experiment/StratifiedSingleRunWithK.js +27 -36
- package/dist/Experiment/StratifiedSingleRunWithK.js.map +1 -1
- package/dist/FeatureSelection/BackwardSelection.js +26 -36
- package/dist/FeatureSelection/BackwardSelection.js.map +1 -1
- package/dist/FeatureSelection/FeatureSubSet.js +72 -82
- package/dist/FeatureSelection/FeatureSubSet.js.map +1 -1
- package/dist/FeatureSelection/FloatingSelection.js +25 -35
- package/dist/FeatureSelection/FloatingSelection.js.map +1 -1
- package/dist/FeatureSelection/ForwardSelection.js +25 -35
- package/dist/FeatureSelection/ForwardSelection.js.map +1 -1
- package/dist/FeatureSelection/SubSetSelection.js +69 -78
- package/dist/FeatureSelection/SubSetSelection.js.map +1 -1
- package/dist/Filter/DiscreteToContinuous.js +50 -60
- package/dist/Filter/DiscreteToContinuous.js.map +1 -1
- package/dist/Filter/DiscreteToIndexed.js +40 -50
- package/dist/Filter/DiscreteToIndexed.js.map +1 -1
- package/dist/Filter/FeatureFilter.js +23 -32
- package/dist/Filter/FeatureFilter.js.map +1 -1
- package/dist/Filter/LaryFilter.js +46 -55
- package/dist/Filter/LaryFilter.js.map +1 -1
- package/dist/Filter/LaryToBinary.js +48 -58
- package/dist/Filter/LaryToBinary.js.map +1 -1
- package/dist/Filter/Normalize.js +33 -41
- package/dist/Filter/Normalize.js.map +1 -1
- package/dist/Filter/Pca.js +86 -96
- package/dist/Filter/Pca.js.map +1 -1
- package/dist/Filter/TrainedFeatureFilter.js +14 -24
- package/dist/Filter/TrainedFeatureFilter.js.map +1 -1
- package/dist/Instance/CompositeInstance.js +46 -56
- package/dist/Instance/CompositeInstance.js.map +1 -1
- package/dist/Instance/Instance.js +145 -154
- package/dist/Instance/Instance.js.map +1 -1
- package/dist/InstanceList/InstanceList.js +466 -443
- package/dist/InstanceList/InstanceList.js.map +1 -1
- package/dist/InstanceList/InstanceListOfSameClass.js +23 -32
- package/dist/InstanceList/InstanceListOfSameClass.js.map +1 -1
- package/dist/InstanceList/Partition.js +167 -177
- package/dist/InstanceList/Partition.js.map +1 -1
- package/dist/Model/DecisionTree/DecisionCondition.js +71 -80
- package/dist/Model/DecisionTree/DecisionCondition.js.map +1 -1
- package/dist/Model/DecisionTree/DecisionNode.js +302 -311
- package/dist/Model/DecisionTree/DecisionNode.js.map +1 -1
- package/dist/Model/DecisionTree/DecisionStump.js +22 -32
- package/dist/Model/DecisionTree/DecisionStump.js.map +1 -1
- package/dist/Model/DecisionTree/DecisionTree.js +89 -98
- package/dist/Model/DecisionTree/DecisionTree.js.map +1 -1
- package/dist/Model/DummyModel.js +64 -73
- package/dist/Model/DummyModel.js.map +1 -1
- package/dist/Model/Ensemble/BaggingModel.js +34 -44
- package/dist/Model/Ensemble/BaggingModel.js.map +1 -1
- package/dist/Model/Ensemble/RandomForestModel.js +31 -41
- package/dist/Model/Ensemble/RandomForestModel.js.map +1 -1
- package/dist/Model/Ensemble/TreeEnsembleModel.js +55 -64
- package/dist/Model/Ensemble/TreeEnsembleModel.js.map +1 -1
- package/dist/Model/Model.js +130 -140
- package/dist/Model/Model.js.map +1 -1
- package/dist/Model/NeuralNetwork/DeepNetworkModel.js +162 -169
- package/dist/Model/NeuralNetwork/DeepNetworkModel.js.map +1 -1
- package/dist/Model/NeuralNetwork/LinearPerceptronModel.js +69 -78
- package/dist/Model/NeuralNetwork/LinearPerceptronModel.js.map +1 -1
- package/dist/Model/NeuralNetwork/MultiLayerPerceptronModel.js +112 -120
- package/dist/Model/NeuralNetwork/MultiLayerPerceptronModel.js.map +1 -1
- package/dist/Model/NeuralNetwork/NeuralNetworkModel.js +197 -201
- package/dist/Model/NeuralNetwork/NeuralNetworkModel.js.map +1 -1
- package/dist/Model/NonParametric/KnnInstance.js +21 -29
- package/dist/Model/NonParametric/KnnInstance.js.map +1 -1
- package/dist/Model/NonParametric/KnnModel.js +101 -108
- package/dist/Model/NonParametric/KnnModel.js.map +1 -1
- package/dist/Model/Parametric/GaussianModel.js +82 -91
- package/dist/Model/Parametric/GaussianModel.js.map +1 -1
- package/dist/Model/Parametric/KMeansModel.js +59 -67
- package/dist/Model/Parametric/KMeansModel.js.map +1 -1
- package/dist/Model/Parametric/LdaModel.js +83 -91
- package/dist/Model/Parametric/LdaModel.js.map +1 -1
- package/dist/Model/Parametric/NaiveBayesModel.js +119 -132
- package/dist/Model/Parametric/NaiveBayesModel.js.map +1 -1
- package/dist/Model/Parametric/QdaModel.js +70 -79
- package/dist/Model/Parametric/QdaModel.js.map +1 -1
- package/dist/Model/RandomModel.js +85 -92
- package/dist/Model/RandomModel.js.map +1 -1
- package/dist/Model/ValidatedModel.js +21 -31
- package/dist/Model/ValidatedModel.js.map +1 -1
- package/dist/Parameter/ActivationFunction.js +9 -19
- package/dist/Parameter/ActivationFunction.js.map +1 -1
- package/dist/Parameter/BaggingParameter.js +24 -33
- package/dist/Parameter/BaggingParameter.js.map +1 -1
- package/dist/Parameter/C45Parameter.js +34 -42
- package/dist/Parameter/C45Parameter.js.map +1 -1
- package/dist/Parameter/DeepNetworkParameter.js +48 -56
- package/dist/Parameter/DeepNetworkParameter.js.map +1 -1
- package/dist/Parameter/KMeansParameter.js +29 -38
- package/dist/Parameter/KMeansParameter.js.map +1 -1
- package/dist/Parameter/KnnParameter.js +25 -34
- package/dist/Parameter/KnnParameter.js.map +1 -1
- package/dist/Parameter/LinearPerceptronParameter.js +56 -62
- package/dist/Parameter/LinearPerceptronParameter.js.map +1 -1
- package/dist/Parameter/MultiLayerPerceptronParameter.js +38 -46
- package/dist/Parameter/MultiLayerPerceptronParameter.js.map +1 -1
- package/dist/Parameter/Parameter.js +21 -30
- package/dist/Parameter/Parameter.js.map +1 -1
- package/dist/Parameter/RandomForestParameter.js +25 -34
- package/dist/Parameter/RandomForestParameter.js.map +1 -1
- package/dist/Performance/ClassificationPerformance.js +24 -33
- package/dist/Performance/ClassificationPerformance.js.map +1 -1
- package/dist/Performance/ConfusionMatrix.js +149 -158
- package/dist/Performance/ConfusionMatrix.js.map +1 -1
- package/dist/Performance/DetailedClassificationPerformance.js +23 -32
- package/dist/Performance/DetailedClassificationPerformance.js.map +1 -1
- package/dist/Performance/ExperimentPerformance.js +184 -161
- package/dist/Performance/ExperimentPerformance.js.map +1 -1
- package/dist/Performance/Performance.js +21 -30
- package/dist/Performance/Performance.js.map +1 -1
- package/dist/StatisticalTest/Combined5x2F.js +41 -51
- package/dist/StatisticalTest/Combined5x2F.js.map +1 -1
- package/dist/StatisticalTest/Combined5x2t.js +42 -52
- package/dist/StatisticalTest/Combined5x2t.js.map +1 -1
- package/dist/StatisticalTest/Paired5x2t.js +40 -50
- package/dist/StatisticalTest/Paired5x2t.js.map +1 -1
- package/dist/StatisticalTest/PairedTest.js +31 -41
- package/dist/StatisticalTest/PairedTest.js.map +1 -1
- package/dist/StatisticalTest/Pairedt.js +38 -48
- package/dist/StatisticalTest/Pairedt.js.map +1 -1
- package/dist/StatisticalTest/Sign.js +50 -60
- package/dist/StatisticalTest/Sign.js.map +1 -1
- package/dist/StatisticalTest/StatisticalTestResult.js +65 -73
- package/dist/StatisticalTest/StatisticalTestResult.js.map +1 -1
- package/dist/StatisticalTest/StatisticalTestResultType.js +11 -21
- package/dist/StatisticalTest/StatisticalTestResultType.js.map +1 -1
- package/dist/index.js +95 -101
- package/dist/index.js.map +1 -1
- package/package.json +7 -6
- package/tsconfig.json +4 -3
- package/source/tsconfig.json +0 -13
|
@@ -1,215 +1,211 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NeuralNetworkModel = void 0;
|
|
4
|
+
const ValidatedModel_1 = require("../ValidatedModel");
|
|
5
|
+
const Vector_1 = require("nlptoolkit-math/dist/Vector");
|
|
6
|
+
const Matrix_1 = require("nlptoolkit-math/dist/Matrix");
|
|
7
|
+
const ActivationFunction_1 = require("../../Parameter/ActivationFunction");
|
|
8
|
+
const CompositeInstance_1 = require("../../Instance/CompositeInstance");
|
|
9
|
+
class NeuralNetworkModel extends ValidatedModel_1.ValidatedModel {
|
|
10
|
+
classLabels;
|
|
11
|
+
K;
|
|
12
|
+
d;
|
|
13
|
+
x;
|
|
14
|
+
y;
|
|
15
|
+
r;
|
|
16
|
+
/**
|
|
17
|
+
* Constructor that sets the class labels, their sizes as K and the size of the continuous attributes as d.
|
|
18
|
+
*
|
|
19
|
+
* @param trainSet {@link InstanceList} to use as train set.
|
|
20
|
+
*/
|
|
21
|
+
initialize(trainSet) {
|
|
22
|
+
if (trainSet != undefined) {
|
|
23
|
+
this.classLabels = trainSet.getDistinctClassLabels();
|
|
24
|
+
this.K = this.classLabels.length;
|
|
25
|
+
this.d = trainSet.get(0).continuousAttributeSize();
|
|
26
|
+
}
|
|
5
27
|
}
|
|
6
|
-
|
|
7
|
-
|
|
28
|
+
/**
|
|
29
|
+
* The allocateLayerWeights method returns a new {@link Matrix} with random weights.
|
|
30
|
+
*
|
|
31
|
+
* @param row Number of rows.
|
|
32
|
+
* @param column Number of columns.
|
|
33
|
+
* @param random Random function to set weights.
|
|
34
|
+
* @return Matrix with random weights.
|
|
35
|
+
*/
|
|
36
|
+
allocateLayerWeights(row, column, random) {
|
|
37
|
+
return new Matrix_1.Matrix(row, column, -0.01, +0.01, random);
|
|
8
38
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
*
|
|
22
|
-
* @param trainSet {@link InstanceList} to use as train set.
|
|
23
|
-
*/
|
|
24
|
-
initialize(trainSet) {
|
|
25
|
-
if (trainSet != undefined) {
|
|
26
|
-
this.classLabels = trainSet.getDistinctClassLabels();
|
|
27
|
-
this.K = this.classLabels.length;
|
|
28
|
-
this.d = trainSet.get(0).continuousAttributeSize();
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* The allocateLayerWeights method returns a new {@link Matrix} with random weights.
|
|
33
|
-
*
|
|
34
|
-
* @param row Number of rows.
|
|
35
|
-
* @param column Number of columns.
|
|
36
|
-
* @param random Random function to set weights.
|
|
37
|
-
* @return Matrix with random weights.
|
|
38
|
-
*/
|
|
39
|
-
allocateLayerWeights(row, column, random) {
|
|
40
|
-
return new Matrix_1.Matrix(row, column, -0.01, +0.01, random);
|
|
39
|
+
/**
|
|
40
|
+
* The normalizeOutput method takes an input {@link Vector} o, gets the result for e^o of each element of o,
|
|
41
|
+
* then sums them up. At the end, divides each e^o by the summation.
|
|
42
|
+
*
|
|
43
|
+
* @param o Vector to normalize.
|
|
44
|
+
* @return Normalized vector.
|
|
45
|
+
*/
|
|
46
|
+
normalizeOutput(o) {
|
|
47
|
+
let sum = 0.0;
|
|
48
|
+
let values = new Array();
|
|
49
|
+
for (let i = 0; i < o.size(); i++) {
|
|
50
|
+
sum += Math.exp(o.getValue(i));
|
|
41
51
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
* then sums them up. At the end, divides each e^o by the summation.
|
|
45
|
-
*
|
|
46
|
-
* @param o Vector to normalize.
|
|
47
|
-
* @return Normalized vector.
|
|
48
|
-
*/
|
|
49
|
-
normalizeOutput(o) {
|
|
50
|
-
let sum = 0.0;
|
|
51
|
-
let values = new Array();
|
|
52
|
-
for (let i = 0; i < o.size(); i++) {
|
|
53
|
-
sum += Math.exp(o.getValue(i));
|
|
54
|
-
}
|
|
55
|
-
for (let i = 0; i < o.size(); i++) {
|
|
56
|
-
values.push(Math.exp(o.getValue(i)) / sum);
|
|
57
|
-
}
|
|
58
|
-
return new Vector_1.Vector(values);
|
|
52
|
+
for (let i = 0; i < o.size(); i++) {
|
|
53
|
+
values.push(Math.exp(o.getValue(i)) / sum);
|
|
59
54
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
55
|
+
return new Vector_1.Vector(values);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The createInputVector method takes an {@link Instance} as an input. It converts given Instance to the {@link Vector}
|
|
59
|
+
* and insert 1.0 to the first element.
|
|
60
|
+
*
|
|
61
|
+
* @param instance Instance to insert 1.0.
|
|
62
|
+
*/
|
|
63
|
+
createInputVector(instance) {
|
|
64
|
+
this.x = instance.toVector();
|
|
65
|
+
this.x.insert(0, 1.0);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The calculateHidden method takes a {@link Vector} input and {@link Matrix} weights, It multiplies the weights
|
|
69
|
+
* Matrix with given input Vector than applies the sigmoid function and returns the result.
|
|
70
|
+
*
|
|
71
|
+
* @param input Vector to multiply weights.
|
|
72
|
+
* @param weights Matrix is multiplied with input Vector.
|
|
73
|
+
* @param activationFunction Activation function.
|
|
74
|
+
* @return Result of sigmoid function.
|
|
75
|
+
*/
|
|
76
|
+
calculateHidden(input, weights, activationFunction) {
|
|
77
|
+
let z = weights.multiplyWithVectorFromRight(input);
|
|
78
|
+
switch (activationFunction) {
|
|
79
|
+
case ActivationFunction_1.ActivationFunction.SIGMOID:
|
|
80
|
+
default:
|
|
81
|
+
z.sigmoid();
|
|
82
|
+
break;
|
|
83
|
+
case ActivationFunction_1.ActivationFunction.TANH:
|
|
84
|
+
z.tanh();
|
|
85
|
+
break;
|
|
86
|
+
case ActivationFunction_1.ActivationFunction.RELU:
|
|
87
|
+
z.relu();
|
|
88
|
+
break;
|
|
69
89
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
90
|
+
return z;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The calculateOneMinusHidden method takes a {@link Vector} as input. It creates a Vector of ones and
|
|
94
|
+
* returns the difference between given Vector.
|
|
95
|
+
*
|
|
96
|
+
* @param hidden Vector to find difference.
|
|
97
|
+
* @return Returns the difference between one's Vector and input Vector.
|
|
98
|
+
*/
|
|
99
|
+
calculateOneMinusHidden(hidden) {
|
|
100
|
+
let one = new Vector_1.Vector(hidden.size(), 1.0);
|
|
101
|
+
return one.difference(hidden);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The calculateForwardSingleHiddenLayer method takes two matrices W and V. First it multiplies W with x, then
|
|
105
|
+
* multiplies V with the result of the previous multiplication.
|
|
106
|
+
*
|
|
107
|
+
* @param W Matrix to multiply with x.
|
|
108
|
+
* @param V Matrix to multiply.
|
|
109
|
+
* @param activationFunction Activation function.
|
|
110
|
+
*/
|
|
111
|
+
calculateForwardSingleHiddenLayer(W, V, activationFunction) {
|
|
112
|
+
let hidden = this.calculateHidden(this.x, W, activationFunction);
|
|
113
|
+
let hiddenBiased = hidden.biased();
|
|
114
|
+
this.y = V.multiplyWithVectorFromRight(hiddenBiased);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The calculateRMinusY method creates a new {@link Vector} with given Instance, then it multiplies given
|
|
118
|
+
* input Vector with given weights Matrix. After normalizing the output, it returns the difference between the newly created
|
|
119
|
+
* Vector and normalized output.
|
|
120
|
+
*
|
|
121
|
+
* @param instance Instance is used to get class labels.
|
|
122
|
+
* @param input Vector to multiply weights.
|
|
123
|
+
* @param weights Matrix of weights/
|
|
124
|
+
* @return Difference between newly created Vector and normalized output.
|
|
125
|
+
*/
|
|
126
|
+
calculateRMinusY(instance, input, weights) {
|
|
127
|
+
this.r = new Vector_1.Vector(this.K, 1.0, this.classLabels.indexOf(instance.getClassLabel()));
|
|
128
|
+
let o = weights.multiplyWithVectorFromRight(input);
|
|
129
|
+
this.y = this.normalizeOutput(o);
|
|
130
|
+
return this.r.difference(this.y);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The predictWithCompositeInstance method takes an ArrayList possibleClassLabels. It returns the class label
|
|
134
|
+
* which has the maximum value of y.
|
|
135
|
+
*
|
|
136
|
+
* @param possibleClassLabels ArrayList that has the class labels.
|
|
137
|
+
* @return The class label which has the maximum value of y.
|
|
138
|
+
*/
|
|
139
|
+
predictWithCompositeInstance(possibleClassLabels) {
|
|
140
|
+
let predictedClass = possibleClassLabels[0];
|
|
141
|
+
let maxY = Number.NEGATIVE_INFINITY;
|
|
142
|
+
for (let i = 0; i < this.classLabels.length; i++) {
|
|
143
|
+
if (possibleClassLabels.includes(this.classLabels[i]) && this.y.getValue(i) > maxY) {
|
|
144
|
+
maxY = this.y.getValue(i);
|
|
145
|
+
predictedClass = this.classLabels[i];
|
|
92
146
|
}
|
|
93
|
-
return z;
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* The calculateOneMinusHidden method takes a {@link Vector} as input. It creates a Vector of ones and
|
|
97
|
-
* returns the difference between given Vector.
|
|
98
|
-
*
|
|
99
|
-
* @param hidden Vector to find difference.
|
|
100
|
-
* @return Returns the difference between one's Vector and input Vector.
|
|
101
|
-
*/
|
|
102
|
-
calculateOneMinusHidden(hidden) {
|
|
103
|
-
let one = new Vector_1.Vector(hidden.size(), 1.0);
|
|
104
|
-
return one.difference(hidden);
|
|
105
147
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
* The calculateRMinusY method creates a new {@link Vector} with given Instance, then it multiplies given
|
|
121
|
-
* input Vector with given weights Matrix. After normalizing the output, it returns the difference between the newly created
|
|
122
|
-
* Vector and normalized output.
|
|
123
|
-
*
|
|
124
|
-
* @param instance Instance is used to get class labels.
|
|
125
|
-
* @param input Vector to multiply weights.
|
|
126
|
-
* @param weights Matrix of weights/
|
|
127
|
-
* @return Difference between newly created Vector and normalized output.
|
|
128
|
-
*/
|
|
129
|
-
calculateRMinusY(instance, input, weights) {
|
|
130
|
-
this.r = new Vector_1.Vector(this.K, 1.0, this.classLabels.indexOf(instance.getClassLabel()));
|
|
131
|
-
let o = weights.multiplyWithVectorFromRight(input);
|
|
132
|
-
this.y = this.normalizeOutput(o);
|
|
133
|
-
return this.r.difference(this.y);
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* The predictWithCompositeInstance method takes an ArrayList possibleClassLabels. It returns the class label
|
|
137
|
-
* which has the maximum value of y.
|
|
138
|
-
*
|
|
139
|
-
* @param possibleClassLabels ArrayList that has the class labels.
|
|
140
|
-
* @return The class label which has the maximum value of y.
|
|
141
|
-
*/
|
|
142
|
-
predictWithCompositeInstance(possibleClassLabels) {
|
|
143
|
-
let predictedClass = possibleClassLabels[0];
|
|
144
|
-
let maxY = Number.NEGATIVE_INFINITY;
|
|
145
|
-
for (let i = 0; i < this.classLabels.length; i++) {
|
|
146
|
-
if (possibleClassLabels.includes(this.classLabels[i]) && this.y.getValue(i) > maxY) {
|
|
147
|
-
maxY = this.y.getValue(i);
|
|
148
|
-
predictedClass = this.classLabels[i];
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
return predictedClass;
|
|
148
|
+
return predictedClass;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* The predict method takes an {@link Instance} as an input, converts it to a Vector and calculates the {@link Matrix} y by
|
|
152
|
+
* multiplying Matrix W with {@link Vector} x. Then it returns the class label which has the maximum y value.
|
|
153
|
+
*
|
|
154
|
+
* @param instance Instance to predict.
|
|
155
|
+
* @return The class label which has the maximum y.
|
|
156
|
+
*/
|
|
157
|
+
predict(instance) {
|
|
158
|
+
this.createInputVector(instance);
|
|
159
|
+
this.calculateOutput();
|
|
160
|
+
if (instance instanceof CompositeInstance_1.CompositeInstance) {
|
|
161
|
+
return this.predictWithCompositeInstance(instance.getPossibleClassLabels());
|
|
152
162
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
* multiplying Matrix W with {@link Vector} x. Then it returns the class label which has the maximum y value.
|
|
156
|
-
*
|
|
157
|
-
* @param instance Instance to predict.
|
|
158
|
-
* @return The class label which has the maximum y.
|
|
159
|
-
*/
|
|
160
|
-
predict(instance) {
|
|
161
|
-
this.createInputVector(instance);
|
|
162
|
-
this.calculateOutput();
|
|
163
|
-
if (instance instanceof CompositeInstance_1.CompositeInstance) {
|
|
164
|
-
return this.predictWithCompositeInstance(instance.getPossibleClassLabels());
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
return this.classLabels[this.y.maxIndex()];
|
|
168
|
-
}
|
|
163
|
+
else {
|
|
164
|
+
return this.classLabels[this.y.maxIndex()];
|
|
169
165
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
return result;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Calculates the posterior probability distribution for the given instance according to neural network model.
|
|
169
|
+
* @param instance Instance for which posterior probability distribution is calculated.
|
|
170
|
+
* @return Posterior probability distribution for the given instance.
|
|
171
|
+
*/
|
|
172
|
+
predictProbability(instance) {
|
|
173
|
+
this.createInputVector(instance);
|
|
174
|
+
this.calculateOutput();
|
|
175
|
+
let result = new Map();
|
|
176
|
+
for (let i = 0; i < this.classLabels.length; i++) {
|
|
177
|
+
result.set(this.classLabels[i], this.y.getValue(i));
|
|
183
178
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
179
|
+
return result;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Loads the class labels from input model file.
|
|
183
|
+
* @param input Input model file.
|
|
184
|
+
*/
|
|
185
|
+
loadClassLabels(input) {
|
|
186
|
+
let items = input.readLine().split(" ");
|
|
187
|
+
this.K = parseInt(items[0]);
|
|
188
|
+
this.d = parseInt(items[1]);
|
|
189
|
+
this.classLabels = new Array();
|
|
190
|
+
for (let i = 0; i < this.K; i++) {
|
|
191
|
+
this.classLabels.push(input.readLine());
|
|
196
192
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Loads the activation function from an input model file.
|
|
196
|
+
* @param input Input model file.
|
|
197
|
+
* @return Activation function read.
|
|
198
|
+
*/
|
|
199
|
+
loadActivationFunction(input) {
|
|
200
|
+
switch (input.readLine()) {
|
|
201
|
+
case "TANH":
|
|
202
|
+
return ActivationFunction_1.ActivationFunction.TANH;
|
|
203
|
+
case "RELU":
|
|
204
|
+
return ActivationFunction_1.ActivationFunction.RELU;
|
|
205
|
+
default:
|
|
206
|
+
return ActivationFunction_1.ActivationFunction.SIGMOID;
|
|
211
207
|
}
|
|
212
208
|
}
|
|
213
|
-
|
|
214
|
-
|
|
209
|
+
}
|
|
210
|
+
exports.NeuralNetworkModel = NeuralNetworkModel;
|
|
215
211
|
//# sourceMappingURL=NeuralNetworkModel.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NeuralNetworkModel.js","sourceRoot":"","sources":["../../../source/Model/NeuralNetwork/NeuralNetworkModel.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"NeuralNetworkModel.js","sourceRoot":"","sources":["../../../source/Model/NeuralNetwork/NeuralNetworkModel.ts"],"names":[],"mappings":";;;AAAA,sDAAiD;AACjD,wDAAmD;AAEnD,wDAAmD;AAEnD,2EAAsE;AACtE,wEAAmE;AAInE,MAAsB,kBAAmB,SAAQ,+BAAc;IAEjD,WAAW,CAAe;IAC1B,CAAC,CAAQ;IACT,CAAC,CAAQ;IACT,CAAC,CAAQ;IACT,CAAC,CAAQ;IACT,CAAC,CAAQ;IAInB;;;;OAIG;IACO,UAAU,CAAC,QAAsB;QACvC,IAAI,QAAQ,IAAI,SAAS,EAAC,CAAC;YACvB,IAAI,CAAC,WAAW,GAAG,QAAQ,CAAC,sBAAsB,EAAE,CAAA;YACpD,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,WAAW,CAAC,MAAM,CAAA;YAChC,IAAI,CAAC,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,uBAAuB,EAAE,CAAA;QACtD,CAAC;IACL,CAAC;IAED;;;;;;;OAOG;IACO,oBAAoB,CAAC,GAAW,EAAE,MAAc,EAAE,MAAc;QACtE,OAAO,IAAI,eAAM,CAAC,GAAG,EAAE,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACzD,C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,CAAC,CAAC,CAAC,CAAC,CAAC;QACxD,CAAC;QACD,OAAO,MAAM,CAAC;IAClB,CAAC;IAED;;;OAGG;IACH,eAAe,CAAC,KAAmB;QAC/B,IAAI,KAAK,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QACvC,IAAI,CAAC,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;QAC3B,IAAI,CAAC,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;QAC3B,IAAI,CAAC,WAAW,GAAG,IAAI,KAAK,EAAU,CAAA;QACtC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,EAAC,CAAC;YAC7B,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAA;QAC3C,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,sBAAsB,CAAC,KAAmB;QACtC,QAAQ,KAAK,CAAC,QAAQ,EAAE,EAAC,CAAC;YACtB,KAAK,MAAM;gBACP,OAAO,uCAAkB,CAAC,IAAI,CAAA;YAClC,KAAK,MAAM;gBACP,OAAO,uCAAkB,CAAC,IAAI,CAAA;YAClC;gBACI,OAAO,uCAAkB,CAAC,OAAO,CAAA;QACzC,CAAC;IACL,CAAC;CACJ;AAvND,gDAuNC"}
|
|
@@ -1,33 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KnnInstance = void 0;
|
|
4
|
+
class KnnInstance {
|
|
5
|
+
distance;
|
|
6
|
+
instance;
|
|
7
|
+
/**
|
|
8
|
+
* The constructor that sets the instance and distance value.
|
|
9
|
+
*
|
|
10
|
+
* @param instance {@link Instance} input.
|
|
11
|
+
* @param distance Double distance value.
|
|
12
|
+
*/
|
|
13
|
+
constructor(instance, distance) {
|
|
14
|
+
this.instance = instance;
|
|
15
|
+
this.distance = distance;
|
|
5
16
|
}
|
|
6
|
-
|
|
7
|
-
|
|
17
|
+
getInstance() {
|
|
18
|
+
return this.instance;
|
|
8
19
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.KnnInstance = void 0;
|
|
13
|
-
class KnnInstance {
|
|
14
|
-
/**
|
|
15
|
-
* The constructor that sets the instance and distance value.
|
|
16
|
-
*
|
|
17
|
-
* @param instance {@link Instance} input.
|
|
18
|
-
* @param distance Double distance value.
|
|
19
|
-
*/
|
|
20
|
-
constructor(instance, distance) {
|
|
21
|
-
this.instance = instance;
|
|
22
|
-
this.distance = distance;
|
|
23
|
-
}
|
|
24
|
-
getInstance() {
|
|
25
|
-
return this.instance;
|
|
26
|
-
}
|
|
27
|
-
getDistance() {
|
|
28
|
-
return this.distance;
|
|
29
|
-
}
|
|
20
|
+
getDistance() {
|
|
21
|
+
return this.distance;
|
|
30
22
|
}
|
|
31
|
-
|
|
32
|
-
|
|
23
|
+
}
|
|
24
|
+
exports.KnnInstance = KnnInstance;
|
|
33
25
|
//# sourceMappingURL=KnnInstance.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"KnnInstance.js","sourceRoot":"","sources":["../../../source/Model/NonParametric/KnnInstance.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"KnnInstance.js","sourceRoot":"","sources":["../../../source/Model/NonParametric/KnnInstance.ts"],"names":[],"mappings":";;;AAEA,MAAa,WAAW;IAEH,QAAQ,CAAQ;IAChB,QAAQ,CAAU;IAEnC;;;;;OAKG;IACH,YAAY,QAAkB,EAAE,QAAgB;QAC5C,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC7B,CAAC;IAED,WAAW;QACP,OAAO,IAAI,CAAC,QAAQ,CAAA;IACxB,CAAC;IAED,WAAW;QACP,OAAO,IAAI,CAAC,QAAQ,CAAA;IACxB,CAAC;CAEJ;AAxBD,kCAwBC"}
|