@@ -0,0 +1,2330 @@
+#!/usr/bin/env python%0A%0A%0Afrom __future__ import print_function%0Afrom random import random%0Aimport click%0A%0A%0Adef forward_multiply_gate(x, y):%0A    return x*y%0A%0A%0A@click.group()%0Adef cli():%0A    pass%0A%0A%0A@cli.command()%0A@click.option('-z', '--step-size', type=float, default=0.01)%0A@click.option('-s', '--steps', type=int, default=1000)%0A@click.argument('x', type=float, nargs=1)%0A@click.argument('y', type=float, nargs=1)%0Adef random_local_search(x, y, step_size, steps, F=forward_multiply_gate):%0A    '''%0A    Random Local Search example.%0A    %0A    Based on javascript code in the blog by%0A        %5BAndrej Karpathy%5D(http://karpathy.github.io/neuralnets/)%0A    %0A    Usage:%0A      random_local_search x y%0A      %0A    Examples:%0A      random_local_search 2 3%0A      %0A      # use the double dash -- so that negative numbers can be specified%0A      random_local_search -- 2 -3%0A    '''%0A    best_out = -2**32%0A    best_x = x%0A    best_y = y%0A    %0A    for _ in xrange(steps):%0A        x2 = x + step_size * (random() * 2 - 1)%0A        y2 = y + step_size * (random() * 2 - 1)%0A%0A        answer = F(x2, y2)%0A        if answer %3E best_out:%0A            best_x = x2%0A            best_y = y2%0A            best_out = answer%0A%0A    print_result(x, y, best_x, best_y, best_out)%0A%0A%0A@cli.command()%0A@click.option('-z', '--step-size', type=float, default=0.01)%0A@click.argument('x', type=float, nargs=1)%0A@click.argument('y', type=float, nargs=1)%0Adef numerical_gradient(x, y, step_size, F=forward_multiply_gate):%0A    '''%0A    Numerical Gradient example.%0A    %0A    Based on javascript code in the blog by%0A        %5BAndrej Karpathy%5D(http://karpathy.github.io/neuralnets/)%0A    %0A    Usage:%0A      numerical_gradient start_x start_y%0A      %0A    Examples:%0A      numerical_gradient 2 3%0A      %0A      # use the double dash -- so that negative numbers can be specified%0A      numerical_gradient -- 2 -3%0A    '''%0A    out = F(x, y)%0A    x_derivative = (F(x + step_size, y) - out) / step_size%0A    y_derivative = (F(x, y + step_size) - out) / step_size%0A%0A    best_x = x + step_size * x_derivative%0A    best_y = y + step_size * y_derivative%0A    best_out = F(best_x, best_y)%0A%0A    print_result(x, y, best_x, best_y, best_out)%0A%0A    %0Adef print_result(x, y, best_x, best_y, best_out):%0A    print(%22(%25.4f, %25.4f) -%3E (%25.4f, %25.4f) = %25.4f%22 %25 (%0A            x, y, best_x, best_y, best_out))%0A%0A    %0Aif __name__ == '__main__':%0A    cli()%0A

add neuralnets.py