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);
}
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations