Current section
Files
Jump to
Current section
Files
lib/ex_neural_network/server.ex
defmodule ExNeuralNetwork.Server do
use GenServer
alias ExNeuralNetwork.Impl
def init(state) do
{:ok, state}
end
def get_inital_state(network, learning_rate) do
%{
learning_rate: learning_rate,
network: network
}
end
def handle_call({:query, input}, _from, state) do
{:reply, Impl.query(state.network, input), state}
end
def handle_call(:get_network, _from, state) do
{:reply, Impl.network_to_lists(state.network), state}
end
def handle_call({:train, input, target}, _from, state) do
trained_network = Impl.train(state.network, input, target, state.learning_rate)
new_state = update_network(state, trained_network)
{:reply, :ok, new_state}
end
def handle_cast({:set_network, network}, state) do
new_state = update_network(state, Impl.lists_to_network(network))
{:noreply, new_state}
end
defp update_network(state, network) do
state |> Map.put(:network, network)
end
end