Skip to content

Instantly share code, notes, and snippets.

View ryanrhymes's full-sized avatar
🎯
Focusing

Liang Wang ryanrhymes

🎯
Focusing
View GitHub Profile
s = window.getSelection();
oRange = s.getRangeAt(0); //get the text range
oRect = oRange.getBoundingClientRect();
@ryanrhymes
ryanrhymes / cpu_count.ml
Created December 9, 2016 22:49
number of cores
let cpu_count () =
try match Sys.os_type with
| "Win32" -> int_of_string (Sys.getenv "NUMBER_OF_PROCESSORS")
| _ ->
let i = Unix.open_process_in "getconf _NPROCESSORS_ONLN" in
let close () = ignore (Unix.close_process_in i) in
try Scanf.fscanf i "%d" (fun n -> close (); n) with e -> close (); raise e
with
| Not_found | Sys_error _ | Failure _ | Scanf.Scan_failure _
| End_of_file | Unix.Unix_error (_, _, _) -> 1
open Owl
open Bigarray
let m, n, o = 10, 1000, 10000
let shape = [|m;n;o|]
let test_op s c op =
let ttime = ref 0. in
for i = 1 to c do
shape = (10,1000,10000)
function f00()
t0 = time()
x = []
t1 = time()
@printf "empty:\t\t%.8f\n" (t1 -t0)
end
function f01()
@ryanrhymes
ryanrhymes / Makefile
Created January 12, 2017 16:39
Makefile for Eigen library
CXXFLAGS0 = -shared -fPIC -ansi -pedantic -W -Wall
CXXFLAGS1 = -shared -fPIC -ansi -pedantic -Wno-extern-c-compat -Wno-c++11-long-long
CXXFLAGS2 = -c -ansi -Wno-extern-c-compat -Wno-c++11-long-long
all:
g++ ${CXXFLAGS2} -I. eigen_dsmat.cpp -o eigen_dsmat.o
g++ ${CXXFLAGS2} -I. eigen_spmat.cpp -o eigen_spmat.o
#gcc -c ffi_eigen_generated_stub.c -I. -I`ocamlfind query ctypes` -I`ocamlc -where` -o eigen_stub.o
ld -r eigen_*.o -o libeigen.o
ocamlfind ocamlc -c -package ctypes ffi_eigen_generated.ml
@ryanrhymes
ryanrhymes / eigen_dsmat.ml
Created January 12, 2017 16:41
Functor of dsmat in Eigen library
(*
* Eigen - an OCaml interface to C++ Eigen library
* Copyright (c) 2016 Liang Wang <liang.wang@cl.cam.ac.uk>
*)
module type MatSig = sig
type elt
type mat
@ryanrhymes
ryanrhymes / owl_nn.ml
Created March 10, 2017 15:54
using owl to build a trivial nn sing AD module from scratch
open Owl_algodiff_ad
type layer = { mutable w : t; mutable b : t; a : t -> t }
type network = { layers : layer array }
let run_layer x l = Maths.((x $@ l.w) + l.b) |> l.a
let run_network x nn = Array.fold_left run_layer x nn.layers
open Owl
open Algodiff.AD
type layer = { mutable w : t; mutable b : t; a : t -> t }
type network = { layers : layer array }
let run_layer x l = Maths.((x $@ l.w) + l.b) |> l.a
let run_network x nn = Array.fold_left run_layer x nn.layers
let l0 = {
let backprop nn eta x y =
let t = tag () in
Array.iter (fun l ->
l.w <- make_reverse l.w t;
l.b <- make_reverse l.b t;
) nn.layers;
let loss = Maths.(cross_entropy y (run_network x nn) / (F (Mat.row_num x |> float_of_int))) in
reverse_prop (F 1.) loss;
Array.iter (fun l ->
l.w <- Maths.((primal l.w) - (eta * (adjval l.w))) |> primal;
let test_model nn x y =
Mat.iter2_rows (fun u v ->
Dataset.print_mnist_image (unpack_mat u);
let p = run_network u nn |> unpack_mat in
Owl.Mat.print p;
Printf.printf "prediction: %i\n" (let _, _, j = Owl.Mat.max_i p in j)
) x y
let _ =
let x, _, y = Dataset.load_mnist_train_data () in