defmodule ExNeuralNetwork do @moduledoc """ A very simple implementation of a neural network in elixir. Currently it can be used for classification. """ defmacro __using__(_) do quote do alias ExNeuralNetwork.Impl alias ExNeuralNetwork.Server @server ExNeuralNetwork.Server @doc """ get a random inital network depending on the input params and start the genserver with this network and learning rate """ @spec start_link(Integer.t(), [Integer.t()], Integer.t(), Float.t()) :: GenServer.on_start() def start_link(input_nodes, hidden_layers, output_nodes, learning_rate) do network = Impl.get_initial_network(input_nodes, hidden_layers, output_nodes) state = Server.get_inital_state(network, learning_rate) GenServer.start_link(@server, state, name: __MODULE__) end @doc """ takes the given input nodes, sends it through the neural network and return the output nodes """ @spec query([Float.t()]) :: {:ok, [Float.t()]} def query(input) do GenServer.call(__MODULE__, {:query, input}) end @doc """ trains the network with the give input nodes and expected output nodes / target """ @spec train([Float.t()], [Float.t()]) :: :ok def train(input, target) do GenServer.call(__MODULE__, {:train, input, target}) end @doc """ get the trained network """ @spec get_network() :: [[Float.t()]] def get_network() do GenServer.call(__MODULE__, :get_network) end @doc """ set/replace the network """ @spec set_network([[Float.t()]]) :: :ok def set_network(network) do GenServer.call(__MODULE__, {:set_network, network}) end end end end