public
Description: Simple Neural Network in Erlang (based on http://www.trapexit.org/Erlang_and_Neural_Networks)
Homepage:
Clone URL: git://github.com/schleyfox/erlang_ann.git
erlang_ann / neural_network.erl
100644 53 lines (38 sloc) 1.15 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
% Neural Network Server
% Acts as the gateway to a cluster of perceptrons
-module(neural_network).
-behaviour(gen_server).
 
-export([start_link/0, calculate/1]).
 
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).
 
-record(state, {start_node}).
 
% API
 
start_link() ->
  gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
 
calculate(Value) ->
  gen_server:call(?MODULE, {calculate, Value}, infinity).
 
% gen_server callbacks
 
init([]) ->
  {ok, A} = perceptron:start_link(),
  {ok, B} = perceptron:start_link(),
  perceptron:connect(A,B),
  {ok, #state{start_node=A}}.
 
handle_call({calculate, Value}, _From, #state{start_node=Start} = State) ->
  perceptron:pass(Start, Value),
  receive
    {perceptron_output, _, Output_Value} ->
      {reply, Output_Value, State}
  end;
handle_call(_Request, _From, State) ->
  Reply = ok,
  {reply, Reply, State}.
 
handle_cast(_Msg, State) ->
  {noreply, State}.
 
handle_info(_Info, State) ->
  {noreply, State}.
 
terminate(_Reason, _State) ->
  ok.
 
code_change(_OldVsn, State, _Extra) ->
  {ok, State}.
 
% Internal Functions