public void GetObjectData(SerializationInfo info, StreamingContext context) { Helper.ValidateNotNull(info, "info"); info.AddValue("inputVector", inputVector, typeof(double[])); info.AddValue("outputVector", outputVector, typeof(double[])); }
/// <summary> /// Creates a new connector between given layers using the connection mode specified. /// </summary> /// <param name="sourceLayer"> /// the source layer /// </param> /// <param name="targetLayer"> /// the target layer /// </param> /// <param name="connectionMode"> /// connection mode to use /// </param> /// <exception cref="ArgumentNullException"> /// If <c>sourceLayer</c> or <c>targetLayer</c> is <c>null</c> /// </exception> /// <exception cref="ArgumentException"> /// If <c>connectionMode</c> is invalid /// </exception> protected Connector(TSourceLayer sourceLayer, TTargetLayer targetLayer, ConnectionMode connectionMode) { // Validate Helper.ValidateNotNull(sourceLayer, "sourceLayer"); Helper.ValidateNotNull(targetLayer, "targetLayer"); targetLayer.SourceConnectors.Add(this); sourceLayer.TargetConnectors.Add(this); this.sourceLayer = sourceLayer; this.targetLayer = targetLayer; this.connectionMode = connectionMode; this.initializer = new NguyenWidrowFunction(); // Since synapses array is readonly, it should be initialized here switch (connectionMode) { case ConnectionMode.Complete: synapses = new TSynapse[sourceLayer.NeuronCount * targetLayer.NeuronCount]; break; case ConnectionMode.OneOne: if (sourceLayer.NeuronCount == targetLayer.NeuronCount) { synapses = new TSynapse[sourceLayer.NeuronCount]; break; } throw new ArgumentException( "One-One connector cannot be formed between these layers", "connectionMode"); default: throw new ArgumentException("Invalid Connection Mode", "connectionMode"); } }
public void GetObjectData(SerializationInfo info, StreamingContext context) { Helper.ValidateNotNull(info, "info"); info.AddValue("inputVectorLength", inputVectorLength); info.AddValue("outputVectorLength", outputVectorLength); info.AddValue("trainingSamples", trainingSamples, typeof(IList <Entrenamiento>)); }
public Ajusteentrenamiento(SerializationInfo info, StreamingContext context) { Helper.ValidateNotNull(info, "info"); this.inputVectorLength = info.GetInt32("inputVectorLength"); this.outputVectorLength = info.GetInt32("outputVectorLength"); this.trainingSamples = info.GetValue("trainingSamples", typeof(IList <Entrenamiento>)) as IList <Entrenamiento>; }
/// <summary> /// Trains the neural network for the given training set (Batch Training) /// </summary> /// <param name="trainingSet"> /// The training set to use /// </param> /// <param name="trainingEpochs"> /// Number of training epochs. (All samples are trained in some random order, in every /// training epoch) /// </param> /// <exception cref="ArgumentNullException"> /// if <c>trainingSet</c> is <c>null</c> /// </exception> /// <exception cref="ArgumentException"> /// if <c>trainingEpochs</c> is zero or negative /// </exception> public virtual void Learn(Ajusteentrenamiento trainingSet, int trainingEpochs) { // Validate Helper.ValidateNotNull(trainingSet, "trainingSet"); Helper.ValidatePositive(trainingEpochs, "trainingEpochs"); if ((trainingSet.InputVectorLength != inputLayer.NeuronCount) || (trainingMethod == TrainingMethod.Supervised && trainingSet.OutputVectorLength != outputLayer.NeuronCount) || (trainingMethod == TrainingMethod.Unsupervised && trainingSet.OutputVectorLength != 0)) { throw new ArgumentException("Invalid training set"); } // Reset isStopping isStopping = false; // Re-Initialize the network Initialize(); for (int currentIteration = 0; currentIteration < trainingEpochs; currentIteration++) { int[] randomOrder = Helper.GetRandomOrder(trainingSet.TrainingSampleCount); // Beginning a new training epoch OnBeginEpoch(currentIteration, trainingSet); // Check for Jitter Epoch if (jitterEpoch > 0 && currentIteration % jitterEpoch == 0) { for (int i = 0; i < connectors.Count; i++) { connectors[i].Jitter(jitterNoiseLimit); } } for (int index = 0; index < trainingSet.TrainingSampleCount; index++) { Entrenamiento randomSample = trainingSet[randomOrder[index]]; // Learn a random training sample OnBeginSample(currentIteration, randomSample); LearnSample(trainingSet[randomOrder[index]], currentIteration, trainingEpochs); OnEndSample(currentIteration, randomSample); // Check if we need to stop if (isStopping) { isStopping = false; return; } } // Training Epoch successfully complete OnEndEpoch(currentIteration, trainingSet); // Check if we need to stop if (isStopping) { isStopping = false; return; } } }
/// <summary> /// Populates the serialization info with the data needed to serialize the connector /// </summary> /// <param name="info"> /// The serialization info to populate the data with /// </param> /// <param name="context"> /// The serialization context to use /// </param> /// <exception cref="ArgumentNullException"> /// If <c>info</c> is <c>null</c> /// </exception> public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { Helper.ValidateNotNull(info, "info"); info.AddValue("sourceLayer", sourceLayer, typeof(TSourceLayer)); info.AddValue("targetLayer", targetLayer, typeof(TTargetLayer)); info.AddValue("initializer", initializer, typeof(IInitializer)); info.AddValue("connectionMode", connectionMode, typeof(ConnectionMode)); }
/// <summary> /// Populates the serialization info with the data needed to serialize the layer /// </summary> /// <param name="info"> /// The serialization info to populate the data with /// </param> /// <param name="context"> /// The serialization context to use /// </param> /// <exception cref="ArgumentNullException"> /// If <c>info</c> is <c>null</c> /// </exception> public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { // Validate Helper.ValidateNotNull(info, "info"); // Populate info.AddValue("neuronCount", neurons.Length); info.AddValue("initializer", initializer, typeof(IInitializer)); info.AddValue("learningRateFunction", learningRateFunction, typeof(ILearningRateFunction)); }
/// <summary> /// Trains the network for the given training sample (Online training mode). Note that this /// method trains the sample only once, irrespective of what current epoch is. The arguments /// are just used to find out training progress and adjust parameters depending on it. /// </summary> /// <param name="trainingSample"> /// Training sample to use /// </param> /// <param name="currentIteration"> /// Current training iteration /// </param> /// <param name="trainingEpochs"> /// Number of training epochs /// </param> /// <exception cref="ArgumentNullException"> /// If <c>trainingSample</c> is <c>null</c> /// </exception> /// <exception cref="ArgumentException"> /// If <c>trainingEpochs</c> is not positive /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// If <c>currentIteration</c> is negative or, if it is not less than <c>trainingEpochs</c> /// </exception> public virtual void Learn(Entrenamiento trainingSample, int currentIteration, int trainingEpochs) { Helper.ValidateNotNull(trainingSample, "trainingSample"); Helper.ValidatePositive(trainingEpochs, "trainingEpochs"); Helper.ValidateWithinRange(currentIteration, 0, trainingEpochs - 1, "currentIteration"); OnBeginSample(currentIteration, trainingSample); LearnSample(trainingSample, currentIteration, trainingEpochs); OnEndSample(currentIteration, trainingSample); }
/// <summary> /// Deserialization Constructor /// </summary> /// <param name="info"> /// Serialization information to deserialize and obtain the data /// </param> /// <param name="context"> /// Serialization context to use /// </param> /// <exception cref="ArgumentNullException"> /// If <c>info</c> is <c>null</c> /// </exception> protected Layer(SerializationInfo info, StreamingContext context) { // Validate Helper.ValidateNotNull(info, "info"); // Deserialize int neuronCount = info.GetInt32("neuronCount"); this.neurons = new TNeuron[neuronCount]; this.initializer = info.GetValue("initializer", typeof(IInitializer)) as IInitializer; this.learningRateFunction = info.GetValue("learningRateFunction", typeof(ILearningRateFunction)) as ILearningRateFunction; }
/// <summary> /// Deserialization constructor. It is assumed that the serialization info provided contains /// valid data. /// </summary> /// <param name="info"> /// The serialization info to deserialize and obtain values /// </param> /// <param name="context"> /// Serialization context /// </param> /// <exception cref="ArgumentNullException"> /// If <c>info</c> is <c>null</c> /// </exception> public Network(SerializationInfo info, StreamingContext context) { // Validate Helper.ValidateNotNull(info, "info"); this.inputLayer = info.GetValue("inputLayer", typeof(ILayer)) as ILayer; this.outputLayer = info.GetValue("outputLayer", typeof(ILayer)) as ILayer; this.layers = info.GetValue("layers", typeof(IList <ILayer>)) as IList <ILayer>; this.connectors = info.GetValue("connectors", typeof(IList <IConnector>)) as IList <IConnector>; this.trainingMethod = (TrainingMethod)info.GetValue("trainingMethod", typeof(TrainingMethod)); this.jitterEpoch = info.GetInt32("jitterEpoch"); this.jitterNoiseLimit = info.GetDouble("jitterNoiseLimit"); }
/// <summary> /// Populates the serialization info with the data necessary to serialize the network. /// </summary> /// <param name="info"> /// The info to populate serialization data with /// </param> /// <param name="context"> /// Serialization context to use /// </param> /// <exception cref="ArgumentNullException"> /// If <c>info</c> is <c>null</c> /// </exception> public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { // Validate Helper.ValidateNotNull(info, "info"); // Populate data info.AddValue("inputLayer", inputLayer, typeof(ILayer)); info.AddValue("outputLayer", outputLayer, typeof(ILayer)); info.AddValue("layers", layers, typeof(IList <ILayer>)); info.AddValue("connectors", connectors, typeof(IList <IConnector>)); info.AddValue("trainingMethod", trainingMethod, typeof(TrainingMethod)); info.AddValue("jitterNoiseLimit", jitterNoiseLimit); info.AddValue("jitterEpoch", jitterEpoch); }
public Entrenamiento(SerializationInfo info, StreamingContext context) { Helper.ValidateNotNull(info, "info"); this.inputVector = (double[])info.GetValue("inputVector", typeof(double[])); this.outputVector = (double[])info.GetValue("outputVector", typeof(double[])); this.normalizedInputVector = Helper.Normalize(inputVector); this.normalizedOutputVector = Helper.Normalize(outputVector); hashCode = 0; for (int i = 0; i < inputVector.Length; i++) { hashCode ^= inputVector[i].GetHashCode(); } }
/// <summary> /// Sets neuron inputs to the values specified by the given array /// </summary> /// <param name="input"> /// the input array /// </param> /// <exception cref="ArgumentNullException"> /// If <c>input</c> is <c>null</c> /// </exception> /// <exception cref="ArgumentException"> /// If length of <c>input</c> array is different from number of neurons /// </exception> public virtual void SetInput(double[] input) { // Validate Helper.ValidateNotNull(input, "input"); if (neurons.Length != input.Length) { throw new ArgumentException("Length of input array should be same as neuron count", "input"); } // Bind inputs for (int i = 0; i < neurons.Length; i++) { neurons[i].Input = input[i]; } }
public void Add(Entrenamiento sample) { // Validation Helper.ValidateNotNull(sample, "sample"); if (sample.InputVector.Length != inputVectorLength) { throw new ArgumentException ("Input vector must be of size " + inputVectorLength, "sample"); } if (sample.OutputVector.Length != outputVectorLength) { throw new ArgumentException ("Output vector must be of size " + outputVectorLength, "sample"); } // Note that the reference is being added. (Sample is immutable) trainingSamples.Add(sample); }
public Entrenamiento(double[] inputVector, double[] outputVector) { // Validate Helper.ValidateNotNull(inputVector, "inputVector"); Helper.ValidateNotNull(outputVector, "outputVector"); // Clone and initialize this.inputVector = (double[])inputVector.Clone(); this.outputVector = (double[])outputVector.Clone(); // Some neural networks require inputs in normalized form. // As an optimization measure, we normalize and store training samples this.normalizedInputVector = Helper.Normalize(inputVector); this.normalizedOutputVector = Helper.Normalize(outputVector); // Calculate the hash code hashCode = 0; for (int i = 0; i < inputVector.Length; i++) { hashCode ^= inputVector[i].GetHashCode(); } }
/// <summary> /// Deserialization constructor /// </summary> /// <param name="info"> /// Serialization information to deserialize and obtain the data /// </param> /// <param name="context"> /// Serialization context to use /// </param> /// <exception cref="ArgumentNullException"> /// If <c>info</c> is <c>null</c> /// </exception> protected Connector(SerializationInfo info, StreamingContext context) { Helper.ValidateNotNull(info, "info"); this.sourceLayer = (TSourceLayer)info.GetValue("sourceLayer", typeof(TSourceLayer)); this.targetLayer = (TTargetLayer)info.GetValue("targetLayer", typeof(TTargetLayer)); this.initializer = (IInitializer)info.GetValue("initializer", typeof(IInitializer)); this.connectionMode = (ConnectionMode)info.GetValue("connectionMode", typeof(ConnectionMode)); targetLayer.SourceConnectors.Add(this); sourceLayer.TargetConnectors.Add(this); if (connectionMode == ConnectionMode.Complete) { synapses = new TSynapse[sourceLayer.NeuronCount * targetLayer.NeuronCount]; } else { synapses = new TSynapse[sourceLayer.NeuronCount]; } }
/// <summary> /// Creates a new neural network /// </summary> /// <param name="inputLayer"> /// The input layer /// </param> /// <param name="outputLayer"> /// The output layer /// </param> /// <param name="trainingMethod"> /// Training method to use /// </param> /// <exception cref="ArgumentNullException"> /// If <c>inputLayer</c> or <c>outputLayer</c> is <c>null</c>. /// </exception> /// <exception cref="ArgumentException"> /// If <c>trainingMethod</c> is invalid /// </exception> protected Network(ILayer inputLayer, ILayer outputLayer, TrainingMethod trainingMethod) { // Validate Helper.ValidateNotNull(inputLayer, "inputLayer"); Helper.ValidateNotNull(outputLayer, "outputLayer"); Helper.ValidateEnum(typeof(TrainingMethod), trainingMethod, "trainingMethod"); // Assign arguments to corresponding variables this.inputLayer = inputLayer; this.outputLayer = inputLayer; this.trainingMethod = trainingMethod; // Initialize jitter parameters with default values this.jitterEpoch = 73; this.jitterNoiseLimit = 0.03d; // Create the list of layers and connectors this.layers = new List <ILayer>(); this.connectors = new List <IConnector>(); // Populate the lists by visiting layers topologically starting from input layer Stack <ILayer> stack = new Stack <ILayer>(); stack.Push(inputLayer); // Indegree map IDictionary <ILayer, int> inDegree = new Dictionary <ILayer, int>(); while (stack.Count > 0) { // Add 'top of stack' to list of layers this.outputLayer = stack.Pop(); layers.Add(this.outputLayer); // Add targetConnectors to connectors list making sure that they do not lead to cycle foreach (IConnector connector in this.outputLayer.TargetConnectors) { connectors.Add(connector); ILayer targetLayer = connector.TargetLayer; if (layers.Contains(targetLayer)) { throw new InvalidOperationException("Cycle Exists in the network structure"); } // Virtually remove this layer inDegree[targetLayer] = inDegree.ContainsKey(targetLayer) ? inDegree[targetLayer] - 1 : targetLayer.SourceConnectors.Count - 1; // Push unvisited target layer onto the stack, if its effective inDegree is zero if (inDegree[targetLayer] == 0) { stack.Push(targetLayer); } } } // The last layer should be same as output layer if (outputLayer != this.outputLayer) { throw new ArgumentException("The outputLayer is invalid", "outputLayer"); } // Initialize the newly created network Initialize(); }
/// <summary> /// Sets the learning rate function. /// </summary> /// <param name="learningRateFunction"> /// Learning rate function to use. /// </param> /// <exception cref="System.ArgumentNullException"> /// If <c>learningRateFunction</c> is <c>null</c> /// </exception> public void SetLearningRate(ILearningRateFunction learningRateFunction) { Helper.ValidateNotNull(learningRateFunction, "learningRateFunction"); this.learningRateFunction = learningRateFunction; }