1. Kota UENISHI
  2. ollio

Commits

UENISHI Kota  committed 4dfd8c2

implementing

  • Participants
  • Parent commits c0ca8e9
  • Branches default

Comments (0)

Files changed (2)

File main.ml

View file
  • Ignore whitespace
+exception Bad_line;;
+exception Unknown;;
+
 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));
       (int_of_string hd), (List.map l2p l);;
 
 
+(* w x -> 1 | -1 *)
 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;;
+(* w -> vector -> result -> w' -> y *)
+let train filename =
+  let input_in = open_in (get_input_filename filename) in
+  let f_tau = tau_getter "PA" in
+  let rec train_ w =
+    try
+      let sign,x = parse_line (input_line input_in) in
+      let y_bar = judge w x in
+      let tau = f_tau x (loss y_bar sign) in
+      train_ (Vector.add w (Vector.s_prod tau w))
+    with
+	End_of_file ->
+	  close_in input_in;
+	  w
+  in
+  train_ (Vector.newv())
 
-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 _ =
+  train "A-init-practice.in";;
 
+(* vector -> float -> flost *)
+let pa x l =
+  l /. (Vector.norm2 x);;
 
-(*let _ =
-  let w = test *)
+let pa1 c x l =
+  let v = pa x l in
+  if v < c then v else c;;
+
+let pa2 c x l =
+  l /. ( (Vector.norm2 x) +. 1.0 /. (2.0 *. c));;
+
+(* "PA"|"PA1"|"PA2" -> ( vector -> float -> flost *)
+let tau_getter = function
+  | "PA" ->  pa;
+  | "PA1" -> pa1 1.0;
+  | "PA2" -> pa2 1.0;
+  | _ -> raise Unknown;;
+
+let loss y_bar ans =
+  let v = 1.0 -. float_of_int (ans * y_bar) in
+  if v < 0.0 then 0.0 else v;;
+
 (*
   INPUT: aggressiveness parameter C > 0
   INITIALIZE: w1 = (0,...,0)

File vector.ml

View file
  • Ignore whitespace
   add_ x y [];;
 
 let newv () = [];;
+
+(* get norm of x with rank 2 *)
+let norm2 x =
+  let rec norm_ s = function
+    | [] -> s;
+    | (_,v)::tl ->
+      norm_ (s +. v *. v) tl
+  in
+  norm_ 0.0 x;;