MCPcopy Create free account
hub / github.com/arrayfire/arrayfire / cost

Function cost

examples/machine_learning/logistic_regression.cpp:38–64  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

36}
37
38void cost(array &J, array &dJ, const array &Weights, const array &X,
39 const array &Y, double lambda = 1.0) {
40 // Number of samples
41 int m = Y.dims(0);
42
43 // Make the lambda corresponding to Weights(0) == 0
44 array lambdat = constant(lambda, Weights.dims());
45
46 // No regularization for bias weights
47 lambdat(0, span) = 0;
48
49 // Get the prediction
50 array H = predict(X, Weights);
51
52 // Cost of misprediction
53 array Jerr = -sum(Y * log(H) + (1 - Y) * log(1 - H));
54
55 // Regularization cost
56 array Jreg = 0.5 * sum(lambdat * Weights * Weights);
57
58 // Total cost
59 J = (Jerr + Jreg) / m;
60
61 // Find the gradient of cost
62 array D = (H - Y);
63 dJ = (matmulTN(X, D) + lambdat * Weights) / m;
64}
65
66array train(const array &X, const array &Y, double alpha = 0.1,
67 double lambda = 1.0, double maxerr = 0.01, int maxiter = 1000,

Callers 1

trainFunction · 0.70

Calls 6

constantFunction · 0.85
logFunction · 0.85
matmulTNFunction · 0.85
predictFunction · 0.70
sumFunction · 0.50
dimsMethod · 0.45

Tested by

no test coverage detected