Java Code Examples for org.nd4j.autodiff.samediff.SDVariable#name()

The following examples show how to use org.nd4j.autodiff.samediff.SDVariable#name() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseTransformOp.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public BaseTransformOp(SameDiff sameDiff,
                       SDVariable i_v,
                       long[] shape,
                       boolean inPlace,
                       Object[] extraArgs) {
    super(sameDiff,inPlace,extraArgs);

    if (i_v != null) {
        SameDiffUtils.validateDifferentialFunctionSameDiff(sameDiff, i_v, this);
        this.xVertexId = i_v.name();
        sameDiff.addArgsFor(new SDVariable[]{i_v},this);
    } else {
        throw new IllegalArgumentException("Input must not null variable.");
    }

}
 
Example 2
Source File: BaseTransformOp.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public BaseTransformOp(SameDiff sameDiff,
                       SDVariable i_v1,
                       SDVariable i_v2,
                       Object[] extraArgs) {
    super(sameDiff,extraArgs);
    if (i_v1 != null && i_v2 != null) {

        SameDiffUtils.validateDifferentialFunctionSameDiff(sameDiff, i_v1, this);
        SameDiffUtils.validateDifferentialFunctionSameDiff(sameDiff, i_v2, this);
        this.sameDiff = sameDiff;
        this.xVertexId = i_v1.name();
        this.yVertexId = i_v2.name();
        sameDiff.addArgsFor(new SDVariable[]{i_v1,i_v2},this);
    } else {
        throw new IllegalArgumentException("Input not null variables.");
    }

}
 
Example 3
Source File: BaseTransformOp.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public BaseTransformOp(SameDiff sameDiff,
                       SDVariable i_v1,
                       SDVariable i_v2,
                       boolean inPlace) {
    super(sameDiff,inPlace,new Object[] {i_v2});
    if (i_v1 != null && i_v2 != null) {
        SameDiffUtils.validateDifferentialFunctionSameDiff(sameDiff, i_v1, this);
        SameDiffUtils.validateDifferentialFunctionSameDiff(sameDiff, i_v2, this);
        this.sameDiff = sameDiff;
        this.inPlace = inPlace;
        this.xVertexId = i_v1.name();
        this.yVertexId = i_v2.name();
        sameDiff.addArgsFor(new SDVariable[]{i_v1,i_v2},this);
    } else {
        throw new IllegalArgumentException("Input not null variables.");
    }


}
 
Example 4
Source File: BaseIndexAccumulation.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public BaseIndexAccumulation(SameDiff sameDiff,
                             SDVariable i_v,
                             SDVariable i_v2,
                             boolean keepDims,
                             int[] dimensions) {
    super(sameDiff,null);
    if (i_v != null) {
        this.dimensions = dimensions;
        SameDiffUtils.validateDifferentialFunctionSameDiff(sameDiff, i_v, this);
        SameDiffUtils.validateDifferentialFunctionSameDiff(sameDiff, i_v2, this);
        this.xVertexId = i_v.name();
        this.yVertexId = i_v2.name();
        sameDiff.addArgsFor(new SDVariable[]{i_v,i_v2},this);
    } else {
        throw new IllegalArgumentException("Input not null variable.");
    }
    this.keepDims = keepDims;
    defineDimensions(dimensions);
}
 
Example 5
Source File: BaseScalarBoolOp.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public BaseScalarBoolOp(SameDiff sameDiff,
                        SDVariable i_v,
                        Number scalar,
                        boolean inPlace,
                        Object[] extraArgs) {
    super(sameDiff,inPlace,extraArgs);
    this.scalarValue = Nd4j.scalar(i_v.dataType(), scalar);
    if (i_v != null) {
        this.xVertexId = i_v.name();
        sameDiff.addArgsFor(new String[]{xVertexId},this);
        SameDiffUtils.validateDifferentialFunctionSameDiff(sameDiff, i_v, this);
    } else {
        throw new IllegalArgumentException("Input not null variable.");
    }

}
 
Example 6
Source File: IntegrationTestRunner.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public static void assertSameDiffEquals(SameDiff sd1, SameDiff sd2){
    assertEquals(sd1.variableMap().keySet(), sd2.variableMap().keySet());
    assertEquals(sd1.getOps().keySet(), sd2.getOps().keySet());
    assertEquals(sd1.inputs(), sd2.inputs());

    //Check constant and variable arrays:
    for(SDVariable v : sd1.variables()){
        String n = v.name();
        assertEquals(n, v.getVariableType(), sd2.getVariable(n).getVariableType());
        if(v.isConstant() || v.getVariableType() == VariableType.VARIABLE){
            INDArray a1 = v.getArr();
            INDArray a2 = sd2.getVariable(n).getArr();
            assertEquals(n, a1, a2);
        }
    }

    //Check ops:
    for(SameDiffOp o : sd1.getOps().values()){
        SameDiffOp o2 = sd2.getOps().get(o.getName());
        assertEquals(o.getOp().getClass(), o2.getOp().getClass());
    }
}
 
Example 7
Source File: BaseReduceOp.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public BaseReduceOp(SameDiff sameDiff,
                    SDVariable i_v,
                    int[] dimensions, boolean keepDims) {
    super(sameDiff, null);
    if (i_v != null) {
        if(dimensions == null || dimensions.length < 1)
            dimensions = new int[] {Integer.MAX_VALUE};

        this.dimensions = dimensions;
        SameDiffUtils.validateDifferentialFunctionSameDiff(sameDiff, i_v, this);
        this.keepDims = keepDims;
        this.xVertexId = i_v.name();
        sameDiff.addArgsFor(new String[]{xVertexId},this);
    } else {
        throw new IllegalArgumentException("Input not null variable.");
    }

    defineDimensions(dimensions);
}
 
Example 8
Source File: BaseReduceOp.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public BaseReduceOp(SameDiff sameDiff,
                    SDVariable i_v,
                    SDVariable i_v2,
                    int[] dimensions, boolean keepDims) {
    super(sameDiff,null);
    if (i_v != null) {
        if(dimensions == null || dimensions.length < 1)
            dimensions = new int[] {Integer.MAX_VALUE};

        this.dimensions = dimensions;

        this.xVertexId = i_v.name();
        this.yVertexId = i_v2.name();
        SameDiffUtils.validateDifferentialFunctionSameDiff(sameDiff, i_v, this);
        SameDiffUtils.validateDifferentialFunctionSameDiff(sameDiff, i_v2, this);
        this.keepDims = keepDims;
        sameDiff.addArgsFor(new String[]{xVertexId,yVertexId},this);

    } else {
        throw new IllegalArgumentException("Input not null variable.");
    }

    defineDimensions(dimensions);
}
 
Example 9
Source File: SDValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Validate that the operation is being applied on an integer type SDVariable
 *
 * @param opName Operation name to print in the exception
 * @param v      Variable to validate datatype for (input to operation)
 */
protected static void validateInteger(String opName, SDVariable v) {
    if (v == null)
        return;
    if (!v.dataType().isIntType())
        throw new IllegalStateException("Cannot apply operation \"" + opName + "\" to variable \"" + v.name() + "\" with non-integer data type " + v.dataType());
}
 
Example 10
Source File: SDValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
protected static void validateInteger(String opName, String inputName, SDVariable[] vars) {
    for (SDVariable v : vars) {
        if (v == null)
            return;
        if (!v.dataType().isIntType())
            throw new IllegalStateException("Input \"" + inputName + "\" for operation \"" + opName + "\" must be an integer type; got variable \"" +
                    v.name() + "\" with non-integer data type " + v.dataType());
    }
}
 
Example 11
Source File: SDValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Validate that the operation is being applied on an floating point type SDVariable
 *
 * @param opName Operation name to print in the exception
 * @param v      Variable to validate datatype for (input to operation)
 */
protected static void validateFloatingPoint(String opName, SDVariable v) {
    if (v == null)
        return;
    if (!v.dataType().isFPType())
        throw new IllegalStateException("Cannot apply operation \"" + opName + "\" to variable \"" + v.name() + "\" with non-floating point data type " + v.dataType());
}
 
Example 12
Source File: TestSessions.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void testMergeSimple(){
        //This isn't really a sensible graph, as merge op behaviour is undefined when multiple inputs are available...

        SameDiff sd = SameDiff.create();
        SDVariable ph1 = sd.placeHolder("x", DataType.FLOAT, 3,3);
        SDVariable ph2 = sd.placeHolder("y", DataType.FLOAT, 3,3);

        SDVariable merge = sd.merge(ph1, ph2);

        SDVariable outVar = sd.identity(merge);

        INDArray x = Nd4j.linspace(1, 9, 9).castTo(DataType.FLOAT).reshape(3,3);
        INDArray y = Nd4j.linspace(0.0, 0.9, 9, DataType.DOUBLE).castTo(DataType.FLOAT).reshape(3,3);
//        ph1.setArray(x);
//        ph2.setArray(y);
//        INDArray out = sd.execAndEndResult();
//        System.out.println(out);


        Map<String,INDArray> m = new HashMap<>();
        m.put("x", x);
        m.put("y", y);

        InferenceSession is = new InferenceSession(sd);
//        String outName = merge.name();
        String outName = outVar.name();
        Map<String,INDArray> outMap = is.output(Collections.singletonList(outName), m, null,
                Collections.<String>emptyList(), null, At.defaultAt(Operation.TRAINING));

        assertEquals(1, outMap.size());
        INDArray out = outMap.get(outName);
        assertTrue(x.equals(out) || y.equals(out));
    }
 
Example 13
Source File: SDValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Validate that the operation is being applied on a boolean type SDVariable
 *
 * @param opName    Operation name to print in the exception
 * @param inputName Name of the input to the op to validate
 * @param v         Variable to validate datatype for (input to operation)
 */
protected static void validateBool(String opName, String inputName, SDVariable v) {
    if (v == null)
        return;
    if (v.dataType() != DataType.BOOL)
        throw new IllegalStateException("Input \"" + inputName + "\" for operation \"" + opName + "\" must be an boolean variable; got variable \"" +
                v.name() + "\" with non-boolean data type " + v.dataType());
}
 
Example 14
Source File: SDValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
protected static void validateNumerical(String opName, String inputName, SDVariable[] vars) {
    for (SDVariable v : vars) {
        if (v == null) continue;
        if (v.dataType() == DataType.BOOL || v.dataType() == DataType.UTF8)
            throw new IllegalStateException("Input \"" + inputName + "\" for operation \"" + opName + "\" must be an numerical type type; got variable \"" +
                    v.name() + "\" with non-integer data type " + v.dataType());
    }
}
 
Example 15
Source File: BaseScalarOp.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public BaseScalarOp(SameDiff sameDiff,
                    @NonNull SDVariable i_v,
                    Number scalar,
                    boolean inPlace,
                    Object[] extraArgs) {
    super(sameDiff,inPlace,extraArgs);
    this.scalarValue = Nd4j.scalar(i_v.dataType(), scalar);
    this.xVertexId = i_v.name();
    sameDiff.addArgsFor(new String[]{xVertexId},this);
    SameDiffUtils.validateDifferentialFunctionSameDiff(sameDiff, i_v, this);
}
 
Example 16
Source File: SDValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Validate that the operation is being applied on a numerical SDVariable (not boolean or utf8).
 * Some operations (such as sum, norm2, add(Number) etc don't make sense when applied to boolean/utf8 arrays
 *
 * @param opName Operation name to print in the exception
 * @param v      Variable to validate datatype for (input to operation)
 */
protected static void validateNumerical(String opName, String inputName, SDVariable v) {
    if (v == null)
        return;
    if (v.dataType() == DataType.BOOL || v.dataType() == DataType.UTF8)
        throw new IllegalStateException("Input \"" + inputName + "\" for operation \"" + opName + "\" must be an numerical type type; got variable \"" +
                v.name() + "\" with non-integer data type " + v.dataType());
}
 
Example 17
Source File: BaseRandomOp.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public BaseRandomOp(SameDiff sameDiff, SDVariable i_v) {
    Preconditions.checkNotNull(i_v, "Input variable can't be null with this constructor");
    this.sameDiff = sameDiff;
    this.xVertexId = i_v.name();
    sameDiff.addArgsFor(new String[]{xVertexId},this);
}
 
Example 18
Source File: SDValidation.java    From deeplearning4j with Apache License 2.0 3 votes vote down vote up
/**
 * Validate that the operation is being applied on a numerical SDVariable (not boolean or utf8).
 * Some operations (such as sum, norm2, add(Number) etc don't make sense when applied to boolean/utf8 arrays
 *
 * @param opName Operation name to print in the exception
 * @param v      Variable to perform operation on
 */
protected static void validateNumerical(String opName, SDVariable v) {
    if (v == null)
        return;
    if (v.dataType() == DataType.BOOL || v.dataType() == DataType.UTF8)
        throw new IllegalStateException("Cannot apply operation \"" + opName + "\" to variable \"" + v.name() + "\" with non-numerical data type " + v.dataType());
}
 
Example 19
Source File: SDValidation.java    From deeplearning4j with Apache License 2.0 2 votes vote down vote up
/**
 * Validate that the operation is being applied on boolean SDVariables
 *
 * @param opName Operation name to print in the exception
 * @param v1     Variable to validate datatype for (input to operation)
 * @param v2     Variable to validate datatype for (input to operation)
 */
protected static void validateBool(String opName, SDVariable v1, SDVariable v2) {
    if (v1.dataType() != DataType.BOOL || v2.dataType() != DataType.BOOL)
        throw new IllegalStateException("Cannot perform operation \"" + opName + "\" on variables  \"" + v1.name() + "\" and \"" +
                v2.name() + "\" if one or both variables are non-boolean: " + v1.dataType() + " and " + v2.dataType());
}
 
Example 20
Source File: SDValidation.java    From deeplearning4j with Apache License 2.0 2 votes vote down vote up
/**
 * Validate that the operation is being applied on numerical SDVariables (not boolean or utf8).
 * Some operations (such as sum, norm2, add(Number) etc don't make sense when applied to boolean/utf8 arrays
 *
 * @param opName Operation name to print in the exception
 * @param v1     Variable to validate datatype for (input to operation)
 * @param v2     Variable to validate datatype for (input to operation)
 */
protected static void validateNumerical(String opName, SDVariable v1, SDVariable v2) {
    if (v1.dataType() == DataType.BOOL || v1.dataType() == DataType.UTF8 || v2.dataType() == DataType.BOOL || v2.dataType() == DataType.UTF8)
        throw new IllegalStateException("Cannot perform operation \"" + opName + "\" on variables  \"" + v1.name() + "\" and \"" +
                v2.name() + "\" if one or both variables are non-numerical: " + v1.dataType() + " and " + v2.dataType());
}