Multi Layer Perceptron

Multi layer perceptron module:

Code example
local Perceptron = require(game:GetService("ReplicatedStorage").Perceptron)

local training_data = {
	{inputs = {0, 0, 0, 0, 0, 0}, expected_output = {0, 0, 0, 0, 0, 0}},
	{inputs = {0, 0, 0, 0, 0, 1}, expected_output = {0, 0, 0, 0, 0, 1}},
	{inputs = {0, 0, 0, 0, 1, 0}, expected_output = {0, 0, 0, 0, 1, 0}},
	{inputs = {0, 0, 0, 1, 0, 0}, expected_output = {0, 0, 0, 1, 0, 0}},
	{inputs = {0, 1, 0, 0, 0, 0}, expected_output = {0, 0, 1, 0, 0, 0}},
	{inputs = {1, 1, 1, 1, 1, 1}, expected_output = {1, 1, 1, 1, 1, 1}},
	{inputs = {1, 0, 0, 0, 0, 0}, expected_output = {1, 0, 0, 0, 0, 0}},
	{inputs = {0, 0, 1, 0, 0, 0}, expected_output = {0, 1, 0, 0, 0, 0}},
	{inputs = {0, 0, 0, 0, 1, 1}, expected_output = {0, 0, 0, 0, 1, 1}},
	{inputs = {1, 0, 1, 0, 0, 0}, expected_output = {1, 1, 0, 0, 0, 0}},
	{inputs = {0, 1, 1, 0, 0, 0}, expected_output = {0, 1, 1, 0, 0, 0}},
	{inputs = {0, 0, 0, 1, 1, 0}, expected_output = {0, 0, 1, 1, 0, 0}},
	{inputs = {1, 1, 0, 0, 0, 0}, expected_output = {1, 1, 0, 0, 0, 0}},
	{inputs = {1, 0, 0, 1, 0, 0}, expected_output = {1, 0, 1, 0, 0, 0}},
	{inputs = {0, 1, 0, 1, 0, 0}, expected_output = {0, 1, 1, 0, 0, 0}},
	{inputs = {1, 1, 1, 0, 0, 0}, expected_output = {1, 1, 1, 0, 0, 0}},
	{inputs = {1, 0, 1, 1, 0, 0}, expected_output = {1, 1, 1, 1, 0, 0}},
	{inputs = {0, 0, 1, 1, 1, 0}, expected_output = {0, 1, 1, 1, 0, 0}},
	{inputs = {1, 1, 0, 1, 0, 0}, expected_output = {1, 1, 1, 0, 0, 0}},
	{inputs = {0, 1, 1, 1, 1, 0}, expected_output = {0, 1, 1, 1, 1, 0}},
	{inputs = {1, 0, 0, 0, 1, 1}, expected_output = {1, 0, 0, 1, 1, 0}},
	{inputs = {0, 0, 0, 1, 0, 1}, expected_output = {0, 0, 1, 0, 1, 0}},
	{inputs = {1, 1, 1, 0, 1, 1}, expected_output = {1, 1, 1, 1, 1, 0}},
	{inputs = {0, 1, 0, 0, 1, 1}, expected_output = {0, 1, 1, 0, 1, 0}},
	{inputs = {1, 0, 1, 0, 1, 1}, expected_output = {1, 1, 1, 1, 1, 0}},
	{inputs = {0, 1, 1, 0, 1, 0}, expected_output = {0, 1, 1, 1, 0, 0}},
	{inputs = {1, 1, 0, 0, 1, 1}, expected_output = {1, 1, 1, 1, 1, 0}},
	{inputs = {1, 0, 0, 1, 1, 0}, expected_output = {1, 1, 1, 0, 1, 0}},
	{inputs = {0, 1, 0, 1, 1, 1}, expected_output = {0, 1, 1, 1, 1, 0}},
	{inputs = {1, 0, 1, 1, 1, 1}, expected_output = {1, 1, 1, 1, 1, 1}},
	{inputs = {0, 0, 1, 0, 0, 1}, expected_output = {0, 1, 0, 0, 1, 0}},
	{inputs = {1, 1, 1, 1, 0, 0}, expected_output = {1, 1, 1, 1, 0, 0}},
	{inputs = {1, 0, 1, 0, 0, 1}, expected_output = {1, 1, 0, 0, 1, 0}},
}
local perceptron = Perceptron.new(6, {6}, 0.1)

print("started")

-- Training
for epoch = 1, 7000 do
	for _, data in ipairs(training_data) do
		perceptron:train(data.inputs, data.expected_output)
	end
end

print("finished training!")

--Testing(assume that less than 0.5 is 0 and more is 1, 
--i am too lazy to write code for thingy)
for _, data in ipairs(training_data) do
	local predicted = perceptron:query(data.inputs)
	print(predicted)
end

(7k epoch)

1 Like

What is this again???

1 Like

β€œAgain”? Did you ever know what a perceptron is?

1 Like