All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bigml.mimir.deepnet.layers.LegacyBlock Maven / Gradle / Ivy

The newest version!
package org.bigml.mimir.deepnet.layers;

import java.io.IOException;
import java.io.ObjectInputStream;

import org.bigml.mimir.deepnet.layers.twod.OutputTensor;

/**
 * A legacy block layer in a deepnet composed of a dense layer followed by a
 * batch normalization layer.  This class is provided for compatibility with
 * early iterations of BigML deepnets.  The layer itself is immutable and
 * thread-safety is guaranteed by the use of the OutputTensor class.
 *
 * @see OutputTensor
 * @author  Charles Parker
 */
public class LegacyBlock implements Layer {

    public LegacyBlock(String afn, Dense dLayer, LegacyBatchNormalize bnLayer) {
        _afn = Activation.getActivator(afn);
        _dense = dLayer;
        _batchnorm = bnLayer;

        _output = new OutputTensor(new int[] {getOutputLength()});
    }

    @Override
    public int getOutputLength() {
        return _batchnorm.getOutputLength();
    }

    @Override
    public float[] propagate(float[] input) {
        float[] output = _dense.propagate(input);
        float[] activations = _batchnorm.propagate(output);

        float[] layerOutputs = _output.get();
        System.arraycopy(activations, 0, layerOutputs, 0, activations.length);


        if (_afn != null && !_afn.equals(Activation.ActivationFn.IDENTITY))
            Activation.activate(layerOutputs, _afn);

        return layerOutputs;
    }

    private void readObject(ObjectInputStream stream)
            throws IOException, ClassNotFoundException {

        stream.defaultReadObject();
        _output = new OutputTensor(new int[] {getOutputLength()});
    }

    private Dense _dense;
    private LegacyBatchNormalize _batchnorm;
    private final Activation.ActivationFn _afn;
    private transient OutputTensor _output;

    private static final long serialVersionUID = 1L;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy