# [C#] Need help with Hopfield ANN

This topic is 1872 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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)
{
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.

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

##### Share on other sites
Ooooooookkkkkkkkk, I know what the problem is . In my DotProduct method, I was doing this:
result += aArray + bArray;
It should have been:
result += aArray * bArray;
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.

##### 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 !

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

Edited by Tournicoti

##### 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 !

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 .

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

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

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

##### Share on other sites

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

Edited by Tournicoti

1. 1
2. 2
3. 3
4. 4
Rutin
18
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631423
• Total Posts
3000005
×