neuralA module for developing neural networks for machine learning models. This module also provides complex number arithmetic and matrix operations. To help you get started quickly, it also includes a built-in utility for loading and processing the MNIST dataset.
activation — A default instance of class activation_function
add()adjoint()argmax()argmin()calculate_error()complex_abs()complex_acos()complex_acosh()complex_add()complex_arg()complex_asin()complex_asinh()complex_atan()complex_atanh()complex_conjugate()complex_cos()complex_cosh()complex_division()complex_exp()complex_log()complex_log10()complex_multiply()complex_norm()complex_polar()complex_pow()complex_sin()complex_sinh()complex_sqrt()complex_substract()complex_tan()complex_tanh()conjugate()cross()determinant()dot()get_max_threads()identity()inverse()kronecker()matrix_random()max()mean()min()mnist_load_image()multiply()multiply_inplace()norm()normalise()ones()powm()prod()scalar_division()scalar_multiply()set_max_threads()sqrtm()squared_norm()substract()sum()trace()transpose()
activation_function — This class provides activation functions for the neural network layers. Note: This class has no instantiation (constructor) method, please use constant activation instead
complex
matrix
mnist
neural_network — A multi-layer neural network class. This class consists of input layer, hidden layers, and output layer
construct() — The constructor. Create an instance of class neural_network back_propagation() — Do a backward propagation process feed_forward() — Move forward the input matrix through the model's layers get_layer_biases() get_layer_outputs() get_layer_weights() load()
import neural
start
' Construct the model with 784 inputs, 2 hidden layers, 10 outputs, and learning rate: 0.01
model = neural_network([784, 20, 10, 10], 0.01)
' Load the MNIST dataset
dataset_train = mnist("dataset/train-images.idx3-ubyte", "dataset/train-labels.idx1-ubyte")
dataset_test = mnist("dataset/t10k-images.idx3-ubyte", "dataset/t10k-labels.idx1-ubyte")
' Train the model with epochs: 5
train(model, dataset_train, 5)
' Test the model and calculate its accuracy
test(model, dataset_test)
stop
function train(model, dataset, epochs = 3)
writeln("Total training data: " & dataset.size())
for i, 1, epochs
writeln("Training... Epoch: " & i & " of " & epochs)
for j, 0, dataset.size() - 1
expected = dataset.outputs(j)
model.feed_forward(dataset.inputs(j))
model.back_propagation(expected)
error = calculate_error(model.outputs(), expected)
endfor
endfor
stop
function test(model, dataset)
total_errors = 0
writeln("Testing")
for i, 0, dataset.size() - 1
input = dataset.inputs(i)
expected = dataset.outputs(i)
model.feed_forward(input)
outputs = model.outputs()
predicted = argmax(outputs)
expc = argmax(expected)
err = calculate_error(outputs, expected)
if predicted != expc; total_errors += 1; endif
endfor
writeln("Total errors: " & total_errors & " - Total data: " & dataset.size() & " - Accuracy: " & (((dataset.size() - total_errors) / dataset.size()) * 100) & "%")
stop
This module is only available on Dinfio version 3.2.0 or later