public Network(string name, int inputs, List<int> hiddens, int outputs, Termination termination, NetworkParameters parameters = null, int constants = 1)
        {
            Name = name;
            Termination = termination;
            if (parameters != null)
            {
                parameters.Assert();
                Parameters = parameters;
            }
            HiddensLayout = hiddens;

            List<List<Neuron>> levels = new List<List<Neuron>>();
            levels.Add(Enumerable.Range(0, inputs).Select(i => new Neuron(NeuronType.Input)).ToList());
            foreach (int hidden in hiddens)
                levels.Add(Enumerable.Range(0, hidden).Select(i => new Neuron(NeuronType.Hidden)).ToList());
            levels.Add(Enumerable.Range(0, outputs).Select(i => new Neuron(NeuronType.Output)).ToList());

            // Connects to all other neurons.. except input ones (this is used as the additive "constant" value attached to each sigmoid unit.)
            List<Neuron> connectToAll = Enumerable.Range(0, constants).Select(i => new Neuron(NeuronType.Constant) { Value = 1 }).ToList();

            for (int i = 0; i + 1 < levels.Count; ++i)
            {
                foreach (Neuron n in levels[i])
                    foreach (Neuron m in levels[i + 1])
                        n.Attach(m, new Weight(Parameters.InitialWeightInterval));

                foreach (Neuron c in connectToAll)
                    foreach (Neuron m in levels[i + 1])
                        c.Attach(m, new Weight(Parameters.InitialWeightInterval));
            }

            Neurons = levels.Aggregate<List<Neuron>>((aggregated, level) => { aggregated.AddRange(level); return aggregated; }).ToList();
            Neurons.AddRange(connectToAll);
        }
Exemplo n.º 2
0
        public Network(string name, int inputs, List <int> hiddens, int outputs, Termination termination, NetworkParameters parameters = null, int constants = 1)
        {
            Name        = name;
            Termination = termination;
            if (parameters != null)
            {
                parameters.Assert();
                Parameters = parameters;
            }
            HiddensLayout = hiddens;

            List <List <Neuron> > levels = new List <List <Neuron> >();

            levels.Add(Enumerable.Range(0, inputs).Select(i => new Neuron(NeuronType.Input)).ToList());
            foreach (int hidden in hiddens)
            {
                levels.Add(Enumerable.Range(0, hidden).Select(i => new Neuron(NeuronType.Hidden)).ToList());
            }
            levels.Add(Enumerable.Range(0, outputs).Select(i => new Neuron(NeuronType.Output)).ToList());

            // Connects to all other neurons.. except input ones (this is used as the additive "constant" value attached to each sigmoid unit.)
            List <Neuron> connectToAll = Enumerable.Range(0, constants).Select(i => new Neuron(NeuronType.Constant)
            {
                Value = 1
            }).ToList();

            for (int i = 0; i + 1 < levels.Count; ++i)
            {
                foreach (Neuron n in levels[i])
                {
                    foreach (Neuron m in levels[i + 1])
                    {
                        n.Attach(m, new Weight(Parameters.InitialWeightInterval));
                    }
                }

                foreach (Neuron c in connectToAll)
                {
                    foreach (Neuron m in levels[i + 1])
                    {
                        c.Attach(m, new Weight(Parameters.InitialWeightInterval));
                    }
                }
            }

            Neurons = levels.Aggregate <List <Neuron> >((aggregated, level) => { aggregated.AddRange(level); return(aggregated); }).ToList();
            Neurons.AddRange(connectToAll);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Trains a network using a given set of examples.
        /// </summary>
        /// <param name="examples">Set of examples.  Make sure features and labels lists are populated before training.</param>
        /// <param name="iterations">Number of iterations to train on the given set of examples</param>
        /// <returns>True if network is fully trained.  False otherwise.  (Based on Termination object given in constructor)</returns>
        public bool TrainNetwork(List <Example> examples, int iterations = 1)
        {
            AssertValidForTraining(examples);

            if (Termination.IsNetworkTrained)
            {
                return(true);
            }

            for (int i = 0; i < iterations; ++i)
            {
                Termination.CompleteIteration();  // Temporarily place in beginning to force gui to show initial error as found when using random weights.
                foreach (Example example in examples)
                {
                    LearnOneExample(example);
                }
                if (Termination.IsNetworkTrained)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 4
0
 public Network(string name, int inputs, int hiddens, int outputs, Termination termination, NetworkParameters parameters = null) : this(name, inputs, new List <int>() { hiddens }, outputs, termination, parameters)
 {
 }
 public Network(string name, int inputs, int hiddens, int outputs, Termination termination, NetworkParameters parameters = null)
     : this(name, inputs, new List<int>(){hiddens}, outputs, termination, parameters)
 {
 }