# Tensorflow

Nd4j allows execution of models via javacpp's tensorflow bindings using nd4j's INDArray as a data structure. Leveraging the nd4j-tensorflow interop is fairly simple.

Note, this is based on tensorflow 1.x. TF java 2 will be coming at a later time.

A tensorflow model can be loaded and executed as follows:

```java
        File f = new File ("path/to/your/model.pb");
        INDArray x = Nd4j.scalar(1.0f).reshape(1,1);
        INDArray y = Nd4j.scalar(1.0f).reshape(1,1);
        List<String> inputNames = Arrays.asList("x","y");
        List<String> outputNames = Arrays.asList("z");
        GraphRunner tensorflowRunner = GraphRunner.builder()
                .inputNames(inputNames)
                .graphPath(f)
                .outputNames(outputNames).
                build();
        Map<String,INDArray> inputs = new LinkedHashMap<>();
        inputs.put("x",x);
        inputs.put("y",y);
        Map<String, INDArray> exec = tensorflowRunner.exec(inputs);
        INDArray z = exec.get("z");
```

This outputs a result with a map of output names to the ndarray result from tensorflow.

In maven, add the following dependency:

```markup
<dependency>
   <groupId>org.nd4j</groupId>
   <artifactId>nd4j-tensorflow</groupId>
   <version>${nd4j.version}</version>
</dependency>
```

Note, this depends on javacpp's tensorflow bindings. This means tensorflow's native binaries are managed by javacpp. Javacpp will bundle all native binaries for all platforms by default unless you specify a platform. You can do this by specifying a -Dplatform=your-platform-of-choice. You may find more [here](https://github.com/bytedeco/javacpp-presets/wiki/Reducing-the-Number-of-Dependencies) in the javacpp docs.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://deeplearning4j.konduit.ai/nd4j/how-to-guides/other-framework-interop/tensorflow.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
