Page 1 of 1

Lua Neural Network

Posted: Wed May 15, 2019 6:53 pm
by bobbyjones
This semester in college I took a computer vision course. In this course, I was required to write a program that can determine if the silhouette of an image is a dog, cat, bird, or dolphin. This program was required to use a Neural Network. Me being very well versed in Lua decided to implement my assignment in Lua/Löve. I extracted the Lua Neural Network and put it in its own GitHub repository. I am really interested in Neural Networks so I plan to expand on this a great deal, but currently, it is extremely simple. I based this code off of a gist I found online. Attached is a horrible example in use. Hopefully, I will make a better example in the future.

https://github.com/Bobbyjoness/LuaNeuralNet/tree/master

Re: Lua Neural Network

Posted: Wed May 15, 2019 9:32 pm
by dusoft
What's the code doing? I see some line only.

Re: Lua Neural Network

Posted: Wed May 15, 2019 9:38 pm
by bobbyjones
It is training the neural net to classify birds, dogs, cats, and dolphins. The line is the softmax value. Essentially its Accuracy/Epoch.
Edit: I meant its accuracy as y and x is epoch(training cycle) it is a graph.

Re: Lua Neural Network

Posted: Fri May 17, 2019 9:52 am
by caralse
Hi! I wrote this code using your Network but it doesn't work.
What am I missing?

Code: Select all

local NeuralNetwork = require("lnn")
math.randomseed(os.time())
local nn = NeuralNetwork.create(2, 1, 2, 2, 0.3)
local training_data = {
	{
		inputs = {0, 1},
		target = {1}
	},
	{
		inputs = {1, 1},
		target = {0}
	},
	{
		inputs = {1, 0},
		target = {1}
	},
	{
		inputs = {0, 0},
		target = {0}
	}
}

for i = 1, 2000 do
	local data = training_data[math.random(#training_data)]
	nn:backwardPropagate(data.inputs, data.target)
end
print(nn:forwardPropagate({0, 1})[1])
print(nn:forwardPropagate({1, 1})[1])
print(nn:forwardPropagate({0, 0})[1])
print(nn:forwardPropagate({1, 0})[1])
That just prints 1.

Re: Lua Neural Network

Posted: Sat May 18, 2019 6:44 pm
by yintercept
Cool as hell

A guy made something lsimilar back in april, you might enjoy talking with him and sharing notes bobby.


viewtopic.php?f=5&t=85089&p=226763&hilit=neural#p226763

Re: Lua Neural Network

Posted: Sun May 19, 2019 4:33 am
by bobbyjones
caralse wrote:
Fri May 17, 2019 9:52 am
Hi! I wrote this code using your Network but it doesn't work.
What am I missing?

Code: Select all

local NeuralNetwork = require("lnn")
math.randomseed(os.time())
local nn = NeuralNetwork.create(2, 1, 2, 2, 0.3)
local training_data = {
	{
		inputs = {0, 1},
		target = {1}
	},
	{
		inputs = {1, 1},
		target = {0}
	},
	{
		inputs = {1, 0},
		target = {1}
	},
	{
		inputs = {0, 0},
		target = {0}
	}
}

for i = 1, 2000 do
	local data = training_data[math.random(#training_data)]
	nn:backwardPropagate(data.inputs, data.target)
end
print(nn:forwardPropagate({0, 1})[1])
print(nn:forwardPropagate({1, 1})[1])
print(nn:forwardPropagate({0, 0})[1])
print(nn:forwardPropagate({1, 0})[1])
That just prints 1.
I pushed a fix to my repo. The softmax function didn't account for a singular output. Also, you have to up the training cycles to a much higher quantity if you want the neural net to output a 1. You can also play with the learning rate and etc.