public static YDataFloat GenerateEdgeDetectionAverageAccel(AcceleratorTarget acceleratorTarget, IEnumerable <YDataFloat> edgeMapList) { var target = GetAcceleratorTarget(acceleratorTarget); var firstEdgeMap = edgeMapList.First(); var width = firstEdgeMap.Width; var height = firstEdgeMap.Height; var maximum = firstEdgeMap.MaximumValue; FloatParallelArray fpTotals = null; var edgeMapCount = 0; foreach (var edgeMap in edgeMapList) { var fpInput = new FloatParallelArray(edgeMap.Data); if (fpTotals == null) { fpTotals = fpInput; } else { fpTotals = ParallelArrays.Add(fpTotals, fpInput); } edgeMapCount++; } var fpOutput = ParallelArrays.Divide(fpTotals, edgeMapCount); var output = target.ToArray2D(fpOutput); return(new YDataFloat(output, maximum)); }
public static YDataFloat GenerateEdgeDetectedImageAccel(AcceleratorTarget acceleratorTarget, YDataFloat imageData) { var target = GetAcceleratorTarget(acceleratorTarget); var width = imageData.Width; var height = imageData.Height; var fpInput = new FloatParallelArray(imageData.Data); var fpInputX = ParallelArrays.Shift(fpInput, new int[] { 1, 0 }); var fpInputY = ParallelArrays.Shift(fpInput, new int[] { 0, 1 }); var fpDX = ParallelArrays.Subtract(fpInputX, fpInput); var fpDY = ParallelArrays.Subtract(fpInputY, fpInput); var fpTotals = ParallelArrays.Add(fpDX, fpDY); var fpOutput = ParallelArrays.Divide(fpTotals, 2.0f); fpOutput = ParallelArrays.Add(fpOutput, imageData.MaximumValue / 2.0f); fpOutput = ParallelArrays.Max(fpOutput, 0.0f); fpOutput = ParallelArrays.Min(fpOutput, imageData.MaximumValue); var output = target.ToArray2D(fpOutput); return(new YDataFloat(output, imageData.MaximumValue)); }
public static void CalculateAverageAndMaximumAccel(AcceleratorTarget acceleratorTarget, float[,] imageData, out float average, out float maximum) { var target = GetAcceleratorTarget(acceleratorTarget); int width = imageData.GetLength(0); int height = imageData.GetLength(1); var fpInput = new FloatParallelArray(imageData); var fpAverage = ParallelArrays.Sum(fpInput); fpAverage = ParallelArrays.Divide(fpAverage, (float)(width * height)); fpAverage = ParallelArrays.Pad(fpAverage, new int[] { 0 }, new int[] { 1 }, 0.0f); var fpMaximum = ParallelArrays.MaxVal(fpInput); fpMaximum = ParallelArrays.Pad(fpMaximum, new int[] { 1 }, new int[] { 0 }, 0.0f); var fpOutput = ParallelArrays.Add(fpAverage, fpMaximum); var output = target.ToArray1D(fpOutput); average = output[0]; maximum = output[1]; }
static public FloatParallelArray Append(FloatParallelArray f1, FloatParallelArray f2, int Dimension) { FloatParallelArray a = ParallelArrays.Replicate(f1, f2.Shape[0] + f1.Shape[0]); a = ParallelArrays.ShiftDefault(a, 0.0f, new int[] { -f2.Shape[0] }); a = ParallelArrays.ShiftDefault(a, 0.0f, new int[] { f2.Shape[0] }); FloatParallelArray b = ParallelArrays.Replicate(f2, f2.Shape[0] + f1.Shape[0]); b = ParallelArrays.ShiftDefault(b, 0.0f, new int[] { f1.Shape[0] }); b = ParallelArrays.ShiftDefault(b, 0.0f, new int[] { -f1.Shape[0] }); return(a + b); }
public void PushN(float[] Elements) { DisposableFloatParallelArray FPAElem = new DisposableFloatParallelArray(Elements); if (IsEmpty) { m_ContArray = FPAElem; IsEmpty = false; } else { m_ContArray = ParallelArraysUtils.Append(m_ContArray, FPAElem, 0); m_ContArray = ParallelArrays.Evaluate(m_ContArray); } }
public void Push(float Element) { FloatParallelArray FPAElem = new FloatParallelArray(Element, new int[] { 1 }); if (IsEmpty) { m_ContArray = FPAElem; IsEmpty = false; } else { m_ContArray = ParallelArraysUtils.Append(m_ContArray, FPAElem, 0); m_ContArray = ParallelArrays.Evaluate(m_ContArray); } }
public override void DoEpoch(float t, float round_t) { float[,] test2d; float[] test; this.FindBMU(); //Slice the pwinner row by row and do some great stuff m_PWinner = ParallelArrays.Evaluate(m_PWinner); Slice[] slices = new Slice[2]; for (int i = 0; i < m_Parent.DataSource.PatternCount; ++i) { slices[1] = new Slice(0, m_Parent.NeuronMap.GetLength(0)); slices[0] = new Slice(i, 1); FloatParallelArray s = ParallelArrays.Section(m_PWinner, slices); s = ParallelArrays.Evaluate(s); FloatParallelArray bmuw = ParallelArrays.DropDimension(ParallelArrays.InnerProduct(s, m_GPUWeight), 0); FloatParallelArray bmuc = ParallelArrays.InnerProduct(s, m_GPUCoord); //Compute distances to bmu DisposableFloatParallelArray bmucEvaluated = ParallelArrays.Evaluate(bmuc); //Workaround bmuc = ParallelArrays.Stretch(bmucEvaluated, m_Parent.NeuronMap.GetLength(0), 1); FloatParallelArray diff = ParallelArrays.Subtract(m_GPUCoord, bmuc); FloatParallelArray dist = ParallelArrays.Multiply(diff, diff); dist = ParallelArrays.Sum(dist, 1); dist = ParallelArrays.Multiply(-1.0f, dist); //Apply update formula FloatParallelArray constE = new FloatParallelArray((float)(Math.E), m_Parent.NeuronMap.GetLength(0)); FloatParallelArray sigma = new FloatParallelArray((float)(Math.Pow(Neighborhood(t, round_t) * 0.85, 2)), m_Parent.NeuronMap.GetLength(0)); FloatParallelArray lrate = new FloatParallelArray((float)LearningRate(t, round_t), m_Parent.NeuronMap.GetLength(0), m_Parent.DataSource.PatternLength); FloatParallelArray omeg = ParallelArrays.Divide(dist, sigma); //FloatParallelArray momeg = ParallelArrays.Pow(constE, omeg); FloatParallelArray momeg = ParallelArrays.Pow2(ParallelArrays.Log2(constE) * omeg); DisposableFloatParallelArray domeg = ParallelArrays.Evaluate(momeg); //Workaround omeg = ParallelArrays.AddDimension(domeg, 1); omeg = ParallelArrays.Stretch(omeg, 1, m_Parent.DataSource.PatternLength); FloatParallelArray sbmuw = ParallelArrays.AddDimension(bmuw, 0); sbmuw = ParallelArrays.Stretch(sbmuw, m_Parent.NeuronMap.GetLength(0), 1); m_GPUWeight = ((m_GPUWeight + ((sbmuw - m_GPUWeight) * omeg * lrate))); } m_GPUWeight = ParallelArrays.Evaluate(m_GPUWeight); }
public FloatParallelArray Pop() { if (IsEmpty) { throw new UnexpectedOperation(); } if (m_ContArray.Shape[0] - 1 == 0) { IsEmpty = true; return(m_ContArray); } Slice slc = new Slice(0, m_ContArray.Shape[0] - 1); FloatParallelArray popelem = ParallelArrays.Section(m_ContArray, slc); m_ContArray = ParallelArrays.Replicate(m_ContArray, m_ContArray.Shape[0] - 1); return(popelem); }
public override void FindBMU() { //Normalize the weight vector FloatParallelArray transpose = ParallelArrays.Transpose(m_GPUWeight, 1, 0); FloatParallelArray weightsq = ParallelArrays.InnerProduct(m_GPUWeight, ParallelArrays.Transpose(m_GPUWeight, 1, 0)); FloatParallelArray weightsum = ParallelArrays.Sum(weightsq, 0); FloatParallelArray weightlength = ParallelArrays.Sqrt(weightsum); weightlength = ParallelArrays.Stretch(ParallelArrays.AddDimension(weightlength, 1), 1, m_Parent.DataSource.PatternLength); FloatParallelArray weightnorm = ParallelArrays.Divide(m_GPUWeight, weightlength); weightnorm = ParallelArrays.Transpose(weightnorm, 1, 0); //Normalize the input vector FloatParallelArray inputsq = ParallelArrays.InnerProduct(m_GPUInput, ParallelArrays.Transpose(m_GPUInput, 1, 0)); FloatParallelArray inputsum = ParallelArrays.Sum(inputsq, 0); FloatParallelArray inputlength = ParallelArrays.Sqrt(inputsum); inputlength = ParallelArrays.Stretch(ParallelArrays.AddDimension(inputlength, 1), 1, m_Parent.DataSource.PatternLength); FloatParallelArray inputnorm = ParallelArrays.Divide(m_GPUInput, inputlength); FloatParallelArray pacc = ParallelArrays.InnerProduct(inputnorm, weightnorm); //Replication bug here... FloatParallelArray bmxval = ParallelArrays.MaxVal(pacc, 1); //MSR Vivian Swelson workaround DisposableFloatParallelArray bmxvalEvaluated = ParallelArrays.Evaluate(bmxval); bmxval = ParallelArrays.AddDimension(bmxvalEvaluated, 1); bmxval = ParallelArrays.Stretch(bmxval, 1, m_Parent.NeuronMap.GetLength(0)); //Winner matrix (0 = winner) FloatParallelArray pwinner = ParallelArrays.Subtract(pacc, bmxval); //Convert to 1 = winner, 0 otherwise FloatParallelArray zero = new FloatParallelArray(0.0f, pwinner.Shape); FloatParallelArray one = new FloatParallelArray(1.0f, pwinner.Shape); BoolParallelArray bmask = ParallelArrays.CompareEqual(pwinner, zero); m_PWinner = ParallelArrays.Cond(bmask, one, zero); }
public static void AdvanceEdgeDetectionAverageAccel(AcceleratorTarget acceleratorTarget, ref YDataFloat edgeMapAverage, YDataFloat edgeDataNew, YDataFloat edgeDataOld, int edgeMapAverageCount) { var target = GetAcceleratorTarget(acceleratorTarget); var width = edgeMapAverage.Width; var height = edgeMapAverage.Height; float edgeMapAverageCountFloat = edgeMapAverageCount; var fpAverage = new FloatParallelArray(edgeMapAverage.Data); var fpNew = new FloatParallelArray(edgeDataNew.Data); var fpOld = new FloatParallelArray(edgeDataOld.Data); var fpAdjust = ParallelArrays.Subtract(fpNew, fpOld); fpAdjust = ParallelArrays.Divide(fpAdjust, edgeMapAverageCountFloat); var fpOutput = ParallelArrays.Subtract(fpAverage, fpAdjust); var output = target.ToArray2D(fpOutput); edgeMapAverage = new YDataFloat(output, edgeMapAverage.MaximumValue); }
public override void Terminate() { float[,] tmp; ParallelArrays.ToArray(m_GPUWeight, out tmp); m_Parent.NeuronMap = tmp; }