## Artificial Neural Networks for lua

SirRanjid
Prole
Posts: 31
Joined: Sun Nov 19, 2017 1:44 pm
Contact:

### Artificial Neural Networks for lua

Hi was being asked to talk a bit more about my implementation of ANN here in lua.

Actually already wrote this but when I tried attching a file via drag&drop(over the ad file button instead of the text entry) so my browser made the tab a download tab for my local file and everything got deleted for my convenience I suppose.

Anyways I got this neural network algorithm to work in lua due to me being to dumb to compile a mainstream-nn-framework for love. No actually it took me months to understand it by myself and youtube while I wanted to do my own. And after months of lazy(when I had time and interest) youtube research and 3 attempts at this I finally got it working and reviewing my own code also grew my understanding of this.

Here are some videos that helped me:

This actually made me understand it but it's German: https://youtu.be/EAtQCut6Qno?t=4m49s

The main problem I had was the chainrule. Most videos presented me the chainrule as concept thats built into the network. And while I thought I got to implement the chainrule it was already included in the formula.
And that the backpropagation is basically the same as the forwardpropagation but reusing the values of the forward pass in the derivative function(here we got the chainrule).

After I built it I broke it again by applying random jitters to all kinds of updates to see what kind of effect it has and just playing around with it.

And here's the .love for you to play around with:
neural_net.love
https://github.com/SirRanjid/lua-nn

PRESS SPACE to pause/unpause.

So the dots on the bottom are vertically one batch of errorvalues for different inputs. They ideally reach the bottom of the screen. Or do nothing when a local minimum is reached. (they are randomly initiated so sometimes they get it, sometimes not for this XOR-datasetlet)

Functionality:

Code: Select all

local nn = require("nn")
local x = 3   --amount of layers
local y = 5   --default amount of neurons per layer
local y_inputs = 2  --nodes of input layer (the original 5 of y become 2 for the first layer, *not* a new layer with 2 as input)
local y_outputs = 1  --nodes of output layer

local activation_func = nn.func.sig
local act_f_drv = nn.func.asig
local lear_rate = 0.02

new_net = nn(x,y,y_inputs,y_outputs,activation_func, act_f_drv, lear_rate) --generate a new_net
new_net:build() --make weights for all connections

local train_pairs = {

{{0,0},{0}},
{{1,0},{1}},
{{0,1},{1}},
{{1,1},{0}},

--{{<inputs>},{<expected_outputs>}},
}
local batches = 10
local batchsize = 10

for I = 1, batches
for J = 1, batchsize do
new_net:smart_train(train_pairs,0.6) --0.6  60% chance of picking a random pair, 40% picking the one that gives least error
--smart_train is experimental and picks a random pair from train_pairs and keeps track of the error
end
end

And here some motivational nerdgasm videoson that topic:

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

### Re: Artificial Neural Networks for lua

made the tab a download tab for my local file and everything got deleted for my convenience I suppose.
Now I feel like an asshole for having asked. Heres hoping you made
backups. Also that image is hilarious.
Anyways I got this neural network algorithm to work in lua due to me being to dumb to compile a mainstream-nn-framework for love

That first link I literally watched a month after it was released, lol. Siraj does a really good job of it too.

If you get a chance, initialize a large hidden layer with
random values, and then attempt to just train the output layer.
Drop out is also an interesting topic to explore on that front.
applying random jitters to all kinds of updates to see what kind of effect it has and just playing around with it.
this is the fun part

And here's the .love for you to play around with:
op friggin delivers
the dots on the bottom are vertically one batch of errorvalues for different inputs.
have you explored generative adversarial networks yet? Might be the next step.

This is really good stuff.
Currently working on something Love2D could really use. Shhh.

SirRanjid
Prole
Posts: 31
Joined: Sun Nov 19, 2017 1:44 pm
Contact:

### Re: Artificial Neural Networks for lua

yintercept wrote:
Wed Apr 11, 2018 8:06 pm

If you get a chance, initialize a large hidden layer with
random values, and then attempt to just train the output layer.
Drop out is also an interesting topic to explore on that front.
Sounds interesting. Dropout was actually something I wanted to implement.
At the moment I got momentum/velocity.
yintercept wrote:
Wed Apr 11, 2018 8:06 pm
this is the fun part
YEEES
yintercept wrote:
Wed Apr 11, 2018 8:06 pm
have you explored generative adversarial networks yet? Might be the next step.

This is really good stuff.
Yeah they are cool but I think a q-learner could be better for love. Actually my next step was linking those networks to automate feeding/backpropagation along multiple chained networks.

Also those long-short-term-memory models sound interesting. (https://www.youtube.com/watch?v=WCUNPb-5EYI ,actually was searching for a video of one of deepminds that guides you through your subway by taking in a photo of the subway-map)

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

### Re: Artificial Neural Networks for lua

Yeah they are cool but I think a q-learner could be better for love.
Oh you'll really enjoy Q learners. You may learn better from video but I found http://mnemstudio.org/path-finding-q-le ... torial.htm this article was *entirely* painless and straightforward.

I've always got lost in this stuff and if you're willing to go outside of neural nets you'll lose days and weeks too. K means is a good place to start for clustering. And then go on to PCA which is a relaxed solution of k-means. And if you go in that direction, when you finish with principle component analysis (I finished k means but never got past PCA), self organizing maps are supposed to be a fun generalization of pca.

One day when I get the time I'm hoping to explore some of the simpler stuff like random forests, utility systems, and other ai more traditional to games. Theres lots of topics worth exploring. Sir, if you expand any more be sure to ping me!
Currently working on something Love2D could really use. Shhh.

SirRanjid
Prole
Posts: 31
Joined: Sun Nov 19, 2017 1:44 pm
Contact:

### Re: Artificial Neural Networks for lua

I bet^^. I think K-means is something I already got. I'm more about trying to make an AI that could really kick some ass and be fun to play against with.
Or imagine a simple zombie ai which is driven by a neural net. For each zombie just apply some small random jitter to the weights and it seems like you got a crowd of zombies that behave differently.

I could imagine using an neural network in combination with k-means to analyze and predict movement of troops/groups for strategy games. A bit like handwriting prediction.
Could also be combined with a GAN I think.

Hmm self organizing maps also sound interesting... (looks like it could also aid a strategy ai or map generation)

Thanks for your input these are definetly interesting topics.

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

### Re: Artificial Neural Networks for lua

> I'm more about trying to make an AI that could really kick some ass

If you want 'fun' AI that is also relatively skilled, if you are fascinated by emergence, you should look into Utility AI and Behavior Trees.

I've done toy examples (on paper) to demonstrate the concept to kids, and both behavior trees and utility ai are fairly easy to grasp.

Always wanted to try capturing the output of a utility ai and transform it into a behavior tree.

If you hook up an NN as the utility function for the utility ai, or just the jitter method you wrote then you could experiment with weight manipulation for the ai in question. Unlike behavior trees, utility has to be recalculated every step so it can be expensive to track a lot of elements (read the articles about the ai in FEAR2 for more), but if there was someway to capture that and 'freeze' the utility values
as a behavior selector then some of these calculations might be mitigated.

I've seen examples that go in this direction, but where the utility ai is a node in the behavior tree, and not the other way around. All and all though, it seems like utility is the 'peanutbutter' to decision and behavior tree 'jelly' and I wish I had the time to explore it more.
Researchers have said the future of AI is through ensemble methods and from what I can tell this is a largely unexplored direction.

Edit: You may also enjoy http://trainsportedgame.no-ip.org/media.php a game about using AI to manage a rail network. Built with love2d I believe.
Currently working on something Love2D could really use. Shhh.

SirRanjid
Prole
Posts: 31
Joined: Sun Nov 19, 2017 1:44 pm
Contact:

### Re: Artificial Neural Networks for lua

I updated it bit to make it color different training pairs differently on the plotter instead of coloring all by their error.
Also added sin and cos functions, some jitter and a new net for eyecandy.
The nn code itself now also outputs the index of the used training-pair.
Attachments
lnn2.love