Search in sources :

Example 41 with SameDiff

use of org.nd4j.autodiff.samediff.SameDiff in project nd4j by deeplearning4j.

the class GradCheckMisc method testSqueezeGradient.

@Test
public void testSqueezeGradient() {
    int[] origShape = new int[] { 3, 4, 5 };
    for (int i = 0; i < 3; i++) {
        int[] shape = origShape.clone();
        shape[i] = 1;
        for (Pair<INDArray, String> p : NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, shape)) {
            INDArray inArr = p.getFirst().muli(100);
            SameDiff sd = SameDiff.create();
            SDVariable in = sd.var("in", inArr);
            SDVariable squeeze = sd.f().squeeze(in, i);
            // Using stdev here: mean/sum would backprop the same gradient for each input...
            SDVariable stdev = sd.standardDeviation("out", squeeze, true);
            int[] expShapePostSqueeze;
            switch(i) {
                case 0:
                    expShapePostSqueeze = new int[] { 4, 5 };
                    break;
                case 1:
                    expShapePostSqueeze = new int[] { 3, 5 };
                    break;
                case 2:
                    expShapePostSqueeze = new int[] { 3, 4 };
                    break;
                default:
                    throw new RuntimeException();
            }
            sd.execAndEndResult();
            INDArray squeezed = squeeze.getArr();
            assertArrayEquals(expShapePostSqueeze, squeezed.shape());
            INDArray out = sd.execAndEndResult();
            INDArray expOut = in.getArr().std(true, Integer.MAX_VALUE);
            assertEquals(expOut, out);
            String msg = "squeezeDim=" + i + ", source=" + p.getSecond();
            boolean ok = GradCheckUtil.checkGradients(sd);
            assertTrue(msg, ok);
        }
    }
}
Also used : SDVariable(org.nd4j.autodiff.samediff.SDVariable) INDArray(org.nd4j.linalg.api.ndarray.INDArray) SameDiff(org.nd4j.autodiff.samediff.SameDiff) Test(org.junit.Test)

Example 42 with SameDiff

use of org.nd4j.autodiff.samediff.SameDiff in project nd4j by deeplearning4j.

the class TensorFlowImportTest method testImportIris.

@Test
@Ignore
public void testImportIris() throws Exception {
    SameDiff graph = TFGraphMapper.getInstance().importGraph(new ClassPathResource("tf_graphs/train_iris.pb").getInputStream());
    assertNotNull(graph);
}
Also used : SameDiff(org.nd4j.autodiff.samediff.SameDiff) ClassPathResource(org.nd4j.linalg.io.ClassPathResource) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 43 with SameDiff

use of org.nd4j.autodiff.samediff.SameDiff in project nd4j by deeplearning4j.

the class TensorFlowImportTest method importGraph3.

@Test
@Ignore
public void importGraph3() throws Exception {
    SameDiff graph = TFGraphMapper.getInstance().importGraph(new ClassPathResource("tf_graphs/max_log_reg.pb.txt").getInputStream());
    assertNotNull(graph);
}
Also used : SameDiff(org.nd4j.autodiff.samediff.SameDiff) ClassPathResource(org.nd4j.linalg.io.ClassPathResource) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 44 with SameDiff

use of org.nd4j.autodiff.samediff.SameDiff in project nd4j by deeplearning4j.

the class TensorFlowImportTest method testInferShape.

@Test
public void testInferShape() throws IOException {
    /**
     * node {
     *         name: "input"
     *         op: "Placeholder"
     *         attr {
     *         key: "dtype"
     *         value {
     *         type: DT_FLOAT
     *         }
     *         }
     *         attr {
     *         key: "shape"
     *         value {
     *         shape {
     *         dim {
     *         size: -1
     *         }
     *         dim {
     *         size: 4
     *         }
     *         }
     *         }
     *         }
     *         }
     *         node {
     *         name: "bias"
     *         op: "Const"
     *         attr {
     *         key: "dtype"
     *         value {
     *         type: DT_FLOAT
     *         }
     *         }
     *         attr {
     *         key: "value"
     *         value {
     *         tensor {
     *         dtype: DT_FLOAT
     *         tensor_shape {
     *         dim {
     *         size: 4
     *         }
     *         }
     *         tensor_content: "\000\000\200?\000\000\000@\000\000@@\000\000\200@"
     *         }
     *         }
     *         }
     *         }
     *         node {
     *         name: "bias/read"
     *         op: "Identity"
     *         input: "bias"
     *         attr {
     *         key: "_class"
     *         value {
     *         list {
     *         s: "loc:@bias"
     *         }
     *         }
     *         }
     *         attr {
     *         key: "T"
     *         value {
     *         type: DT_FLOAT
     *         }
     *         }
     *         }
     *         node {
     *         name: "output"
     *         op: "BiasAdd"
     *         input: "input"
     *         input: "bias/read"
     *         attr {
     *         key: "data_format"
     *         value {
     *         s: "NHWC"
     *         }
     *         }
     *         attr {
     *         key: "T"
     *         value {
     *         type: DT_FLOAT
     *         }
     *         }
     *         }
     *         library {
     *         }
     */
    SameDiff graph = TFGraphMapper.getInstance().importGraph(new ClassPathResource("tf_graphs/examples/bias_add/frozen_model.pb").getInputStream());
    assertNotNull(graph);
    INDArray input = Nd4j.linspace(1, 40, 40).reshape(10, 4);
    INDArray expectedOutput = Nd4j.linspace(1, 40, 40).reshape(10, 4).addRowVector(Nd4j.linspace(1, 4, 4));
    INDArray actual = graph.execWithPlaceHolderAndEndResult(Collections.singletonMap("input", input));
    assertEquals(input, graph.getVariable("input").getArr());
    assertArrayEquals(input.shape(), graph.getShapeForVarName(graph.getVariable("input").getVarName()));
    assertEquals(expectedOutput, actual);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) SameDiff(org.nd4j.autodiff.samediff.SameDiff) ClassPathResource(org.nd4j.linalg.io.ClassPathResource) Test(org.junit.Test)

Example 45 with SameDiff

use of org.nd4j.autodiff.samediff.SameDiff in project nd4j by deeplearning4j.

the class TensorFlowImportTest method importGraph4.

@Test
@Ignore
public void importGraph4() throws Exception {
    SameDiff graph = TFGraphMapper.getInstance().importGraph(new ClassPathResource("tf_graphs/max_multiply.pb.txt").getInputStream());
    assertNotNull(graph);
    val p0 = Nd4j.create(10, 10).assign(2.0);
    val p1 = Nd4j.create(10, 10).assign(3.0);
    graph.associateArrayWithVariable(p0, graph.variableMap().get("Placeholder"));
    graph.associateArrayWithVariable(p1, graph.variableMap().get("Placeholder_1"));
    graph.var("Placeholder", p0);
    graph.var("Placeholder_1", p1);
    val res = graph.execAndEndResult();
    assertEquals(6.0, res.meanNumber().doubleValue(), 1e-5);
}
Also used : lombok.val(lombok.val) SameDiff(org.nd4j.autodiff.samediff.SameDiff) ClassPathResource(org.nd4j.linalg.io.ClassPathResource) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

SameDiff (org.nd4j.autodiff.samediff.SameDiff)50 Test (org.junit.Test)42 SDVariable (org.nd4j.autodiff.samediff.SDVariable)41 INDArray (org.nd4j.linalg.api.ndarray.INDArray)37 ArrayList (java.util.ArrayList)10 DynamicCustomOp (org.nd4j.linalg.api.ops.DynamicCustomOp)10 Ignore (org.junit.Ignore)7 ClassPathResource (org.nd4j.linalg.io.ClassPathResource)6 lombok.val (lombok.val)4 LossFunctions (org.nd4j.autodiff.loss.LossFunctions)4 LossInfo (org.nd4j.autodiff.loss.LossInfo)4 BernoulliDistribution (org.nd4j.linalg.api.ops.random.impl.BernoulliDistribution)4 DifferentialFunction (org.nd4j.autodiff.functions.DifferentialFunction)2 Triple (org.nd4j.linalg.primitives.Triple)2 ZeroInitScheme (org.nd4j.weightinit.impl.ZeroInitScheme)2 DataOutputStream (java.io.DataOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1