API Reference‎ > ‎

hopfieldnn_t

class hopfieldnn_t

            This is an implementation of a Hopfield Neural Network



Header


Namespace

  • nu


Constructors

  • hopfieldnn_t()default;
    Default constructor. Create a not initialized neural network.
    You can initialize the net later by loading the net status from a string stream.
  • hopfieldnn_t(std::stringstreamss);
    Create a net using data serialized into a given stream
  • hopfieldnn_t(const size_tn_of_inputsnoexcept;
    Create a net with pattern size equal to n_of_inputs
  • hopfieldnn_t(const hopfieldnn_t& nn) = default;
    Copy constructor
  • hopfieldnn_t(hopfieldnn_t&& nn) noexcept;
    Move constructor

Copy Operators

  • hopfieldnn_toperator=(const hopfieldnn_t& nn) = default;
    assignment copy operator
  • hopfieldnn_toperator=(hopfieldnn_t&& nn) noexcept;
    default assignement-move operator

String Stream Operators

  • friend std::stringstreamoperator>>(std::stringstream& ss, hopfieldnn_t& net);
    Build the net by using data of the given string stream
    In case of invalid stream format this method will throw an exception exception_t::invalid_sstream_format
  • friend std::stringstream& operator<<(std::stringstream& ss, hopfieldnn_t& net) noexcept;
    Save net status into the given string stream

Output Stream Operators

  • friend std::ostreamoperator<<(std::ostream& os, hopfieldnn_t& net) noexcept;
    Print the net state out to the given output stream

Public methods

  • void clear() noexcept;
    Reset the net status
  • size_t get_capacity() const noexcept;
    Returns the capacity of the net
  • size_t get_n_of_patterns() const noexcept;
    Returns the number of patterns added to the net
  • size_t get_inputs_count() const noexcept;
    Returns the number of inputs
  • std::stringstreamload(std::stringstream& ss);
    Build the net by using data of the given string stream
    In case of invalid stream format this method will throw an exception exception_t::invalid_sstream_format
  • std::stringstreamsave(std::stringstream& ss) noexcept;
    Save net status into the given string stream
  • std::ostreamdump(std::ostream& os) noexcept;
    Print the net state out to the given ostream
  • void add_pattern(const rvector_t& input_pattern);
    const rvector_t& input_pattern: [in] input pattern

    Adds specified pattern.
    If input_pattern.size() != get_inputs_count(), this methods will throw an exception exception_t::size_mismatch
  • void recall(const rvector_t& input_pattern, rvector_t& output_pattern);
    const rvector_t& input_pattern: [in]     input pattern
    rvector_t& output_pattern:      [out]   output pattern

    Recall a pattern using as key the input one. It must be a vector containing [-1,1] values
    If input_pattern.size() != get_inputs_count(), this methods will throw an exception exception_t::size_mismatch

Example:


/*
 * Hopfield neural network test
 * The Hopfield network is used to solve the recall problem of matching 
 * copy for an input pattern to an associated pre-learned pattern
 */


/* -------------------------------------------------------------------------- */

#include "nu_hopfieldnn.h"
#include <iostream>


/* -------------------------------------------------------------------------- */

const size_t pattern_size = 100;
const size_t n_of_patterns = 5;

std::string g_learning_patterns[] = 
{  // 0123456789
   { "   ***    "   //0
     "  ****    "   //1
     " *****    "   //2
     "   ***    "   //3
     "   ***    "   //4
     "   ***    "   //5
     "   ***    "   //6
     "   ***    "   //7
     " *******  "   //8
     " *******  " },//9

   { "**********"
     "**********"
     "**********"
     "**********"
     "**********"
     "          "
     "          "
     "          "
     "          "
     "          " },

   { "*****     "
     "*****     "
     "*****     "
     "*****     "
     "*****     "
     "     *****"
     "     *****"
     "     *****"
     "     *****"
     "     *****" },

   { "**********"
     "**********"
     "**      **"
     "**      **"
     "**      **"
     "**********"
     "**********"
     "**      **"
     "**      **"
     "**      **" },

   { "**********"
     "*        *"
     "* ****** *"
     "* *    * *"
     "* * ** * *"
     "* * ** * *"
     "* *    * *"
     "* ****** *"
     "*        *"
     "**********" } 
};


std::string g_test_patterns[] =
{  // 0123456789
 { "   ***    "   //0
   "   ***    "   //1
   "   ***    "   //2
   "   ***    "   //3
   "   ***    "   //4
   "   ***    "   //5
   "   ***    "   //6
   "   ***    "   //7
   "   ***    "   //8
   "   ***    " },//9

 { "**********"
   "**********"
   "          "
   "          "
   "          "
   "          "
   "          "
   "          "
   "          "
   "          " },

 { "          "
   "          "
   "*****     "
   "*****     "
   "*****     "
   "     *****"
   "     *****"
   "     *****"
   "          "
   "          " },

 { "**********"
   "*        *"
   "*        *"
   "*        *"
   "*        *"
   "**********"
   "**********"
   "*        *"
   "*        *"
   "*        *" },

 { "**********"
   "*        *"
   "* ****** *"
   "* *    * *"
   "* *    * *"
   "* *    * *"
   "* *    * *"
   "* ****** *"
   "*        *"
   "**********" }
};


/* -------------------------------------------------------------------------- */

static void print_pattern(const std::string& pattern)
{
   std::cout << "+----------+" << std::endl;
   for (int y = 0; y < 10; ++y)
   {
      std::cout << '|';
      for (int x = 0; x < 10; ++x)
         std::cout << pattern[y * 10 + x];
      std::cout << '|';
      std::cout << std::endl;
   }
   std::cout << "+----------+" << std::endl;
   std::cout << std::endl;
}


/* -------------------------------------------------------------------------- */

static void print_pattern(const nu::hopfieldnn_t::rvector_t& pattern)
{
   std::string s_pattern;
   for (int y = 0; y < 10; ++y)
      for (int x = 0; x < 10; ++x)
         s_pattern += (pattern[y * 10 + x] == 1.0 ? '*' : ' ');

   print_pattern(s_pattern);
}


/* -------------------------------------------------------------------------- */

static void convert_pattern_into_input_v(
   const std::string& pattern,
   nu::hopfieldnn_t::rvector_t & input_vector)
{
   size_t i = 0;
   for (auto c : pattern)
      input_vector[i++] = c == '*' ? 1 : -1;
}



/* -------------------------------------------------------------------------- */

int main(int argc, char* argv[])
{
   nu::hopfieldnn_t net(pattern_size);

   std::cout << "LEARNING THE FOLLOWING IMAGES:" << std::endl;

   for (int i = 0; i < n_of_patterns; ++i)
   {
      nu::hopfieldnn_t::rvector_t input_v(pattern_size);
      convert_pattern_into_input_v(g_learning_patterns[i], input_v);
      net.add_pattern(input_v);
      print_pattern(g_learning_patterns[i]);
   }

   // Test the net
   for (int i = 0; i < n_of_patterns; ++i)
   {
      nu::hopfieldnn_t::rvector_t input_v(pattern_size);
      nu::hopfieldnn_t::rvector_t output_pattern(pattern_size);

      convert_pattern_into_input_v(g_test_patterns[i], input_v);
      net.recall(input_v, output_pattern);

      std::cout << std::endl << std::endl;
      std::cout << std::endl << " THIS IMAGE" << std::endl;

      print_pattern(g_test_patterns[i]);

      std::cout << std::endl << "  RECALLS" << std::endl;

      print_pattern(output_pattern);
   }

   return 0;
}

Comments