Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Predicts passed attributes using the sigmoid function and returns its chance to fit to the certain label.
- * @param data
- * @return
- */
- float predict(MatrixXf &data){ // Data is basically a vector here, just rows no columns
- // LogisticRegression network graph like calculation
- auto sum = 0.0f;
- for (auto index = 0; index < data.rows(); index++) {
- auto weight = weights(index); // Weights is also a vector
- auto attribute = data(index);
- auto result = weight * attribute;
- sum += result;
- }
- // Sigmoid
- return 1.0f / (1.0f + exp(-sum));
- }
- /**
- * Learns by a given set of dataSet and trains the perceptron.
- * @param dataSet
- */
- void learn(int iterations, MatrixXf &dataSet, MatrixXf &labelSet){ // Data is
- auto difference = MatrixXf(labelSet.rows(), 1); // The vector being used as ( a - y ) ^ T from the formula
- for (auto index = 0; index < iterations; ++index) {
- // Update differences... basically ( prediction - label ) -> ( a - y )^T from the formula
- for (auto col = 0; col < dataSet.cols(); ++col) {
- auto data = dataSet.col(col); // The first data vector
- auto predicted = predict(data.nestedExpression()); // Predicted value
- difference(col) = predicted - labelSet(col); // Insert into ( a - y ) ^ T vector
- }
- auto gradient = ( 1.0f/(float)dataSet.cols() * ( dataSet * difference)); // 1/m * X * ( A - Y ) ^ T
- weights -= step * gradient; // Update gradients
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement