## Lua Neural Network

bobbyjones
Party member
Posts: 704
Joined: Sat Apr 26, 2014 7:46 pm

### Lua Neural Network

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
Attachments
example.love
Check out my most recent medium post, please give feedback.

dusoft
Citizen
Posts: 91
Joined: Fri Nov 08, 2013 12:07 am

### Re: Lua Neural Network

What's the code doing? I see some line only.

bobbyjones
Party member
Posts: 704
Joined: Sat Apr 26, 2014 7:46 pm

### Re: Lua Neural Network

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.
Check out my most recent medium post, please give feedback.

caralse
Prole
Posts: 1
Joined: Fri May 17, 2019 9:47 am

### Re: Lua Neural Network

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.

yintercept
Prole
Posts: 38
Joined: Mon Apr 02, 2018 3:31 pm

### Re: Lua Neural Network

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
Just standing here, whistling, with my thumbs in my pockets.

bobbyjones
Party member
Posts: 704
Joined: Sat Apr 26, 2014 7:46 pm

### Re: Lua Neural Network

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.
Check out my most recent medium post, please give feedback.