Search in sources :

Example 1 with Explodable

use of com.simiacryptus.mindseye.layers.cudnn.Explodable in project MindsEye by SimiaCryptus.

the class StandardLayerTests method run.

/**
 * Test.
 *
 * @param log the log
 */
public void run(@Nonnull final NotebookOutput log) {
    long seed = (long) (Math.random() * Long.MAX_VALUE);
    int[][] smallDims = getSmallDims(new Random(seed));
    final Layer smallLayer = getLayer(smallDims, new Random(seed));
    int[][] largeDims = getLargeDims(new Random(seed));
    final Layer largeLayer = getLayer(largeDims, new Random(seed));
    try {
        if (smallLayer instanceof DAGNetwork) {
            try {
                log.h1("Network Diagram");
                log.p("This is a network apply the following layout:");
                log.code(() -> {
                    return Graphviz.fromGraph(TestUtil.toGraph((DAGNetwork) smallLayer)).height(400).width(600).render(Format.PNG).toImage();
                });
            } catch (Throwable e) {
                logger.info("Error plotting graph", e);
            }
        } else if (smallLayer instanceof Explodable) {
            try {
                Layer explode = ((Explodable) smallLayer).explode();
                if (explode instanceof DAGNetwork) {
                    log.h1("Exploded Network Diagram");
                    log.p("This is a network apply the following layout:");
                    @Nonnull DAGNetwork network = (DAGNetwork) explode;
                    log.code(() -> {
                        @Nonnull Graphviz graphviz = Graphviz.fromGraph(TestUtil.toGraph(network)).height(400).width(600);
                        @Nonnull File file = new File(log.getResourceDir(), log.getName() + "_network.svg");
                        graphviz.render(Format.SVG_STANDALONE).toFile(file);
                        log.link(file, "Saved to File");
                        return graphviz.render(Format.SVG).toString();
                    });
                }
            } catch (Throwable e) {
                logger.info("Error plotting graph", e);
            }
        }
        @Nonnull ArrayList<TestError> exceptions = standardTests(log, seed);
        if (!exceptions.isEmpty()) {
            if (smallLayer instanceof DAGNetwork) {
                for (@Nonnull Invocation invocation : getInvocations(smallLayer, smallDims)) {
                    log.h1("Small SubTests: " + invocation.getLayer().getClass().getSimpleName());
                    log.p(Arrays.deepToString(invocation.getDims()));
                    tests(log, getLittleTests(), invocation, exceptions);
                    invocation.freeRef();
                }
            }
            if (largeLayer instanceof DAGNetwork) {
                testEquivalency = false;
                for (@Nonnull Invocation invocation : getInvocations(largeLayer, largeDims)) {
                    log.h1("Large SubTests: " + invocation.getLayer().getClass().getSimpleName());
                    log.p(Arrays.deepToString(invocation.getDims()));
                    tests(log, getBigTests(), invocation, exceptions);
                    invocation.freeRef();
                }
            }
        }
        log.code(() -> {
            throwException(exceptions);
        });
    } finally {
        smallLayer.freeRef();
        largeLayer.freeRef();
    }
    getFinalTests().stream().filter(x -> null != x).forEach(test -> {
        final Layer perfLayer;
        perfLayer = getLayer(largeDims, new Random(seed));
        perfLayer.assertAlive();
        @Nonnull Layer copy;
        copy = perfLayer.copy();
        Tensor[] randomize = randomize(largeDims);
        try {
            test.test(log, copy, randomize);
        } finally {
            test.freeRef();
            for (@Nonnull Tensor tensor : randomize) {
                tensor.freeRef();
            }
            perfLayer.freeRef();
            copy.freeRef();
        }
    });
}
Also used : JsonObject(com.google.gson.JsonObject) Graphviz(guru.nidi.graphviz.engine.Graphviz) Arrays(java.util.Arrays) Tensor(com.simiacryptus.mindseye.lang.Tensor) ReferenceCountingBase(com.simiacryptus.mindseye.lang.ReferenceCountingBase) NotebookReportBase(com.simiacryptus.mindseye.test.NotebookReportBase) HashMap(java.util.HashMap) Random(java.util.Random) Result(com.simiacryptus.mindseye.lang.Result) DataSerializer(com.simiacryptus.mindseye.lang.DataSerializer) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Format(guru.nidi.graphviz.engine.Format) Map(java.util.Map) Layer(com.simiacryptus.mindseye.lang.Layer) CudaError(com.simiacryptus.mindseye.lang.cudnn.CudaError) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) ReferenceCounting(com.simiacryptus.mindseye.lang.ReferenceCounting) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) SysOutInterceptor(com.simiacryptus.util.test.SysOutInterceptor) Collection(java.util.Collection) TestUtil(com.simiacryptus.mindseye.test.TestUtil) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) LayerBase(com.simiacryptus.mindseye.lang.LayerBase) Explodable(com.simiacryptus.mindseye.layers.cudnn.Explodable) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) LifecycleException(com.simiacryptus.mindseye.lang.LifecycleException) Explodable(com.simiacryptus.mindseye.layers.cudnn.Explodable) Graphviz(guru.nidi.graphviz.engine.Graphviz) Tensor(com.simiacryptus.mindseye.lang.Tensor) Nonnull(javax.annotation.Nonnull) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) Layer(com.simiacryptus.mindseye.lang.Layer) Random(java.util.Random) File(java.io.File)

Example 2 with Explodable

use of com.simiacryptus.mindseye.layers.cudnn.Explodable in project MindsEye by SimiaCryptus.

the class ImageClassifier method add.

/**
 * Add.
 *
 * @param layer the layer
 * @param model the model
 * @return the layer
 */
@Nonnull
protected static Layer add(@Nonnull Layer layer, @Nonnull PipelineNetwork model) {
    name(layer);
    if (layer instanceof Explodable) {
        Layer explode = ((Explodable) layer).explode();
        try {
            if (explode instanceof DAGNetwork) {
                ((DAGNetwork) explode).visitNodes(node -> name(node.getLayer()));
                log.info(String.format("Exploded %s to %s (%s nodes)", layer.getName(), explode.getClass().getSimpleName(), ((DAGNetwork) explode).getNodes().size()));
            } else {
                log.info(String.format("Exploded %s to %s (%s nodes)", layer.getName(), explode.getClass().getSimpleName(), explode.getName()));
            }
            return add(explode, model);
        } finally {
            layer.freeRef();
        }
    } else {
        model.wrap(layer);
        return layer;
    }
}
Also used : Explodable(com.simiacryptus.mindseye.layers.cudnn.Explodable) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) EntropyLossLayer(com.simiacryptus.mindseye.layers.java.EntropyLossLayer) ActivationLayer(com.simiacryptus.mindseye.layers.cudnn.ActivationLayer) SimpleConvolutionLayer(com.simiacryptus.mindseye.layers.cudnn.SimpleConvolutionLayer) FullyConnectedLayer(com.simiacryptus.mindseye.layers.cudnn.FullyConnectedLayer) Layer(com.simiacryptus.mindseye.lang.Layer) ConvolutionLayer(com.simiacryptus.mindseye.layers.cudnn.ConvolutionLayer) LinearActivationLayer(com.simiacryptus.mindseye.layers.java.LinearActivationLayer) BiasLayer(com.simiacryptus.mindseye.layers.java.BiasLayer) Nonnull(javax.annotation.Nonnull)

Aggregations

Layer (com.simiacryptus.mindseye.lang.Layer)2 Explodable (com.simiacryptus.mindseye.layers.cudnn.Explodable)2 DAGNetwork (com.simiacryptus.mindseye.network.DAGNetwork)2 Nonnull (javax.annotation.Nonnull)2 JsonObject (com.google.gson.JsonObject)1 DataSerializer (com.simiacryptus.mindseye.lang.DataSerializer)1 LayerBase (com.simiacryptus.mindseye.lang.LayerBase)1 LifecycleException (com.simiacryptus.mindseye.lang.LifecycleException)1 ReferenceCounting (com.simiacryptus.mindseye.lang.ReferenceCounting)1 ReferenceCountingBase (com.simiacryptus.mindseye.lang.ReferenceCountingBase)1 Result (com.simiacryptus.mindseye.lang.Result)1 Tensor (com.simiacryptus.mindseye.lang.Tensor)1 CudaError (com.simiacryptus.mindseye.lang.cudnn.CudaError)1 ActivationLayer (com.simiacryptus.mindseye.layers.cudnn.ActivationLayer)1 ConvolutionLayer (com.simiacryptus.mindseye.layers.cudnn.ConvolutionLayer)1 FullyConnectedLayer (com.simiacryptus.mindseye.layers.cudnn.FullyConnectedLayer)1 SimpleConvolutionLayer (com.simiacryptus.mindseye.layers.cudnn.SimpleConvolutionLayer)1 BiasLayer (com.simiacryptus.mindseye.layers.java.BiasLayer)1 EntropyLossLayer (com.simiacryptus.mindseye.layers.java.EntropyLossLayer)1 LinearActivationLayer (com.simiacryptus.mindseye.layers.java.LinearActivationLayer)1