Пример #1
0
        public void Save(string path)
        {
            List <double> saveDouble = new List <double>();

            saveDouble.Add(Layers.Count());
            saveDouble.Add(Layers[0].NeuronCount);

            for (int l = 1; l < Layers.Count(); l++)
            {
                saveDouble.Add(Layers[l].NeuronCount);

                int prevLayerNeurons = Layers[l - 1].NeuronCount;

                for (int n = 0; n < Layers[l].NeuronCount; n++)
                {
                    saveDouble.AddRange(Layers[l].Neurons[n].Weights);
                }
            }

            List <byte> saveByte = new List <byte>();

            foreach (double d in saveDouble)
            {
                saveByte.AddRange(BitConverter.GetBytes(d));
            }

            File.WriteAllBytes(path, saveByte.ToArray());
        }
        /// <summary>
        /// Creates a SpriteVisual which contains SpriteVisuals representing each of the
        /// render layers in the CanvasElement
        /// </summary>
        /// <param name="generator">ICompositionGenerator</param>
        /// <param name="width">Target width of the rendered geometry</param>
        /// <param name="height">Target height of the rendered geometry</param>
        /// <param name="offset">Offset of the rendered geometry</param>
        /// <param name="padding">Padding of the surface on which the geometry
        /// is rendered.</param>
        /// <param name="rotation">Rotation angle (in radians) about the center of the
        /// geometry</param>
        /// <returns>SpriteVisual</returns>
        public SpriteVisual CreateVisual(ICompositionGenerator generator, float width, float height,
                                         Vector2 offset, Vector4 padding, float rotation)
        {
            var rootVisual = generator.Compositor.CreateSpriteVisual();

            rootVisual.Size = new Vector2(width, height);

            for (var i = 0; i < Layers.Count(); i++)
            {
                var geometry = GetGeometry(i, width, height, offset, padding, rotation);
                if (geometry == null)
                {
                    continue;
                }

                var fill   = GetFill(i, width, height, offset, padding, rotation);
                var stroke = GetStroke(i, width, height, offset, padding, rotation);

                var geometrySurface = generator.CreateGeometrySurface(rootVisual.Size.ToSize(), geometry, stroke, fill);
                var surfaceBrush    = generator.Compositor.CreateSurfaceBrush(geometrySurface.Surface);
                var visual          = generator.Compositor.CreateSpriteVisual();
                visual.Size  = rootVisual.Size;
                visual.Brush = surfaceBrush;

                rootVisual.Children.InsertAtTop(visual);
            }

            return(rootVisual);
        }
Пример #3
0
 public List<double[,]> Forward(double[] input,/* double dropout, */WriteToCMDLine write)
 {
     List<double[,]> Results = new List<double[,]>();
     double[,] resultinput = new double[2,input.Count()];
     resultinput.SetRank(input, 0);
     resultinput.SetRank(input, 1);
     Results.Add(resultinput);
     for (int k = 0; k < Layers.Count(); k++)
     {
         /*
         double[,] output = new double[2, Layers[k].Biases.Count()];
         var rank = Layers[k].Output(Results.Last().GetRank(1));
         if(rank.Any(x => double.IsNaN(x)))
         {
             write("Layer " + k + " in " + Datatype.ToString() + " Network  has NaN Values");
         }
         output.SetRank(Layers[k].Output(Results.Last().GetRank(1)), 0);
         var drop = DropOut(output.GetRank(0), dropout, write);
         output.SetRank(drop, 1);
         Results.Add(output);
         */
         Results.Add(Layers[k].Output(Results.Last().GetRank(1));
     }
     return Results;
 }
Пример #4
0
        private void OnIsCheckPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (_fromInternal)
            {
                return;
            }

            if (e.PropertyName == "IsChecked")
            {
                _fromInternal = true;
                var checkedCount = Layers.Count(it => it.IsChecked);
                if (Layers.Count == checkedCount)
                {
                    IsSelectAll = true;
                }
                else if (checkedCount == 0)
                {
                    IsSelectAll = false;
                }
                else
                {
                    IsSelectAll = null;
                }

                _fromInternal = false;
            }
        }
Пример #5
0
 public void AddNewLayer(string name)
 {
     Layers.Add(new Layer {
         Name = name, Index = Layers.Count()
     });
     _SelectedLayer = 1;
 }
Пример #6
0
 public double[] Forward(double[] results)
 {
     for(int i = 0; i < Layers.Count(); i++)
     {
         results = Layers[i].Output(results);
     }
     return results;
 }
Пример #7
0
 /// <summary>
 /// Determines if the internal layer contains no cell passes from the internal stack of cell passes.
 /// Note: This count is derived from teh start and stop index within the list of cells; layers do
 /// not separately contain the cell passes themselves.
 /// </summary>
 /// <returns></returns>
 public bool IsEmpty()
 {
     try
     {
         // ReSharper disable once UseMethodAny.2
         return(Layers.Count() == 0);
     }
     catch
     {
         return(true);
     }
 }
Пример #8
0
        /// <summary>
        /// Adds a new layer to the layers collection for this cell
        /// </summary>
        /// <param name="layer"></param>
        /// <param name="layerRecycledIndex"></param>
        public void AddLayer(IProfileLayer layer, int layerRecycledIndex)
        {
            // ReSharper disable once UseMethodAny.2
            if (!(Layers.Count() == 0 || layer.LastLayerPassTime >= Layers.Last().LastLayerPassTime))
            {
                Log.LogError("Layer times are out of order");
                layerRecycledIndex = -1;
                return;
            }

            // If the layer has been recycled then there is no need to add it again to the layer list
            Layers.Add(layer, layerRecycledIndex);
        }
Пример #9
0
        public double Run(double[,] image)
        {
            double[,] input = Serializer.DeepClone(image);

            //Forward
            for (int i = 0; i < Layers.Count; i++)
            {
                if (i == 0)
                {
                    Layers[i].Calculate(input); continue;
                }
                Layers[i].Calculate(Layers[i - 1].Values, i == Layers.Count - 1);
            }
            return(Layers[Layers.Count() - 1].Values[0]);
        }
Пример #10
0
        /// <summary>
        /// Determines if the recorded time of a given pass lies within the time range of a layer that is
        /// deemed to be superseded by another layer
        /// </summary>
        /// <param name="Pass"></param>
        /// <returns></returns>
        public bool IsInSupersededLayer(CellPass Pass)
        {
            for (int I = 0; I < Layers.Count(); I++)
            {
                IProfileLayer layer = Layers[I];
                if ((layer.Status & LayerStatus.Superseded) != 0)
                {
                    if (Pass.Time >= Passes.FilteredPassData[layer.StartCellPassIdx].FilteredPass.Time &&
                        Pass.Time <= layer.LastLayerPassTime)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Пример #11
0
        /// <summary>
        /// Use this function to get a count of whole passes. It takes two half passes to make one whole pass
        /// </summary>
        /// <param name="includeSupersededLayers"></param>
        /// <returns></returns>
        public int TotalNumberOfWholePasses(bool includeSupersededLayers)
        {
            var Result = 0;

            var layerCount = Layers.Count();

            for (int i = 0; i < layerCount; i++)
            {
                if (!includeSupersededLayers && (LayerStatus.Superseded & Layers[i].Status) != 0)
                {
                    continue;
                }

                Result += FilteredHalfPassCount > 0 ? FilteredHalfPassCount / 2 : Layers[i].PassCount;
            }

            return(Result);
        }
Пример #12
0
        /// <summary>
        /// Use this function to loop through all passes regardless if half pass or not
        /// if no half passes present then this is a one for one relationship
        /// </summary>
        /// <param name="includeSupersededLayers"></param>
        /// <returns></returns>
        public int TotalNumberOfHalfPasses(bool includeSupersededLayers)
        {
            var Result = 0;

            var layerCount = Layers.Count();

            for (int i = 0; i < layerCount; i++)
            {
                if (!includeSupersededLayers && (LayerStatus.Superseded & Layers[i].Status) != 0)
                {
                    continue;
                }

                Result += Layers[i].PassCount; // count all passes even if half
            }

            return(Result);
        }
Пример #13
0
        private void UpdateLayerView()
        {
            for (int i = 0; i < Layers.Count(); i++)
            {
                Layers[i].Name     = GetName(Layers[i].LayerType, i);
                Layers[i].Position = i;
            }

            if (Layers.Count != 0)
            {
                ShowLayerDetails(Layers[Layers.Count - 1]);
            }
            else
            {
                ShowLayerAdding();
            }

            terrainEngine.ResetTerrainEngine();
        }
Пример #14
0
        /// <summary>
        /// Constructs a new layer from the set of cell passes contained in a multiple cell pass filtering result
        /// </summary>
        /// <param name="filteredPassValues"></param>
        public void AddLayer(FilteredMultiplePassInfo filteredPassValues)
        {
            if (Layers.Count() != 0)
            {
                Log.LogError("Cannot add a layer via FilteredPassValues if there are already layers in the cell");
                return;
            }

            if (filteredPassValues.PassCount == 0)
            {
                return;
            }

            IProfileLayer NewLayer = RequestNewLayer(out int LayerRecycledIndex);

            NewLayer.StartCellPassIdx = 0;
            NewLayer.EndCellPassIdx   = filteredPassValues.PassCount - 1;

            NewLayer.Assign(filteredPassValues);

            AddLayer(NewLayer, LayerRecycledIndex);
        }
Пример #15
0
        public double[] Backward(List<double[,]> Results, double[] desired, NetworkMem mem, WriteToCMDLine write)
        {
            var DValues = desired;

            for (int l = Layers.Count() - 1; l >= 0; l--)
            {
                try
                {
                    if (l != Layers.Count() - 1)
                        DValues = InverseDropOut(DValues, Results[l].GetRank(1));
                    DValues = mem.Layers[l].DActivation(DValues, Results[l + 1].GetRank(0));
                    mem.Layers[l].DBiases(DValues, Layers[l]);
                    mem.Layers[l].DWeights(DValues, Results[l].GetRank(0), Layers[l]);
                    DValues = mem.Layers[l].DInputs(DValues, Layers[l]);
                }
                catch (Exception e)
                {
                    write("Failed at Layer : " + l);
                    e.OutputError();
                }
            }
            return DValues;
        }
        /// <summary>
        /// Renders the layers of the CanvasElement, based on the specified dimensions,
        /// offset, padding and rotation, on a Canvas.
        /// </summary>
        /// <param name="session">CanvasDrawingSession</param>
        /// <param name="width">Target width of the rendered geometry</param>
        /// <param name="height">Target height of the rendered geometry</param>
        /// <param name="offset">Offset of the rendered geometry</param>
        /// <param name="padding">Padding of the surface on which the geometry
        /// is rendered.</param>
        /// <param name="rotation">Rotation angle (in radians) about the center of the
        /// geometry</param>
        /// <param name="fill">Overrides layers having valid fill with the given fill.</param>
        /// <param name="stroke">Ovderrides layers having valid stroke with the given stroke.</param>
        public void Render(CanvasDrawingSession session, float width, float height, Vector2 offset,
                           Vector4 padding, float rotation, ICanvasBrush fill, ICanvasStroke stroke)
        {
            for (var i = 0; i < Layers.Count(); i++)
            {
                var geometry = GetGeometry(i, width, height, offset, padding, rotation);
                if (geometry == null)
                {
                    continue;
                }

                var oldFill = GetFill(i, width, height, offset, padding, rotation);
                if (oldFill != null)
                {
                    session.FillGeometry(geometry, fill);
                }

                var oldStroke = GetStroke(i, width, height, offset, padding, rotation);
                if (oldStroke != null)
                {
                    session.DrawGeometry(geometry, stroke.Brush, stroke.Width, stroke.Style);
                }
            }
        }
Пример #17
0
        /// <summary>
        /// Compute the first, last, lowest and highest elevations for the set of cell passes in the layer
        /// </summary>
        /// <param name="hasElevationTypeFilter"></param>
        /// <param name="elevationType"></param>
        public void SetFirstLastHighestLowestElevations(bool hasElevationTypeFilter, ElevationType elevationType)
        {
            for (int j = 0; j < Layers.Count(); j++) // from oldest to newest
            {
                if (FilteredPassCount > 0)
                {
                    if ((LayerStatus.Superseded & Layers[j].Status) != 0)
                    {
                        continue;
                    }

                    if (Layers[j].LastPassHeight != Consts.NullHeight)
                    {
                        CellLastElev = Layers[j].LastPassHeight; // keep updating to last layer
                    }
                    if (CellFirstElev == Consts.NullHeight)
                    {
                        CellFirstElev = Layers[j].FirstPassHeight; // record if not yet set for first value
                    }
                    if (((Layers[j].MaximumPassHeight != Consts.NullHeight) && (Layers[j].MaximumPassHeight > CellHighestElev)) ||
                        (CellHighestElev == Consts.NullHeight))
                    {
                        CellHighestElev = Layers[j].MaximumPassHeight;
                    }
                    if (((Layers[j].MinimumPassHeight != Consts.NullHeight) && (Layers[j].MinimumPassHeight < CellLowestElev)) ||
                        (CellLowestElev == Consts.NullHeight))
                    {
                        CellLowestElev = Layers[j].MinimumPassHeight;
                    }
                }
            }

            if (hasElevationTypeFilter)
            {
                // this means we are only interested in one pass so other results should match elev type selected
                switch (elevationType)
                {
                case ElevationType.Last:
                {
                    CellLowestElev  = CellLastElev;
                    CellHighestElev = CellLastElev;
                    CellFirstElev   = CellLastElev;
                    break;
                }

                case ElevationType.First:
                {
                    CellLowestElev  = CellFirstElev;
                    CellHighestElev = CellFirstElev;
                    CellLastElev    = CellFirstElev;
                    break;
                }

                case ElevationType.Highest:
                {
                    CellLowestElev = CellHighestElev;
                    CellFirstElev  = CellHighestElev;
                    CellLastElev   = CellHighestElev;
                    break;
                }

                case ElevationType.Lowest:
                {
                    CellHighestElev = CellLowestElev;
                    CellFirstElev   = CellLowestElev;
                    CellLastElev    = CellLowestElev;
                    break;
                }
                }
            }
        }
        /// <summary>
        /// Validates the network by checking all layers, neurons, and connections. Exception will be thrown if an invalid configuration is present.
        /// </summary>
        protected override void ValidateNetwork()
        {
            if (Layers == null)
            {
                throw new InvalidOperationException("Network has not been initialized with layers.");
            }

            if (Layers.Count(l => l is IInputLayer) != 1)
            {
                throw new InvalidOperationException("Network should contain exactly one input layer.");
            }

            if (Layers.Count(l => l is IOutputLayer) != 1)
            {
                throw new InvalidOperationException("Network should contain exactly one output layer.");
            }

            foreach (var layer in Layers)
            {
                if (layer == null)
                {
                    throw new InvalidOperationException("Network contains null layer(s).");
                }

                foreach (var neuron in layer.Neurons)
                {
                    if (neuron == null)
                    {
                        throw new InvalidOperationException("Network contains null neuron(s).");
                    }

                    if (neuron.Connections == null || !neuron.Connections.Any())
                    {
                        throw new InvalidOperationException("Neuron contains null or invalid connections.");
                    }

                    if (layer is IInputLayer && !(neuron is IInputNeuron))
                    {
                        throw new InvalidOperationException("Input layer contains neuron(s) that aren't input neurons.");
                    }

                    else if (layer is IHiddenLayer && !(neuron is IHiddenNeuron))
                    {
                        throw new InvalidOperationException("Hidden layer contains neuron(s) that aren't hidden neurons.");
                    }

                    else if (layer is IOutputLayer && !(neuron is IOutputNeuron))
                    {
                        throw new InvalidOperationException("Output layer contains neuron(s) that aren't output neurons.");
                    }

                    if (neuron is IHiddenNeuron && (!neuron.Connections.Any(c => c is IIncomingConnection) || !neuron.Connections.Any(c => c is IOutgoingConnection)))
                    {
                        throw new InvalidOperationException("Hidden neuron doesn't contain both incoming and outgoing connection(s).");
                    }

                    foreach (var connection in neuron.Connections)
                    {
                        if (connection == null)
                        {
                            throw new InvalidOperationException("Network contains null connection(s).");
                        }

                        if (neuron is IInputNeuron && !(connection is IOutgoingConnection))
                        {
                            throw new InvalidOperationException("Input neuron contains connection(s) that aren't outgoing or are invalid.");
                        }

                        else if (neuron is IOutputNeuron && !(connection is IIncomingConnection))
                        {
                            throw new InvalidOperationException("Input neuron contains connection(s) that aren't outgoing or are invalid.");
                        }

                        else if ((connection is IIncomingConnection && ((IIncomingConnection)connection).FromNeuron == null) ||
                                 (connection is IOutgoingConnection && ((IOutgoingConnection)connection).ToNeuron == null))
                        {
                            throw new InvalidOperationException("Hidden neuron contains connection(s) that are invalid.");
                        }
                    }
                }
            }
        }
Пример #19
0
        /// <summary>
        /// Serializes content to the writer
        /// </summary>
        public override void InternalToBinary(IBinaryRawWriter writer)
        {
            base.InternalToBinary(writer);

            VersionSerializationHelper.EmitVersionByte(writer, VERSION_NUMBER);

            int count = Layers?.Count() ?? 0;

            writer.WriteInt(count);
            for (int i = 0; i < count; i++)
            {
                Layers[i].ToBinary(writer);
            }

            writer.WriteFloat(CellLowestElev);
            writer.WriteFloat(CellHighestElev);
            writer.WriteFloat(CellLastElev);
            writer.WriteFloat(CellFirstElev);
            writer.WriteFloat(CellLowestCompositeElev);
            writer.WriteFloat(CellHighestCompositeElev);
            writer.WriteFloat(CellLastCompositeElev);
            writer.WriteFloat(CellFirstCompositeElev);

            writer.WriteShort(CellCCV);
            writer.WriteShort(CellTargetCCV);
            writer.WriteShort(CellPreviousMeasuredCCV);
            writer.WriteShort(CellPreviousMeasuredTargetCCV);

            writer.WriteFloat(CellCCVElev);

            writer.WriteShort(CellMDP);
            writer.WriteShort(CellTargetMDP);
            writer.WriteFloat(CellMDPElev);

            writer.WriteByte(CellCCA);
            writer.WriteShort(CellTargetCCA);
            writer.WriteFloat(CellCCAElev);

            writer.WriteFloat(CellTopLayerThickness);
            writer.WriteBoolean(IncludesProductionData);

            writer.WriteInt(TopLayerPassCount);
            writer.WriteInt(TopLayerPassCountTargetRangeMin);
            writer.WriteInt(TopLayerPassCountTargetRangeMax);

            writer.WriteInt(CellMaxSpeed);
            writer.WriteInt(CellMinSpeed);

            Passes.ToBinary(writer);

            writer.WriteBooleanArray(FilteredPassFlags);
            writer.WriteInt(FilteredPassCount);
            writer.WriteInt(FilteredHalfPassCount);
            writer.WriteInt((int)AttributeExistenceFlags);

            writer.WriteInt(CellMaterialTemperature);
            writer.WriteInt(CellMaterialTemperatureWarnMin);
            writer.WriteInt(CellMaterialTemperatureWarnMax);

            writer.WriteFloat(CellMaterialTemperatureElev);
        }