Raised This Month: $106 Target: $400

Artificial intelligence (neural networks)

Post New Thread Reply   
Thread Tools Display Modes
Author Message
Senior Member
Join Date: Dec 2012
Location: Argentina, Santa Fe
Old 09-03-2018 , 23: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. Also I replicated a version in JavaScript.

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

#include neural_simple

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

neural neural_create(2); //crea la red con 2 inputs (entradas)
    //will be a random result at the beginning
server_print("2+2 -> %f"neural_think(neuralFloat:{ 2.02.0 }) * 100);

i1000i++) { //1.000 repetitions
        //Float:{ input1, input2 }, output
        //we teach him some basic sums
neural_learn(neuralFloat:{ 1.01.0 }, 0.02);
neural_learn(neuralFloat:{ 1.00.0 }, 0.01);
neural_learn(neuralFloat:{ 0.01.0 }, 0.01);
//a network has to know that 1 + 0 = 1
        //and 0 + 1 = 1, since they are two different operations

        //the results are in decimals, 0.04 means 4
neural_learn(neuralFloat:{ 2.02.0 }, 0.04);
//more examples = more precise learning

        //internally, the network is managed with 0-1

        /*neural_learn(neural, Float:{ 2.0, 1.0 }, 0.03); 
        neural_learn(neural, Float:{ 1.0, 2.0 }, 0.03);
        neural_learn(neural, Float:{ 0.0, 2.0 }, 0.02);*/

//    1.000 | 50.000 (repetitions)
    // 19.014957 | 18.999979
server_print("15+4 -> %f"neural_think(neuralFloat:{ 15.04.0 }) * 100);
//we handle the outputs in numbers like 0.04, then multiply by 100

    // -3.950722 | -3.999993
server_print("-9+5 -> %f"neural_think(neuralFloat:{ -9.05.0 }) * 100);
// 79.519 | 79.999908
server_print("38+42 -> %f"neural_think(neuralFloat:{ 38.042.0 }) * 100); 

At first the network did not know how much it was 2 + 2 but after a little learning with some simple sums we teach the addition pattern, that way you can then handle sums that we do not teach you.
This exercise is very easy for the network because it does not present a difficult pattern.

You can invent a simple pattern and teach it to the network.

You can try to teach him something else, for example:

But first, what is a layer?

Remember what an NPC is? Non-Player Character

Let's see an example for the multi layer.
PHP Code:
#include <amxmodx>

#include neural_multi

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

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

for(new iFloat:mse5000i++) { //5.000 limit iterations
mse += neural_learn(Float:{ 0.00.0 }, Float:{ 0.0 }, 0.45);
mse += neural_learn(Float:{ 1.00.0 }, Float:{ 1.0 }, 0.45);
mse += neural_learn(Float:{ 0.01.0 }, Float:{ 1.0 }, 0.45);
mse += neural_learn(Float:{ 1.01.0 }, Float:{ 0.0 }, 0.45);
mse /= 4//simple average of the errors in each learning
        //0.45 is the learning rate
        //don't really need to get the mse (medium square error)
        //can teach him no matter the error

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

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

//in theory, a simple network can't learn this pattern
    //because technically it's not a linear problem
    //a multilayer network can solve non linear patterns!
    //OR is linear and XOR isn't -> https://vgy.me/dSbEu0.png

new Float:outputs[NEURAL_MAX_OUTPUTS];
outputs neural_think(Float:{ 0.00.0 });
server_print("0.0 [0.0] -> %f"outputs[0]); //0.014664

outputs neural_think(Float:{ 1.00.0 });
server_print("1-0 [1.0] -> %f"outputs[0]); //0.992533

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

    //also, can use

    //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

This is just a beginning, as I said before, there are many types of neural networks.
These two are the ones that I have used the most, 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).

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

How do neural networks learn?

Same learning, different weights?

Why is it difficult to handle so many neurons?

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):

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.

Note: I was not allowed to upload .js, so I called it neural_javascript.sma for the JavaScript one. I did it similar to amxx, I know that the js can be improved a lot but it was not the point.

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

Attached Files
File Type: inc neural_simple.inc (1.1 KB, 44 views)
File Type: inc neural_multi.inc (7.7 KB, 53 views)
File Type: sma Get Plugin or Get Source (neural_javascript.sma - 123 views - 3.3 KB)
LuKks is offline
Join Date: Apr 2017
Location: Morocco
Old 11-23-2018 , 13:59   Re: Artificial intelligence (neural networks)
Reply With Quote #2

Intresting subject, thank's.

Last edited by SpawnerF; 11-23-2018 at 13:59.
SpawnerF is offline
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 11-23-2018 , 22: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 22:18.
Bugsy is offline
Celena Luna
Senior Member
Join Date: Aug 2013
Location: Okinawa, Japan
Old 11-26-2018 , 02: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 02:05.
Celena Luna is offline
Some Guy Yellin'
Join Date: Jul 2010
Location: Argentina
Old 11-26-2018 , 10: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.-
Senior Member
Join Date: Jun 2010
Old 11-26-2018 , 18: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
DR Timer (selling) - Top15 images
JocAnis is offline
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 11-27-2018 , 05:24   Re: Artificial intelligence (neural networks)
Reply With Quote #7

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

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 01:09.

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