/// <summary> /// One hot encodes the REBER strings /// </summary> /// <param name="strList">A list of REBER sequences</param> /// <returns>A data table with matrices to represent the sequences of vectors and their corresponding outputs</returns> public static IDataTable GetOneHot(IEnumerable <string> strList) { var strList2 = strList.ToList(); // build the following item table var following = new Dictionary <string, HashSet <int> >(); foreach (var str in strList2) { var sb = new StringBuilder(); string prev = null; foreach (var ch in str) { sb.Append(ch); var key = sb.ToString(); if (prev != null) { if (!following.TryGetValue(prev, out HashSet <int> temp)) { following.Add(prev, temp = new HashSet <int>()); } temp.Add(_ch[ch]); } prev = key; } } var builder = DataTableBuilder.CreateTwoColumnMatrix(); foreach (var str in strList2) { var inputList = new FloatVector[str.Length]; var outputList = new FloatVector[str.Length]; var sb = new StringBuilder(); for (var i = 0; i < str.Length; i++) { var ch = str[i]; sb.Append(ch); var input = new float[_ch.Count]; var output = new float[_ch.Count]; input[_ch[ch]] = 1f; if (following.TryGetValue(sb.ToString(), out HashSet <int> temp)) { foreach (var item in temp) { output[item] = 1f; } } inputList[i] = new FloatVector { Data = input }; outputList[i] = new FloatVector { Data = output }; } builder.Add(FloatMatrix.Create(inputList), FloatMatrix.Create(outputList)); } return(builder.Build()); }
/// <summary> /// Creates random integers added together as feature vectors /// The input feature contains two features, one for each bit at that position /// The output feature contains a single feature: 1 or 0 if that bit is set in the result /// </summary> /// <param name="sampleCount">How many samples to generate</param> /// <param name="stochastic">True to generate random integers</param> /// <returns>A list of sequences</returns> public static IDataTable Addition(int sampleCount, bool stochastic) { Random rand = stochastic ? new Random() : new Random(0); var builder = DataTableBuilder.CreateTwoColumnMatrix(); for (var i = 0; i < sampleCount; i++) { // generate some random numbers (sized to prevent overflow) var a = rand.Next(int.MaxValue / 2); var b = rand.Next(int.MaxValue / 2); var a2 = _GetBitArray(a); var b2 = _GetBitArray(b); var r2 = _GetBitArray(a + b); var inputList = new FloatVector[r2.Length]; var outputList = new FloatVector[r2.Length]; for (int j = 0; j < r2.Length; j++) { inputList[j] = new FloatVector { Data = new[] { a2[j], b2[j] } }; outputList[j] = new FloatVector { Data = new[] { r2[j] } }; } builder.Add(FloatMatrix.Create(inputList), FloatMatrix.Create(outputList)); } return(builder.Build()); }