public void SerializationTest1() { var parameters = GetDefaultParams(); parameters.setInputDimensions(new int[] { 500 }); parameters.setColumnDimensions(new int[] { 2048 }); parameters.setNumActiveColumnsPerInhArea(0.02 * 2048); parameters.setGlobalInhibition(true); var sp1 = new SpatialPooler(); var mem1 = new Connections(); parameters.apply(mem1); sp1.init(mem1); sp1.Serializer("spTesting.json"); string ser = File.ReadAllText("spTesting.json"); var sp2 = SpatialPooler.Deserializer("spTesting.json"); // sp2.Serializer("spTestingDeserialized"); // string des = File.ReadAllText("spTestingDeserialized"); // Assert.IsTrue(ser.SequenceEqual(des)); /* JsonSerializerSettings settings = new JsonSerializerSettings * { * * DefaultValueHandling = DefaultValueHandling.Include, * ObjectCreationHandling = ObjectCreationHandling.Auto, * ReferenceLoopHandling = ReferenceLoopHandling.Serialize, * ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor, * TypeNameHandling = TypeNameHandling.Auto * * }; * * var jsonData = JsonConvert.SerializeObject(sp1, settings); * File.WriteAllText("spSerializedFile.json", jsonData); * * * var sp2 = JsonConvert.DeserializeObject<SpatialPooler>(File.ReadAllText("spSerializedFile.json"), settings); * var jsonData2 = JsonConvert.SerializeObject(sp2, settings); * * * * File.WriteAllText("spSerializedFile2.json", jsonData2); * * var sp3 = JsonConvert.DeserializeObject<SpatialPooler>(File.ReadAllText("spSerializedFile2.json"), settings); * var jsonData3 = JsonConvert.SerializeObject(sp3, settings); * * File.WriteAllText("spSerializedFile3.json", jsonData3); * * Assert.IsTrue(jsonData2.SequenceEqual(jsonData3)); * Assert.IsTrue(jsonData.SequenceEqual(jsonData2)); */ }
public void CategorySequenceExperiment() { bool learn = true; Parameters p = Parameters.getAllDefaultParameters(); p.Set(KEY.RANDOM, new ThreadSafeRandom(42)); p.Set(KEY.INPUT_DIMENSIONS, new int[] { 100 }); p.Set(KEY.CELLS_PER_COLUMN, 30); string[] categories = new string[] { "A", "B", "C", "D" }; //string[] categories = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "K", "L" , "M", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "Ö" }; CortexNetwork net = new CortexNetwork("my cortex"); List <CortexRegion> regions = new List <CortexRegion>(); CortexRegion region0 = new CortexRegion("1st Region"); regions.Add(region0); SpatialPooler sp1 = new SpatialPooler(); TemporalMemory tm1 = new TemporalMemory(); var mem = new Connections(); p.apply(mem); sp1.init(mem, UnitTestHelpers.GetMemory()); tm1.init(mem); Dictionary <string, object> settings = new Dictionary <string, object>(); //settings.Add("W", 25); settings.Add("N", 100); //settings.Add("Radius", 1); EncoderBase encoder = new CategoryEncoder(categories, settings); //encoder.Encode() CortexLayer <object, object> layer1 = new CortexLayer <object, object>("L1"); region0.AddLayer(layer1); layer1.HtmModules.Add("encoder", encoder); layer1.HtmModules.Add("sp", sp1); //layer1.HtmModules.Add(tm1); //layer1.Compute(); //IClassifier<string, ComputeCycle> cls = new HtmClassifier<string, ComputeCycle>(); HtmClassifier <string, ComputeCycle> cls = new HtmClassifier <string, ComputeCycle>(); HtmUnionClassifier <string, ComputeCycle> cls1 = new HtmUnionClassifier <string, ComputeCycle>(); //string[] inputs = new string[] { "A", "B", "C", "D" }; string[] inputs = new string[] { "A", "B", "C", "D" }; // // This trains SP. foreach (var input in inputs) { Debug.WriteLine($" ** {input} **"); for (int i = 0; i < 3; i++) { var lyrOut = layer1.Compute((object)input, learn) as ComputeCycle; } } sp1.Serializer("spCSTSerialized.json"); var sp2 = SpatialPooler.Deserializer("spCSTSerialized.json"); layer1.HtmModules.Remove("sp"); layer1.HtmModules.Add("sp", sp2); // Here we add TM module to the layer. layer1.HtmModules.Add("tm", tm1); // // Now, training with SP+TM. SP is pretrained on pattern. for (int i = 0; i < 200; i++) { foreach (var input in inputs) { var lyrOut = layer1.Compute(input, learn) as ComputeCycle; //cls1.Learn(input, lyrOut.activeCells.ToArray(), learn); //Debug.WriteLine($"Current Input: {input}"); cls.Learn(input, lyrOut.ActiveCells.ToArray(), lyrOut.predictiveCells.ToArray()); Debug.WriteLine($"Current Input: {input}"); if (learn == false) { Debug.WriteLine($"Predict Input When Not Learn: {cls.GetPredictedInputValue(lyrOut.predictiveCells.ToArray())}"); } else { Debug.WriteLine($"Predict Input: {cls.GetPredictedInputValue(lyrOut.predictiveCells.ToArray())}"); } Debug.WriteLine("-----------------------------------------------------------\n----------------------------------------------------------"); } if (i == 10) { Debug.WriteLine("Stop Learning From Here----------------------------"); learn = false; } // tm1.reset(mem); } Debug.WriteLine("------------------------------------------------------------------------\n----------------------------------------------------------------------------"); /* * learn = false; * for (int i = 0; i < 19; i++) * { * foreach (var input in inputs) * { * layer1.Compute((object)input, learn); * } * } */ sp1.Serialize("tm.serialize.json"); }
public void SerializationTestWithTrainedData() { var parameters = GetDefaultParams(); parameters.setInputDimensions(new int[] { 16 * 16 }); parameters.setColumnDimensions(new int[] { 32 * 32 }); parameters.setNumActiveColumnsPerInhArea(0.02 * 32 * 32); parameters.setMinPctOverlapDutyCycles(0.01); var mem = new Connections(); parameters.apply(mem); var sp1 = new SpatialPooler(); sp1.init(mem); int[] activeArray = new int[32 * 32]; int[] inputVector = Helpers.GetRandomVector(16 * 16, parameters.Get <Random>(KEY.RANDOM)); /* int [] inputVector = { * 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, * 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, * 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, * 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1, * 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0, * 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, * 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, * 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, * 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1, * 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, * 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1, * 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0, * 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, * 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, * 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, * 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0 }; * */ string str1 = String.Empty; for (int i = 0; i < 5; i++) { sp1.compute(inputVector, activeArray, true); var activeCols1 = ArrayUtils.IndexWhere(activeArray, (el) => el == 1); str1 = Helpers.StringifyVector(activeCols1); Debug.WriteLine(str1); } /* JsonSerializerSettings settings = new JsonSerializerSettings * { * * DefaultValueHandling = DefaultValueHandling.Include, * ObjectCreationHandling = ObjectCreationHandling.Auto, * ReferenceLoopHandling = ReferenceLoopHandling.Serialize, * ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor, * TypeNameHandling = TypeNameHandling.Auto * * }; */ // var jsConverted = JsonConvert.SerializeObject(sp1, Formatting.Indented, settings); // string file2 = "spSerializeTrain-newtonsoft.json"; // File.WriteAllText(file2, jsConverted); sp1.Serializer("spTrain1.json"); string ser1 = File.ReadAllText("spTrain1.json"); var sp2 = SpatialPooler.Deserializer("spTrain1.json"); sp2.Serializer("spTrainDes1.json"); string des1 = File.ReadAllText("spTrainDes1.json"); Assert.IsTrue(ser1.SequenceEqual(des1)); // SpatialPooler sp2 = JsonConvert.DeserializeObject<SpatialPooler>(File.ReadAllText(file2), settings); for (int i = 5; i < 10; i++) { sp2.compute(inputVector, activeArray, false); var activeCols2 = ArrayUtils.IndexWhere(activeArray, (el) => el == 1); var str2 = Helpers.StringifyVector(activeCols2); Debug.WriteLine(str2); Assert.IsTrue(str1.SequenceEqual(str2)); } sp2.Serializer("spTrain2.json"); string ser2 = File.ReadAllText("spTrain2.json"); // Assert.IsTrue(ser2.SequenceEqual(des1)); /* string serializedSecondPooler = JsonConvert.SerializeObject(sp2, Formatting.Indented, settings); * string fileSecondPooler = "spSerializeTrain-secondpooler-newtonsoft.json"; * File.WriteAllText(fileSecondPooler, serializedSecondPooler); * * SpatialPooler sp3 = JsonConvert.DeserializeObject<SpatialPooler>(File.ReadAllText(fileSecondPooler), settings); * string serializedThirdPooler = JsonConvert.SerializeObject(sp3, Formatting.Indented, settings); * * * Assert.IsTrue(serializedThirdPooler.SequenceEqual(serializedSecondPooler), "Third and second poolers are not equal"); * Assert.IsTrue(jsConverted.SequenceEqual(serializedSecondPooler), "First and second poolers are not equal"); */ }