/* * Created on Aug 3, 2005 * */ package aima.learning.statistics; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import aima.probability.Randomizer; public class Layer { private List neurons; public Layer(int n) { neurons = new ArrayList(n); for (int i = 0; i < n; i++) { neurons.add(new Neuron()); } } public Layer(int n, double bias, ActivationFunction function) { neurons = new ArrayList(n); for (int i = 0; i < n; i++) { neurons.add(new Neuron(bias,function)); } } public Layer(int n, List biases, ActivationFunction function) { neurons = new ArrayList(n); for (int i = 0; i < n; i++) { neurons.add(new Neuron(biases.get(i),function)); } } public Neuron getNeuron(int zeroBasedIndex) { return neurons.get(zeroBasedIndex); } public void acceptInput(List input) { for (int i = 0; i < input.size(); i++) { neurons.get(i).acceptAsInput(input.get(i)); } } public List getError(List values) { List error = new ArrayList(); for (int i = 0; i < values.size(); i++) { error.add(neurons.get(i).error(values.get(i))); } return error; } public void connectTo(Layer layer, Randomizer weights) { for (Neuron source : neurons) { for (Neuron target : layer.neurons) { source.connectTo(target, weights.nextDouble()); } } } public void update() { for (Neuron n : neurons) { n.update(); } } public List activation() { List result = new ArrayList(); for (Neuron n: neurons){ result.add(n.activation()); } return result; } public Iterator iterator() { return neurons.iterator(); } /** * @return Returns the neurons. */ public List getNeurons() { return neurons; } public List weights(){ List weights = new ArrayList(); for(Neuron n:neurons){ for (Double weight :n.weights()){ weights.add(weight); } } return weights; } }