• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# [C#] Need help with Hopfield ANN

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1ysg  Members

Posted 07 May 2013 - 02:34 PM

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.
}
}
}

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.

### #2ysg  Members

Posted 07 May 2013 - 02:39 PM

In case someone is wondering, I got the code from "Introduction to Neural Networks with C# 2nd Edition".

### #3ysg  Members

Posted 07 May 2013 - 05:51 PM

Ooooooookkkkkkkkk, I know what the problem is . 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 . 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.

### #4Tournicoti  Prime Members

Posted 07 May 2013 - 06:57 PM

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 !

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

Edited by Tournicoti, 08 May 2013 - 05:05 AM.

### #5ysg  Members

Posted 08 May 2013 - 05:10 AM

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 !

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 .

### #6Tournicoti  Prime Members

Posted 08 May 2013 - 07:03 AM

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

### #7ysg  Members

Posted 08 May 2013 - 07:14 AM

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

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

### #8Tournicoti  Prime Members

Posted 08 May 2013 - 11:45 PM

Hello

I hope the comments will help.

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

#### Attached Files

Edited by Tournicoti, 09 May 2013 - 06:00 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.