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

Function cost

examples/machine_learning/softmax_regression.cpp:42–68  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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