• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ysg

[C#] Need help with Hopfield ANN

7 posts in this topic

Hi, I attached my code for your convenience.

This problem stems from my lack of understanding of how Hopfield Artificial Neural Networks (this applies to my understanding of all artificial neural networks) work. I think I have the concept behind "learning" down (for the most part), but when it comes to using the "trained" matrix to recognize a pattern, that's where I'm falling on my face.

I've attached the files as links to make it easier for some:
http://bin.cakephp.org/view/1255828170
http://bin.cakephp.org/view/873169308
http://bin.cakephp.org/view/908185693
http://bin.cakephp.org/view/152716718

The main method:
namespace artificialneuralnetwork
{
  class MainClass
  {
    public static string FormatBoolean(bool [] b)
    {
      System.Text.StringBuilder result = new System.Text.StringBuilder();

      result.Append ('[');

      for (int i = 0; i < b.Length; i++)
      {
        if (b[i])
        {
          result.Append ("T");
        }
        else
        {
          result.Append ("F");
        }

        if (i != b.Length - 1)
        {
          result.Append (",");
        }
      }

      result.Append (']');

      return result.ToString();
    }

    public static void Main (string[] args)
    {
      // create a neural network.
      HopfieldNetwork network = new HopfieldNetwork(4);
      // this pattern will be trained.
      //bool [] pattern1 = {true, true, false, false};
      bool [] pattern1 = {false, true, false, true};
      // this pattern will be represented.
      //bool [] pattern2 = {true, false, false, false};
      bool [] pattern2 = {true, true, false, true};
      bool [] result;

      // train the neural network with pattern1.
      System.Console.WriteLine("Training Hopfield network with: "
        + FormatBoolean(pattern1));
      network.Train(pattern1);
      // present pattern1 and see it recognized.
      System.Console.WriteLine("Presenting pattern: "
        + FormatBoolean(pattern1));
      // present pattern2, which is similar to pattern1.

      // pattern1 should be recalled.
      result = network.Present(pattern2);

      System.Console.WriteLine ("Presenting pattern: "
        + FormatBoolean(pattern1) + ", and got "
        + FormatBoolean(result) + " .");

      // present pattern2, which is similar to pattern1.  Pattern1 should be
      //  recalled.
      result = network.Present (pattern2);
      System.Console.WriteLine("Presenting pattern: "
        + FormatBoolean(pattern2) + ", and got "
        + FormatBoolean(result) + " .");

      // let's write more code, not less.
      System.Threading.Thread.Sleep(100000);
    }
  }
}
I'm still debugging and trying to figure what's going on, but I'd really love some help on trying to wrap my mind around this concept.
0

Share this post


Link to post
Share on other sites
In case someone is wondering, I got the code from "Introduction to Neural Networks with C# 2nd Edition".
0

Share this post


Link to post
Share on other sites
Ooooooookkkkkkkkk, I know what the problem is smile.png . In my DotProduct method, I was doing this:
result += aArray[i] + bArray[i];
It should have been:
result += aArray[i] * bArray[i];
Forgot to multiply! Aaaaaaarrrrgghhhh! Logic problems are -- honestly -- the most fun to diagnose smile.png . I was doing the math out on paper and figured it out that I was tripping on the dot-product function, but wasn't sure how and where. I double-checked the code and figured it out that that was where I was tripping up.
0

Share this post


Link to post
Share on other sites

Not sure if you did this in your initialization, but in the weight matrix  :

 

Mat i,i=0

Mat i,j= Mat j,i

 

... is generally used : it permits to be sure the network will reach a stable state.

 

 

 

The capacity of a hopfield network is approximately nbPatterns=0.138*nbNodes, so 4 nodes is too small, maybe try with 10x10 nodes ( about 13 patterns in term of capacity )

 

I noticed you have implemented a synchronous method to compute the network state. It's totally possible even if initially, it was supposed to be asynchronous and stochastic.

 

Hope it can help !smile.png

 

If you still have problems with your implementation, I can post some pseudo-code if necessary

Edited by Tournicoti
1

Share this post


Link to post
Share on other sites

Not sure if you did this in your initialization, but in the weight matrix  :
 
Mat i,i=0
Mat i,j= Mat j,i
 
... is generally used : it permits to be sure the network will reach a stable state.
 
 
 
The capacity of a hopfield network is approximately nbPatterns=0.138*nbNodes, so 4 nodes is too small, maybe try with 10x10 nodes ( about 13 patterns in term of capacity )
 
I noticed you have implemented a synchronous method to compute the network state. It's totally possible even if initially, it was supposed to be asynchronous and stochastic.
 
Hope it can help !smile.png
 
If you still have problems with your implementation, I can post some pseudo-code if necessary

Hi there. Honestly, I'm just starting out. I'm a total n00b when it comes to this subject. If you want to post info about Hopfield networks, heck, post away! The worst that'll happen is that I'll learn something smile.png .
0

Share this post


Link to post
Share on other sites

Hello

Ok, I'm doing something in java. Something with digits. The user draws a digit as  input, and the network answers the nearest learned digit from it.

Not sure if I'll be able to post it today , but I do as fast as I can wink.png

0

Share this post


Link to post
Share on other sites

Hello
Ok, I'm doing something in java. Something with digits. The user draws a digit as  input, and the network answers the nearest learned digit from it.
Not sure if I'll be able to post it today , but I do as fast as I can wink.png

Awesome. Do it when it's most convenient for you.
0

Share this post


Link to post
Share on other sites

Hello smile.png

 

I hope the comments will help.

Anyway please feel free to ask for precisions.

 

The application uses a network of 10*8 nodes.

You can paint (left/right buttons) the panel to change the network state.

You press compute to get the next stable state.

 

I'm a bit lazy, I 've put only '0', '1' and '2' digits in the dataset tongue.png , but you can add more if you want (max 11 patterns)

 

Good luck !

 

The network implementation is divided into 2 classes :

HopfieldUnit.java which defines the behavior of an unit. (output computing and learning)

HopfieldNetwork.java which defines standard Hopfield Network. (asynchronous computing and a Hebbian offline learning method)

 

The file Patterns.java defines the patterns to be learnt . For now, 3 patterns ('0', '1', '2') are defined, but some others can be added directly.

 

The others are just UI stuff so let's ignore them happy.png

Edited by Tournicoti
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0