Neural Network in pure PHP

Overview

rn Neural Network in pure PHP - ML Machine Learning - AI Artificial Intelligence

RED NEURONAL

WHAT DO THIS LIBRARY IN PURE PHP OF ARTIFICIAL INTELLIGENCE?:

It is a library for machine learning (deep learning) to learn patterns with your datasets. You can create a neural network structure of the layers you want with the number of neurons you want. Each layer can have a different activation function. Ya can train your data in the network with backpropagation function integrated. The result can be exported in JSON format to take the trained network to any other server. 100% written in PHP (pure PHP). Easy to use on any type of server. You can use it freely and working without install any package more. With the default configuration of your server, this code, with your trained data in local server, can be used in your shared hosting server, for example, as simple as that πŸ˜ƒ

No more easy use is possible. You only need to include a master file .php as... require_once( 'rn.class.php' ); and with very little code begin to train your data. You have an example.php file for test it.

SCREENSHOT:

Screenshot of the neural network written in Pure PHP

  • This screenshot belongs to the result of the example file example.php, executed on a raspberry pi (B + 512MB 700 MHz ARM11). The time spend for the script is less than 2 seconds. The output is: 1) Default network values, 2) Process of learning and backpropagation, 3) Final values of the network learned, 4) Export of the values of the configuration of our neural network in JSON format.

REQUERIMENTS:

  • A minimum (minimum, minimum, minimum requeriments is needed). Tested on:

    • Simple Raspberry pi (B + 512MB 700 MHz ARM11) with Raspbian Lite PHP7.3 (i love this gadgets) 😍

    • VirtualBox Ubuntu Server 20.04.2 LTS (Focal Fossa) with PHP7.4.3

  • Needed 1 hidden layer at least

FILES:

There are 3 basic files:

rn.class.php -> Neural network class. This file is the main file that you need to include in your code. This file includes inside rn_layer.class.php

rn_layer.class.php -> Layer class. This file includes inside rn_node.class.php

rn_node.class.php -> Node/Neuron class

INSTALLATION:

A lot of easy πŸ˜ƒ . It is written in PURE PHP. Only need to include the files. Tested on basic PHP installation

     require_once( 'rn.class.php' );

BASIC USAGE:

  • Define train input items array

      $arrTrainInputItems	= [
         [0, 0],
         [0, 1],
         [1, 0],
         [1, 1]
      ];
    
  • Define desired output values array

      $arrTrainOutputItems 	= [
         [0.1, 0.2],
         [0.3, 0.4],
         [0.5, 0.6],
         [0.7, 0.8]
      ];
    
  • Create neural network object

      $rn = new rn( [3, 1, 2] );  // 3x1x2 = 3 layers. 3 input neurons, hidden layer with 1 neuron, 2 output neurons
    

If you want for example 4 layers (3x12x8x2): 3 input neurons, hidden layer with 12 neurons, hidden layer with 8 neurons, output layer with 2 neurons, simply do:

     $rn = new rn( [3, 12, 8, 2] );
  • Print All Train Input data, Neural Network Output data & Train Desired Data

      $num_sample_data = count($arrTrainInputItems);
    
      echo "Default Values: ".PHP_EOL;
    
      for($i=0;$i<$num_sample_data;$i++){
    
            $rn->EchoOutputValues( $arrTrainInputItems[$i], $arrTrainOutputItems[$i] );
    
      }
    
  • Do learn process:

      $rn->Learn($arrTrainInputItems, $arrTrainOutputItems);
    

RESUME OF METHODS:

  • CREATE NEURAL NETWORK:

$rn = new rn( [ARRAY OF INT] );

Example:

    $rn = new rn( [3, 1, 2] );  // 3x1x2 = 3 layers. 3 input neurons, hidden layer with 1 neuron, 2 output neurons
  • PRINT ALL TRAIN INPUT DATA, NEURAL NETWORK OUTPUT DATA & TRAIN DESIRED DATA:

$rn->EchoOutputValues( $arrTrainInputItems, $arrTrainOutputItems );

Example:

    $rn->EchoOutputValues( $arrTrainInputItems[$i], $arrTrainOutputItems[$i] );
  • PRINT ALL TRAIN INPUT DATA & NEURAL NETWORK OUTPUT:

$rn->EchoOutputValues( $arrTrainInputItems );

This method is the same as previous method, with only 1 parameter. The parameter of DesiredData is optional in this method.

Example:

    $rn->EchoOutputValues( $arrTrainInputItems[$i] );
  • PROCESS OF LEARN:

$rn->Learn([ARRAY OF FLOAT], [ARRAY OF FLOAT], [ARRAY OF FLOAT], [ARRAY OF FLOAT], [ARRAY OF FLOAT], [ARRAY OF FLOAT], INT);

1.- Array of Train Items

2.- Array of Train Desired Outputs

3.- Optionally. Array of Validation Items (if not set, they will be Train Items by default)

4.- Optionally. Array of Validation Desired outputs (if not set, they will be Train Desired Items by default)

5.- Optionally. Array of Test Items (if not set, they will be Validation Items by default)

6.- Optionally. Array of Test Desired outputs (if not set, they will be Validation Desired Items by default)

7.- Optionally. Number of Epochs

Example:

    $rn->Learn($arrTrainInputItems, $arrTrainOutputItems);
  • SET THE NUMBER OF EPOCHS:

$rn->fSet_num_epochs( INT );

Example:

    $rn->fSet_num_epochs( 10000 ); // 10000 Epochs
  • SET THE ACTIVATION FUNCTION FOR ALL OF LAYERS:

$rn->fSet_activation_function( STRING );

Example:

    $rn->fSet_activation_function( 'sigm' ); // ['sigm' | 'tanh' | 'relu'] Default: 'sigm'
  • SET THE ACTIVATION FUNCTION FOR ONE LAYER:

$layer->fSet_activation_function( STRING );

Example:

    $rn->layer[1]->fSet_activation_function( 'sigm' ); // ['sigm' | 'tanh' | 'relu'] Default: 'sigm'
  • SET LEARNING RATE:

$rn->set_alpha( FLOAT );

Example:

        $rn->set_alpha( .5 );
  • GET THE OUPUT VALUE OF NEURAL NETWORK OF ONE OUTPUT NODE:

  • Output node: If we have 2 neurons, we can get the output value for Neuron[0] | Neuron[1]

$rn->run( INT OUTPUT NODE ID, ARRAY OF FLOAT INPUT VALUES );

Example:

        $rn->run( $id_output_node, $arrInputValues );
  • GET THE MEANSQUARE ERROR OF THE MODEL:

$rn->MeanSquareError(ARRAY OF INPUT VALUES, ARRAY OF DESIRED VALUES);

Example:

        $rn->MeanSquareError($arrTrainInputItems, $arrTrainOutputItems);
  • EXPORT THE TRAINED MODEL CONFIGURATION TO A STANDARD JSON STRING:

          echo $rn->exportData2Json();
    
  • IMPORT A TRAINED DATA STRING IN JSON FORMAT TO OUR NEURAL NETWORK CLASS:

$rn->importJson2Data( STRING JSON );

Example:

$JsonDataStr = '{"InaticaNeuralNetwork":{"NumInputNeurons":2,"NumOutputNeurons":2,"CreationDate":"2021-07-17 09:36:34","NumTotalLayers":3,"NumHiddenLayers":1,"NumEpochs":1000,"MeanSquareError":0.00010787600008628726,"Layers":[{"Layer":{"num_nodes":2,"imFirst":true,"imLast":false,"activation_function":"sigm","Nodes":[{"Node":{"arr_weights_to_next_layer":[3.131511783516275],"bias":0.5}},{"Node":{"arr_weights_to_next_layer":[2.0871213721523483],"bias":0.5}}]}},{"Layer":{"num_nodes":1,"imFirst":false,"imLast":false,"activation_function":"sigm","Nodes":[{"Node":{"arr_weights_to_next_layer":[3.3833022095458305,3.221540608053369],"bias":-2.4182794115880726}}]}},{"Layer":{"num_nodes":2,"imFirst":false,"imLast":true,"activation_function":"sigm","Nodes":[{"Node":{"arr_weights_to_next_layer":[0.5],"bias":-2.3007544811282843}},{"Node":{"arr_weights_to_next_layer":[0.5],"bias":-1.7218937670832613}}]}}]}}';
  
$JsonData = json_decode( $JsonDataStr );
  
$rn->importJson2Data($JsonData->InaticaNeuralNetwork);
  • INFORM ABOUT THE LEARNING PROCESS

We can to do echoes periodically of the actual neural network process while the Machine is learning, with 2 simple variables of the network class:

$rn->InformEachXBlock

$rn->InformEachXEpoch

If the process of learning is really fast, we can use InformEachXEpoch, for example, for do one echo of the values every 100 Epochs:

        $rn->InformEachXEpoch = 100;

If the process of learning is tooooo slooooow, we can use InformEachXBlock, for example, for do one echo every block of 10 samples learned:

        $rn->InformEachXBlock = 10;

FUTURE PLANS

Machine learning is magic. Artificial intelligence is an exciting world, but deep learning process at Backpropagation algorithm take a lot of time. PHP is not the most efficient programming language for do tasks of deep learning, but it is perhaps the most extensive programming language in the world (and i love it 😍 ). The opportunity to train complex models on local machines without need to install almost anything and later implement them on STANDARD production servers (like shared hosting services) without need to configure anything, gives a clear advantage to this programming model and opens up endless possibilities.

1) ADD SOME FEATURES

I have in mind to implement different characteristics to the class, such as MOMENTUM, other activation functions as SOFTMAX, .... among others.

It would be very interesting to add specific functions to speed up programming and its use in convolutional neural networks.

Another interesting feature will be to add to the class the option to save or read the current configuration of the neural network learned data from a file. Currently, it is possible to import and export the configuration of our network using the JSON data format as input or output, but reading and writing these same data into files would speed up many processes in an automated way.

As an extra utility, we also want to prepare the system so that it can obtain the train, desired and evaluation data directly from .CSV files.

ACCELERATE LEARNING SPEED

2) MULTITHREAD & MULTI-PROCESSORS

One solution to improve the speed spend in the process of deep learning is using multi-processor threads (process parallelization)... and YES!!!. PHP can do it natively!!!! 😊

With PHP, parallelization is possible. I will have new code soon for the class with parallelization feature. This future code obligatorily WILL NEED to be executed on GNU/LINUX servers and CLI environtment, but the code for execute learned models can be executed on any type of server with php, as a basic web server, for example (CLI, cgi, windows, linux, web server on shared hosting, ....).

You will need to wait some time.... Why? My life is not only virtual and PHP πŸ˜‰ , but i promise to upload the code as fast as i can. There are many problems that can arise when working with multithreads (system messages between processes, shared memory between them, ...). All this must be controlled correctly by means of semaphores so that some processes do not interfere with others giving system errors ... and as if that were not enough, the temperature of the CPU must be controlled, since the deep learning process is a hard task for the CPU. Have you put your CPU up to 100ΒΊC 🌑️ doing deep learning???? I do.... in case you are curious to know what happens, the server stops immediately πŸ˜… πŸ€¦β€β™‚οΈ

THE GREAT PROJECT

3) DEEP LEARNING SERVER FARM WITH PHP

The last step will be to create a service of Deep Learning Server Farm... but we need to wait. Everything will come in due time. Much work remains to be done before πŸ˜ƒ

Of course. You can use it freely πŸ–– πŸ‘½

By Rafa.

CHANGES

v.1.0.1 2021-07-19

  • New activation function RELU added
  • Changed Learn Method & MeanSquaredError to accept Validation & Test data.
  • Solved 'not 100% good results' on MeanSquaredError. Now the results are 100% Reliable.

@author Rafael Martin Soto

@author {@link http://www.inatica.com/ Inatica}

@since July 2021

@version 1.0.1

@license GNU General Public License v3.0

A LOT OF THANKS TO:

You might also like...
Community-created, unofficial PHP SDK for the Skynet Decentralized Internet Network. siasky.net

Skynet PHP SDK This SDK is a community-created, unofficial SDK in PHP for the Skynet Decentralized Internet Network. It is taken as primarily a port f

Plant Watering Sensor Project for Zigbee Network (based on the Source Code of the DIYRUZ Flower Project  - https://github.com/diyruz/flower).
Plant Watering Sensor Project for Zigbee Network (based on the Source Code of the DIYRUZ Flower Project - https://github.com/diyruz/flower).

Plant-Watering-Sensor-Zigbee Plant Watering Sensor Project for Zigbee Network (based on the Source Code of the DIYRUZ Flower Project

Fresns core library: Cross-platform general-purpose multiple content forms social network service software
Fresns core library: Cross-platform general-purpose multiple content forms social network service software

About Fresns Fresns is a free and open source social network service software, a general-purpose community product designed for cross-platform, and su

Chat over your local network: 127.0.0.1

#Howto: install packages: apache2 (or nginx but I wouldn't prefer it if you're using your local computer) php for ubuntu/debian instance: $ apt instal

Starless Sky is a network protocol for secure identities, providing the use of assymetric identities, public information, end-to-end messaging and smart contracts
Starless Sky is a network protocol for secure identities, providing the use of assymetric identities, public information, end-to-end messaging and smart contracts

Descentralized network protocol providing smart identity over an secure layer. What is the Starless Sky Protocol? Starless Sky is a network protocol f

Xplico is a Network Forensic Analisys Tool NFAT, for Unix and Unix-like operating systems

Xplico is a Network Forensic Analisys Tool NFAT, for Unix and Unix-like operating systems. It uses libpcap, a packet capture and filtering library.

Nagios/Icinga plugin for checking SATEON field network device status

check_sateon Nagios/Icinga plugin for checking SATEON field network device status Max.Fischer [email protected] Tested on CentOS GNU/Linux 6.5 with Ici

The Current US Version of PHP-Nuke Evolution Xtreme v3.0.1b-beta often known as Nuke-Evolution Xtreme. This is a hardened version of PHP-Nuke and is secure and safe. We are currently porting Xtreme over to PHP 8.0.3
The Current US Version of PHP-Nuke Evolution Xtreme v3.0.1b-beta often known as Nuke-Evolution Xtreme. This is a hardened version of PHP-Nuke and is secure and safe. We are currently porting Xtreme over to PHP 8.0.3

2021 Nightly Builds Repository PHP-Nuke Evolution Xtreme Developers TheGhost - Ernest Allen Buffington (Lead Developer) SeaBeast08 - Sebastian Scott B

A sampling profiler for PHP written in PHP, which reads information about running PHP VM from outside of the process.

Reli Reli is a sampling profiler (or a VM state inspector) written in PHP. It can read information about running PHP script from outside of the proces

Comments
  • MULTITHREAD & MULTI-PROCESSORS

    MULTITHREAD & MULTI-PROCESSORS

    One solution to improve the speed spend in the process of deep learning is using multi-processor threads (process parallelization)... and YES. PHP can do it!!!!

    With php, parallelization is possible, then i will have new code soon for the class with parallelization feature. This code will need to be executed on linux servers and CLI environtment, but the code for execute learned models will be remain standar for execute it on any type of standard server as a basic web server with PHP installation, for example (CLI, cgi, windows, linux, web server on shared hosting, ....).

    opened by vivesweb 0
Owner
Rafael Martin Soto
PHP programmer. Passionate about PHP programming and artificial intelligence. Nice projects done with servers and raspberry pi ^_^
Rafael Martin Soto
A pure PHP implementation of the MessagePack serialization format / msgpack.org[PHP]

msgpack.php A pure PHP implementation of the MessagePack serialization format. Features Fully compliant with the latest MessagePack specification, inc

Eugene Leonovich 368 Dec 19, 2022
A pure PHP implementation of the open Language Server Protocol. Provides static code analysis for PHP for any IDE.

A pure PHP implementation of the open Language Server Protocol. Provides static code analysis for PHP for any IDE.

Felix Becker 1.1k Jan 4, 2023
Columnar analytics for PHP - a pure PHP library to read and write simple columnar files in a performant way.

Columnar Analytics (in pure PHP) On GitHub: https://github.com/envoymediagroup/columna About the project What does it do? This library allows you to w

Envoy Media Group 2 Sep 26, 2022
A beautiful, fully open-source, tunneling service - written in pure PHP

Expose A completely open-source ngrok alternative - written in pure PHP. Documentation For installation instructions, in-depth usage and deployment de

Beyond Code 3.9k Jan 7, 2023
A lightweight framework-agnostic library in pure PHP for part-of-speech tagging

N-ai php pos tagger A lightweight framework-agnostic library in pure PHP for part-of-speech tagging. Can be used for chatbots, personal assistants, ke

Giorgio Rey 8 Nov 8, 2022
A pure PHP library for reading and writing presentations documents

Branch Master : Branch Develop : PHPPresentation is a library written in pure PHP that provides a set of classes to write to different presentation fi

PHPOffice 1.2k Jan 2, 2023
APM pure Php package

Pure Php sample project is just a sample project and here is what I usualy do for each new Php project.

AmirHossein Mohammadi 6 Jul 25, 2022
A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package.

Net A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package. Features: No hard dependencies; Favours

Minibase 16 Jun 7, 2022
A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package.

Net A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package. Features: No hard dependencies; Favours

Minibase 16 Jun 7, 2022
A library for create network applications with PHP.

SocketServer A library for create network applications with PHP. Installation. $ composer require thenlabs/socket-server Usage. The below code show a

ThenLabs 3 Apr 11, 2022