neuralA module for developing neural networks for machine learning models. This module also provides matrix operations and MNIST dataset reader.
add()argmax()argmin()calculate_error()cross()determinant()dot()get_max_threads()identity()inverse()matrix_random()max()mean()min()mnist_load_image()multiply()multiply_inplace()norm()normalise()ones()prod()scalar_division()scalar_multiply()set_max_threads()squared_norm()substract()sum()transpose()
activation_function
matrix
mnist
neural_network
construct() back_propagation() feed_forward() 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