Skip to content

Commit

Permalink
added takevector and dropvector functions
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-taylor committed Aug 19, 2012
1 parent f8b667c commit fcc4ef2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/AI/Learning/LinearRegression.hs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ ridgeRegress x y useConst lambda
(_,r) = qr x
rr = takeRows n r
ww = if useConst
then diag (fromList (0 : replicate (n-1) 1))
then diag $ join [0, constant 1 (n-1)]
else ident n
in (trans rr <> rr + lambda `scale` ww) <\> trans x <> y

Expand Down
7 changes: 3 additions & 4 deletions src/AI/Learning/LogisticRegression.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ lrLogLikRegularized y x useConst lambda theta = (cost, grad)
where
m = fromIntegral (rows x)
h = sigmoid $ x <> theta
w = if useConst
then join [0, constant 1 (dim theta - 1)]
else constant 1 (dim theta)
theta' = w * theta
theta' = if useConst
then join [0, dropVector 1 theta]
else theta
cost = sumVector (y * log h + (1-y) * log (1-h)) / m + (sumVector $ mapVector (^2) theta') / (2 * m)
grad = (1/m `scale` (y - h) <> x) + (lambda / m) `scale` theta'

Expand Down
15 changes: 6 additions & 9 deletions src/AI/Util/Matrix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@ module AI.Util.Matrix where

import Foreign.Storable (Storable)
import Numeric.LinearAlgebra
import Numeric.LinearAlgebra.Util (ones,zeros)

-- |Create an @rxc@ matrix of zeros.
--zeros :: (Num a, Container Vector a) => Int -> Int -> Matrix a
--zeros r c = konst 0 (r,c)

---- |Create an @rxc@ matrix of ones.
--ones :: (Num a, Container Vector a) => Int -> Int -> Matrix a
--ones r c = konst 1 (r,c)

-- |Return the size of a matrix as a 2-tuple.
size :: Matrix a -> (Int,Int)
Expand All @@ -30,6 +21,12 @@ vertcat = fromBlocks . map return
-- Functions on Vectors --
--------------------------

takeVector :: Storable a => Int -> Vector a -> Vector a
takeVector n v = subVector 0 n v

dropVector :: Storable a => Int -> Vector a -> Vector a
dropVector n v = subVector n (dim v - n) v

sumVector :: (Num a, Storable a) => Vector a -> a
sumVector xs = foldVector (+) 0 xs

Expand Down

0 comments on commit fcc4ef2

Please sign in to comment.