Permalink
Browse files

First shot at a Neuron. I have a much funnier/complicated impl that's…

… not sure to provide better performances. I'll benchmark them, but meanwhile it provides a basis for the recurrent neural nets module
  • Loading branch information...
1 parent c1059c7 commit 1989c1eebb855ed462e0148deac34ab41c72b3f7 @alpmestan committed Oct 11, 2012
Showing with 133 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +39 −0 AI/HNN/Neuron.hs
  3. +30 −0 LICENSE
  4. +2 −0 Setup.hs
  5. +58 −0 hnn.cabal
View
4 .gitignore
@@ -0,0 +1,4 @@
+dist/
+*~
+*.o
+*.hi
View
39 AI/HNN/Neuron.hs
@@ -0,0 +1,39 @@
+{-# LANGUAGE BangPatterns, RecordWildCards #-}
+
+module Net (Neuron(..), compute, sigmoid, tanh) where
+-- tanh is from Prelude
+
+import Data.Vector.Unboxed (Vector, Unbox)
+import qualified Data.Vector.Unboxed as U
+
+
+-- | Our Neuron type, parametrized by the "number" type, which should be:
+-- 1/ an instance of Num
+-- 2/ an instance of Data.Vector.Unboxed.Unbox
+data Neuron a = Neuron
+ { weights :: !(Vector a)
+ , threshold :: !a
+ , activation :: a -> a
+ , activation' :: a -> a
+ }
+
+compute :: (Num a, Unbox a) => Neuron a -> Vector a -> a
+compute (Neuron{..}) !inputs = activation $ U.sum (U.zipWith (*) weights inputs)
+{-# SPECIALIZE compute :: Neuron Double -> Vector Double -> Double #-}
+{-# SPECIALIZE compute :: Neuron Float -> Vector Float -> Float #-}
+{-# INLINE compute #-}
+
+sigmoid :: Floating a => a -> a
+sigmoid !x = 1 / (1 + exp (-x))
+
+sigmoid' :: Floating a => a -> a
+sigmoid' !x = case sigmoid x of
+ s -> s * (1 - s)
+
+{-# SPECIALIZE sigmoid :: Double -> Double #-}
+{-# SPECIALIZE sigmoid :: Float -> Float #-}
+{-# INLINE sigmoid #-}
+
+{-# SPECIALIZE sigmoid' :: Double -> Double #-}
+{-# SPECIALIZE sigmoid' :: Float -> Float #-}
+{-# INLINE sigmoid' #-}
View
30 LICENSE
@@ -0,0 +1,30 @@
+Copyright (c) 2012, Alp Mestanogullari <alpmestan@gmail.com>
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+
+ * Neither the name of Alp Mestanogullari <alpmestan@gmail.com> nor the names of other
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
2 Setup.hs
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
View
58 hnn.cabal
@@ -0,0 +1,58 @@
+-- Initial hnn.cabal generated by cabal init. For further documentation,
+-- see http://haskell.org/cabal/users-guide/
+
+-- The name of the package.
+name: hnn
+
+-- The package version. See the Haskell package versioning policy (PVP)
+-- for standards guiding when and how versions should be incremented.
+-- http://www.haskell.org/haskellwiki/Package_versioning_policy
+-- PVP summary: +-+------- breaking API changes
+-- | | +----- non-breaking API additions
+-- | | | +--- code changes with no API change
+version: 0.2.0.0
+
+synopsis: A simple and fast neural networks library in haskell
+
+-- A longer description of the package.
+-- description:
+
+-- URL for the project homepage or repository.
+homepage: http://github.com/alpmestan/hnn
+
+-- The license under which the package is released.
+license: BSD3
+
+-- The file containing the license text.
+license-file: LICENSE
+
+-- The package author(s).
+author: Alp Mestanogullari <alpmestan@gmail.com>
+
+-- An email address to which users can send suggestions, bug reports, and
+-- patches.
+maintainer: Alp Mestanogullari <alpmestan@gmail.com>
+
+-- A copyright notice.
+-- copyright:
+
+category: AI, Math
+
+build-type: Simple
+
+-- Constraint on the version of Cabal needed to build this package.
+cabal-version: >=1.8
+
+
+library
+ -- Modules exported by the library.
+ -- exposed-modules:
+
+ -- Modules included in this library but not exported.
+ -- other-modules:
+
+ -- Other library packages from which modules are imported.
+ build-depends: base ==4.5.*,
+ vector,
+ ad
+

0 comments on commit 1989c1e

Please sign in to comment.