-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCostFunction.cpp
More file actions
91 lines (78 loc) · 2.56 KB
/
CostFunction.cpp
File metadata and controls
91 lines (78 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "CostFunction.h"
#include <iostream>
/**
* @brief Guard against invalid vector lengths.
*
* This function checks if the lengths of two input vectors `a` and `b` are equal.
* If the lengths are not equal, it throws an exception indicating the invalid vector lengths.
*
* @param a First input vector of type `std::vector<double>`.
* @param b Second input vector of type `std::vector<double>`.
* @throws std::invalid_argument if the lengths of `a` and `b` are not equal.
*/
void CostFunction::guardAgainstInvalidVectorLengths(vector<double> a, vector<double> b)
{
if (a.size() != b.size()) {throw invalid_argument("Invalid call to cost function: vectors have different size."); }
}
/**
* @class MeanSquaredError
* @brief Calculates the mean squared error between predicted and expected values.
*
* @param predicted the values estimated by the network.
* @param expected the actual values as given by the label.
*/
double MeanSquaredError::Cost(vector<double> predicted, vector<double> expected)
{
guardAgainstInvalidVectorLengths(predicted, expected);
double cost = 0;
for(int index = 0; index < predicted.size(); ++index)
{
cost += pow((predicted[index] - expected[index]), 2);
}
return cost;
}
/**
* Returns the derivative of the cost function at the given inputs.
*
* @param predicted the predicted value to calculate the derivative at.
* @param expected the expected value to calculate the derivative at.
* @return the derivative.
*/
double MeanSquaredError::Derivative(double predicted, double expected)
{
return 2 * (predicted - expected);
}
/**
* @class CrossEntropy
* @brief Calculates the cross entropy loss between predicted and expected values.
*
* @param predicted the predicted values by the network.
* @param expected the actual values as given by the label.
*/
double CrossEntropy::Cost(vector<double> predicted, vector<double> expected)
{
// cost is sum (for all x,y pairs) of: 0.5 * (x-y)^2
double cost = 0;
for (int i = 0; i < predicted.size(); i++)
{
double v = (expected[i] == 1) ? -log(predicted[i]) : -log(1 - predicted[i]);
cost += isnan(v) ? 0 : v;
}
return cost;
}
/**
* Returns the derivative of the cost function at the given inputs.
*
* @param predicted the predicted value to calculate the derivative at.
* @param expected the expected value to calculate the derivative at.
* @return the derivative.
*/
double CrossEntropy::Derivative(double predicted, double expected)
{
if (predicted == 0 || predicted == 1)
{
return 0;
}
double output = (-predicted + expected) / (predicted * (predicted - 1));
return output;
}