Skip to content

Instantly share code, notes, and snippets.

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
vector pred = sigmoid(dot(X, W, 4, 4, 1 ) );
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
vector<float> pred_error = y - pred;
vector<float> pred_delta = pred_error * sigmoid_d(pred);
vector W_delta = dot(transpose( &X[0], 4, 4 ), pred_delta, 4, 4, 1);
W = W + W_delta;
vector<float> X {
5.1, 3.5, 1.4, 0.2,
4.9, 3.0, 1.4, 0.2,
6.2, 3.4, 5.4, 2.3,
5.9, 3.0, 5.1, 1.8
};
vector<float> y {
0,
0,
vector <float> operator-(const vector <float>& m1, const vector <float>& m2){
/* Returns the difference between two vectors.
Inputs:
m1: vector
m2: vector
Output: vector, m1 - m2, difference between two vectors m1 and m2.
*/
const unsigned long VECTOR_SIZE = m1.size();
vector <float> sigmoid_d (const vector <float>& m1) {
/* Returns the value of the sigmoid function derivative f'(x) = f(x)(1 - f(x)),
where f(x) is sigmoid function.
Input: m1, a vector.
Output: x(1 - x) for every element of the input matrix m1.
*/
const unsigned long VECTOR_SIZE = m1.size();
vector <float> output (VECTOR_SIZE);