Permalink
Browse files

ロジステック関数関連を追加

  • Loading branch information...
1 parent b29dcd9 commit 3ab9876f454eb71eca959d49a76c2c2c6babda29 @syou6162 committed Feb 29, 2012
Showing with 63 additions and 0 deletions.
  1. +35 −0 src/fobos_clj/logistic.clj
  2. +28 −0 test/fobos_clj/test/logistic.clj
View
@@ -0,0 +1,35 @@
+(ns fobos_clj.logistic
+ (:use [fobos_clj.fobos]))
+
+(defn logistic-deriv [y inner-product xi]
+ (* (- y)
+ xi
+ (Math/exp (* (- y) inner-product))
+ (/ 1.0 (+ 1.0 (Math/exp (* (- y) inner-product))))))
+
+(defn muldiff [weight fv y scale]
+ (let [inner-product (dotproduct weight fv)]
+ (reduce (fn [w [k xi]]
+ (assoc w k
+ (- (get w k 0.0)
+ (* scale
+ (logistic-deriv y inner-product xi)))))
+ weight fv)))
+
+(defrecord Logistic
+ [examples weight eta lambda]
+ Fobos
+ (update-weight [_ iter]
+ (let [example-size (count examples)]
+ (assoc _ :weight
+ (l1-regularize
+ (reduce (fn [w [y fv]]
+ (muldiff w fv y eta))
+ weight examples)
+ iter example-size lambda))))
+ (classify [_ fv]
+ (letfn [(prob [fv]
+ (let [inner-prod (dotproduct weight fv)]
+ (/ 1.0
+ (+ 1.0 (Math/exp (- inner-prod))))))]
+ (if (> (prob fv) 0.5) 1 -1))))
@@ -0,0 +1,28 @@
+(ns fobos_clj.test.logistic
+ (:use [fobos_clj.logistic])
+ (:use [clojure.test]))
+
+(deftest test-logistic-deriv
+ (logistic-deriv 1 0.1 0.3)
+ (logistic-deriv -1 0.1 0.3))
+
+(deftest test-muldiff
+ (muldiff {} [[0 1] [1 1.5]] 1 1.0)
+ (muldiff {} [[0 1] [1 1.5]] -1 1.0)
+ (muldiff {0 1.5} [[0 1] [1 1.5]] 1 1.0)
+ (muldiff {0 1.5 1 1.0} [[0 1] [1 1.5]] 1 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
+ logistic (fobos_clj.logistic.Logistic. examples init-weight eta lambda)]
+ (.update-weight logistic iter)))
+
+(deftest test-classify
+ (let [weight {0 0.1, 1 0.2, 2 0.5, 3 -0.1}
+ logistic (fobos_clj.logistic.Logistic. nil weight nil nil)]
+ (is (= (.classify logistic [[1 1] [2 1]]) 1))))

0 comments on commit 3ab9876

Please sign in to comment.