Permalink
Browse files

defprotocolやdefrecordを使って書き換えた

  • Loading branch information...
1 parent d84ff84 commit b29dcd9f33818b67a62e7b05a6022bc514069c40 @syou6162 committed Feb 29, 2012
Showing with 40 additions and 32 deletions.
  1. +6 −9 src/fobos_clj/fobos.clj
  2. +20 −11 src/fobos_clj/svm.clj
  3. +0 −8 test/fobos_clj/test/fobos.clj
  4. +14 −4 test/fobos_clj/test/svm.clj
View
@@ -1,11 +1,15 @@
(ns fobos_clj.fobos)
+(defprotocol Fobos
+ (update-weight [_ iter])
+ (classify [_ fv]))
+
(defn clip-by-zero [a b]
(if (> a 0.0)
(if (> a b) (- a b) 0.0)
(if (< a (- b)) (+ a b) 0.0)))
-(defn dotproduct "内積"
+(defn dotproduct
[weight fv]
(reduce (fn [sum [k v]]
(+ sum
@@ -16,8 +20,7 @@
"各iterationで重みを減衰させていく"
(/ 1.0 (+ 1.0 (/ iter example-size))))
-(defn l1-regularize
- "L1正則化をかけて、sparseにした重みベクトルを返す"
+(defn l1-regularize
[weight iter example-size lambda]
(let [lambda-hat (* (get-eta iter example-size) lambda)]
(reduce (fn [w [k v]]
@@ -26,9 +29,3 @@
(dissoc tmp-w k)
tmp-w)))
weight weight)))
-
-(defn muladd [weight fv y scale]
- (reduce (fn [w [k xi]]
- (assoc w k (+ (get-in w [k] 0.0)
- (* y xi scale))))
- weight fv))
View
@@ -4,15 +4,24 @@
(defn margin [weight fv y]
(* (dotproduct weight fv) y))
-(defn update-weight [examples weight iter eta lambda]
- (let [example-size (count examples)]
- (l1-regularize
- (reduce (fn [w [y fv]]
- (if (< (margin w fv y) 1.0)
- (muladd w fv y eta)
- w))
- weight examples)
- iter example-size lambda)))
+(defn muladd [weight fv y scale]
+ (reduce (fn [w [k xi]]
+ (assoc w k (+ (get-in w [k] 0.0)
+ (* y xi scale))))
+ weight fv))
-(defn classify [weight fv y]
- (> (margin weight fv y) 0.0))
+(defrecord SVM
+ [examples weight eta lambda]
+ Fobos
+ (update-weight [_ iter]
+ (let [example-size (count examples)]
+ (assoc _ :weight
+ (l1-regularize
+ (reduce (fn [w [y fv]]
+ (if (< (margin w fv y) 1.0)
+ (muladd w fv y eta)
+ w))
+ weight examples)
+ iter example-size lambda))))
+ (classify [_ fv]
+ (if (> (dotproduct weight fv) 0.0) 1 -1)))
@@ -64,11 +64,3 @@
lambda 1.0]
(is (not (empty?
(l1-regularize weight iter example-size lambda)))))))
-
-(deftest test-muladd
- (let [weight {0 0.1, 1 0.2, 2 0.5, 3 -0.1}
- fv [[1 1] [2 1.0] [100 1]]]
- (is (= (muladd weight fv 1 1.0)
- {0 0.1, 1 1.2, 2 1.5, 3 -0.1, 100 1.0}))
- (is (= (muladd weight fv -1 1.0)
- {0 0.1, 1 -0.8, 2 -0.5, 3 -0.1, 100 -1.0}))))
@@ -10,15 +10,25 @@
(is (= (margin weight fv -1)
-0.7))))
+(deftest test-muladd
+ (let [weight {0 0.1, 1 0.2, 2 0.5, 3 -0.1}
+ fv [[1 1] [2 1.0] [100 1]]]
+ (is (= (muladd weight fv 1 1.0)
+ {0 0.1, 1 1.2, 2 1.5, 3 -0.1, 100 1.0}))
+ (is (= (muladd weight fv -1 1.0)
+ {0 0.1, 1 -0.8, 2 -0.5, 3 -0.1, 100 -1.0}))))
+
(deftest test-update-weight
(let [examples [[1 [[1 1] [2 1]]]
[-1 [[1 2] [2 0]]]]
init-weight {0 0.1, 1 0.2, 2 0.5, 3 -0.1}
iter 10
eta 0.1
- lambda 0.1]
- (update-weight examples init-weight iter eta lambda)))
+ lambda 0.1
+ svm (fobos_clj.svm.SVM. examples init-weight eta lambda)]
+ (.update-weight svm iter)))
(deftest test-classify
- (let [weight {0 0.1, 1 0.2, 2 0.5, 3 -0.1}]
- (is (classify weight [[1 1] [2 1]] 1))))
+ (let [weight {0 0.1, 1 0.2, 2 0.5, 3 -0.1}
+ svm (fobos_clj.svm.SVM. nil weight nil nil)]
+ (is (= (.classify svm [[1 1] [2 1]]) 1))))

0 comments on commit b29dcd9

Please sign in to comment.