Class: RubyFann::Neurotica

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_fann/neurotica.rb

Overview

Generates directed graph from a RubyFann neural network. Requires the GraphViz gem 0.8.0 (or higher) to be installed, as well as graphviz proper 2.14.1 (or higher).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Neurotica

Initialize neurotica grapher with the following args:

:connector_colors    - array of graphviz-friendly color names/numbers
:input_layer_color   - graphviz-friendly color name/number
:hidden_layer_colors - array of graphviz-friendly color names/numbers
:output_layer_color  - graphviz-friendly color name/number


19
20
21
22
23
24
25
26
27
28
# File 'lib/ruby_fann/neurotica.rb', line 19

def initialize(args={})      
  @connector_colors = args[:connector_colors]
  @input_layer_color = args[:input_layer_color]
  @hidden_layer_colors = args[:hidden_layer_colors]
  @output_layer_color = args[:output_layer_color]
  @connector_colors ||= ['red', 'blue', 'yellow', 'green', 'orange', 'black', 'pink', 'gold', 'lightblue', 'firebrick4', 'purple']   
  @input_layer_color ||= 'green'
  @hidden_layer_colors ||= ['bisque2', 'yellow', 'blue', 'orange', 'khaki3']   
  @output_layer_color ||= 'purple'
end

Instance Attribute Details

#connector_colorsObject

:nodoc:



9
10
11
# File 'lib/ruby_fann/neurotica.rb', line 9

def connector_colors
  @connector_colors
end

#hidden_layer_colorsObject

Returns the value of attribute hidden_layer_colors.



11
12
13
# File 'lib/ruby_fann/neurotica.rb', line 11

def hidden_layer_colors
  @hidden_layer_colors
end

#input_layer_colorObject

Returns the value of attribute input_layer_color.



10
11
12
# File 'lib/ruby_fann/neurotica.rb', line 10

def input_layer_color
  @input_layer_color
end

#output_layer_colorObject

Returns the value of attribute output_layer_color.



12
13
14
# File 'lib/ruby_fann/neurotica.rb', line 12

def output_layer_color
  @output_layer_color
end

Instance Method Details

#graph(neural_net, output_path, args = {}) ⇒ Object

Generate output graph with given neural network to the given output path (PNG)

If args[:three_dimensional] is set, then a 3d VRML graph will be generated (experimental)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ruby_fann/neurotica.rb', line 32

def graph(neural_net, output_path, args={})
  if (args[:three_dimensional])
    graph_viz = GraphViz::new( "G", :dim=>'3') # , :size=>"17,11"     
    shape="point"
  else
    graph_viz = GraphViz::new( "G", :dim=>'2') # , :size=>"17,11"     
    shape="egg"
  end
  
  neurons = neural_net.get_neurons()
  graph_node_hash = {}
  max_layer = neurons.max {|a,b| a[:layer] <=> b[:layer] }[:layer]


  # Add nodes:
  neurons.each do |neuron|
    fillcolor = "transparent"  # : "khaki3"
    layer = neuron[:layer]
    fillcolor = case layer
      when 0
        @input_layer_color
      when max_layer
        @output_layer_color
      else
        @hidden_layer_colors[(layer-1) % @hidden_layer_colors.length]
    end
    
    #puts "adding neuron with #{neuron[:value]}"
    node_id = neuron.object_id.to_s
#        label = (layer==0) ? ("%d-%0.3f-%0.3f" % [neuron[:layer], neuron[:value], neuron[:sum]]) : ("%d-%0.3f-%0.3f" % [neuron[:layer], neuron[:value], neuron[:sum]])       
    label = (layer==0 || layer==max_layer) ? ("%0.3f" % neuron[:value]) : ("%0.3f" % rand) #neuron[:sum]) 
    graph_node_hash[node_id] = graph_viz.add_node(
      node_id,
      :label=>label,
      :style=>"filled",
      :fillcolor=>fillcolor,
      #:color=>fillcolor,
      :shape=>shape,
      :z=>"#{rand(100)}", # TODO
      # :z=>"0", # TODO
      # :width=>"1",
      # :height=>"1",
      :fontname=>"Verdana"
    )        
  end
        
  previous_neurons = nil
  layer_neurons = nil
  0.upto(max_layer) do |layer|
    previous_neurons = layer_neurons
    layer_neurons = neurons.find_all{|n| n[:layer]==layer}
    
    if previous_neurons
      previous_neurons.each do |pn|
        node_id = pn.object_id.to_s
        
        layer_neurons.each do |n|
          dest_id = n.object_id.to_s
          graph_viz.add_edge(
            graph_node_hash[node_id], 
            graph_node_hash[dest_id], 
            :weight=>"10",
            :color=>"#{connector_colors[layer % connector_colors.length]}"
          )                      
        end
      end
    end
    
  end      
  
  if (args[:three_dimensional])
    graph_viz.output(:vrml=>output_path)
  else
    graph_viz.output(:png=>output_path)
  end
  
  
end