schleyfox / erlang_ann

Simple Neural Network in Erlang (based on http://www.trapexit.org/Erlang_and_Neural_Networks)

erlang_ann / ann_graph.erl
100644 66 lines (57 sloc) 2.149 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
-module(ann_graph).
-export([grapher/2, output/2, start/0]).
 
start() ->
  register(ann_grapher, spawn(fun() -> grapher(0, []) end)).
 
grapher(Frame, Connections) ->
  receive
    {connect, From, To} ->
      New_connections = [{connection, From, To}|Connections],
      output(Frame, New_connections),
      grapher(Frame + 1, New_connections);
    {stimulate, From, To, Value} ->
      Stim_connections =
        stimulate_connection(From, To, Value, Connections),
      output(Frame, Stim_connections),
      grapher(Frame + 1, Connections);
    {output, From, Value} ->
      Output_connections = [{output, From, Value}|Connections],
      output(Frame, Output_connections),
      grapher(Frame + 1, Connections);
    {backprop, From, To, Value} ->
      Learning_connections = [{backpropagation, From, To, Value}|Connections],
      output(Frame, Learning_connections),
      grapher(Frame + 1, Connections)
      
  end.
 
output(Frame, Connections) ->
  file:write_file(sprintf("graph/~3.10.0B.dot", [Frame]),
    lists:concat([
      sprintf("digraph ann~w~n{~n", [Frame]) |
        lists:map(fun(Conn) ->
          case Conn of
          {connection, From, To} ->
            sprintf(" \"~w\" -> \"~w\";~n", [From, To]);
          {stimulation, From, To, Value} ->
            sprintf(" \"~w\" -> \"~w\" [label=\"~w\" color=red];~n",
              [From, To, Value]);
          {output, From, Value} ->
            sprintf(" \"~w\" -> \"Output\" [label=\"~w\" color=green];~n\"Output\" [shape=box color=green]", [From, Value]);
          {backpropagation, From, To, Value} ->
            sprintf(" \"~w\" -> \"~w\" [label=\"~w\" color=blue];~n",
            [From, To, Value])
 
          end
          end,
          lists:reverse(Connections))] ++
      ["}"])).
 
stimulate_connection(From, To, Value, Connections) ->
  replace_elem({connection, From, To},
               {stimulation, From, To, Value},
               Connections).
 
replace_elem(Old, New, List) ->
  lists:map(fun(Elem) ->
      case Elem of
        Old -> New;
        _ -> Elem
      end
    end, List).
 
sprintf(Format, Args) ->
  io_lib:format(Format, Args).