Friday, August 17, 2007

Color SOM

With this article i would like to demostrate the Color SOM (Self Organizing Map)

The basics of this application comes to creating and training a neural network that groups colors with eachother.

The neural network itself is a SOM (Self Organizing Map) or SON (Self Organizing Network) map. It has 3 input nodes, and in this example a 40 by 40 matrix of output nodes. In this network there are no 'hidden' layers.

The architecture looks like this:



We have our input values, (RGB color), in this case 3 inputs, so the input vector is R,G,B (values from 0 to 255)
We also have a feature map of size X and Y, (40 x 40 in my examples)
Every node of this feature map has a feature vector to an input-node.
This is called the feature vector, and every vector has a weight (Wij)

Now if we look to what I fabricated, then we have to initialize the weights of the vectors connecting each input-node to each output-node with gradient values (left pic) or with random values (right pic).



Then I made a data-set where I trained the network with.
In my example i have trained a network with 9 colors, and a one with random colors ( with 1600 unique colors in it).
While the network is training, it is going to adjust the weights of the feature vectors so that a 'grouping' is brought to life (using epoch- and sigmoid functions etc, look in "my links" to find a tutorial on ai-junkie, which is pretty good).

If I initialize the "feature vector-weights" with gradient values
((0,0,0) on the top-left and (255,255,255) on the bottom-right)
then we get, with the same map-size and color inputs offcourse, always the same end-result if the network is fully trained.

Example of training a gradient colormap with 9 colors as input dataset:



Example of training a gradient colormap (40x40) with 1600 unique colors:



If I train the map with random initialization and random colors as inputs,
then I get a simular result.
When I do it with 9 colors and random initialization, then the 9 colors will be on another place each time we (re)train the network.

Now, think about what for use this could have?
The network groups simular colors in the case, bottomline is, that it can group values with each other, I will show you later on a nice example where I used this technique to group pictures with each other in a nice learning environment ;)

I hope I could give you a little more information or insight about is topic, I will surely discuss this later with a little more depth.

If you don't know anything of A.I, then i strongly suggest to take a look at my links, and follow a couple of tutorials, you will be on your way pretty soon.

Regards,

Whizzo

Thursday, August 16, 2007

Artificial Intelligence Links

Because i don't go deep on the fundamentals of A.I., I want to give you a couple of handy links, where you can learn more about Artificial Intelligence and Neural Networks.

Here are the places where i've been to gather some information:

Artificial Intelligence

Howdy,

Since a while ago i gained interest in A.I., I'm interested in it since a long time, but never had the time to really get started with it.

But hippieyee, when it was my birthday, a bought a couple of books and actually read them also. Then the A.I vibes came really to life :)

After reading the book(s) i started to implement a framework for neural networks,
because the N.N. part of A.I interests me alot.
It's pretty cool how many simularities you can see in it, in relation with a human brain.

Now, i learned that there are many different kinds of neural nets, with the framework, based on explenation from a book, i think i managed to get the biggest kinds implemented, that's ADELINE, BackPropagation, BAM and SOM (or SON).

If you look at the working of those networks, you'll see that there are certain simularities in it. I'll go deeper on that later.

My Object Oriented Neural Network Framework looks like this:



Like you see i only show the classnames, because the complete content doesn't fit on the screen.

The thing that all N.N. have in common is a basenode, from there we make a feedforward-node, SON-Node (for self organizing maps), InputNode for giving input values and a baseNetwork, that actually consists of BaseNodes.



Then we have the BaseLink, it's a link between the Neuron-nodes (a neuron is an element of a Neural Network). We have the adeline links, Backprop links with the epochlink (for learning) and the BAM and SON links.



With this construction we can make all combinations of neural networks. Maybe you will have to inherit something to extend functionality.

The usual practices to learn something about neural networks (which i encountered the most) are:

  • Self Origanizing Nets

  • Color SOM

  • XOR problem

  • ... there are others also offcourse.


I also made a deviation, that i picked op on www.generation5.org, and that's detecting picture simularity with neural networks.

I'll post more on those subjects later.

Regards,

F.

Thursday, February 8, 2007

Object comparer

With this post i want to show a compare for objects.
This class compares 2 objects with eachother (custom objects)
With the possibility to do a deep compare.

You can off course implement a IComparable interface and do it manually,
but i find it a little more useful to do it with an utility, so that i can use it on no-mather which class.

If you have comments or can make it better, don't hesitate to post a comment.

There are probably other ways to do it, but i find this method a fast-enough method for most of the applications.


   1: /// <summary>
   2: /// Compares 2 objects with each other.
   3: /// </summary>
   4: public class Comparer
   5: {
   6:     #region Declarations
   7:  
   8:     private static readonly Type _stringType;
   9:     private delegate int ILCompareHandler(object o1, object o2);
  10:     private static ILCompareHandler _ILComparer;
  11:  
  12:     #endregion
  13:  
  14:     #region Static Constructor
  15:  
  16:     static Comparer()
  17:     {
  18:         _stringType = typeof(string);
  19:     }
  20:  
  21:     #endregion
  22:  
  23:     #region Public Methods
  24:  
  25:     /// <summary>
  26:     /// Checks wether 2 objects are equal.
  27:     /// </summary>
  28:     /// <param name="o1">Object 1</param>
  29:     /// <param name="o2">Object 2</param>
  30:     /// <param name="valueTypesOnly">Only check the value-types? (false = also check the associated-classes)</param>
  31:     /// <returns>Wether the 2 objects are equal in VALUE</returns>
  32:     public static bool CompareTo(object o1, object o2, bool valueTypesOnly)
  33:     {
  34:         Type to1 = o1.GetType();
  35:         Type to2 = o1.GetType();
  36:         if (to1.FullName.CompareTo(to2.FullName) != 0)
  37:             return false;
  38:         PropertyInfo[] props = to1.GetProperties();
  39:         // Types are equal, compare each value of each object
  40:         if (valueTypesOnly)
  41:         {
  42:             return TestOnValuesOnly(props, o1, o2);
  43:         }
  44:         else
  45:         {
  46:             bool innervaluesAreEqual = TestRecursiveValues(props, o1, o2, false);
  47:             return innervaluesAreEqual;
  48:         }
  49:     }
  50:  
  51:     #endregion
  52:  
  53:     #region Private Methods
  54:  
  55:     private static bool TestRecursiveValues(PropertyInfo[] props, object o1, object o2, bool inside)
  56:     {
  57:         if (o1 == null && o2 == null)
  58:             return true;
  59:         if ((o1 != null && o2 == null) || (o1 == null && o2 != null))
  60:             return false;
  61:  
  62:         bool classOK = true;
  63:         for (int i = 0; i < props.Length; i++)
  64:         {
  65:             PropertyInfo pi = props[i];
  66:             if (pi.PropertyType.IsClass && pi.PropertyType != _stringType)
  67:             {
  68:                 object object1 = pi.GetValue(o1, null);
  69:                 object object2 = pi.GetValue(o2, null);
  70:                 if (object1 is IEnumerable)
  71:                 {
  72:                     classOK = false;
  73:                     IEnumerator enumerator1 = (object1 as IEnumerable).GetEnumerator();
  74:                     IEnumerator enumerator2 = (object2 as IEnumerable).GetEnumerator();
  75:                     while (enumerator1.MoveNext() && enumerator2.MoveNext())
  76:                     {
  77:                         classOK = TestRecursiveValues(enumerator1.Current.GetType().GetProperties(), enumerator1.Current, enumerator2.Current, true);
  78:                         if (!classOK)
  79:                             return classOK;
  80:                     }
  81:                     if (!classOK)
  82:                         return classOK;
  83:                 }
  84:                 else
  85:                 {
  86:                     classOK = TestRecursiveValues(pi.PropertyType.GetProperties(), object1, object2, true);
  87:                     if (!classOK)
  88:                         return classOK;
  89:                 }
  90:             }
  91:             else
  92:             {
  93:                 if (inside)
  94:                 {
  95:                     if (o1.GetType().IsValueType || o1 is string)
  96:                     {
  97:                         if (o1.GetType().IsValueType)
  98:                             if (ILCompareValueTypes(o1, o2) != 0)
  99:                                 return false;
 100:                         if (o1 is string && o1 != o2)
 101:                             return false;
 102:                     }
 103:                     else
 104:                     {
 105:                         // We have an inside class , check our values
 106:                         classOK = TestOnValuesOnly(props, o1, o2);
 107:                         if (!classOK)
 108:                             return classOK;
 109:                     }
 110:                 }
 111:             }
 112:         }
 113:         return classOK;
 114:     }
 115:  
 116:     private static bool TestOnValuesOnly(PropertyInfo[] props, object o1, object o2)
 117:     {
 118:         if (o1 == null && o2 == null)
 119:             return true;
 120:         if ((o1 != null && o2 == null) || (o1 == null && o2 != null))
 121:             return false;
 122:         for (int i = 0; i < props.Length; i++)
 123:         {
 124:             PropertyInfo pi = props[i];
 125:             if (pi.PropertyType.IsValueType && !pi.PropertyType.IsArray)
 126:             {
 127:                 object so1 = pi.GetValue(o1, null) ?? string.Empty;
 128:                 object so2 = pi.GetValue(o2, null) ?? string.Empty;
 129:  
 130:                 if (ILCompareValueTypes(so1, so2) != 0)
 131:                     return false;
 132:             }
 133:             if (pi.PropertyType == _stringType)
 134:             {
 135:                 object so1 = pi.GetValue(o1, null) as string ?? string.Empty;
 136:                 object so2 = pi.GetValue(o2, null) as string ?? string.Empty;
 137:  
 138:                 if (so1 != so2)
 139:                     return false;
 140:             }
 141:         }
 142:         return true;
 143:     }
 144:  
 145:     private static int ILCompareValueTypes(object value1, object value2)
 146:     {
 147:         if (_ILComparer == null)
 148:         {
 149:             Type[] _argTypes = { typeof(object), typeof(object) };
 150:  
 151:             DynamicMethod dynam =
 152:                 new DynamicMethod(
 153:                 "ILComparer",
 154:                 typeof(int),
 155:                 _argTypes,
 156:                 typeof(Comparer));
 157:             ILGenerator il = dynam.GetILGenerator();
 158:  
 159:             il.Emit(OpCodes.Ldarg_0);
 160:             il.Emit(OpCodes.Ldarg_1);
 161:             il.Emit(OpCodes.Ceq);
 162:             il.Emit(OpCodes.Ret);
 163:             
 164:             _ILComparer = dynam.CreateDelegate(typeof(ILCompareHandler)) as ILCompareHandler;
 165:         }
 166:         return _ILComparer(value1, value2);
 167:     }
 168:  
 169:     #endregion
 170: }

Thursday, February 1, 2007

C# ReadOnly class

This class is meant for variables in C# that must be readonly.
Offcourse you can use the "readonly" keyword of C#, but you can only initialize it in the constructor of a type.

This little class makes it possible to initialize it in the constructor and assign it once somewhere else. The class is generic and supports implicit casting to the generic type.

usage:


   1: int x = 0; 
   2: ReadOnly myInt = new ReadOnly(); 
   3: myInt = 10; 
   4: x = myInt; 
   5: myInt = 15; // throws an exception 
   6: Console.WriteLine("val: {0}", x); 


The class definition:


   1: using System;
   2: using System.Collections.Generic;
   3: using System.Text;
   4:  
   5: namespace Whizzo
   6: {
   7:     public struct ReadOnly
   8:     {
   9:         private static ReadOnlyVar? _readOnlyVar;
  10:  
  11:         #region Constructor
  12:  
  13:         public ReadOnly(T var)
  14:         {
  15:             _readOnlyVar = new ReadOnlyVar(var);
  16:         }
  17:  
  18:         #endregion
  19:  
  20:         #region Implicit casts
  21:  
  22:         /// 
  23:         /// User-defined conversion from T to ReadOnly
  24:         /// 
  25:         /// type to cast to
  26:         /// a new readonly variable, filled in
  27:         public static implicit operator ReadOnly(T var)
  28:         {
  29:             if (_readOnlyVar == null)
  30:                 return new ReadOnly(var);
  31:             throw new InvalidOperationException("Variable is read-only, and already assigned.");
  32:         }
  33:  
  34:         /// 
  35:         /// User-defined conversion from ReadOnly to T
  36:         /// 
  37:         /// 
  38:         /// 
  39:         public static implicit operator T(ReadOnly var)
  40:         {
  41:             return _readOnlyVar.HasValue ? _readOnlyVar.Value.Var : default(T);
  42:         }
  43:  
  44:         #endregion
  45:  
  46:         #region Overrides
  47:  
  48:         public override string ToString()
  49:         {
  50:             return _readOnlyVar.HasValue ? _readOnlyVar.Value.Var.ToString() : "No value";
  51:         }
  52:  
  53:         #endregion
  54:  
  55:         #region Nested Types
  56:  
  57:         private struct ReadOnlyVar
  58:         {
  59:             private readonly T _var;
  60:  
  61:             public ReadOnlyVar(T var)
  62:             {
  63:                 _var = var;
  64:             }
  65:  
  66:             public T Var
  67:             {
  68:                 get { return _var; }
  69:             }
  70:         }
  71:  
  72:         #endregion
  73:     }
  74: }


Just copy the code into a ReadOnly.cs file or something like that, and start using ;)
There are many different ways to make something like this, but i find this a handy way.