Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Artificial Neural Network in a Tic Tac Toe Symfony Console Application - SymfonyCon 2014

2,667 views

Published on

Among all the C libraries bindings PHP offers, there is one for FANN: Fast Artificial Neural Network (libfann). With it you can easily create a neural Network with different activation functions for each neuron/ layer. ANNs (Artificial Neural Networks) are used for machine learning and for recommendation systems. In this Talk we will show an implementation (and running demo) of a basic IA that will learn to play Tic Tac Toe leveraging the Symfony console componente as UI While we demo, we will show a detailed log of what is going on.
SymfonyCon Madrid 2014

Published in: Technology

Artificial Neural Network in a Tic Tac Toe Symfony Console Application - SymfonyCon 2014

  1. 1. with PHP on a Symfony Console TicTacToe game Artificial Neural Networks
  2. 2. ANN with PHP SymfonyCon Madrid 2014 Agenda 1. Demo 2. Symfony Console 3. Symfony Console Helpers 4. ANN Theory 6. PHP + FANN 7. Show me the code 8. Demo 9. Q&A
  3. 3. ANN with PHP SymfonyCon Madrid 2014 Demo vs.
  4. 4. ANN with PHP SymfonyCon Madrid 2014 Symfony Console
  5. 5. ANN with PHP SymfonyCon Madrid 2014 Symfony Console - Installation bin/console #!/usr/bin/env php <?php require __DIR__ . ‘/../vendor/autoload.php'; use SymfonyComponentConsoleApplication; $app = new Application(); $app->run(); composer.json { ... "require": { "symfony/console": "~2.5" } ... }
  6. 6. ANN with PHP SymfonyCon Madrid 2014 Symfony Console - Installation bin/console #!/usr/bin/env php <?php require __DIR__ . ‘/../vendor/autoload.php'; use SymfonyComponentConsoleApplication; $app = new Application(); $app->run(); composer.json { ... "require": { "symfony/console": "~2.5" } ... } $ php bin/console $ chmod +x bin/console $ bin/console
  7. 7. ANN with PHP SymfonyCon Madrid 2014
  8. 8. ANN with PHP SymfonyCon Madrid 2014 Symfony console style guide https://github.com/symfony/symfony-docs/issues/4265 Created by @javiereguiluz Improved by Symfony community
  9. 9. ANN with PHP SymfonyCon Madrid 2014 Symfony Console Helpers Progress bar Table Question Formatter
  10. 10. ANN with PHP SymfonyCon Madrid 2014 Command.php Progress bar <?php // create a new progress bar (10 units) $progress = new ProgressBar($output, 10); // start and displays the progress bar $progress->start(); $i = 0; while ($i++ < 10) { // ... do some work // advance the progress bar 1 unit $progress->advance(); } // ensure that the progress bar is at 100% $progress->finish();
  11. 11. ANN with PHP SymfonyCon Madrid 2014 We all techies love progress bars
  12. 12. ANN with PHP SymfonyCon Madrid 2014 Table Command.php <?php $table = new Table($output); $table ->setHeaders(array('Component', 'Package')) ->setRows(array( array('Console', 'symfony/console'), array('Form', 'symfony/form'), array('Finder', 'symfony/finder'), array('Config', 'symfony/config'), array('...', '...'), )) ; $table->render();
  13. 13. ANN with PHP SymfonyCon Madrid 2014 Question Command.php <?php $helper = $this->getHelper('question'); $question = new ConfirmationQuestion( 'Dou you want to play again? ', false ); if (!$helper->ask($input, $output, $question)) { $output->writeln("Bye bye!"); return; } $output->writeln("Let's play again!");
  14. 14. ANN with PHP SymfonyCon Madrid 2014 Formatter Command.php <?php $formatter = $this->getHelper('formatter'); $formattedLine = $formatter->formatSection( 'Game finished', 'Player one wins.' ); $output->writeln($formattedLine); $errorMessages = array( 'Error!', 'Move already done.’ ); $formattedBlock = $formatter ->formatBlock($errorMessages, 'error'); $output->writeln($formattedBlock);
  15. 15. ANN with PHP SymfonyCon Madrid 2014 Our helper
  16. 16. ANN with PHP SymfonyCon Madrid 2014 Board Command.php <?php // Board example usage $board = new Board( $output, $this->getApplication() ->getTerminalDimensions(), 3, // Board size false // Don’t override the screen ); // Update and display the board status $board->updateGame(0,0,1);
  17. 17. ANN with PHP SymfonyCon Madrid 2014 Board Command.php <?php // Board example - four in a row $board = new BoardHelper( $output, $this->getApplication() ->getTerminalDimensions(), 4, // Board size false // Don’t override screen true // Board with backgrounded cells ); // Update the board status (Player 1) $board->updateGame(0,0,1); // Update the board status (Player 2) $board->updateGame(0,1,2);
  18. 18. ANN with PHP SymfonyCon Madrid 2014 Board Command.php <?php // Example TicTacToe with overwrite $board = new TicTacToeHelper( $output, $this->getApplication() ->getTerminalDimensions(), 3, // Board size true // Overwrite screen ); // Update the board status and display $board->updateGame(1,1,1); $board->updateGame(0,0,2); $board->updateGame(2,0,1); $board->updateGame(0,2,2); $board->updateGame(0,1,1); $board->updateGame(2,1,2); $board->updateGame(1,0,1); $board->updateGame(1,2,2); $board->updateGame(2,2,1);
  19. 19. ANN with PHP SymfonyCon Madrid 2014 Board Board.php <?php namespace PHPGamesConsoleHelper; class Board { /** * @var SymfonyComponentConsoleOutputOutputInterface */ private $output; /** * Clears the output buffer */ private function clear() { $this->output->write("e[2J"); } // ... Instruction that clears the screen
  20. 20. ANN with PHP SymfonyCon Madrid 2014 There is a bonus helper
  21. 21. ANN with PHP SymfonyCon Madrid 2014 Symfony Console - HAL Command.php <?php $hal = new HAL($output); $hal->sayHello();
  22. 22. ANN with PHP SymfonyCon Madrid 2014 Artificial Neural Networks Computer model that intends to simulate how the brain works
  23. 23. ANN with PHP SymfonyCon Madrid 2014 A typical ANN Input neuron Hidden neuron Output neuron Signal & weight
  24. 24. ANN with PHP SymfonyCon Madrid 2014 Activation Functions Functions to process the input and produce a signal as output
  25. 25. ANN with PHP SymfonyCon Madrid 2014 Activation Functions • Step - output is 0 or 1 • Linear Combination - output is input sum plus a linear bias • Continuous Log-Sigmoid
  26. 26. ANN with PHP SymfonyCon Madrid 2014 Activation Functions • Step - output is 0 or 1 • Linear Combination - output is input sum plus a linear bias • Continuous Log-Sigmoid
  27. 27. ANN with PHP SymfonyCon Madrid 2014 Activation Functions • Step - output is 0 or 1 • Linear Combination - output is input sum plus a linear bias • Continuous Log-Sigmoid
  28. 28. ANN with PHP SymfonyCon Madrid 2014 Backpropagation Backward propagation of errors Passes error signals backwards through the network during training to update the weights of the network
  29. 29. ANN with PHP SymfonyCon Madrid 2014 ANN Types
  30. 30. ANN with PHP SymfonyCon Madrid 2014 ANN types • Feedforward neural network Information goes only in one direction, forward. • Radial basis function network (RBF) Interpolation in multidimensional space. • Kohonen self-organizing network A set of artificial neurons learn to map points in an input space to coordinates in an output space.
  31. 31. ANN with PHP SymfonyCon Madrid 2014 ANN types • Feedforward neural network Information goes only in one direction, forward. • Radial basis function network (RBF) Interpolation in multidimensional space. • Kohonen self-organizing network A set of artificial neurons learn to map points in an input space to coordinates in an output space.
  32. 32. ANN with PHP SymfonyCon Madrid 2014 ANN types • Feedforward neural network Information goes only in one direction, forward. • Radial basis function network (RBF) Interpolation in multidimensional space. • Kohonen self-organizing network A set of artificial neurons learn to map points in an input space to coordinates in an output space.
  33. 33. ANN with PHP SymfonyCon Madrid 2014 ANN Learning
  34. 34. ANN with PHP SymfonyCon Madrid 2014 ANN Learning • Supervised • Unsupervised • Reinforcement
  35. 35. ANN with PHP SymfonyCon Madrid 2014 ANN Learning • Supervised • Unsupervised • Reinforcement
  36. 36. ANN with PHP SymfonyCon Madrid 2014 ANN Learning • Supervised • Unsupervised • Reinforcement
  37. 37. ANN with PHP SymfonyCon Madrid 2014 We've used Reinforcement Learning With some slights adaptations to solve
  38. 38. ANN with PHP SymfonyCon Madrid 2014 Temporal Credit Assignment Problem
  39. 39. ANN with PHP SymfonyCon Madrid 2014 (WTF) ANN with What The Fann Artificial Neural Networks with PHP
  40. 40. ANN with PHP SymfonyCon Madrid 2014
  41. 41. ANN with PHP SymfonyCon Madrid 2014 (WTF) ANN with Meet libfann & PECL fann $~> sudo apt-get install libfann; sudo pecl install fann
  42. 42. ANN with PHP SymfonyCon Madrid 2014 (WTF) ANN with  OS X 1. Install autoconf 2. Install cmake 3. Compile FANN 4. Install php fann extension with PECL 5. Add the extension to php.ini $ brew install autoconf
  43. 43. ANN with PHP SymfonyCon Madrid 2014 (WTF) ANN with  OS X 1. Install autoconf 2. Install cmake 3. Compile FANN 4. Install php fann extension with PECL 5. Add the extension to php.ini $ brew install cmake
  44. 44. ANN with PHP SymfonyCon Madrid 2014 (WTF) ANN with  OS X 1. Install autoconf 2. Install cmake 3. Compile FANN 4. Install php fann extension with PECL 5. Add the extension to php.ini $ cd FANN-2.2.X-Source $ cmake . $ sudo make install
  45. 45. ANN with PHP SymfonyCon Madrid 2014 (WTF) ANN with  OS X 1. Install autoconf 2. Install cmake 3. Compile FANN 4. Install php fann extension with PECL 5. Add the extension to php.ini $ sudo pecl install fann
  46. 46. ANN with PHP SymfonyCon Madrid 2014 (WTF) ANN with  OS X 1. Install autoconf 2. Install cmake 3. Compile FANN 4. Install php fann extension with PECL 5. Add the extension to php.ini extension=fann.so
  47. 47. ANN with PHP SymfonyCon Madrid 2014 (WTF) ANN with Enjoy!
  48. 48. ANN with PHP SymfonyCon Madrid 2014 (WTF) ANN with Show me the code!
  49. 49. ANN with PHP SymfonyCon Madrid 2014 Demo vs.
  50. 50. ANN with PHP SymfonyCon Madrid 2014
  51. 51. ANN with PHP SymfonyCon Madrid 2014 The two neurons behind this talk
  52. 52. ANN with PHP SymfonyCon Madrid 2014 Eduardo Gulias @egulias • EmailValidator (Symfony >= 2.5, Drupal 8). • ListenersDebugCommandBundle (ezPublish 5). • PHPMad UG co-founder (Former Symfony Madrid). • Team leader at
  53. 53. ANN with PHP SymfonyCon Madrid 2014 Ariel Ferrandini @aferrandini • Symfony simple password encoder service. (Symfony >=2.6). • Symfony DX application collaborator. • PHPMad UG co-founder. • Team leader at Paradigma Tecnológico
  54. 54. ANN with PHP SymfonyCon Madrid 2014 Resources • Wikipedia (http://en.wikipedia.org/wiki/Artificial_neural_network) • Introduction for beginners (http://arxiv.org/pdf/cs/0308031.pdf) • Introduction to ANN (http://www.theprojectspot.com/tutorial-post/ introduction-to-artificial-neural-networks-part-1/7) • Reinforcement learning (http://www.willamette.edu/~gorr/classes/ cs449/Reinforcement/reinforcement0.html) • PHP FANN (http://www.php.net/fann)
  55. 55. ANN with PHP SymfonyCon Madrid 2014 Resources • Repo PHPGames (https://github.com/phpgames/ANNTicTacToe) • Repo Board helper (https://github.com/phpgames/BoardHelper) • Repo HAL helper (https://github.com/phpgames/HALHelper)
  56. 56. ANN with PHP SymfonyCon Madrid 2014 Thank you! https://joind.in/12951
  57. 57. ANN with PHP SymfonyCon Madrid 2014 Questions? https://joind.in/12951

×