This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.cpp | |
// mlperceptron | |
// | |
// Created by Sergei Bugrov on 7/1/17. | |
// Copyright © 2017 Sergei Bugrov. All rights reserved. | |
// | |
#include <iostream> | |
#include <vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main(int argc, const char * argv[]) { | |
for (unsigned i = 0; i != 50; ++i) { | |
vector<float> pred = sigmoid(dot(X, W, 4, 4, 1 ) ); | |
vector<float> pred_error = y - pred; | |
vector<float> pred_delta = pred_error * sigmoid_d(pred); | |
vector<float> W_delta = dot(transpose( &X[0], 4, 4 ), pred_delta, 4, 4, 1); | |
W = W + W_delta; | |
}; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
X W pred y | |
5.1 3.5 1.4 0.2 0.5 0.00 0 | |
4.9 3.0 1.4 0.2 0.5 0.00 0 | |
6.2 3.4 5.4 2.3 0.5 0.99 1 | |
5.9 3.0 5.1 1.8 0.5 0.99 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vector pred = sigmoid(dot(X, W, 4, 4, 1 ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vector <float> dot (const vector <float>& m1, const vector <float>& m2, | |
const int m1_rows, const int m1_columns, const int m2_columns) { | |
/* Returns the product of two matrices: m1 x m2. | |
Inputs: | |
m1: vector, left matrix of size m1_rows x m1_columns | |
m2: vector, right matrix of size m1_columns x m2_columns | |
(the number of rows in the right matrix must be equal | |
to the number of the columns in the left one) | |
m1_rows: int, number of rows in the left matrix m1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vector <float> sigmoid (const vector <float>& m1) { | |
/* Returns the value of the sigmoid function f(x) = 1/(1 + e^-x). | |
Input: m1, a vector. | |
Output: 1/(1 + e^-x) for every element of the input matrix m1. | |
*/ | |
const unsigned long VECTOR_SIZE = m1.size(); | |
vector <float> output (VECTOR_SIZE); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vector<float> pred_error = y - pred; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vector<float> pred_delta = pred_error * sigmoid_d(pred); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vector W_delta = dot(transpose( &X[0], 4, 4 ), pred_delta, 4, 4, 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
W = W + W_delta; |
OlderNewer