February 2018
Intermediate to advanced
378 pages
10h 14m
English
The cost function is something we can use to assess the prediction quality:
// cost(y, h) = -sum(y.*log(h)+(1-y).*log(1-h))/m public func cost(trueVec: [Double], predictedVec: [Double]) -> Double { let count = trueVec.count // Calculate squared Euclidean distance. var result = 0.0 var left = [Double](repeating: 0.0, count: count) var right = [Double](repeating: 0.0, count: count) // log(h) var outputLength = Int32(count) vvlog(&left, predictedVec, &outputLength) // -y.*log(h) left = vecMultiply(trueVec, left) // 1-y var minusOne = -1.0 var oneMinusTrueVec = [Double](repeating: 0.0, count: count) vDSP_vsaddD(trueVec, 1, &minusOne, &oneMinusTrueVec, 1, vDSP_Length(count)) vDSP_vnegD(oneMinusTrueVec, 1, &oneMinusTrueVec, 1, vDSP_Length(count)) ...Read now
Unlock full access