예제 #1
0
        public float RunReturn(DistanceOperation operation,
                               CudaDeviceVariable <float> A, int sizeA,
                               CudaDeviceVariable <float> B, int sizeB)
        {
            if (m_temp == null)
            {
                MyLog.ERROR.WriteLine("Init the object with a valid temp block of size at least one to enable RunReturn.");
                return(float.NaN);
            }

            switch (operation)
            {
            case DistanceOperation.None:
                return(float.NaN);

            case DistanceOperation.EuclidDist:
                Run(DistanceOperation.EuclidDistSquared, A, sizeA, B, sizeB, m_temp.GetDevice(m_caller), sizeA);
                m_temp.SafeCopyToHost(0, 1);

                return((float)Math.Sqrt(m_temp.Host[0]));

            default:
                Run(operation, A, sizeA, B, sizeB, m_temp.GetDevice(m_caller), sizeA);
                m_temp.SafeCopyToHost(0, 1);

                return(m_temp.Host[0]);
            }
        }
예제 #2
0
        public override void Run(MatOperation operation, MyMemoryBlock <float> A, MyMemoryBlock <float> B, MyMemoryBlock <float> Result)
        {
            Result.Fill(.0f);
            switch (operation)
            {
            case MatOperation.EuclidDist:
                if (B.Count == A.ColumnHint)
                {
                    A.SafeCopyToHost();
                    B.SafeCopyToHost();
                    for (int row = 0; row < A.Count / A.ColumnHint; row++)
                    {
                        Result.Host[row] = 0;
                        for (int Bindex = 0; Bindex < B.Count; Bindex++)
                        {
                            Result.Host[row] += (B.Host[Bindex] - A.Host[A.ColumnHint * row + Bindex]) * (B.Host[Bindex] - A.Host[A.ColumnHint * row + Bindex]);
                        }
                        Result.Host[row] = (float)Math.Sqrt((double)Result.Host[row]);
                        //System.Console.Write(" " + Result.Host[row]);
                    }
                    Result.SafeCopyToDevice();
                }
                break;

            default:
                MyLog.Writer.WriteLine(MyLogLevel.ERROR, "Trying to run cpu mat ops. for undefined MatOperation");
                break;
            }
        }
예제 #3
0
 public override void Run(MatOperation operation, MyMemoryBlock<float> A, MyMemoryBlock<float> B, MyMemoryBlock<float> Result)
 {
     Result.Fill(.0f);
     switch (operation)
     {
         case MatOperation.EuclidDist:
             if (B.Count == A.ColumnHint)
             {
                 A.SafeCopyToHost();
                 B.SafeCopyToHost();
                 for (int row = 0; row < A.Count / A.ColumnHint; row++)
                 {
                     Result.Host[row] = 0;
                     for (int Bindex = 0; Bindex < B.Count; Bindex++)
                     {
                         Result.Host[row] += (B.Host[Bindex] - A.Host[A.ColumnHint * row + Bindex]) * (B.Host[Bindex] - A.Host[A.ColumnHint * row + Bindex]);
                     }
                     Result.Host[row] = (float)Math.Sqrt( (double) Result.Host[row] );
                     //System.Console.Write(" " + Result.Host[row]);
                 }
                 Result.SafeCopyToDevice();
             }
             break;
         default:
             MyLog.Writer.WriteLine(MyLogLevel.ERROR, "Trying to run cpu mat ops. for undefined MatOperation");
             break;
     }
 }
예제 #4
0
        /// <summary>
        /// Returns float array of value from memory block of given node
        /// </summary>
        /// <param name="nodeId">Node ID</param>
        /// <param name="blockName">Memory block name</param>
        /// <returns>Retrieved values</returns>
        public float[] GetValues(int nodeId, string blockName = "Output")
        {
            MyMemoryBlock <float> block = GetMemBlock(nodeId, blockName);

            block.SafeCopyToHost();
            return(block.Host);
        }
예제 #5
0
        public override void Run(MatOperation operation, MyMemoryBlock <float> A, MyMemoryBlock <float> B, MyMemoryBlock <float> Result)
        {
            switch (operation)
            {
            case MatOperation.Multiplication: // vectors/matrices have to be always in the correct dimesions!
                if (A.Count == 1)             // valueA * B
                {
                    Result.Fill(.0f);
                    A.SafeCopyToHost();
                    MyCublasFactory.Instance.Axpy(A.Host[0], B.GetDevice(callee), 1, Result.GetDevice(callee), 1);
                }
                else if (B.Count == 1)     // A * valueB
                {
                    Result.Fill(.0f);
                    B.SafeCopyToHost();
                    MyCublasFactory.Instance.Axpy(B.Host[0], A.GetDevice(callee), 1, Result.GetDevice(callee), 1);
                }
                else     // another executions...
                {
                    Run(operation, A.GetDevice(callee), A.Count, A.ColumnHint, B.GetDevice(callee), B.Count, B.ColumnHint, Result.GetDevice(callee), Result.Count, Result.ColumnHint, 0);
                }
                break;

            case MatOperation.DotProd:
                Run(operation, A.GetDevice(callee), A.Count, A.ColumnHint, B.GetDevice(callee), B.Count, B.ColumnHint, Result.GetDevice(callee), Result.Count, Result.ColumnHint, 0);
                break;

            default:
                MyLog.Writer.WriteLine(MyLogLevel.ERROR, "Trying to run cublas for undefined MatOperation");
                break;
            }
        }
 private void GenerateWeightFromInitialWeights()
 {
     m_weightBlock.SafeCopyToHost();
     for (int i = 0; i < m_initialWeight.Length; i++)
     {
         m_weightBlock.Host[m_weightOffset + i] = m_initialWeight[i];
     }
     m_weightBlock.SafeCopyToDevice();
 }
 private void GenerateBiasFromInitialWeights()
 {
     m_biasBlock.SafeCopyToHost();
     for (int i = 0; i < m_initialBias.Length; i++)
     {
         m_biasBlock.Host[m_biasOffset + i] = m_initialBias[i];
     }
     m_biasBlock.SafeCopyToDevice();
 }
예제 #8
0
 public void PrintMemBlock2Console(MyMemoryBlock <float> m, string s = "")
 {
     System.Console.Write("  + " + s + ": ");
     m.SafeCopyToHost();
     for (int i = 0; i < Math.Min(30, m.Count); i++)
     {
         System.Console.Write(m.Host[i] + " ");
     }
     System.Console.WriteLine("");
 }
예제 #9
0
 public override void Run(MatOperation operation, MyMemoryBlock <float> A, MyMemoryBlock <float> B, MyMemoryBlock <float> Result)
 {
     if (OpersKerlsDictionary.ContainsKey(operation))
     {
         if (operation == MatOperation.GetRow)
         {
             B.SafeCopyToHost();
             OpersKerlsDictionary[operation].SetupExecution(A.ColumnHint);
             OpersKerlsDictionary[operation].Run(A, A.Count, A.ColumnHint, Result, Result.Count, Result.ColumnHint, B.Host[0]);
         }
         else if (operation == MatOperation.GetCol)
         {
             B.SafeCopyToHost();
             OpersKerlsDictionary[operation].SetupExecution(A.Count / A.ColumnHint);
             OpersKerlsDictionary[operation].Run(A, A.Count, A.ColumnHint, Result, Result.Count, Result.ColumnHint, B.Host[0]);
         }
         else if (operation == MatOperation.MultiplElemntWise | operation == MatOperation.Addition | operation == MatOperation.Substraction | operation == MatOperation.Pow)
         {
             if (A.Count >= B.Count)
             {
                 OpersKerlsDictionary[operation].SetupExecution(A.Count);
                 OpersKerlsDictionary[operation].Run(A, A.Count, A.ColumnHint, B, B.Count, B.ColumnHint, Result, Result.Count, Result.ColumnHint, float.NaN);
             }
             else
             {
                 OpersKerlsDictionary[operation].SetupExecution(B.Count);
                 OpersKerlsDictionary[operation].Run(B, B.Count, B.ColumnHint, A, A.Count, A.ColumnHint, Result, Result.Count, Result.ColumnHint, float.NaN);
             }
         }
         else
         { // other executions are performed by ,,standartezied'' kernel-call
             OpersKerlsDictionary[operation].SetupExecution(A.Count);
             OpersKerlsDictionary[operation].Run(A, A.Count, A.ColumnHint, B, B.Count, B.ColumnHint, Result, Result.Count, Result.ColumnHint);
         }
     }
     else
     {
         MyLog.Writer.WriteLine(MyLogLevel.ERROR, "Trying to run kernel MatrixOps for uninitialized kernel");
     }
 }
예제 #10
0
 private bool CropHasUsefullValueAndCopy2Host(MyMemoryBlock <float> Crop)
 {
     if (Owner.XCrop == null)
     {
         return(false);
     }
     Crop.SafeCopyToHost();
     if (Crop.Host[0] < 0.1f && Crop.Host[0] > -0.1f)
     {
         return(false);
     }
     return(true);
 }
예제 #11
0
        /// <summary>
        /// Normalizes vectors along the leading dimension.
        /// </summary>
        public static void NormalizeLeadingDim(
            MyMemoryBlock <float> vectors, MyMemoryBlock <float> temp,
            int leadingDim, int otherDim,
            MyProductKernel <float> dotKernel, MyCudaKernel multKernel, int GPU)
        {
            var count = leadingDim * otherDim;

            Debug.Assert(vectors != null && temp != null, "Missing data!");
            Debug.Assert(dotKernel != null && multKernel != null, "Missing kernels.");
            Debug.Assert(leadingDim > 0 && otherDim > 0, "Negative matrix dimensions!");
            Debug.Assert(vectors.Count >= count, "Too little vectors to orthonormalize!");
            Debug.Assert(temp.Count >= Math.Max(leadingDim, otherDim), "Too little temp space!");

            multKernel.SetupExecution(leadingDim);


            for (int i = 0; i < otherDim; i++)
            {
                var seg = vectors.GetDevicePtr(GPU, i * leadingDim);
                //dotKernel.Run(temp, i, seg, seg, leadingDim, /* distributed: */ 0);
                dotKernel.outOffset = i;
                dotKernel.Run(temp, seg, seg, leadingDim);
            }

            temp.SafeCopyToHost(0, otherDim);


            for (int i = 0; i < otherDim; i++)
            {
                if (temp.Host[i] < 0.0000001f)
                {
                    temp.Host[i] = 0;
                }
                else
                {
                    temp.Host[i] = (float)(1 / Math.Sqrt(temp.Host[i]));
                }
            }

            temp.SafeCopyToDevice(0, otherDim);


            for (int i = 0; i < otherDim; i++)
            {
                var seg = vectors.GetDevicePtr(GPU, i * leadingDim);
                var len = temp.GetDevicePtr(GPU, i);
                multKernel.Run(seg, len, seg, (int)MyJoin.MyJoinOperation.Multiplication, leadingDim, 1);
            }
        }
예제 #12
0
        public float getValMax_inColumn(MyMemoryBlock <float> dt, int column)
        {
            dt.SafeCopyToHost();
            float max = -1000.0f;

            for (int i = 0; i < dt.Count / dt.ColumnHint; i++)
            {
                int idx = i * dt.ColumnHint + column;
                if (dt.Host[idx] > max)
                {
                    max = dt.Host[idx];
                }
            }
            return(max);
        }
예제 #13
0
 private bool CropHasUsefullValueAndCopy2Host(MyMemoryBlock <float> Crop)
 {
     if (Crop == null)
     {
         MyLog.WARNING.WriteLine("One Crop not connected, not cropping this dimension");
         return(false);
     }
     Crop.SafeCopyToHost();
     // deadband aroud zero
     if (Crop.Host[0] < 0.1f && Crop.Host[0] > -0.1f)
     {
         return(false);
     }
     return(true);
 }
        private List <int> GetListOfMaxValues(MyMemoryBlock <float> data)
        {
            data.SafeCopyToHost();
            float maxVal = float.MinValue;

            for (int i = 0; i < data.Count; i++)
            {
                if (data.Host[i] > maxVal)
                {
                    maxVal = data.Host[i];
                }
            }
            List <int> maxIndexes = new List <int>();

            for (int i = 0; i < data.Count; i++)
            {
                if (data.Host[i] == maxVal)
                {
                    maxIndexes.Add(i);
                }
            }
            return(maxIndexes);
        }
예제 #15
0
 public void PrintMemBlock2Console(MyMemoryBlock<float> m, string s = "")
 {
     System.Console.Write("  + " + s + ": ");
     m.SafeCopyToHost();
     for (int i = 0; i < Math.Min(30,m.Count); i++)
     {
         System.Console.Write(m.Host[i]+" ");
     }
     System.Console.WriteLine("");
 }
예제 #16
0
 public override void Run(MatOperation operation, MyMemoryBlock<float> A, MyMemoryBlock<float> B, MyMemoryBlock<float> Result)
 {
     switch (operation)
     {
         case MatOperation.Multiplication:  // vectors/matrices have to be always in the correct dimesions!
             if (A.Count == 1) // valueA * B
             {
                 Result.Fill(.0f);
                 A.SafeCopyToHost();
                 MyCublasFactory.Instance.Axpy(A.Host[0], B.GetDevice(callee), 1, Result.GetDevice(callee), 1);
             }
             else if (B.Count == 1) // A * valueB
             {
                 Result.Fill(.0f);
                 B.SafeCopyToHost();
                 MyCublasFactory.Instance.Axpy(B.Host[0], A.GetDevice(callee), 1, Result.GetDevice(callee), 1);
             }
             else // another executions...
             {
                 Run(operation, A.GetDevice(callee), A.Count, A.ColumnHint, B.GetDevice(callee), B.Count, B.ColumnHint, Result.GetDevice(callee), Result.Count, Result.ColumnHint, 0);
             }
             break;
         case MatOperation.DotProd:
             Run(operation, A.GetDevice(callee), A.Count, A.ColumnHint, B.GetDevice(callee), B.Count, B.ColumnHint, Result.GetDevice(callee), Result.Count, Result.ColumnHint, 0);
             break;
         default:
             MyLog.Writer.WriteLine(MyLogLevel.ERROR, "Trying to run cublas for undefined MatOperation");
             break;
     }
 }
예제 #17
0
        /// <summary>
        /// Normalizes vectors along the leading dimension.
        /// </summary>
        public static void NormalizeLeadingDim(
            MyMemoryBlock<float> vectors, MyMemoryBlock<float> temp,
            int leadingDim, int otherDim,
            MyProductKernel<float> dotKernel, MyCudaKernel multKernel, int GPU)
        {
            var count = leadingDim * otherDim;

            Debug.Assert(vectors != null && temp != null, "Missing data!");
            Debug.Assert(dotKernel != null && multKernel != null, "Missing kernels.");
            Debug.Assert(leadingDim > 0 && otherDim > 0, "Negative matrix dimensions!");
            Debug.Assert(vectors.Count >= count, "Too little vectors to orthonormalize!");
            Debug.Assert(temp.Count >= Math.Max(leadingDim, otherDim), "Too little temp space!");

            multKernel.SetupExecution(leadingDim);

            for (int i = 0; i < otherDim; i++)
            {
                var seg = vectors.GetDevicePtr(GPU, i * leadingDim);
                //dotKernel.Run(temp, i, seg, seg, leadingDim, /* distributed: */ 0);
                dotKernel.outOffset = i;
                dotKernel.Run(temp, seg, seg, leadingDim);
            }

            temp.SafeCopyToHost(0, otherDim);

            for (int i = 0; i < otherDim; i++)
            {
                if (temp.Host[i] < 0.0000001f)
                    temp.Host[i] = 0;
                else
                    temp.Host[i] = (float)(1 / Math.Sqrt(temp.Host[i]));
            }

            temp.SafeCopyToDevice(0, otherDim);

            for (int i = 0; i < otherDim; i++)
            {
                var seg = vectors.GetDevicePtr(GPU, i * leadingDim);
                var len = temp.GetDevicePtr(GPU, i);
                multKernel.Run(seg, len, seg, (int)MyJoin.MyJoinOperation.Multiplication, leadingDim, 1);
            }
        }
            public override void Execute()
            {
                currentGen++;
                // If not genetically training. Return

                //Get first population member from the network
                getFFWeights(population[0]);
                population[0].SafeCopyToDevice();
                if (!DirectEvolution)
                {
                    MyCublasFactory.Instance.Gemm(Operation.NonTranspose, Operation.NonTranspose,
                                                  arr_size, arr_size, arr_size, 1.0f,
                                                  multiplier.GetDevice(Owner), arr_size,
                                                  population[0].GetDevice(Owner), arr_size,
                                                  0.0f, outputPop[0].GetDevice(Owner), arr_size
                                                  );

                    MyCublasFactory.Instance.Gemm(Operation.NonTranspose, Operation.Transpose,
                                                  arr_size, arr_size, arr_size, 1.0f,
                                                  outputPop[0].GetDevice(Owner), arr_size,
                                                  multiplier.GetDevice(Owner), arr_size,
                                                  0.0f, population[0].GetDevice(Owner), arr_size
                                                  );
                }
                //Read the saved coeffs from the initial weight matrix into the first chromosome
                population[0].CopyToMemoryBlock(cudaMatrices, 0, 0, arr_size * arr_size);
                m_extractKernel.SetupExecution(1);
                m_extractKernel.Run(cudaMatrices, chromosomePop, CoefficientsSaved, arr_size);


                // Recombine and grow the population
                if (DirectEvolution)
                {
                    m_geneticKernel.Run(cudaMatrices, arr_size, m_weights, Owner.PopulationSize, chromosomePop, noise, Owner.MutationRate, Owner.Survivors, fitnesses, marking, WeightMagnitude);
                }
                else
                {
                    m_geneticKernel.Run(cudaMatrices, arr_size, CoefficientsSaved, Owner.PopulationSize, chromosomePop, noise, Owner.MutationRate, Owner.Survivors, fitnesses, marking, Alpha);
                }


                chromosomePop.SafeCopyToHost();
                cudaMatrices.Fill(0.0f);
                m_implantKernel.SetupExecution(Owner.PopulationSize);
                m_implantKernel.Run(cudaMatrices, chromosomePop, CoefficientsSaved, arr_size);


                for (int i = 0; i < Owner.PopulationSize; i++)
                {
                    // Read the cudaMatrices into the population
                    population[i].CopyFromMemoryBlock(cudaMatrices, i * arr_size * arr_size, 0, arr_size * arr_size);

                    if (!DirectEvolution)
                    {
                        MyCublasFactory.Instance.Gemm(Operation.Transpose, Operation.NonTranspose,
                                                      arr_size, arr_size, arr_size, 1.0f,
                                                      multiplier.GetDevice(Owner), arr_size,
                                                      population[i].GetDevice(0), arr_size,
                                                      0.0f, outputPop[i].GetDevice(0), arr_size
                                                      );

                        MyCublasFactory.Instance.Gemm(Operation.NonTranspose, Operation.NonTranspose,
                                                      arr_size, arr_size, arr_size, 1.0f,
                                                      outputPop[i].GetDevice(0), arr_size,
                                                      multiplier.GetDevice(Owner), arr_size,
                                                      0.0f, population[i].GetDevice(0), arr_size
                                                      );
                    }
                    population[i].SafeCopyToHost();
                    noise.Host[i] = (float)m_rand.NextDouble();
                }
                noise.SafeCopyToDevice();



                // Determine the fitness of each member
                determineFitnesses();
                chromosomePop.SafeCopyToHost();

                #region Sort Chromosomes
                //sort the chromosomes and populations by fitness
                //bubble sort, can be improved
                float tmpfit;
                int   len = Owner.PopulationSize;
                int   newlen;

                while (len != 0)
                {
                    newlen = 0;
                    for (int i = 1; i < len; i++)
                    {
                        if (fitnesses.Host[i - 1] < fitnesses.Host[i])
                        {
                            // Swap fitnesses on the host
                            tmpfit = fitnesses.Host[i - 1];
                            fitnesses.Host[i - 1] = fitnesses.Host[i];
                            fitnesses.Host[i]     = tmpfit;
                            newlen = i;
                            // Swap Chromosomes on the device
                            for (int x = 0; x < CoefficientsSaved; x++)
                            {
                                tmpfit = chromosomePop.Host[i * CoefficientsSaved + x];
                                chromosomePop.Host[i * CoefficientsSaved + x]       = chromosomePop.Host[(i - 1) * CoefficientsSaved + x];
                                chromosomePop.Host[(i - 1) * CoefficientsSaved + x] = tmpfit;
                            }

                            for (int x = 0; x < arr_size * arr_size; x++)
                            {
                                tmpfit = population[i - 1].Host[x];
                                population[i - 1].Host[x] = population[i].Host[x];
                                population[i].Host[x]     = tmpfit;
                            }
                        }
                    }
                    len = newlen;
                }

                MyLog.INFO.WriteLine("Top {0} networks:", Math.Max(Owner.Survivors, Owner.PopulationSize / 10));
                for (int i = 0; i < Math.Max(Owner.Survivors, Owner.PopulationSize / 10); i++)
                {
                    MyLog.INFO.Write("Fitness of network {0} is: {1}", i, fitnesses.Host[i]);
                    if (i < Owner.Survivors)
                    {
                        MyLog.INFO.Write(" - surviving");
                    }
                    MyLog.INFO.Write(" \n");
                }

                #endregion

                // Best candidate to write to the network is the top of the population list
                MyLog.INFO.WriteLine("Fitness of selected network is: " + fitnesses.Host[0]);
                if (fitnesses.Host[0] >= Owner.TargetFitness)
                {
                    MyLog.INFO.WriteLine("Found satisfying network, halting...");
                    Owner.Owner.SimulationHandler.PauseSimulation();
                }

                setFFWeights(population[0]);
                MyLog.INFO.WriteLine("Written weights to network");
                if (currentGen >= Owner.Generations && Owner.Generations > 0)
                {
                    MyLog.INFO.WriteLine("Generation limit reached, halting...");
                    Owner.Owner.SimulationHandler.PauseSimulation();
                }
            }
예제 #19
0
 public float RunReturn(MatOperation operation, MyMemoryBlock<float> A, MyMemoryBlock<float> Result)
 {
     Run(operation, A, Result);
     Result.SafeCopyToHost();
     return Result.Host[0];
 }
예제 #20
0
        public void Run(VectorOperation operation,
            MyMemoryBlock<float> a,
            MyMemoryBlock<float> b,
            MyMemoryBlock<float> result)
        {
            if (!Validate(operation, a.Count, b.Count))
                return;

            switch (operation)
            {
                case VectorOperation.Rotate:
                {
                    b.SafeCopyToHost();
                    float rads = DegreeToRadian(b.Host[0]);
                    float[] transform = { (float)Math.Cos(rads), -(float)Math.Sin(rads), (float)Math.Sin(rads), (float)Math.Cos(rads) };
                    Array.Copy(transform, m_temp.Host, transform.Length);
                    m_temp.SafeCopyToDevice();
                    m_matOperation.Run(MatOperation.Multiplication, m_temp, a, result);
                }
                break;

                case VectorOperation.Angle:
                {
                    m_matOperation.Run(MatOperation.DotProd, a, b, result);
                    result.SafeCopyToHost();
                    float dotProd = result.Host[0];
                    float angle = RadianToDegree((float)Math.Acos(dotProd));
                    result.Fill(0);
                    result.Host[0] = angle;
                    result.SafeCopyToDevice();
                }
                break;

                case VectorOperation.DirectedAngle:
                {
                    result.Host[0] = -90;
                    result.SafeCopyToDevice();
                    Run(VectorOperation.Rotate, a, result, result);
                    result.CopyToMemoryBlock(m_temp, 0, 0, result.Count);

                    m_matOperation.Run(MatOperation.DotProd, a, b, result);
                    result.SafeCopyToHost();
                    float dotProd = result.Host[0];
                    float angle;
                    if (Math.Abs(Math.Abs(dotProd) - 1) < 1E-4)
                        angle = 0;
                    else
                        angle = RadianToDegree((float)Math.Acos(dotProd));

                    m_matOperation.Run(MatOperation.DotProd, m_temp, b, result);
                    result.SafeCopyToHost();
                    float perpDotProd = result.Host[0];

                    if (perpDotProd > 0)
                        angle *= -1;
                    result.Fill(0);
                    result.Host[0] = angle;
                    result.SafeCopyToDevice();
                }
                break;
            }
        }
예제 #21
0
        public override void Render()
        {
            GL.ClearColor(.15f, .15f, .15f, 0.0f);
            GL.Enable(EnableCap.AlphaTest);
            GL.Disable(EnableCap.DepthTest);
            //Gl.AlphaFunc(GL_NOTEQUAL, 0);
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One);
            GL.Enable(EnableCap.PointSmooth);

            //////////////////////////////////////////////////////
            //   P O I N T S
            GL.Enable(EnableCap.PointSmooth);
            GL.PointSize(4.0f);
            GL.Begin(PrimitiveType.Points);
            MyAbstractLayer       currentLayer     = m_firstLayer;
            MyMemoryBlock <float> memBlockData2Vis = currentLayer.Output;
            bool normalizeData2Vis = false;
            int  a = 0;

            while (currentLayer != null)
            {
                //--- which data to dispaly
                switch (ThisObserverObject.PointVisMode)
                {
                case MyNeuralNetworkGroupObserver.MyPointVisMode.Output:
                    memBlockData2Vis = currentLayer.Output;
                    break;

                case MyNeuralNetworkGroupObserver.MyPointVisMode.Delta:
                    memBlockData2Vis  = currentLayer.Delta;
                    normalizeData2Vis = true;
                    break;

                default:
                    memBlockData2Vis = currentLayer.Output;
                    break;
                }
                memBlockData2Vis.SafeCopyToHost();
                //--- go through neurons and plot each
                for (int j = 0; j < currentLayer.Neurons; j++) // this is super in efficint :(
                {
                    int   id  = a * m_dataDim;
                    float val = memBlockData2Vis.Host[j];
                    val = Math.Abs(val);   // value has to be >0
                    if (normalizeData2Vis) // norlaimnze deltas :)
                    {
                        val = val * 3f;
                    }
                    System.Drawing.Color col = MyObserverHelpers.ColorFromHSV(120f, 0.5f, Math.Min(val, 1.0f)); // value has to be 0-1
                    GL.Color3(col.R / 256f, col.G / 256f, col.B / 256f);
                    GL.Vertex3(m_data[id], m_data[id + 1], m_data[id + 2]);
                    ++a;
                }
                MyAbstractLayer nextLayer = currentLayer.NextTopologicalLayer;
                currentLayer = currentLayer.NextTopologicalLayer;
            }
            GL.End();

            //////////////////////////////////////////////////////
            //    L I N E S
            if (ThisObserverObject.EdgeVisMode != MyNeuralNetworkGroupObserver.MyEdgeVisMode.None)
            {
                GL.LineWidth(0.1f);
                GL.Color4(.2f, .2f, .5f, 0.06f);
                GL.Begin(PrimitiveType.Lines);
                currentLayer = m_firstLayer;
                int curIdxStart = 0; // start of the current index
                while (currentLayer != null)
                {
                    int             nextIdxStart = curIdxStart + currentLayer.Neurons;
                    MyAbstractLayer nextLayer    = currentLayer.NextTopologicalLayer;
                    if (nextLayer != null)
                    {
                        //--- what to show
                        switch (ThisObserverObject.EdgeVisMode)
                        {
                        case MyNeuralNetworkGroupObserver.MyEdgeVisMode.Ones:
                            break;

                        case MyNeuralNetworkGroupObserver.MyEdgeVisMode.Output:
                            currentLayer.Output.SafeCopyToHost();
                            break;

                        case MyNeuralNetworkGroupObserver.MyEdgeVisMode.Weights:
                            (nextLayer as MyHiddenLayer).Weights.SafeCopyToHost();
                            break;

                        case MyNeuralNetworkGroupObserver.MyEdgeVisMode.WeightsXOut:
                            (nextLayer as MyHiddenLayer).Weights.SafeCopyToHost();
                            currentLayer.Output.SafeCopyToHost();
                            break;

                        default:
                            break;
                        }

                        for (int nc = 0; nc < currentLayer.Neurons; nc++)
                        {
                            for (int nn = 0; nn < nextLayer.Neurons; nn++)
                            {
                                float edgeWeight = .007f;
                                switch (ThisObserverObject.EdgeVisMode)
                                {
                                case MyNeuralNetworkGroupObserver.MyEdgeVisMode.Output:
                                    edgeWeight = currentLayer.Output.Host[nc] / 50f;
                                    break;

                                case MyNeuralNetworkGroupObserver.MyEdgeVisMode.Weights:
                                    edgeWeight = (nextLayer as MyHiddenLayer).Weights.Host[nn * currentLayer.Neurons + nc];
                                    break;

                                case MyNeuralNetworkGroupObserver.MyEdgeVisMode.WeightsXOut:
                                    edgeWeight  = currentLayer.Output.Host[nc];
                                    edgeWeight *= (nextLayer as MyHiddenLayer).Weights.Host[nn * currentLayer.Neurons + nc];
                                    break;

                                default:
                                    break;
                                }
                                int i_c = (nc + curIdxStart) * m_dataDim;  // index current
                                int i_n = (nn + nextIdxStart) * m_dataDim; // index next
                                GL.Color4(.5f, .5f, .5f, edgeWeight * ThisObserverObject.EdgeVisMultiplier);
                                GL.Vertex3(m_data[i_c], m_data[i_c + 1], m_data[i_c + 2]);
                                GL.Vertex3(m_data[i_n], m_data[i_n + 1], m_data[i_n + 2]);
                            }
                        }
                    }
                    currentLayer = nextLayer;
                    curIdxStart  = nextIdxStart;
                }
                GL.End();
            }
        }
예제 #22
0
 public override void Run(MatOperation operation, MyMemoryBlock<float> A, MyMemoryBlock<float> B, MyMemoryBlock<float> Result)
 {
     if (OpersKerlsDictionary.ContainsKey(operation))
     {
         if (operation == MatOperation.GetRow)
         {
             B.SafeCopyToHost();
             OpersKerlsDictionary[operation].SetupExecution(A.ColumnHint);
             OpersKerlsDictionary[operation].Run(A, A.Count, A.ColumnHint, Result, Result.Count, Result.ColumnHint, B.Host[0]);
         }
         else if (operation == MatOperation.GetCol)
         {
             B.SafeCopyToHost();
             OpersKerlsDictionary[operation].SetupExecution(A.Count / A.ColumnHint);
             OpersKerlsDictionary[operation].Run(A, A.Count, A.ColumnHint, Result, Result.Count, Result.ColumnHint, B.Host[0]);
         }
         else if (operation == MatOperation.MultiplElemntWise | operation == MatOperation.Addition | operation == MatOperation.Substraction | operation == MatOperation.Pow)
         {
             if (A.Count >= B.Count)
             {
                 OpersKerlsDictionary[operation].SetupExecution(A.Count);
                 OpersKerlsDictionary[operation].Run(A, A.Count, A.ColumnHint, B, B.Count, B.ColumnHint, Result, Result.Count, Result.ColumnHint, float.NaN);
             }
             else
             {
                 OpersKerlsDictionary[operation].SetupExecution(B.Count);
                 OpersKerlsDictionary[operation].Run(B, B.Count, B.ColumnHint, A, A.Count, A.ColumnHint, Result, Result.Count, Result.ColumnHint, float.NaN);
             }
         }
         else
         { // other executions are performed by ,,standartezied'' kernel-call
             OpersKerlsDictionary[operation].SetupExecution(A.Count);
             OpersKerlsDictionary[operation].Run(A, A.Count, A.ColumnHint, B, B.Count, B.ColumnHint, Result, Result.Count, Result.ColumnHint);
         }
     }
     else
     {
         MyLog.Writer.WriteLine(MyLogLevel.ERROR, "Trying to run kernel MatrixOps for uninitialized kernel");
     }
 }
예제 #23
0
        public void Run(VectorOperation operation,
                        MyMemoryBlock <float> a,
                        MyMemoryBlock <float> b,
                        MyMemoryBlock <float> result)
        {
            if (!Validate(operation, a.Count, b.Count))
            {
                return;
            }

            switch (operation)
            {
            case VectorOperation.Rotate:
            {
                b.SafeCopyToHost();
                float   rads      = DegreeToRadian(b.Host[0]);
                float[] transform = { (float)Math.Cos(rads), -(float)Math.Sin(rads), (float)Math.Sin(rads), (float)Math.Cos(rads) };
                Array.Copy(transform, m_temp.Host, transform.Length);
                m_temp.SafeCopyToDevice();
                m_matOperation.Run(MatOperation.Multiplication, m_temp, a, result);
            }
            break;

            case VectorOperation.Angle:
            {
                m_matOperation.Run(MatOperation.DotProd, a, b, result);
                result.SafeCopyToHost();
                float dotProd = result.Host[0];
                float angle   = RadianToDegree((float)Math.Acos(dotProd));
                result.Fill(0);
                result.Host[0] = angle;
                result.SafeCopyToDevice();
            }
            break;

            case VectorOperation.DirectedAngle:
            {
                result.Host[0] = -90;
                result.SafeCopyToDevice();
                Run(VectorOperation.Rotate, a, result, result);
                result.CopyToMemoryBlock(m_temp, 0, 0, result.Count);

                m_matOperation.Run(MatOperation.DotProd, a, b, result);
                result.SafeCopyToHost();
                float dotProd = result.Host[0];
                float angle;
                if (Math.Abs(Math.Abs(dotProd) - 1) < 1E-4)
                {
                    angle = 0;
                }
                else
                {
                    angle = RadianToDegree((float)Math.Acos(dotProd));
                }

                m_matOperation.Run(MatOperation.DotProd, m_temp, b, result);
                result.SafeCopyToHost();
                float perpDotProd = result.Host[0];

                if (perpDotProd > 0)
                {
                    angle *= -1;
                }
                result.Fill(0);
                result.Host[0] = angle;
                result.SafeCopyToDevice();
            }
            break;
            }
        }
예제 #24
0
 public float RunReturn(MatOperation operation, MyMemoryBlock <float> A, MyMemoryBlock <float> Result)
 {
     Run(operation, A, Result);
     Result.SafeCopyToHost();
     return(Result.Host[0]);
 }