Making my simple NN yield better results

I’m starting to learn NNs which I’ll need for an AI in my game, so I started with a simple one.
The NN will see if the number is negative or positive, it should return 1 if it’s positive.
After a lot of testing, the best result I could get was 64% - I know this can be so much better.
Here’s the current code:

local module = require(game:GetService("ReplicatedStorage").Modules.KNNLibrary)
local runservice = game:GetService("RunService")

local network = module.createNet(1,3,2,1,"LeakyReLU")
local timesToTrain = 1000000
local timesToTry = 10000
local learningRate = 0.01
local tock = tick()
local testNumber = 1

while true do	
	for i = 1, timesToTrain do
		local randomNumber = math.random(-500, 500)
		local correctAnswer = 1
		if randomNumber < 0 then
			correctAnswer = 0
		module.backwardNet(network, learningRate, {randomNumber}, {correctAnswer})
		if (tick() - tock) >= 0.1 then
			tock = tick()
			print((i/timesToTrain)*100 .. "% trained")

	local wins = 0
	local count = 0

	for i = 1, timesToTry do
		local number = math.random(-500, 500)/1000
		local answer = module.forwardNet(network, {number})[1]
		local shouldBeCorrect = 1
		if number < 0 then
			shouldBeCorrect = 0
		if math.abs(shouldBeCorrect - answer) <= 0.4 then
			wins = wins + 1
			print("Got", answer, "- classed as correct")

		print("Testing, " .. (count/timesToTry)*100 .. "%")	
		count = count + 1
	print("Test number:", testNumber .. "\n" .. (wins/timesToTry)*100 .. "% successful")
	testNumber = testNumber + 1

(yes it’s messy, but it’s just a test right now)

The library I’m using is the KNNLibrary, I recommend you check it out if you haven’t already.

Any help is appriciated.


This is because LeakyReLU does not handle negative numbers well at all. It doesn’t blatantly cut them off like ReLU does, but it is still bad.
For a network of this context, I’d recommend a Tanh activator with the same setup. Tanh gives positive and negative numbers the same weight, just in opposite spectrums. Since Tanh is vulnerable to gradient vanishing, inputs must be scaled carefully, but you already have that.
I’d also recommend not using while true do since that will repeat the training infinitely with no ability to stop it.


It got a whopping 100% success rate (wow), I didn’t think it’d be so game changing.
Here’s the network if you or anyone else wants to use it: