예제 #1
0
 /// <summary>
 /// Resets the neurons in the entire graph (see <see cref="Neuron.Reset(NetworkTrainingProperties)"/>).
 /// </summary>
 /// <param name="properties">Network training properties object.</param>
 public void ResetStates(NetworkTrainingProperties properties)
 {
     foreach (var node in this.GetVertices().OfType <Neuron>())
     {
         node.Reset(properties);
     }
 }
예제 #2
0
        /// <summary>Calculates and returns the error derivative (<see cref="Delta"/>) of this node.</summary>
        /// <param name="t">Error derivative from the previous layer (n + 1).</param>
        /// <param name="properties">Network training properties.</param>
        /// <returns>A double.</returns>
        public virtual double Error(double t, NetworkTrainingProperties properties)
        {
            _DeltaL = Delta;

            if (Out.Count == 0)
            {
                Delta = delta = -(t - Output);
            }

            else
            {
                if (In.Count > 0 && Out.Count > 0)
                {
                    double hp = this.ActivationFunction.Derivative(this.Input);
                    delta = Out.Sum(e => e.Weight * t) * hp;
                }

                Delta = Out.Sum(s => s.Target.delta * this.Output);
            }

            if (this.In.Count > 0)
            {
                for (int edge = 0; edge < this.In.Count; edge++)
                {
                    this.In[edge].Source.Error(this.Delta, properties);
                }
            }

            return(Delta);
        }
예제 #3
0
        public virtual ISequenceModel Generate(Matrix X, Matrix Y)
        {
            this.Preprocess(X);
            // because I said so...
            if (MaxIterations == -1)
            {
                MaxIterations = 500;
            }

            var network = Network.New().Create(X.Cols, Y.Cols, Activation, OutputFunction, epsilon: Epsilon);

            INetworkTrainer trainer = new GradientDescentTrainer();

            var model = new NeuralNetworkModel
            {
                Descriptor        = Descriptor,
                NormalizeFeatures = base.NormalizeFeatures,
                FeatureNormalizer = base.FeatureNormalizer,
                FeatureProperties = base.FeatureProperties,
                Network           = network
            };

            OnModelChanged(this, ModelEventArgs.Make(model, "Initialized"));

            NetworkTrainingProperties properties = NetworkTrainingProperties.Create(network, X.Rows, X.Cols, this.LearningRate, this.Lambda, this.MaxIterations);

            Vector loss = Vector.Zeros(this.MaxIterations);

            for (int i = 0; i < MaxIterations; i++)
            {
                properties.Iteration = i;

                network.ResetStates(properties);

                for (int x = 0; x < X.Rows; x++)
                {
                    network.Forward(X[x, VectorType.Row]);
                    //OnModelChanged(this, ModelEventArgs.Make(model, "Forward"));
                    network.Back(Y[x, VectorType.Row], properties, trainer);

                    loss[i] += network.Cost;
                }

                var output = String.Format("Run ({0}/{1}): {2}", i, MaxIterations, network.Cost);
                OnModelChanged(this, ModelEventArgs.Make(model, output));

                if (this.LossMinimized(loss, i))
                {
                    break;
                }
            }

            return(model);
        }
예제 #4
0
        /// <summary>
        /// Propagates a weight update event upstream through the network.
        /// </summary>
        /// <param name="properties">Network training properties.</param>
        /// <param name="networkTrainer">Network training method.</param>
        public virtual void Update(NetworkTrainingProperties properties, INetworkTrainer networkTrainer)
        {
            for (int edge = 0; edge < this.In.Count; edge++)
            {
                Delta = (1.0 / properties.Examples) * Delta;

                if (edge > 0)
                {
                    Delta = Delta + ((properties.Lambda / properties.Examples) * this.In[edge].Weight);
                }

                if (!this.Constrained)
                {
                    this.In[edge].Weight = networkTrainer.Update(this.In[edge].ParentId, this.In[edge].ChildId,
                                                                 nameof(Edge.Weight), this.In[edge].Weight, Delta, properties);
                }
                this.In[edge].Source.Update(properties, networkTrainer);
            }
        }
예제 #5
0
        /// <summary>Backpropagates the errors through the network given the supplied sequence label.</summary>
        /// <param name="y">Output vector to process.</param>
        /// <param name="properties">Network training properties for use in learning.</param>
        /// <param name="networkTrainer">Network training method.</param>
        /// <param name="update">Indicates whether to update the weights after computing the errors.</param>
        public void Back(Vector y, NetworkTrainingProperties properties, INetworkTrainer networkTrainer, bool update = true)
        {
            this.Cost = this.LossFunction.Compute(this.Output(), y);

            // CK
            // propagate error gradients
            for (int i = 0; i < Out.Length; i++)
            {
                Out[i].Error(y[i], properties);
            }

            if (update)
            {
                // reset weights
                for (int i = 0; i < Out.Length; i++)
                {
                    Out[i].Update(properties, networkTrainer);
                }
            }
        }
예제 #6
0
 /// <summary>Backpropagates the errors through the network given the supplied label.</summary>
 /// <param name="y">Label to process.</param>
 /// <param name="properties">Network training properties for use in learning.</param>
 /// <param name="networkTrainer">Network training method.</param>
 public void Back(double y, NetworkTrainingProperties properties, INetworkTrainer networkTrainer)
 {
     this.Back(Vector.Create(this.Out.Length, () => y), properties, networkTrainer);
 }
예제 #7
0
 /// <summary>
 /// Propogates a reset event downstream through the network.
 /// </summary>
 /// <param name="properties">Network training properties.</param>
 public virtual void Reset(NetworkTrainingProperties properties)
 {
 }