Skip to content

Instantly share code, notes, and snippets.

@timo
Created May 17, 2018 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timo/2c0962ddb7e18629fb9237f59c9f1ab0 to your computer and use it in GitHub Desktop.
Save timo/2c0962ddb7e18629fb9237f59c9f1ab0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl6
# inspired by https://narimiran.github.io/2018/05/10/python-numpy-nim.html
use nqp;
constant N = 10_000;
constant sigma = 0.1e0;
constant f = (2 / N).Num;
constant mu = 0.001e0;
constant nEpochs = 1_000;
sub normdist ($m, $σ) {
my $r = sqrt -2 * log rand;
my $Θ = τ * rand;
$r * cos($Θ) * $σ + $m;
}
sub gradientDescent(num @x, num @d, $in_mu, $in_nEpochs) {
my num $start = now.Num;
my num @y;
my num $err;
my num $w0 = 0e0;
my num $w1 = 0e0;
my num $mu = $in_mu;
my int $nEpochs = $in_nEpochs;
@y[N - 1] = 0e0;
for ^$nEpochs {
my num $grad0 = 0e0;
my num $grad1 = 0e0;
for ^N -> $i {
$err = nqp::mul_n(f, nqp::sub_n(@d.AT-POS($i), @y.AT-POS($i)));
$grad0 = nqp::add_n($grad0, $err);
$grad1 = nqp::add_n($grad1, nqp::mul_n($err, @x.AT-POS($i)));
}
$w0 += nqp::mul_n(mu, $grad0);
$w1 += nqp::mul_n(mu, $grad1);
for ^N -> $i {
@y[$i] = nqp::add_n($w0, nqp::mul_n($w1, @x[$i]));
}
}
($w0, $w1);
}
my num @input_x;
my num @input_d;
@input_x[N - 1] = @input_d[N - 1] = 0e0;
note "generating values";
for ^N {
my num $ival = $_.Num;
@input_x[$_] = f * $ival;
@input_d[$_] = 3e0 + 2e0 * $ival + normdist(0, 0.1);
}
note "done";
my $start = now;
say gradientDescent(@input_x, @input_d, mu, nEpochs);
say "Perl6 time: { now - $start }";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment