class Num::NN::Network(T)

Overview

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

Defined in:

nn/network.cr

Constructors

Instance Method Summary

Constructor Detail

def self.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

[View source]

Instance Method Detail

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

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

Arguments

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)

[View source]
def layers : Num::NN::NetworkInfo(T) #

[View source]
def 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

Examples

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

[View source]
def 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

[View source]