Skip to content

Instantly share code, notes, and snippets.

@kmdupr33
Created September 5, 2019 12:43
Show Gist options
  • Save kmdupr33/2908ff090b3cbcb38320673d857a0038 to your computer and use it in GitHub Desktop.
Save kmdupr33/2908ff090b3cbcb38320673d857a0038 to your computer and use it in GitHub Desktop.
fun updateGuess(
guess: Pair<Double, Double>,
learningRate: Double,
videoGameData: Array<Pair<Double, Double>>
): Pair<Double, Double> {
val (m, b) = guess
val mGradient = videoGameData.fold(0.0) { acc: Double, pair: Pair<Double, Double> ->
val (x, y) = pair
acc + ((((m * x) + b) - y) * x)
} / videoGameData.size
val bGradient = videoGameData.fold(0.0) { acc: Double, pair: Pair<Double, Double> ->
val (x, y) = pair
acc + ((((m * x) + b) - y))
} / videoGameData.size
val newM = m - (learningRate * mGradient)
val newB = b - (learningRate * bGradient)
return Pair(newM, newB)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment