ollio / main.ml

let get_input_filename default = 
  if Array.length Sys.argv > 1 then 
    Array.get Sys.argv 1
  else default;;

let read_lines num input_in = 
  let rec read_ rem list = 
    if rem=0 then List.rev list 
    else
      let line = input_line input_in in
	read_ (rem-1) (line::list)
  in read_ num [];;

let get_token str =
  let l = Str.split (Str.regexp " ") str in
  l;;

exception Bad_line;;

let parse_line line =
  let l2p = function
    | i::v::[] -> ((int_of_string i), (float_of_string v));
    | _ -> raise Bad_line
  in
  match Str.split (Str.regexp " ") line with
    | [] -> raise Bad_line;
    | hd::tl ->
      let l = List.map (Str.split (Str.regexp ":")) tl in
(*      print_int judge; *)
      (int_of_string hd), (List.map l2p l);;


let judge w x =
  let y = Vector.prod w x in
  if y > 0.0 then 1
  else -1;;

let rec step w lines = match lines with
  | [] -> w;
  | line::tl ->
    let sign,x = parse_line line in
    let y_bar = judge w x in
    let tau = float_of_int (y_bar * sign) in
    step (Vector.add w (Vector.s_prod tau w)) tl;;

let train =
  let input_in = open_in (get_input_filename "A-init-practice.in") in
  let lines = read_lines 10 input_in in
(*  let _ = List.map parse_line lines in *)
  let w = step (Vector.newv()) lines in
    (*  let _ = process_cases num_cases input_in in *)
  close_in input_in;
  w;;


(*let _ =
  let w = test *)
(*
  INPUT: aggressiveness parameter C > 0
  INITIALIZE: w1 = (0,...,0)
  For	t = 1,2,...
    • receive instance: xt ∈ Rn
    • predict: yˆt = sign(wt · xt )
    • receive correct label: yt ∈ {−1, +1}
    • suffer loss: lt = max{0 , 1−yt(wt ·xt)}
  • update:
    1. set:
       τt = lt / |x_t|^2	(PA) ∥xt∥
       τt =min C, lt/∥xt∥2	(PA-I)
       τ= lt / (|xt|^2 + 1/2C)  (PA-II)
    2.update: wt+1=wt+τtytxt
*)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.