Class: CooCoo::Grapher
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize ⇒ Grapher
constructor
A new instance of Grapher.
- #pen_color(x) ⇒ Object
- #pen_scale(x) ⇒ Object
- #populate(name, network, edge_widths = nil) ⇒ Object
- #populate_inputs(num, g) ⇒ Object
- #populate_layer(layer, layer_index, edge_widths, g) ⇒ Object
- #populate_layers(layers, edge_widths, g) ⇒ Object
- #populate_neuron_link(neuron, neuron_index, layer_index, edge_widths, g) ⇒ Object
- #populate_neuron_node(neuron_id, neuron_index, g) ⇒ Object
- #populate_outputs(num_outputs, last_layer, edge_widths, g) ⇒ Object
Constructor Details
#initialize ⇒ Grapher
Returns a new instance of Grapher.
5 6 |
# File 'lib/coo-coo/grapher.rb', line 5 def initialize() end |
Instance Method Details
#pen_color(x) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/coo-coo/grapher.rb', line 75 def pen_color(x) x = pen_scale(x) color = NMatrix[[ 0, 0, 0 ]] if x > 0.01 color = NMatrix[[ 1, 0, 0 ]] elsif x < 0.01 color = NMatrix[[ 0, 0, 1 ]] end '#' + color.to_a.collect { |n| (n.abs * 255).to_i.to_s(16).rjust(2, "0") }.join end |
#pen_scale(x) ⇒ Object
103 104 105 |
# File 'lib/coo-coo/grapher.rb', line 103 def pen_scale(x) x / 10.0 end |
#populate(name, network, edge_widths = nil) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/coo-coo/grapher.rb', line 8 def populate(name, network, edge_widths = nil) Dot::Graph.new(:digraph, :label => name, :ranksep => 3) do |g| populate_inputs(network.num_inputs, g) populate_layers(network.layers, edge_widths, g) populate_outputs(network.num_outputs, network.num_layers - 1, edge_widths, g) end end |
#populate_inputs(num, g) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/coo-coo/grapher.rb', line 22 def populate_inputs(num, g) g.add_subgraph("cluster_inputs", :label => "Inputs", :rank => "same") do |sg| inputs = num.times.collect do |i| "input_#{i}" end sg.add_block("") do |ssg| inputs.each_with_index do |name, i| ssg.add_node(name, :label => i) end ssg.add_edge(inputs, :style => "invis") end end end |
#populate_layer(layer, layer_index, edge_widths, g) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/coo-coo/grapher.rb', line 37 def populate_layer(layer, layer_index, edge_widths, g) g.add_subgraph("cluster_layer_#{layer_index}", :label => "Layer #{layer_index}") do |sg| sg.add_subgraph("layer_#{layer_index}", :rank => "same") do |ssg| nodes = layer.neurons.each_with_index.collect do |n, ni| name = "neuron_#{layer_index}_#{ni}" populate_neuron_node(name, ni, ssg) name end ssg.add_edge(nodes, :style => "invis") end layer.neurons.each_with_index do |n, ni| populate_neuron_link(n, ni, layer_index, edge_widths, sg) end end end |
#populate_layers(layers, edge_widths, g) ⇒ Object
16 17 18 19 20 |
# File 'lib/coo-coo/grapher.rb', line 16 def populate_layers(layers, edge_widths, g) layers.each_with_index do |l, i| populate_layer(l, i, edge_widths && edge_widths[i], g) end end |
#populate_neuron_link(neuron, neuron_index, layer_index, edge_widths, g) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/coo-coo/grapher.rb', line 58 def populate_neuron_link(neuron, neuron_index, layer_index, edge_widths, g) neuron.weights.each_with_index do |w, wi| w = (edge_widths && edge_widths[wi]) || (w / 10.0) #w = w / 10.0 if layer_index == 0 g.add_edge([ "input_#{wi}", "neuron_#{layer_index}_#{neuron_index}" ], :penwidth => pen_scale(w.abs), :color => pen_color(w)) else g.add_edge([ "neuron_#{layer_index - 1}_#{wi}", "neuron_#{layer_index}_#{neuron_index}"], :penwidth => pen_scale(w.abs), :color => pen_color(w)) end end end |
#populate_neuron_node(neuron_id, neuron_index, g) ⇒ Object
54 55 56 |
# File 'lib/coo-coo/grapher.rb', line 54 def populate_neuron_node(neuron_id, neuron_index, g) g.add_node(neuron_id, :label => neuron_index) end |
#populate_outputs(num_outputs, last_layer, edge_widths, g) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/coo-coo/grapher.rb', line 88 def populate_outputs(num_outputs, last_layer, edge_widths, g) g.add_subgraph("cluster_outputs", :label => "Outputs") do |sg| num_outputs.times do |o| sg.add_node("output_#{o}", :label => o) end end num_outputs.times do |o| w = edge_widths && edge_widths[-1][o] || 1.0 g.add_edge([ "neuron_#{last_layer}_#{o}", "output_#{o}" ], :penwidth => pen_scale(w.abs), :pencolor => pen_color(w)) end end |