class

Num::NN::Network(T)

Inherits Reference / Object

A neural network can be defined as a biologically inspired computational model that consists of a network architecture composed of artificial neurons. This structure contains a set of parameters, which can be adjusted to perform specific tasks.

This class is a loose wrapper that primarily provides syntactic sugar around moving data through a network -> forward, and propogating loss <- backwards

Constructors

new(context : Num::Grad::Context(T), **options, &)

Convenience method to allow for creation of a Network with as little code as possible. Taps an instance of a LayerArray in order to allow layers to be added to the network in a block

Examples

Network(Float32).new do
  layer(2, 3, :tanh)
  layer(3, 1, :sigmoid)
end
Source

Instance methods

forward(train : Num::Grad::Variable(T)) : Num::Grad::Variable(T)

Propogates an input through a network, returning the final prediction from the network

Arguments

  • train : Num::Grad::Variable(T) - Training input data

Examples

a = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]].to_tensor
v = ctx.variable(a)
net.forward(v)
Source
layers
Source
loss(output : Num::Grad::Variable(T), target : T)

Uses the Network's loss function to calculate the loss based on the final output from the Network, as well as the target output

Arguments

  • output : Num::Grad::Variable(T) - Prediction by the network
  • target : T - Tensor containing ground truth values

Examples

epochs.times do |epoch|
  y_pred = net.forward(x)
  loss = net.loss(y_pred, y_actual)
end
Source
optimizer

Return the Network's optimizer to allow updating the weights and biases of the network

Examples

epochs.times do |epoch|
  y_pred = net.forward(x)
  loss = net.loss(y_pred, y_actual)
  net.optimizer.update
end
Source