Search in sources :

Example 6 with IOutputLayer

use of org.deeplearning4j.nn.api.layers.IOutputLayer in project deeplearning4j by deeplearning4j.

the class MultiLayerNetwork method labelProbabilities.

/**
     * Returns the probabilities for each label
     * for each example row wise
     *
     * @param examples the examples to classify (one example in each row)
     * @return the likelihoods of each example and each label
     */
@Override
public INDArray labelProbabilities(INDArray examples) {
    List<INDArray> feed = feedForward(examples);
    IOutputLayer o = (IOutputLayer) getOutputLayer();
    return o.labelProbabilities(feed.get(feed.size() - 1));
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) IOutputLayer(org.deeplearning4j.nn.api.layers.IOutputLayer)

Example 7 with IOutputLayer

use of org.deeplearning4j.nn.api.layers.IOutputLayer in project deeplearning4j by deeplearning4j.

the class MultiLayerNetwork method score.

/**Calculate the score (loss function) of the prediction with respect to the true labels<br>
     * @param data data to calculate score for
     * @param training If true: score during training. If false: score at test time. This can affect the application of
     *                 certain features, such as dropout and dropconnect (which are applied at training time only)
     * @return the score (value of the loss function)
     */
public double score(DataSet data, boolean training) {
    boolean hasMaskArray = data.hasMaskArrays();
    if (hasMaskArray)
        setLayerMaskArrays(data.getFeaturesMaskArray(), data.getLabelsMaskArray());
    // activation for output layer is calculated in computeScore
    List<INDArray> activations = feedForwardToLayer(layers.length - 2, data.getFeatureMatrix(), training);
    int n = activations.size();
    setLabels(data.getLabels());
    if (getOutputLayer() instanceof IOutputLayer) {
        IOutputLayer ol = (IOutputLayer) getOutputLayer();
        INDArray olInput = activations.get(n - 1);
        if (getLayerWiseConfigurations().getInputPreProcess(n - 1) != null) {
            olInput = getLayerWiseConfigurations().getInputPreProcess(n - 1).preProcess(olInput, input.size(0));
        }
        //Feedforward doesn't include output layer for efficiency
        ol.setInput(olInput);
        ol.setLabels(data.getLabels());
        ol.computeScore(calcL1(true), calcL2(true), training);
        this.score = ol.score();
    } else {
        log.warn("Cannot calculate score wrt labels without an OutputLayer");
        return 0.0;
    }
    if (hasMaskArray)
        clearLayerMaskArrays();
    return score();
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) IOutputLayer(org.deeplearning4j.nn.api.layers.IOutputLayer)

Example 8 with IOutputLayer

use of org.deeplearning4j.nn.api.layers.IOutputLayer in project deeplearning4j by deeplearning4j.

the class KerasModel method getComputationGraphConfiguration.

/**
     * Configure a ComputationGraph from this Keras Model configuration.
     *
     * @return          ComputationGraph
     */
public ComputationGraphConfiguration getComputationGraphConfiguration() throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException {
    if (!this.className.equals(MODEL_CLASS_NAME_MODEL) && !this.className.equals(MODEL_CLASS_NAME_SEQUENTIAL))
        throw new InvalidKerasConfigurationException("Keras model class name " + this.className + " incompatible with ComputationGraph");
    NeuralNetConfiguration.Builder modelBuilder = new NeuralNetConfiguration.Builder();
    ComputationGraphConfiguration.GraphBuilder graphBuilder = modelBuilder.graphBuilder();
    /* Build String array of input layer names, add to ComputationGraph. */
    String[] inputLayerNameArray = new String[this.inputLayerNames.size()];
    this.inputLayerNames.toArray(inputLayerNameArray);
    graphBuilder.addInputs(inputLayerNameArray);
    /* Build InputType array of input layer types, add to ComputationGraph. */
    List<InputType> inputTypeList = new ArrayList<InputType>();
    for (String inputLayerName : this.inputLayerNames) inputTypeList.add(this.layers.get(inputLayerName).getOutputType());
    InputType[] inputTypes = new InputType[inputTypeList.size()];
    inputTypeList.toArray(inputTypes);
    graphBuilder.setInputTypes(inputTypes);
    /* Build String array of output layer names, add to ComputationGraph. */
    String[] outputLayerNameArray = new String[this.outputLayerNames.size()];
    this.outputLayerNames.toArray(outputLayerNameArray);
    graphBuilder.setOutputs(outputLayerNameArray);
    Map<String, InputPreProcessor> preprocessors = new HashMap<String, InputPreProcessor>();
    /* Add layersOrdered one at a time. */
    for (KerasLayer layer : this.layersOrdered) {
        /* Get inbound layer names. */
        List<String> inboundLayerNames = layer.getInboundLayerNames();
        String[] inboundLayerNamesArray = new String[inboundLayerNames.size()];
        inboundLayerNames.toArray(inboundLayerNamesArray);
        /* Get inbound InputTypes and InputPreProcessor, if necessary. */
        List<InputType> inboundTypeList = new ArrayList<InputType>();
        for (String layerName : inboundLayerNames) inboundTypeList.add(this.outputTypes.get(layerName));
        InputType[] inboundTypeArray = new InputType[inboundTypeList.size()];
        inboundTypeList.toArray(inboundTypeArray);
        InputPreProcessor preprocessor = layer.getInputPreprocessor(inboundTypeArray);
        if (layer.usesRegularization())
            modelBuilder.setUseRegularization(true);
        if (layer.isLayer()) {
            /* Add DL4J layer. */
            if (preprocessor != null)
                preprocessors.put(layer.getLayerName(), preprocessor);
            graphBuilder.addLayer(layer.getLayerName(), layer.getLayer(), inboundLayerNamesArray);
            if (this.outputLayerNames.contains(layer.getLayerName()) && !(layer.getLayer() instanceof IOutputLayer))
                log.warn("Model cannot be trained: output layer " + layer.getLayerName() + " is not an IOutputLayer (no loss function specified)");
        } else if (layer.isVertex()) {
            /* Add DL4J vertex. */
            if (preprocessor != null)
                preprocessors.put(layer.getLayerName(), preprocessor);
            graphBuilder.addVertex(layer.getLayerName(), layer.getVertex(), inboundLayerNamesArray);
            if (this.outputLayerNames.contains(layer.getLayerName()) && !(layer.getVertex() instanceof IOutputLayer))
                log.warn("Model cannot be trained: output vertex " + layer.getLayerName() + " is not an IOutputLayer (no loss function specified)");
        } else if (layer.isInputPreProcessor()) {
            if (preprocessor == null)
                throw new UnsupportedKerasConfigurationException("Layer " + layer.getLayerName() + " could not be mapped to Layer, Vertex, or InputPreProcessor");
            graphBuilder.addVertex(layer.getLayerName(), new PreprocessorVertex(preprocessor), inboundLayerNamesArray);
        }
        if (this.outputLayerNames.contains(layer.getLayerName()))
            log.warn("Model cannot be trained: output " + layer.getLayerName() + " is not an IOutputLayer (no loss function specified)");
    }
    graphBuilder.setInputPreProcessors(preprocessors);
    /* Whether to use standard backprop (or BPTT) or truncated BPTT. */
    if (this.useTruncatedBPTT && this.truncatedBPTT > 0)
        graphBuilder.backpropType(BackpropType.TruncatedBPTT).tBPTTForwardLength(truncatedBPTT).tBPTTBackwardLength(truncatedBPTT);
    else
        graphBuilder.backpropType(BackpropType.Standard);
    return graphBuilder.build();
}
Also used : PreprocessorVertex(org.deeplearning4j.nn.conf.graph.PreprocessorVertex) InputPreProcessor(org.deeplearning4j.nn.conf.InputPreProcessor) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) InputType(org.deeplearning4j.nn.conf.inputs.InputType) ComputationGraphConfiguration(org.deeplearning4j.nn.conf.ComputationGraphConfiguration) IOutputLayer(org.deeplearning4j.nn.api.layers.IOutputLayer)

Example 9 with IOutputLayer

use of org.deeplearning4j.nn.api.layers.IOutputLayer in project deeplearning4j by deeplearning4j.

the class MultiLayerNetwork method pretrain.

/**
     * @deprecated use {@link #pretrain(DataSetIterator)} or {@link #pretrainLayer(int, DataSetIterator)} or {@link #pretrainLayer(int, INDArray)}.
     * Pretraining each layer in a row on a single minibatch (as per this method) instead of N epochs per layer is not advisable.
     */
@Deprecated
public void pretrain(INDArray input) {
    if (!layerWiseConfigurations.isPretrain())
        return;
    if (flattenedGradients == null)
        initGradientsView();
    /* During pretrain, feed forward expected activations of network, use activation cooccurrences during pretrain  */
    int miniBatchSize = input.size(0);
    INDArray layerInput = null;
    Layer layer;
    int nPretrainLayers = getnLayers();
    if (getLayer(getnLayers() - 1) instanceof IOutputLayer)
        nPretrainLayers--;
    for (int i = 0; i < nPretrainLayers; i++) {
        layer = getLayer(i);
        if (i == 0) {
            if (getLayerWiseConfigurations().getInputPreProcess(i) != null) {
                layerInput = getLayerWiseConfigurations().getInputPreProcess(i).preProcess(input, miniBatchSize);
            } else {
                layerInput = input;
            }
        } else {
            layerInput = activationFromPrevLayer(i - 1, layerInput, true);
        }
        layer.conf().setPretrain(true);
        layer.fit(layerInput);
        layer.conf().setPretrain(false);
    }
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) FeedForwardLayer(org.deeplearning4j.nn.conf.layers.FeedForwardLayer) FrozenLayer(org.deeplearning4j.nn.layers.FrozenLayer) IOutputLayer(org.deeplearning4j.nn.api.layers.IOutputLayer) RecurrentLayer(org.deeplearning4j.nn.api.layers.RecurrentLayer) IOutputLayer(org.deeplearning4j.nn.api.layers.IOutputLayer)

Example 10 with IOutputLayer

use of org.deeplearning4j.nn.api.layers.IOutputLayer in project deeplearning4j by deeplearning4j.

the class MultiLayerNetwork method initialize.

/**
     * Sets the input and labels from this dataset
     *
     * @param data the dataset to initialize with
     */
public void initialize(DataSet data) {
    setInput(data.getFeatureMatrix());
    feedForward(getInput());
    this.labels = data.getLabels();
    if (getOutputLayer() instanceof IOutputLayer) {
        IOutputLayer ol = (IOutputLayer) getOutputLayer();
        ol.setLabels(labels);
    }
}
Also used : IOutputLayer(org.deeplearning4j.nn.api.layers.IOutputLayer)

Aggregations

IOutputLayer (org.deeplearning4j.nn.api.layers.IOutputLayer)17 INDArray (org.nd4j.linalg.api.ndarray.INDArray)13 FrozenLayer (org.deeplearning4j.nn.layers.FrozenLayer)6 RecurrentLayer (org.deeplearning4j.nn.api.layers.RecurrentLayer)5 FeedForwardLayer (org.deeplearning4j.nn.conf.layers.FeedForwardLayer)5 Gradient (org.deeplearning4j.nn.gradient.Gradient)4 NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)3 DefaultGradient (org.deeplearning4j.nn.gradient.DefaultGradient)3 Pair (org.deeplearning4j.berkeley.Pair)2 Triple (org.deeplearning4j.berkeley.Triple)2 Layer (org.deeplearning4j.nn.api.Layer)2 InputPreProcessor (org.deeplearning4j.nn.conf.InputPreProcessor)2 InputType (org.deeplearning4j.nn.conf.inputs.InputType)2 GraphVertex (org.deeplearning4j.nn.graph.vertex.GraphVertex)2 VertexIndices (org.deeplearning4j.nn.graph.vertex.VertexIndices)2 ArrayList (java.util.ArrayList)1 Evaluation (org.deeplearning4j.eval.Evaluation)1 Updater (org.deeplearning4j.nn.api.Updater)1 ComputationGraphConfiguration (org.deeplearning4j.nn.conf.ComputationGraphConfiguration)1 PreprocessorVertex (org.deeplearning4j.nn.conf.graph.PreprocessorVertex)1