Class NeuralNetwork<T extends Trainer>

java.lang.Object
deepnetts.net.NeuralNetwork<T>
All Implemented Interfaces:
TrainerProvider<T>, Serializable, AutoCloseable
Direct Known Subclasses:
CBOWNetwork, ConvolutionalNetwork, FeedForwardNetwork, FeedForwardNetworkLM, SkipGramNetwork

public class NeuralNetwork<T extends Trainer> extends Object implements TrainerProvider<T>, Serializable, AutoCloseable
Base class for all neural networks in Deep Netts. Holds a list of abstract layers and loss function. Provides methods for forward and backward calculation, and to access input and output layers. Also provides network and output labels.
See Also:
  • Field Details

  • Constructor Details

    • NeuralNetwork

      protected NeuralNetwork()
  • Method Details

    • setInput

      public void setInput(TensorBase inputs)
      Sets network input and calculates entire network (triggers forward pass).
      Parameters:
      inputs - input tensor
    • setInput

      @Deprecated public void setInput(float[] inputs)
      Deprecated.
    • getOutput

      public float[] getOutput()
      Returns network's output.
      Returns:
      network's output
    • getOutputAsTensor

      public TensorBase getOutputAsTensor()
    • setOutputError

      public void setOutputError(TensorBase outputErrors)
      Sets the network's output errors, which are a difference between actual(predicted) and target output.
      Parameters:
      outputErrors - array of errors for each output, a difference between actual(predicted) and target output
    • train

      public void train(javax.visrec.ml.data.DataSet<? extends MLDataItem> trainingSet)
      Trains the neural network using specified training set.
      Parameters:
      trainingSet - example data given as (input, output) pairs to train the network
    • predict

      public TensorBase predict(TensorBase input)
      Returns the prediction of this neural network for the given input. This is the main method to use a trained neural network for inference/prediction. A well trained neural network should provide predictions with low error. Both input and returned prediction are tensors, which are essentially multidimensional arrays.
      Parameters:
      input - input for the neural network
      Returns:
      network's prediction.
      See Also:
    • test

      public javax.visrec.ml.eval.EvaluationMetrics test(javax.visrec.ml.data.DataSet<? extends MLDataItem> testSet)
      Tests how good are predictions of this network using specified test set. Automatically detects which type of task is network configured to perform and applies appropriate evaluation/test procedure using corresponding Evaluator.
      Parameters:
      testSet - data set to test/evaluate predictions
      Returns:
      evaluation metrics that show how good this network is at predicting unseen data
      See Also:
    • applyWeightChanges

      public void applyWeightChanges()
      Applies weight changes calculated in backward pass to all layers.
    • forward

      protected void forward()
      Performs a forward pass - calculations of all layers in the network for the network's current input. This method iterates and calculates all layers in this network. Assumes that network has previously set input using setInput method.
    • backward

      public void backward()
      Performs a backward bass across all layers in neural network, which is the calculation of corrections for the network internal parameters (weights). This method invokes the training steps for all the layers starting from the last/output layer and going backwards to first/input layer.
    • addLayer

      protected void addLayer(AbstractLayer layer)
      Adds a specified layer as a next layer in the network.
      Parameters:
      layer - layer to add to the network
      See Also:
    • getLayers

      public List<AbstractLayer> getLayers()
      Gets layers of this neural network.
      Returns:
      layers of this neural network
    • getLayerAt

      public AbstractLayer getLayerAt(int idx)
    • getInputLayer

      public InputLayer getInputLayer()
      Returns the input layer of this neural network. Input layer is the first layer in network which accepts the external input for the network, and forwards it to the next layer in the network.
      Returns:
      input layer of this neural network
      See Also:
    • getOutputLayer

      public OutputLayer getOutputLayer()
      Returns the output layer of this network. Output layer is the last layer of the network which provides final result of the network - predictions.
      Returns:
      output layer of this network.
      See Also:
    • setOutputLabels

      public void setOutputLabels(String... outputLabels)
      Sets output labels of this network.
      Parameters:
      outputLabels - labels which correspond to outputs of the network.
    • getOutputLabels

      public String[] getOutputLabels()
      Returns all labels for outputs of this network. Each output of the network should have a label which describes what that output represents.
      Returns:
      labels for outputs of this network.
    • getOutputLabel

      public String getOutputLabel(int i)
      Gets a label of the i-th output this network. Each output of the network should have a label which describes what that output represents.
      Parameters:
      i - idx position of the output
      Returns:
      label for the i-th output
    • setInputLayer

      protected void setInputLayer(InputLayer inputLayer)
      Sets input layer of this network during the network building.
      Parameters:
      inputLayer - layer to be used as input of the network.
      See Also:
    • setOutputLayer

      protected void setOutputLayer(OutputLayer outputLayer)
      Sets output layer of this network during the network building.
      Parameters:
      outputLayer - layer to be used as output.
      See Also:
    • getLossFunction

      public LossFunction getLossFunction()
      Returns a loss function of this network, which is used to calculate total network error during the training.
      Returns:
      loss function of this network
      See Also:
    • setLossFunction

      public void setLossFunction(LossFunction lossFunction)
      Sets a loss function of this network, which is used to calculate total network error during the training.
      Parameters:
      lossFunction - loss function to use during the training
    • getLabel

      public String getLabel()
      Returns the label(name) of this neural network
      Returns:
      label of this network
    • setLabel

      public void setLabel(String label)
      Sets label(name) for this neural network.
      Parameters:
      label - label for this network
    • getL2RegSum

      public float getL2RegSum()
      Calculates and returns L2 regularization sum of the entire network (all layers included). This value is used during the training to prevent over-fitting.
      Returns:
      L2 regularization sum
    • getL1RegSum

      public float getL1RegSum()
      Calculates and returns L1 regularization sum of the entire network (all layers included). This value is used during the training to prevent over-fitting.
      Returns:
      L2 regularization sum
    • getTrainer

      public T getTrainer()
      Returns a training algorithm of this neural network. Training algorithm performs tuning of the network's internal parameter(weights) in order to minimize an error.
      Specified by:
      getTrainer in interface TrainerProvider<T extends Trainer>
      Returns:
      training algorithm of this network
      See Also:
    • setTrainer

      public void setTrainer(T trainer)
      Sets the training algorithm of this neural network.
      Specified by:
      setTrainer in interface TrainerProvider<T extends Trainer>
      Parameters:
      trainer - training algorithm to use for this network
      See Also:
    • getNormalizer

      public AbstractScaler getNormalizer()
      Returns data normalization method that is applied to network's inputs.
      Returns:
    • setNormalizer

      public void setNormalizer(AbstractScaler normalizer)
      Sets normalization data normalization method that is applied to network's inputs.
      Parameters:
      normalizer -
    • toString

      public String toString()
      Returns string representation of this network including all layers and settings.
      Overrides:
      toString in class Object
      Returns:
      string representation of this network
    • getPreprocessing

      public Preprocessing<Tensor> getPreprocessing()
      Gets preprocessing that needs to be performed before input is fed to this network.
      Returns:
    • setPreprocessing

      public void setPreprocessing(Preprocessing<Tensor> preprocessing)
      Sets preprocessing that needs to be performed before input is fed to this network.
      Parameters:
      preprocessing -
    • save

      public void save(String fileName) throws IOException
      Saves this network using serialization to file with specified fileName.
      Parameters:
      fileName - name of the file to save network
      Throws:
      IOException
    • load

      public static <T> T load(String fileName, Class<T> clazz) throws IOException, ClassNotFoundException
      Loads and returns neural network previously saved to a file.
      Type Parameters:
      T - type(class) of the network to lead and return.
      Parameters:
      fileName - name of the file to load network from
      clazz - class of the neural network to load
      Returns:
      loaded neural network
      Throws:
      IOException
      ClassNotFoundException
    • getMode

      public Mode getMode()
    • setMode

      public void setMode(Mode mode)
    • getThreadPool

      public DeepNettsThreadPool getThreadPool()
    • close

      public void close() throws Exception
      Specified by:
      close in interface AutoCloseable
      Throws:
      Exception