Neural Nets

structure

I have started using Data.Graph but i think i will now use Data.Graph.Inductive I am hoping the constructors are somewhat similar or that i can transform one to another

Data.Graph

I used the Data.Graph package in haskell to create the network structure. As far as i can see it has two advantages

  • it gets around creating recurrent structures, which i understand can be quite tricky in a purely functional language
  • it is a functor and can generate a net with the directions reversed which can be handy

Data.Graph.Inductive

I am also considering using the Data.Graph.Inductive graph library , this seems to have the advantage that it can add or change nodes in a graph which i think might be necessary for doing parallel programming

Code

module MyGraph where
import Data.Graph


--http://web.engr.oregonstate.edu/~erwig/papers/abstracts.html#JFP01
data Neuron  = Neuron { z::Double, error::Double, output::Double} deriving Show
type VertexId = String
type ParentVertexIds = [VertexId]
type VertexDef a = (a, VertexId, [VertexId]) 

data LayerDef = LayerDef {getLayerId::String, getLayerSize::Integer}

data NetworkDef = NetworkDef {getLayerDef::LayerDef, getSubLayerDefs::[NetworkDef]}

buildVertexDef :: ParentVertexIds -> String -> Integer -> VertexDef Neuron
buildVertexDef parentlayerkeys lid neuronidx = (Neuron 0 0 0,"layer"++lid ++ show neuronidx, parentlayerkeys)

buildLayerDef :: ParentVertexIds  -> LayerDef -> [VertexDef Neuron]
buildLayerDef pkeys ldef = map (buildVertexDef pkeys lid) [1..n]
                         where lid = getLayerId ldef
                               n = getLayerSize ldef



buildNetworkDef :: ParentVertexIds ->NetworkDef -> [VertexDef Neuron]
buildNetworkDef pkeys (NetworkDef ldef [])   = buildLayerDef pkeys ldef                    
buildNetworkDef pkeys (NetworkDef ldef subs) =  concat $ thislayer : subdefs                         
                                                where thislayer = buildLayerDef pkeys ldef
                                                      thislayerkeys = map (\(_, k, _) -> k) thislayer
                                                      subdefs =   map (buildNetworkDef thislayerkeys) subs

myNetWorkSchema::NetworkDef
myNetWorkSchema = NetworkDef (LayerDef "i" 5) [layerj] 
               where layerj = NetworkDef (LayerDef "j" 5) []

myNetWorkDef::[VertexDef Neuron]                     
myNetWorkDef = buildNetworkDef [] myNetWorkSchema                     

myNetwork :: (Graph,Vertex -> VertexDef Neuron,VertexId -> Maybe Vertex)
myNetwork = graphFromEdges myNetWorkDef

main :: IO()
main = print myNetWorkDef

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s