Created
September 14, 2017 00:12
-
-
Save edubart/c0709ca0667fd939870ece93cb2896e7 to your computer and use it in GitHub Desktop.
Testing arraymancer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ../src/arraymancer | |
import math | |
proc `/`*[T: SomeNumber](a: T, t: Tensor[T]): Tensor[T] {.noSideEffect.} = | |
return t.fmap(proc(x: T): T = a / x) | |
proc `+`*[T: SomeNumber](t: Tensor[T], a: T): Tensor[T] {.noSideEffect.} = | |
return t.fmap(proc(x: T): T = x + a) | |
proc `+`*[T: SomeNumber](a: T, t: Tensor[T]): Tensor[T] {.noSideEffect.} = | |
return t.fmap(proc(x: T): T = a + x) | |
proc `-`*[T: SomeNumber](t: Tensor[T], a: T): Tensor[T] {.noSideEffect.} = | |
return t.fmap(proc(x: T): T = x - a) | |
proc `-`*[T: SomeNumber](a: T, t: Tensor[T]): Tensor[T] {.noSideEffect.} = | |
return t.fmap(proc(x: T): T = a - x) | |
proc `-`*[T: SomeNumber](t: Tensor[T]): Tensor[T] {.noSideEffect.} = | |
return t.fmap(proc(x: T): T = -x) | |
proc sigmoid[T](x: T): T = | |
result = 1.0f / (1.0f + exp(-x)) | |
proc forward_and_backward(w: Tensor[float32], b: float32, X: Tensor[float32], Y: Tensor[float32]): auto = | |
let m = float32(X.shape[1]) | |
# forward propagation | |
let Z = w.transpose() * X + b | |
let A = sigmoid(Z) | |
let cost = - ((Y |*| ln(A)) + ((1.0f - Y) |*| ln(1.0f - A))).sum() / m | |
# bacward propagation | |
let dw = (X * (A - Y).transpose()) / m | |
let db = (A - Y).sum() / m | |
return (dw, db, cost) | |
proc optimize(w: var Tensor[float32], b: var float32, | |
X: Tensor[float32], Y: Tensor[float32], | |
max_iterations=1000, learning_rate=1e-3f) = | |
for i in 0..<max_iterations: | |
let (dw, db, cost) = forward_and_backward(w, b, X, Y) | |
w -= learning_rate * dw | |
b -= learning_rate * db | |
if i mod 100 == 0: | |
echo "loss: ", cost | |
proc predict(w: Tensor[float32], b: float32, X: Tensor[float32]): Tensor[float32] = | |
let A = sigmoid((w.transpose() * X) + b) | |
result = A.fmap(proc(x: float32): float32 = | |
if x <= 0.5f: result = 0.0f else: result = 1.0f | |
) | |
let | |
X = [[1.0f,2.0f],[3.0f,4.0f]].toTensor() | |
Y = [[1.0f,0.0f]].toTensor() | |
var | |
w = zeros([X.shape[1], 1], float32) | |
b = 0.0f | |
optimize(w, b, X, Y, 1000, 1f) | |
let Y_pred = predict(w, b, X) | |
let accuracy = (Y_pred - Y).mean() * 100 | |
echo "accuracy: ", accuracy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment