예제 #1
0
        internal override float CalculateErrorByOutputGradient(NNDetailedBackpropagationData detailedBackpropagationData, int fromNeuronIndex)
        {
            float[] dE_dz = detailedBackpropagationData.dE_dz[this];

            var idcs = PreviousLayer.ConvertFromNeuronIndex(fromNeuronIndex);
            int x    = idcs.x;
            int y    = idcs.x;
            int z    = idcs.z;

            float dE_do = 0;

            for (int zIdx = 0; zIdx < Depth; zIdx++)
            {
                for (int yIdx = 0; yIdx < Height; yIdx++)
                {
                    for (int xIdx = 0; xIdx < Width; xIdx++)
                    {
                        int wx = xIdx - x;
                        int wy = yIdx - y;

                        if (wx < 0 || wx >= FilterSize || wy < 0 || wy >= FilterSize)
                        {
                            continue;
                        }

                        dE_do += this.weights[ToWeightIndex(wx, wy, z, zIdx)] * dE_dz[ConvertToNeuronIndex(xIdx, yIdx, zIdx)];
                    }
                }
            }

            return(dE_do);
        }
        private void ComputeFilteredImage()
        {
            FilteredImage image = (FilteredImage)PreviousLayer.GetData();

            FilteredImageChannel[] newChannels = new FilteredImageChannel[image.NumberOfChannels];
            int size = image.Size;

            for (int i = 0; i < image.NumberOfChannels; i++)
            {
                double[,] newValues = new double[size, size];

                for (int valuesI = 0; valuesI < size; valuesI++)
                {
                    for (int valuesJ = 0; valuesJ < size; valuesJ++)
                    {
                        if (GlobalRandom.GetRandomDouble() < Rate)
                        {
                            newValues[valuesI, valuesJ] = 0;
                        }
                        else
                        {
                            newValues[valuesI, valuesJ] = image.Channels[i].Values[valuesI, valuesJ];
                        }
                    }
                }

                newChannels[i] = new FilteredImageChannel(size, newValues);
            }

            Output = new FilteredImage(image.NumberOfChannels, newChannels);
        }
        public override void ComputeOutput()
        {
            FilteredImageChannel[] channels = new FilteredImageChannel[FilterNumber];
            FilteredImage          img      = (FilteredImage)PreviousLayer.GetData();

            bool samePadding = (Padding == "same") ? true : false;


            Task[] tasks = new Task[FilterNumber];

            for (int i = 0; i < FilterNumber; i++)
            {
                int taski = 0 + i;

                tasks[taski] = Task.Run(() =>
                {
                    channels[taski] = Filters[taski].Convolve(img, samePadding);
                });
            }

            Task.WaitAll(tasks);



            //for (int i = 0; i < FilterNumber; i++)
            //{
            //    channels[i] = Filters[i].Convolve(img, samePadding);
            //}

            OutputBeforeActivation = new FilteredImage(FilterNumber, channels);
            OutputImage            = ActivationFunction.Activate(OutputBeforeActivation);
        }
        public override LayerOutput[] Backpropagate(LayerOutput[] nextOutput, double learningRate)
        {
            FilteredImage image       = (FilteredImage)PreviousLayer.GetData();
            int           outputIndex = 0;

            FilteredImageChannel[] channels = new FilteredImageChannel[image.NumberOfChannels];

            for (int channel = 0; channel < image.NumberOfChannels; channel++)
            {
                double[,] values = new double[image.Size, image.Size];
                for (int valuesI = 0; valuesI < image.Size; valuesI++)
                {
                    for (int valuesJ = 0; valuesJ < image.Size; valuesJ++)
                    {
                        values[valuesI, valuesJ] = nextOutput[outputIndex].Sum();
                        outputIndex++;
                    }
                }
                channels[channel] = new FilteredImageChannel(image.Size, values);
            }

            return(new FilteredImage[1] {
                new FilteredImage(image.NumberOfChannels, channels)
            });
        }
        public override void ComputeOutput()
        {
            FilteredImage image       = (FilteredImage)PreviousLayer.GetData();
            int           outputIndex = 0;

            //for(int valuesI = 0; valuesI < image.Size; valuesI++)
            //     {
            //     for (int valuesJ = 0; valuesJ < image.Size; valuesJ++)
            //     {
            //         for (int channel = 0; channel < image.NumberOfChannels; channel++)
            //         {

            //             Output.Values[outputIndex] = image.Channels[channel].Values[valuesI, valuesJ];
            //             outputIndex++;
            //         }
            //     }
            //     }

            for (int channel = 0; channel < image.NumberOfChannels; channel++)
            {
                for (int valuesI = 0; valuesI < image.Size; valuesI++)
                {
                    for (int valuesJ = 0; valuesJ < image.Size; valuesJ++)
                    {
                        Output.Values[outputIndex] = image.Channels[channel].Values[valuesI, valuesJ];
                        outputIndex++;
                    }
                }
            }
        }
        public override LayerOutput[] Backpropagate(LayerOutput[] nextOutput, double learningRate)
        {
            FilteredImage input = (FilteredImage)PreviousLayer.GetData();

            FilteredImageChannel[] outputChannels = new FilteredImageChannel[input.NumberOfChannels];

            FilteredImage nextErrors = (FilteredImage)nextOutput[0];

            int errorSize  = nextErrors.Size;
            int outputSize = input.Size;

            for (int i = 0; i < input.NumberOfChannels; i++)
            {
                outputChannels[i] = new FilteredImageChannel(outputSize);
            }

            Task[] tasks = new Task[input.NumberOfChannels];

            for (int channel = 0; channel < input.NumberOfChannels; channel++)
            {
                int taskc = 0 + channel;

                tasks[taskc] = Task.Run(() =>
                {
                    for (int channelI = 0; channelI + Pool <= outputSize; channelI += Pool)
                    {
                        for (int channelJ = 0; channelJ + Pool <= outputSize; channelJ += Pool)
                        {
                            int maxi    = 0, maxj = 0;
                            double maxx = input.Channels[taskc].Values[channelI, channelJ];

                            for (int poolI = 0; poolI < Pool; poolI++)
                            {
                                for (int poolJ = 0; poolJ < Pool; poolJ++)
                                {
                                    int i = channelI + poolI, j = channelJ + poolJ;
                                    if (input.Channels[taskc].Values[i, j] > maxx)
                                    {
                                        maxx = input.Channels[taskc].Values[i, j];
                                        maxi = i;
                                        maxj = j;
                                    }
                                }
                            }

                            outputChannels[taskc].Values[maxi, maxj] =
                                nextErrors.Channels[taskc].Values[channelI / Pool, channelJ / Pool];
                        }
                    }
                });
            }

            Task.WaitAll(tasks);

            return(new FilteredImage[1] {
                new FilteredImage(input.NumberOfChannels, outputChannels)
            });
        }
        public override LayerOutput[] Backpropagate(LayerOutput[] nextOutput, double learningRate)
        {
            FilteredImage[] newErrors = new FilteredImage[FilterNumber];

            FilteredImage previous = (FilteredImage)PreviousLayer.GetData();

            FilteredImage nextErrors = (FilteredImage)nextOutput[0];

            FilteredImage activationDerivatives = ActivationFunction.GetDerivative(OutputBeforeActivation);

            for (int i = 0; i < nextErrors.NumberOfChannels; i++)
            {
                for (int j = 0; j < nextErrors.Size; j++)
                {
                    for (int k = 0; k < nextErrors.Size; k++)
                    {
                        nextErrors.Channels[i].Values[j, k] *= activationDerivatives.Channels[i].Values[j, k];
                    }
                }
            }

            bool samePadding = (Padding == "same") ? true : false;

            Task[] tasks = new Task[FilterNumber];

            for (int i = 0; i < FilterNumber; i++)
            {
                int taski = 0 + i;

                tasks[taski] = Task.Run(() =>
                {
                    newErrors[taski] = Filters[taski].Backpropagate(previous,
                                                                    nextErrors.Channels[taski], learningRate, samePadding);
                });
            }

            Task.WaitAll(tasks);


            //for (int i = 0; i < FilterNumber; i++)
            //{
            //    newErrors[i] = Filters[i].Backpropagate(previous,
            //            nextErrors.Channels[i], learningRate, samePadding);
            //}



            //for (int i = 0; i < FilterNumber; i++)
            //{
            //    newErrors[i] = Filters[i].Backpropagate(previous,
            //        activationDerivatives.Channels[i], learningRate, samePadding);
            //}

            return(newErrors);
        }
예제 #8
0
        private RelevantObjectCollection.RelevantObjectCollection GetAllPreviousLayersCollection()
        {
            if (PreviousLayer == null)
            {
                return(null);
            }

            var collection = PreviousLayer.GetAllPreviousLayersCollection();

            return(collection == null ?
                   PreviousLayer.Objects :
                   RelevantObjectCollection.RelevantObjectCollection.Merge(collection, PreviousLayer.Objects));
        }
        public override void ComputeOutput()
        {
            FilteredImage input = (FilteredImage)PreviousLayer.GetData();

            FilteredImageChannel[] outputChannels = new FilteredImageChannel[input.NumberOfChannels];
            int inputSize  = input.Size;
            int outputSize = inputSize / Pool;

            Task[] tasks = new Task[input.NumberOfChannels];

            for (int channel = 0; channel < input.NumberOfChannels; channel++)
            {
                int taskc = 0 + channel;

                tasks[taskc] = Task.Run(() =>
                {
                    FilteredImageChannel auxInput = input.Channels[taskc];

                    double[,] outputValues = new double[outputSize, outputSize];

                    for (int channelI = 0; channelI + Pool <= inputSize; channelI += Pool)
                    {
                        for (int channelJ = 0; channelJ + Pool <= inputSize; channelJ += Pool)
                        {
                            double maxx = auxInput.Values[channelI, channelJ];

                            for (int poolI = 0; poolI < Pool; poolI++)
                            {
                                for (int poolJ = 0; poolJ < Pool; poolJ++)
                                {
                                    if (maxx < auxInput.Values[channelI + poolI, channelJ + poolJ])
                                    {
                                        maxx = auxInput.Values[channelI + poolI, channelJ + poolJ];
                                    }
                                }
                            }

                            outputValues[channelI / Pool, channelJ / Pool] = maxx;
                        }
                    }
                    outputChannels[taskc] = new FilteredImageChannel(outputSize, outputValues);
                });
            }

            Task.WaitAll(tasks);

            Output = new FilteredImage(input.NumberOfChannels, outputChannels);
        }
 private void InitializeOutput()
 {
     if (Output == null)
     {
         LayerOutput previousData = PreviousLayer.GetData();
         if (previousData is FlattenedImage)
         {
             FlattenedImage previous = (FlattenedImage)previousData;
             Output = new FlattenedImage(previous.Size);
         }
         else
         {
             FilteredImage previous = (FilteredImage)previousData;
             Output = new FilteredImage(previous.NumberOfChannels, previous.Size);
         }
     }
 }
예제 #11
0
        public override void ComputeOutput()
        {
            FlattenedImage previous = (FlattenedImage)PreviousLayer.GetData();

            Task[] tasks = new Task[NumberOfUnits];

            for (int i = 0; i < NumberOfUnits; i++)
            {
                int taski = 0 + i;

                tasks[taski] = Task.Run(() =>
                {
                    Output.Values[taski] = Units[taski].ComputeOutput(previous);
                });
            }

            Task.WaitAll(tasks);

            Output = ActivationFunction.Activate(Output);
        }
        private void ComputeFlattenedImage()
        {
            FlattenedImage previous = (FlattenedImage)PreviousLayer.GetData();

            double[] newValues = new double[previous.Size];

            for (int i = 0; i < previous.Size; i++)
            {
                if (GlobalRandom.GetRandomDouble() < Rate)
                {
                    newValues[i] = 0;
                }
                else
                {
                    newValues[i] = previous.Values[i];
                }
            }

            Output = new FlattenedImage(previous.Size, newValues);
        }
예제 #13
0
        internal override NNDetailedFeedData FeedForward(NNFeedData input)
        {
            float[] output     = new float[Neurons];
            int[]   maxIndices = new int[Neurons];

            for (int zIdx = 0; zIdx < PreviousLayer.Depth; zIdx++)
            {
                for (int yiIdx = 0, yoIdx = 0; yoIdx < Height; yiIdx += this.Stride, yoIdx++)
                {
                    for (int xiIdx = 0, xoIdx = 0; xoIdx < Width; xiIdx += this.Stride, xoIdx++)
                    {
                        float max    = float.MinValue;
                        int   maxIdx = -1;

                        for (int fyIdx = 0; fyIdx < this.FilterSize; fyIdx++)
                        {
                            for (int fxIdx = 0; fxIdx < this.FilterSize; fxIdx++)
                            {
                                int idx = PreviousLayer.ConvertToNeuronIndex(xiIdx + fxIdx, yiIdx + fyIdx, zIdx);

                                if (input[idx] > max)
                                {
                                    max    = input[idx];
                                    maxIdx = idx;
                                }
                            }
                        }

                        int i = ConvertToNeuronIndex(xoIdx, yoIdx, zIdx);
                        output[i]     = max;
                        maxIndices[i] = maxIdx;
                    }
                }
            }

            NNDetailedFeedData feedData = new NNDetailedFeedData(this, output, output);

            feedData.CustomData[nameof(maxIndices)] = maxIndices;

            return(feedData);
        }
예제 #14
0
        public void PopLayer(bool instant = false)
        {
            PlayNewFloor();

            if (savedLayers.Count == 0)
            {
                return;
            }

            if (CurrentLayer != null)
            {
                PreviousLayer = CurrentLayer;
                PreviousLayer.ClearEntities();
            }

            var poppedLayer = savedLayers.Dequeue();

            CurrentLayer = poppedLayer;
            Tile cachedTile = null;

            var layerOffset            = Vector2Int.zero;
            var previousLayerPlayerPos = PreviousLayer?.PlayerPosition;

            if (previousLayerPlayerPos != null)
            {
                var nextPlayerPos = CurrentLayer.GetPlayerTilePosition();
                if (nextPlayerPos != null)
                {
                    layerOffset.x = previousLayerPlayerPos.Value.x - nextPlayerPos.Value.x;
                    layerOffset.y = previousLayerPlayerPos.Value.y - nextPlayerPos.Value.y;

                    cachedTile = PreviousLayer?.GetTileAtPosition(previousLayerPlayerPos.Value);
                    if (cachedTile != null)
                    {
                        cachedTile.transform.SetParent(null);
                    }
                }
            }

            lastLayerOffset = layerOffset;

            var newPosition = lastPosition;

            newPosition.x += layerOffset.x;
            newPosition.z += layerOffset.y;
            lastPosition   = newPosition;

            var spawnPos = newPosition;

            spawnPos.x      += 5f;
            spawnPos.z      += 5f;
            previousSpawnPos = spawnPos;

            poppedLayer.OnLayerPushed(
                spawnPos,
                newPosition,
                () =>
            {
                cachedPlayer.transform.SetParent(null);

                if (PreviousLayer == null)
                {
                    cachedPlayer.transform.SetParent(((Layer)poppedLayer).transform);
                }

                PreviousLayer?.OnLayerPopped(() =>
                {
                    cachedPlayer.RevokeEvents();
                    cachedPlayer.transform.SetParent(((Layer)poppedLayer).transform);

                    if (cachedTile != null)
                    {
                        cachedTile.transform.SetParent(((MonoBehaviour)CurrentLayer).transform);
                        CurrentLayer.SetTile(cachedTile.CurrentPosition, cachedTile);
                        CurrentLayer.RefreshPlayerPossibleMoves();
                    }
                }, layerOffset, true);

                if (!instant)
                {
                    var mainCamera = CameraController.Instance.MainCamera;
                    mainCamera.DOShakePosition(.2f, 1f, 2, 160f).OnComplete(() =>
                    {
                        mainCamera.transform.SetPosition(null, mainCamera.transform.position.y + poppedLayer.Height / 2f, null);
                    });
                }
            }, cachedPlayer, instant);
        }
예제 #15
0
        internal override void PropagateBackward(NNDetailedBackpropagationData backpropagationData)
        {
            float[] lastRawOutput = backpropagationData.FeedForwardData[this].RawOutputData;

            float[] dE_dz      = new float[Neurons];
            float[] newWeights = new float[this.weights.Length];

            // For each filter
            for (int fIdx = 0; fIdx < FilterCount; fIdx++)
            {
                float   dE_db = 0;
                float[] dE_dw = new float[FilterSize * FilterSize * PreviousLayer.Depth];

                for (int yIdx = 0; yIdx < Height; yIdx++)
                {
                    for (int xIdx = 0; xIdx < Width; xIdx++)
                    {
                        int oIdx = ConvertToNeuronIndex(xIdx, yIdx, fIdx);

                        float do_dz = Activation.Gradient(lastRawOutput[oIdx], lastRawOutput);

                        float dE_do;
                        if (NextLayer == null)
                        {
                            float o = backpropagationData.FeedForwardData[this].OutputData[oIdx];
                            dE_do = backpropagationData.ErrorGradient(o, oIdx);
                        }
                        else
                        {
                            dE_do = NextLayer.CalculateErrorByOutputGradient(backpropagationData, oIdx);
                        }

                        float dE_dz_tmp = dE_do * do_dz;
                        dE_dz[oIdx] = dE_dz_tmp;

                        for (int fzIdx = 0; fzIdx < PreviousLayer.Depth; fzIdx++)
                        {
                            for (int fyIdx = 0; fyIdx < FilterSize; fyIdx++)
                            {
                                for (int fxIdx = 0; fxIdx < FilterSize; fxIdx++)
                                {
                                    float dz_dw = backpropagationData.FeedForwardData[PreviousLayer].OutputData[PreviousLayer.ConvertToNeuronIndex(xIdx * Stride + fxIdx, yIdx * Stride + fyIdx, fzIdx)];

                                    dE_dw[ToWeightIndex(fxIdx, fyIdx, fzIdx, 0)] += dE_dz_tmp * dz_dw;
                                }
                            }
                        }

                        dE_db += dE_do * do_dz;   // dz_dw = 1 for bias
                    }
                }

                for (int fzIdx = 0; fzIdx < PreviousLayer.Depth; fzIdx++)
                {
                    for (int fyIdx = 0; fyIdx < FilterSize; fyIdx++)
                    {
                        for (int fxIdx = 0; fxIdx < FilterSize; fxIdx++)
                        {
                            int weightIndex = ToWeightIndex(fxIdx, fyIdx, fzIdx, fIdx);
                            newWeights[weightIndex] = backpropagationData.CalculateNewWeight(this.weights[weightIndex], dE_dw[ToWeightIndex(fxIdx, fyIdx, fzIdx, 0)], this, weightIndex);
                        }
                    }
                }

                this.biases[fIdx] += backpropagationData.CalculateNewWeight(this.biases[fIdx], dE_db, this, fIdx);
            }

            backpropagationData.dE_dz[this]          = dE_dz;
            backpropagationData.UpdatedWeights[this] = newWeights;
        }
예제 #16
0
        public override LayerOutput[] Backpropagate(LayerOutput[] nextOutput, double learningRate)
        {
            int weightsPerUnit = Units[0].NumberOfWeights;

            FlattenedImage[] result   = new FlattenedImage[weightsPerUnit];
            FlattenedImage   previous = (FlattenedImage)PreviousLayer.GetData();

            for (int i = 0; i < weightsPerUnit; i++)
            {
                result[i] = new FlattenedImage(NumberOfUnits);
            }

            FlattenedImage activationDerivative = ActivationFunction.GetDerivative(Output);

            Task[] tasks = new Task[NumberOfUnits];

            for (int unit = 0; unit < NumberOfUnits; unit++)
            {
                int tasku = 0 + unit;

                tasks[tasku] = Task.Run(() =>
                {
                    Unit unitAux = Units[tasku];

                    FlattenedImage nextErrors = (FlattenedImage)nextOutput[tasku];

                    double unitSum        = nextErrors.Values.Sum();
                    double unitDerivative = unitSum * activationDerivative.Values[tasku];

                    for (int weight = 0; weight < unitAux.NumberOfWeights; weight++)
                    {
                        Monitor.Enter(result);
                        result[weight].Values[tasku] = unitDerivative * unitAux.Weights[weight];
                        Monitor.Exit(result);
                        double deltaW            = unitDerivative * previous.Values[weight];
                        unitAux.Weights[weight] -= learningRate * deltaW;
                    }
                });
            }

            Task.WaitAll(tasks);

            //for (int unit = 0; unit < NumberOfUnits; unit++)
            //{
            //    Unit unitAux = Units[unit];

            //        FlattenedImage nextErrors = (FlattenedImage)nextOutput[unit];

            //        double unitSum = nextErrors.Sum();
            //        double unitDerivative = unitSum * activationDerivative.Values[unit];

            //        for (int weight = 0; weight < unitAux.NumberOfWeights; weight++)
            //        {
            //            Monitor.Enter(result);
            //            result[weight].Values[unit] = unitDerivative * unitAux.Weights[weight];
            //            Monitor.Exit(result);
            //            double deltaW = unitDerivative * previous.Values[weight];
            //            unitAux.Weights[weight] -= learningRate * deltaW;
            //        }
            //}

            return(result);
        }