Beispiel #1
0
        /// <summary>
        /// Changes the perceived length of the underlying integer buffer for the duration of the <paramref name="action"/> invokation. The
        /// perceived buffer size is changed to <paramref name="length"/> and then the <paramref name="action"/> callback is invoked. During
        /// that time, the code in the <paramref name="action"/> callback sees the buffer as having the given <paramref name="length"/>.
        /// When the callback returns, the buffer's perceived size is returned to its normal value.
        /// </summary>
        /// <remarks>Note: This method is NOT thread safe! Do not expect delayed actions to perceive the buffer as having the given length,
        /// since the length will be returned to normal as soon as the action delegate finishes.</remarks>
        /// <param name="length">The length with which the underlying list buffer will be perceived. If it is less than zero, the action is
        /// not performed and this method exits immediately. If it is larger than the actual buffer size, this method acts as if it's the
        /// same as the buffer size.</param>
        /// <param name="action">The action delegate during whose invokation the buffer will have a modified length. If it is null, this
        /// method exits immediately.</param>
        public unsafe void AsArrayOfLength(int length, ArrayAction action)
        {
            if (action == null || length <= 0)
            {
                return;
            }
            if (length > size)
            {
                length = size;
            }

            void *pBuffer = GetBufferPointer();

            // Get the header
            ArrayHeader *header = (ArrayHeader *)pBuffer - 1;

            // Change the length
            UIntPtr originalLength = header->length;

            header->length = new UIntPtr((ulong)length);

            // Do stuff with the changed array
            action(buffer);

            // Revert back to old length
            header->length = originalLength;
        }
Beispiel #2
0
        /// <summary>
        /// Set return array with trimed-out free space. For non-web build we use unsafe code to change buffer duration
        /// in array header.
        /// </summary>
        public void TrimBuffer(ArrayAction action)
        {
#if !UNITY_WEBPLAYER
            AsArrayOfLength(size, action);
#else
            T[] bufferCopy = new T[size];
            Array.Copy(buffer, bufferCopy, size);
            action(bufferCopy);
#endif
        }
Beispiel #3
0
        //得到输出
        /// <summary>
        /// 前向传播
        /// </summary>
        /// <param name="xi"></param>
        /// <returns></returns>
        public float[] GetX_out(float[] xi)
        {
            float[] x_out = new float[xi.Length];
            if (ClassPublicValue.TrainOrTest.Equals("train"))
            {
                x_hat        = new float[xi.Length];
                Ub           = ArrayAction.average(xi);
                Sigmab       = ArrayAction.variance(xi, Ub);
                running_mean = momentum * running_mean + (1 - momentum) * Ub;
                running_var  = momentum * running_var + (1 - momentum) * Sigmab;
                float sigmabTemp = (float)Math.Sqrt(Sigmab + Adam.E);
                for (int i = 0; i < x_hat.Length; i++)
                {
                    x_hat[i] = (xi[i] - Ub) / sigmabTemp;
                    x_out[i] = Gamma * x_hat[i] + Bata;
                }
                //if (ClassPublicValue.isReadyToUbAndSigmma == true)
                //{
                //    int countNumber = BNStep;//当前的数量
                //    UbAverage = (UbAverage * (countNumber - 1) + Ub) / countNumber;
                //    SigmmaAverage = (SigmmaAverage * (countNumber - 1) + Sigmab) / countNumber;
                //}
                return(x_out);
            }
            else if (ClassPublicValue.TrainOrTest.Equals("test"))
            {
                //scale = gamma / np.sqrt(running_var + eps)
                double scale = gamma / Math.Sqrt(running_var + Adam.E);
                x_out[0] = (float)(xi[0] * scale + (bata - running_mean * scale));
                return(x_out);

                //int Mcount = AgentClass.Mini_batchSizeTrain;
                ////float xishu = 1;
                //float UbTemp = UbAverage;
                //float SigmmaTemp = SigmmaAverage * Mcount/(Mcount-1);
                //float x_hatTest = (float)((xi[0] - UbTemp) / Math.Sqrt(SigmmaTemp + Adam.E));
                //x_out[0] = Gamma * x_hatTest + Bata;
                //// x_out[0] = (Gamma / (Math.Sqrt(Sigmabtrain + Adam.E))) * xi[0] + (Bata - Gamma * Ubtrain / Math.Sqrt(Sigmabtrain + Adam.E));

                //return x_out ;
            }
            return(null);
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            string[] array1 = { "The", "Clean", "and", "Coder" };
            string[] array2 = { "Doodle", "Puzzle", "and", "Learn" };
            string[] array3 = { "Head", "First", "Head", "Hunter" };

            ArrayAction arrayAction = new ArrayAction();

            Result result = null;

            result = arrayAction.JoinAndSort(SortOrder.AtoZ, array1, array2);

            Console.WriteLine($"Unique Values, Sorted by {SortOrder.AtoZ}");
            foreach (var item in result.UniqueArray)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine($"Duplicates(if any)");
            foreach (var item in result.DuplicateValues)
            {
                Console.WriteLine(item);
            }

            result = null;
            result = arrayAction.JoinAndSort(SortOrder.ZtoA, array1, array2, array3);

            Console.WriteLine();

            Console.WriteLine($"Unique Values, Sorted by {SortOrder.ZtoA}");
            foreach (var item in result.UniqueArray)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine($"Duplicates(if any)");
            foreach (var item in result.DuplicateValues)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
        private ArrayAction GetNewAction(int xor_0, int xor_1, bool isXor0Valid, bool isXor1Valid, int arrValForXor0, int arrValForXor1)
        {
            ArrayAction newAction = ArrayAction.None;

            if (isXor0Valid && isXor1Valid)
            {
                if (arrValForXor0 == MemoryArr[xor_0] && arrValForXor1 == MemoryArr[xor_1])
                {
                    newAction = ArrayAction.None;
                }
                else if (arrValForXor0 == MemoryArr[xor_0] && arrValForXor1 != MemoryArr[xor_1])
                {
                    newAction = ArrayAction.Reverse;
                }
                else if (arrValForXor0 != MemoryArr[xor_0] && arrValForXor1 == MemoryArr[xor_1])
                {
                    newAction = ArrayAction.ReverseComplemented;
                }
                else if (arrValForXor0 != MemoryArr[xor_0] && arrValForXor1 != MemoryArr[xor_1])
                {
                    newAction = ArrayAction.Complemented;
                }
            }
            else if (isXor1Valid)
            {
                if (arrValForXor1 != MemoryArr[xor_1])
                {
                    newAction = ArrayAction.Complemented; // or Complemented
                }
            }
            else if (isXor0Valid)
            {
                if (arrValForXor0 != MemoryArr[xor_0])
                {
                    newAction = ArrayAction.Complemented;
                }
            }

            return(newAction);
        }
Beispiel #6
0
        public int[] ArrayModify(ArrayAction action, int[] arr)
        {
            int[] result = null;
            switch (action)
            {
            case ArrayAction.Complemented:
                result = arr.Select(x => 1 - x).ToArray();
                break;

            case ArrayAction.Reverse:
                result = arr.Reverse().ToArray();
                break;

            case ArrayAction.ReverseComplemented:
                result = arr.Select(x => 1 - x).Reverse().ToArray();
                break;

            default:
                result = arr.Select(x => x).ToArray();
                break;
            }
            return(result);
        }
Beispiel #7
0
        public void updateWi()
        {
            //输出节点
            FullyLayer lastFullyLayer = fullyLayers[fullyLayers.Count - 1];

            for (int i = 0; i < lastFullyLayer.fullyneurns.Count; i++)
            {
                lastFullyLayer.fullyneurns[i].backpropagation(lossValue[i]);
            }
            //隐藏节点
            for (int i = fullyLayers.Count - 1; i > 0; i--)
            {
                backward(fullyLayers[i - 1], fullyLayers[i]);
            }
            //输出敏感度
            if (OutSensitive == null)
            {
                OutSensitive = new List <float[]>(AgentClass.Mini_batchsize);
                for (int i = 0; i < AgentClass.Mini_batchsize; i++)
                {
                    OutSensitive.Add(new float[NumberOfLayorNeuron[0]]);
                }
            }
            for (int i = 0; i < AgentClass.Mini_batchsize; i++)
            {
                for (int j = 0; j < fullyLayers[0].fullyneurns.Count; j++)
                {
                    OutSensitive[i][j] = fullyLayers[0].fullyneurns[j].SensitiveValue[i];
                }
            }
            //更新权值
            //初始化ADAM
            if (isAdamInit == false)
            {
                for (int i = 0; i < fullyLayers.Count - 1; i++)
                {
                    for (int j = 0; j < fullyLayers[i].fullyneurns.Count; j++)
                    {
                        Fullyneuron fullyneuron = fullyLayers[i].fullyneurns[j];
                        if (fullyneuron.S == null)
                        {
                            fullyneuron.S = new float[NumberOfLayorNeuron[i + 1]];
                            fullyneuron.V = new float[NumberOfLayorNeuron[i + 1]];
                        }
                    }
                }
                isAdamInit = true;
            }
            //用于保存各个dw
            float[] dwArray = new float[AgentClass.Mini_batchsize];
            //更新权值
            for (int i = 0; i < fullyLayers.Count - 1; i++)
            {
                float dw = 0;
                for (int j = 0; j < fullyLayers[i].fullyneurns.Count; j++)
                {
                    Fullyneuron fullyneuron = fullyLayers[i].fullyneurns[j];
                    for (int k = 0; k < fullyneuron.Wi.Length; k++)
                    {
                        for (int mini = 0; mini < AgentClass.Mini_batchsize; mini++)
                        {
                            dwArray[mini] = fullyneuron.Xout[mini] * fullyLayers[i + 1].fullyneurns[k].SensitiveValue[mini];
                        }
                        dw = ArrayAction.average(dwArray);
                        fullyneuron.AdamUpdateWi(dw, k);
                        //float quanzhi = Adam.GetAdamNumber(fullyneuron.V[k], fullyneuron.S[k], dw,out fullyneuron.V[k],out fullyneuron.S[k]);
                        //fullyneuron.Wi[k] -= AgentClass.Study_rate * quanzhi;
                    }
                }
            }
        }
Beispiel #8
0
 public ArrayActionWrapper(ArrayAction <T1> del) : base(del)
 {
     ArgumentTypes = new ImmutableArray <Type>(new Type[1] {
         typeof(T1)
     });
 }
Beispiel #9
0
        private void UpdateWi(List <ArrayMap> inputArray, List <float[, ]> bnout)
        {
            //这是minibatch个样本的总的dwis
            List <List <float[, ]> > dwis = new List <List <float[, ]> >(inputArray.Count);

            for (int i = 0; i < inputArray.Count; i++)
            {
                dwis.Add(new List <float[, ]>(Wi.Count));
                for (int j = 0; j < Wi.Count; j++)
                {
                    dwis[i].Add(new float[Filter_size, Filter_size]);
                }
            }
            Parallel.For(0, inputArray.Count, (xp) => {
                int i = xp;
                for (int j = 0; j < inputArray[i].map.Count; j++)
                {
                    dwis[i][j] = ArrayWiConvoLute(inputArray[i].map[j], dwis[i][j], bnout[i]);
                }
            });
            //不用并行
            //for (int i = 0; i < inputArray.Count; i++)
            //{
            //    //dwis[i] = ArrayWiConvoLute(inputArray[i].map, dwis[i], bnout[i]);
            //    for (int j = 0; j < inputArray[i].map.Count; j++)
            //    {
            //        dwis[i][j] = ArrayWiConvoLute(inputArray[i].map[j], dwis[i][j], bnout[i]);
            //    }
            //}
            List <float[, ]> dwAverage = ArrayAction.ListAverage(dwis);

            if (V == null)
            {
                V = new List <float[, ]>(Wi.Count);
                for (int i = 0; i < Wi.Count; i++)
                {
                    V.Add(new float[Filter_size, Filter_size]);
                }
            }
            if (S == null)
            {
                S = new List <float[, ]>(Wi.Count);
                for (int i = 0; i < Wi.Count; i++)
                {
                    S.Add(new float[Filter_size, Filter_size]);
                }
            }
            float agentClass_B1_temp = (float)(1 - AgentClass.B1);

            for (int i = 0; i < V.Count; i++)
            {
                for (int x = 0; x < V[i].GetLength(0); x++)
                {
                    for (int y = 0; y < V[i].GetLength(1); y++)
                    {
                        V[i][x, y] = (float)(AgentClass.B1 * V[i][x, y] + agentClass_B1_temp * dwAverage[i][x, y]);
                    }
                }
            }
            float agentClass_B2_temp = (float)(1 - AgentClass.B2);

            for (int i = 0; i < S.Count; i++)
            {
                for (int x = 0; x < S[i].GetLength(0); x++)
                {
                    for (int y = 0; y < S[i].GetLength(1); y++)
                    {
                        S[i][x, y] = (float)(AgentClass.B2 * S[i][x, y] + agentClass_B2_temp * dwAverage[i][x, y] * dwAverage[i][x, y]);
                    }
                }
            }
            List <float[, ]> V_cor = new List <float[, ]>(Wi.Count);
            List <float[, ]> S_cor = new List <float[, ]>(Wi.Count);

            for (int i = 0; i < Wi.Count; i++)
            {
                V_cor.Add(new float[Filter_size, Filter_size]);
                S_cor.Add(new float[Filter_size, Filter_size]);
            }

            float b1_t = (float)(1 - Adam.B1_pow);
            float b2_t = (float)(1 - Adam.B2_pow);

            for (int i = 0; i < V_cor.Count; i++)
            {
                for (int x = 0; x < V_cor[i].GetLength(0); x++)
                {
                    for (int y = 0; y < V_cor[i].GetLength(1); y++)
                    {
                        V_cor[i][x, y] = V[i][x, y] / b1_t;
                        S_cor[i][x, y] = S[i][x, y] / b2_t;
                    }
                }
            }


            //更新
            for (int i = 0; i < S_cor.Count; i++)
            {
                int Wtemp = S_cor[i].GetLength(0);
                int Htemp = S_cor[i].GetLength(1);
                for (int x = 0; x < Wtemp; x++)
                {
                    for (int y = 0; y < Htemp; y++)
                    {
                        Wi[i][x, y] -= (float)(adjustBNNumber * AgentClass.Study_rate * V_cor[i][x, y] / (Math.Sqrt(S_cor[i][x, y]) + Adam.E));
                    }
                }
            }
        }