Raised This Month: $7 Target: $400
 1% 

Artificial intelligence (neural networks)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
LuKks
Senior Member
Join Date: Dec 2012
Location: Argentina, Santa Fe
Old 09-03-2018 , 22:11   Artificial intelligence (neural networks)
Reply With Quote #1

As developers, we can create neural networks, there are many types of networks and incredible uses.

A neural network is basically a neurons set connected to each other and the form or complexity of connection will vary its type.

In AMXX it seems that there is no API to create a neural network, that is what we are going for.

Let's start with an example and then more advanced explanations.
PHP Code:
#include <amxmodx>

#include neural

public plugin_init() {
    
register_plugin("Neural network example""1.0.0""LuKks");

    
//layers
    
neural_layer(83); //(neurons, inputs) -> hidden/input layer
    //neural_layer(8); //(neurons) -> hidden layer
    //neural_layer(8); //(neurons) -> can add more hidden layers
    
neural_layer(1); //(outputs) -> output layer is the last one
    
    //inside the include, at the beginning, there are maximums setted
    //for example, 6 layers maximum, can modify it

    //this is required if you will use neural_learn or neural_think instead of _raw
    
neural_input_range(00.0255.0); //0 -> first input
    
neural_input_range(10.0255.0); //1 -> second input
    
neural_input_range(20.0255.0); //2 -> third input

    
neural_output_range(00.01.0); //0 -> first output
    //0.0-1.0 is the default but anyway to be clear

    
new Float:rate 0.25//learning rate; depends on the layers, neurons, iterations, etc

    
new Float:outputs[NEURAL_MAX_SIZE];

    
//benchmark();

    //we query to the network if 255, 255 and 255 is light or dark
    
outputs neural_think(Float:{ 255.0255.0255.0 });
    
server_print("255, 255, 255 [1.0] -> %f"outputs[0]); //random value
    
    //we query to the network if 0, 0 and 0 is light or dark
    
outputs neural_think(Float:{ 0.00.00.0 });
    
server_print("0, 0, 0 [0.0] -> %f"outputs[0]); //random value

    
for(new iFloat:mse5001i++) { //iterations
        
mse 0.0;
    
        
//two ways to pass data
        //raw
        
mse += neural_learn_raw(Float:{ 1.00.00.0 }, Float:{ 1.0 }, rate); //255, 0, 0

        //automatic (using the range predefined), between 8% and 27% less efficient
        
mse += neural_learn(Float:{ 0.0255.00.0 }, Float:{ 1.0 }, rate);

        
//must be in range with the min and max defineds
        
mse += neural_learn(Float:{ 0.00.0255.0 }, Float:{ 1.0 }, rate); //light
        
mse += neural_learn(Float:{ 0.00.00.0 }, Float:{ 0.0 }, rate); //dark
        
mse += neural_learn(Float:{ 100.0100.0100.0 }, Float:{ 1.0 }, rate); //light
        
mse += neural_learn(Float:{ 107.0181.0255.0 }, Float:{ 0.0 }, rate); //dark
        
mse += neural_learn(Float:{ 0.053.0105.0 }, Float:{ 0.0 }, rate); //dark
        
mse += neural_learn(Float:{ 150.0150.075.0 }, Float:{ 1.0 }, rate); //light
        
mse += neural_learn(Float:{ 75.075.00.0 }, Float:{ 0.0 }, rate); //dark
        
mse += neural_learn(Float:{ 0.075.075.0 }, Float:{ 0.0 }, rate); //dark
        
mse += neural_learn(Float:{ 150.074.0142.0 }, Float:{ 1.0 }, rate); //light
        
mse += neural_learn(Float:{ 50.050.075.0 }, Float:{ 0.0 }, rate); //dark
        
mse += neural_learn(Float:{ 103.022.094.0 }, Float:{ 0.0 }, rate); //dark
    
        
mse /= 13//simple average of the errors in each learning
        
        //don't really need to get the mse (medium square error)
        //can teach him no matter the error

        
if(mse 0.01) { //stop learn when mean squared error reach this limit
            
server_print("mse threshold on iter %d"i);
            break;
        }

        if(!(
1000)) {
            
server_print("iter %d, mse %f"imse);
        }
    }

    
//a simple network can't learn any pattern, only linear ones
    //a multilayer network can solve non linear patterns!
    //OR is linear and XOR isn't -> https://vgy.me/dSbEu0.png
    
    //two ways to think data
    
outputs neural_think_raw(Float:{ 0.9520.7010.039 }); //raw (243, 179, 10)
    
server_print("243, 179, 10 [1.0] -> %f"outputs[0]); //0.999930

    
outputs neural_think(Float:{ 75.050.050.0 }); //automatic (using the range predefined)
    
server_print("75, 50, 50 [0.0] -> %f"outputs[0]); //0.022316
    
    
outputs neural_think(Float:{ 95.099.0104.0 });
    
server_print("95, 99, 104 [1.0] -> %f"outputs[0]); //0.961524
    
    
outputs neural_think(Float:{ 65.038.070.0 });
    
server_print("65, 38, 70 [0.0] -> %f"outputs[0]); //0.018278

    //if have a lot outputs, you can ->
    /*for(new i; i < layers[layers_id - 1][N_max_neurons]; i++) {
        server_print("output %d -> %f", i, outputs[i]);
    }*/

    //also can use
    //neural_save("zombie-npc");
    //neural_load("zombie-npc");

    //after load, you can't create more layers (neural_layer) and nothing involved to config
    //if have a neural created and you load a new neural, it will be overwritted, so you can do it
}

public 
benchmark() {
    new 
time_starttime_end;

    
//raw
    
time(__time_start);
    for(new 
i200000i++) { //this takes 7s
        //then this takes ~0.000035s (1/3 of a millisecond)
        //but my cpu is extremely low: AMD A-10 7860K
        
neural_think_raw(Float:{ 1.01.01.0 });
    }
    
time(__time_end);
    
    
server_print("neural_think_raw = %d -> %d"time_starttime_end); //33 -> 40 (7s)

    //automatic
    
time(__time_start);
    for(new 
i200000i++) { //this takes 9s
        //then this takes ~0.000045s (less than half millisecond)
        
neural_think(Float:{ 255.0255.0255.0 });
    }
    
time(__time_end);
    
    
server_print("neural_think = %d -> %d"time_starttime_end); //24 -> 33 (9s)
    
    //with this results I'm able to make real time thinking without problem

At first the network did not know that 255, 255 and 255 was a light color (actually totally white).
Then we show him some color patterns, so that way he was able to handle colors that we did not teach him.

By the way, the outputs at the end of learning, do not get to be exact. It has his explanation.

But first, what is a layer?
Spoiler


Remember what an NPC is? Non-Player Character
Spoiler


This is just a beginning, as I said before, there are many types of neural networks. https://i.imgur.com/fJ0Rs4R.png
This type (deep feed forward) is the one that I used more, for other types I have always helped myself from libraries or systems already done because for me it is not worth learning and re-creating so much functionality.
The current systems always served me as learning and to get greater reasoning.

A neural network claims to be equal to or better than a human.
A neural network will not distracted, doesn't rest, doesn't look the other way, etc.
A network has a margin of error (it's not probability, that is different).
How does the margin of error works? Well, you saw it right at the beginning with the simple network (and again with multi-layer).
Spoiler


Why is not there a neural network that simulates a human brain?
Spoiler


How do neural networks learn?
Spoiler


Same learning, different weights?
Spoiler


Why is it difficult to handle so many neurons?
Spoiler


As I said at the beginning, there are many types of networks. For example, a multi-layer perceptron neural network with back-propagation can handle patterns, image recognition and surely many other uses, probably for translation of languages ​​according to my memory.
Surely driving a car can also, is that there are so many types of networks that sometimes there is a better solution but it is understood.

Learn about neural networks (Google, YouTube, etc):
Spoiler


Regarding the examples, you can always make a comment and ask me.
If it's a problem that you are having with a plugin maybe you should create a separate publication (send me a private message with the link to the post because I don't check the new ones, thank you) but if it is a specific question to the code or the topic, I think you could directly comment it so the rest see your question as well.

Requires fvault.inc because neural_save/load, download -> https://forums.alliedmods.net/attach...5&d=1297052495

Changelog
Spoiler


GitHub: https://github.com/LuKks/neural-amxx
Attached Files
File Type: inc neural.inc (16.1 KB, 300 views)

Last edited by LuKks; 05-30-2019 at 19:11.
LuKks is offline
SpawnerF
Member
Join Date: Apr 2017
Location: Morocco
Old 11-23-2018 , 12:59   Re: Artificial intelligence (neural networks)
Reply With Quote #2

Intresting subject, thank's.

Last edited by SpawnerF; 11-23-2018 at 12:59.
SpawnerF is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 11-23-2018 , 21:14   Re: Artificial intelligence (neural networks)
Reply With Quote #3

Cutting edge technology.

You should create a plugin to demonstrate this. While it is interesting, it will get buried quickly since many will not know what to do with it. You got lucky and SpawnerF saved it.
__________________

Last edited by Bugsy; 11-23-2018 at 21:18.
Bugsy is offline
Celena Luna
Veteran Member
Join Date: Aug 2013
Location: Nagazora
Old 11-26-2018 , 01:04   Re: Artificial intelligence (neural networks)
Reply With Quote #4

This is interesting but also quite compilcated.

The game is too old now so the current active player is not as many so not many scripter would try to tackle on this imo.
__________________
My plugin:

Last edited by Celena Luna; 11-26-2018 at 01:05.
Celena Luna is offline
Neeeeeeeeeel.-
Some Guy Yellin'
Join Date: Jul 2010
Location: Argentina
Old 11-26-2018 , 09:47   Re: Artificial intelligence (neural networks)
Reply With Quote #5

Interesting topic. I agree with Bugsy that you should create a demostration plugin or something useful to show it's potential and motivate devs to use it.
__________________
Neeeeeeeeeel.- is offline
Send a message via Skype™ to Neeeeeeeeeel.-
JocAnis
Veteran Member
Join Date: Jun 2010
Old 11-26-2018 , 17:01   Re: Artificial intelligence (neural networks)
Reply With Quote #6

i didnt undestand very well this one...any practical example (hopefully not for npc) is really welcomed
__________________
KZ Public Autocup - PrimeKZ

My blog: http://primekz.xyz (in progress...) - not active (dec 2022)
JocAnis is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 11-27-2018 , 04:24   Re: Artificial intelligence (neural networks)
Reply With Quote #7

Quote:
Originally Posted by JocAnis View Post
i didnt undestand very well this one...any practical example (hopefully not for npc) is really welcomed
Don't bother, it's a really advanced topic and requires a ton of math to fully understand.
__________________
HamletEagle is offline
LuKks
Senior Member
Join Date: Dec 2012
Location: Argentina, Santa Fe
Old 03-09-2019 , 01:36   Re: Artificial intelligence (neural networks)
Reply With Quote #8

Updated.

Last changes
- Example changed to a more practical and understandable one.
- Added some functions to easy usage: neural_values, neural_learn_values and neural_think_values.
- Optimization correction but nothing remarkable.
- Removed comments about neural_simple since it's not very useful but learning so I keep the include attached.

Thank you for the feedback.
LuKks is offline
iNvectus
Member
Join Date: Sep 2014
Location: Bulgaria
Old 03-13-2019 , 04:46   Re: Artificial intelligence (neural networks)
Reply With Quote #9

Ouch! Some serious topic here. Great job on creating the library. Currently exploring it, alongside doing course for neural networks, soon will do something with it!

Greetings,
iNvectus!
iNvectus is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-17-2019 , 10:48   Re: Artificial intelligence (neural networks)
Reply With Quote #10

Showing a real-world purpose for this in a sample plugin would be great.
__________________
Bugsy is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:54.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode