# Introduction to Tensorflow for Java In this tutorial, we’ll go through the basics of TensorFlow and how to use it in Java. Please note that the TensorFlow Java API is an experimental API and hence not covered under any stability guarantee. We’ll coveTensorFlow Java APIr later in the tutorial possible use cases for using the TensorFlow Java API.

1. Overview

TensorFlow is an open source library for dataflow programming. This was originally developed by Google and is available for a wide array of platforms. Although TensorFlow can work on a single core, it can as easily benefit from multiple CPU, GPU or TPU available.

In this tutorial, we’ll go through the basics of TensorFlow and how to use it in Java. Please note that the TensorFlow Java API is an experimental API and hence not covered under any stability guarantee. We’ll cover later in the tutorial possible use cases for using the TensorFlow Java API.

2. Basics

TensorFlow computation basically revolves around two fundamental concepts: Graph and Session. Let’s go through them quickly to gain the background needed to go through the rest of the tutorial.

### 2.1. TensorFlow Graph

To begin with, let’s understand the fundamental building blocks of TensorFlow programs. Computations are represented as graphs in TensorFlow. A graph is typically a directed acyclic graph of operations and data, for example:

The above picture represents the computational graph for the following equation:

`f(x, y) = z = a*x + b*y`

A TensorFlow computational graph consists of two elements:

1. Tensor: These are the core unit of data in TensorFlow. They are represented as the edges in a computational graph, depicting the flow of data through the graph. A tensor can have a shape with any number of dimensions. The number of dimensions in a tensor is usually referred to as its rank. So a scalar is a rank 0 tensor, a vector is a rank 1 tensor, a matrix is a rank 2 tensor, and so on and so forth.
2. Operation: These are the nodes in a computational graph. They refer to a wide variety of computation that can happen on the tensors feeding into the operation. They often result in tensors as well which emanate from the operation in a computational graph.

### 2.2. TensorFlow Session

Now, a TensorFlow graph is a mere schematic of the computation which actually holds no values. Such a graph must be run inside what is called a TensorFlow session for the tensors in the graph to be evaluated. The session can take a bunch of tensors to evaluate from a graph as input parameters. Then it runs backward in the graph and runs all the nodes necessary to evaluate those tensors.

With this knowledge, we are now ready to take this and apply it to the Java API!

3. Maven Setup

We’ll set-up a quick Maven project to create and run a TensorFlow graph in Java. We just need the tensorflow dependency:

``````<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow</artifactId>
<version>1.12.0</version>
</dependency>

``````
4. Creating the Graph

Let’s now try to build the graph we discussed in the previous section using the TensorFlow Java API. More precisely, for this tutorial we’ll be using TensorFlow Java API to solve the function represented by the following equation:

`z = 3*x + 2*y`

The first step is to declare and initialize a graph:

``````Graph graph = new Graph()

``````

Now, we have to define all the operations required. Remember, that operations in TensorFlow consume and produce zero or more tensors. Moreover, every node in the graph is an operation including constants and placeholders. This may seem counter-intuitive, but bear with it for a moment!

The class Graph has a generic function called opBuilder() to build any kind of operation on TensorFlow.

### 4.1. Defining Constants

To begin with, let’s define constant operations in our graph above. Note that a constant operation will need a tensor for its value:

``````Operation a = graph.opBuilder("Const", "x")
.setAttr("dtype", DataType.fromClass(Double.class))
.setAttr("value", Tensor.<Double>create(3.0, Double.class))
.build();
Operation b = graph.opBuilder("Const", "y")
.setAttr("dtype", DataType.fromClass(Double.class))
.setAttr("value", Tensor.<Double>create(2.0, Double.class))
.build();

``````

Here, we have defined an Operation of constant type, feeding in the Tensor with Double values 2.0 and 3.0. It may seem little overwhelming to begin with but that’s just how it is in the Java API for now. These constructs are much more concise in languages like Python.

### 4.2. Defining Placeholders

While we need to provide values to our constants, placeholders don’t need a value at definition-time. The values to placeholders need to be supplied when the graph is run inside a session. We’ll go through that part later in the tutorial.

For now, let’s see how can we define our placeholders:

``````Operation x = graph.opBuilder("Placeholder", "x")
.setAttr("dtype", DataType.fromClass(Double.class))
.build();
Operation y = graph.opBuilder("Placeholder", "y")
.setAttr("dtype", DataType.fromClass(Double.class))
.build();

``````

Note that we did not have to provide any value for our placeholders. These values will be fed as Tensors when run.

### 4.3. Defining Functions

Finally, we need to define the mathematical operations of our equation, namely multiplication and addition to get the result.

These are again nothing but _Operation_s in TensorFlow and Graph.opBuilder() is handy once again:

``````Operation ax = graph.opBuilder("Mul", "ax")
.build();
Operation by = graph.opBuilder("Mul", "by")
.build();
.build();

``````

Here, we have defined there Operation, two for multiplying our inputs and the final one for summing up the intermediate results. Note that operations here receive tensors which are nothing but the output of our earlier operations.

Please note that we are getting the output Tensor from the Operation using index ‘0’. As we discussed earlier, an Operation can result in one or more Tensor and hence while retrieving a handle for it, we need to mention the index. Since we know that our operations are only returning one Tensor, ‘0’ works just fine!

5. Visualizing the Graph

It is difficult to keep a tab on the graph as it grows in size. This makes it important to visualize it in some way. We can always create a hand drawing like the small graph we created previously but it is not practical for larger graphs. TensorFlow provides a utility called TensorBoard to facilitate this.

Unfortunately, Java API doesn’t have the capability to generate an event file which is consumed by TensorBoard. But using APIs in Python we can generate an event file like:

``````writer = tf.summary.FileWriter('.')
......
writer.flush()

``````

Please do not bother if this does not make sense in the context of Java, this has been added here just for the sake of completeness and not necessary to continue rest of the tutorial.

We can now load and visualize the event file in TensorBoard like:

``````tensorboard --logdir .

``````

TensorBoard comes as part of TensorFlow installation.

Note the similarity between this and the manually drawn graph earlier!

6. Working with Session

We have now created a computational graph for our simple equation in TensorFlow Java API. But how do we run it? Before addressing that, let’s see what is the state of Graph we have just created at this point. If we try to print the output of our final Operation “z”:

``````System.out.println(z.output(0));

``````

This will result in something like:

``````<Add 'z:0' shape=<unknown> dtype=DOUBLE>

``````

This isn’t what we expected! But if we recall what we discussed earlier, this actually makes sense. The Graph we have just defined has not been run yet, so the tensors therein do not actually hold any actual value. The output above just says that this will be a Tensor of type Double.

Let’s now define a Session to run our Graph:

``````Session sess = new Session(graph)

``````

Finally, we are now ready to run our Graph and get the output we have been expecting:

``````Tensor<Double> tensor = sess.runner().fetch("z")
.feed("x", Tensor.<Double>create(3.0, Double.class))
.feed("y", Tensor.<Double>create(6.0, Double.class))
.run().get(0).expect(Double.class);
System.out.println(tensor.doubleValue());

``````

So what are we doing here? It should be fairly intuitive:

• Get a Runner from the Session
• Define the Operation to fetch by its name “z”
• Feed in tensors for our placeholders “x” and “y”
• Run the Graph in the Session

And now we see the scalar output:

`21.0`

This is what we expected, isn’t it!

7. The Use Case for Java API

At this point, TensorFlow may sound like overkill for performing basic operations. But, of course, TensorFlow is meant to run graphs much much larger than this.

Additionally, the tensors it deals with in real-world models are much larger in size and rank. These are the actual machine learning models where TensorFlow finds its real use.

It’s not difficult to see that working with the core API in TensorFlow can become very cumbersome as the size of the graph increases. To this end, TensorFlow provides high-level APIs like Keras to work with complex models. Unfortunately, there is little to no official support for Keras on Java just yet.

However, we can use Python to define and train complex models either directly in TensorFlow or using high-level APIs like Keras. Subsequently, we can export a trained model and use that in Java using the TensorFlow Java API.

Now, why would we want to do something like that? This is particularly useful for situations where we want to use machine learning enabled features in existing clients running on Java. For instance, recommending caption for user images on an Android device. Nevertheless, there are several instances where we are interested in the output of a machine learning model but do not necessarily want to create and train that model in Java.

This is where TensorFlow Java API finds the bulk of its use. We’ll go through how this can be achieved in the next section.

8. Using Saved Models

We’ll now understand how we can save a model in TensorFlow to the file system and load that back possibly in a completely different language and platform. TensorFlow provides APIs to generate model files in a language and platform neutral structure called Protocol Buffer.

### 8.1. Saving Models to the File System

We’ll begin by defining the same graph we created earlier in Python and saving that to the file system.

Let’s see we can do this in Python:

``````import tensorflow as tf
graph = tf.Graph()
builder = tf.saved_model.builder.SavedModelBuilder('./model')
with graph.as_default():
a = tf.constant(2, name='a')
b = tf.constant(3, name='b')
x = tf.placeholder(tf.int32, name='x')
y = tf.placeholder(tf.int32, name='y')
sess = tf.Session()
sess.run(z, feed_dict = {x: 2, y: 3})
builder.save()

``````

As the focus of this tutorial in Java, let’s not pay much attention to the details of this code in Python, except for the fact that it generates a file called “saved_model.pb”. Do note in passing the brevity in defining a similar graph compared to Java!

We’ll now load “saved_model.pb” into Java. Java TensorFlow API has SavedModelBundle to work with saved models:

``````SavedModelBundle model = SavedModelBundle.load("./model", "serve");
Tensor<Integer> tensor = model.session().runner().fetch("z")
.feed("x", Tensor.<Integer>create(3, Integer.class))
.feed("y", Tensor.<Integer>create(3, Integer.class))
.run().get(0).expect(Integer.class);
System.out.println(tensor.intValue());

``````

It should by now be fairly intuitive to understand what the above code is doing. It simply loads the model graph from the protocol buffer and makes available the session therein. From there onward, we can pretty much do anything with this graph as we would have done for a locally-defined graph.

9. Conclusion

To sum up, in this tutorial we went through the basic concepts related to the TensorFlow computational graph. We saw how to use the TensorFlow Java API to create and run such a graph. Then, we talked about the use cases for the Java API with respect to TensorFlow.

In the process, we also understood how to visualize the graph using TensorBoard, and save and reload a model using Protocol Buffer.

As always, the code for the examples is available over on GitHub.

======================================

## How to use TensorFlow in Java In this tutorial, we'll go through the basics of TensorFlow and how to use it in Java.

### Introduction

Machine Learning is gaining popularity and usage over the globe. It has already drastically changed the way certain applications are built and will likely continue to be a huge (and increasing) part of our daily lives.

There's no sugarcoating it, Machine Learning isn't simple. It's pretty daunting and can seem very complex to many.

Companies such as Google took it upon themselves to bring Machine Learning concepts closer to developers and allow them to gradually, with major help, make their first steps.

Thus, frameworks such as TensorFlow were born.

### What is TensorFlow?

TensorFlow is an open-source Machine Learning framework developed by Google in Python and C++.

It helps developers easily acquire data, prepare and train models, predict future states, and perform large-scale machine learning.

With it, we can train and run deep neural networks which are most often used for Optical Character Recognition, Image Recognition/Classification, Natural Language Processing, etc.

### Tensors and Operations

TensorFlow is based on computational graphs, which you can imagine as a classic graph with nodes and edges.

Each node is referred to as an operation, and they take zero or more tensors in and produce zero or more tensors out. An operation can be very simple, such as basic addition, but they can also be very complex.

Tensors are depicted as edges of the graph, and are the core data unit. We perform different functions on these tensors as we feed them to operations. They can have a single or multiple dimensions, which are sometimes referred to as their ranks - (Scalar: rank 0, Vector: rank 1, Matrix: rank 2)

This data flows through the computational graph through tensors, impacted by operations - hence the name TensorFlow.

Tensors can store data in any number of dimensions, and there are three main types of tensors: placeholdersvariables, and constants.

### Installing TensorFlow

Using Maven, installing TensorFlow is as easy as including the dependency:

``````<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow</artifactId>
<version>1.13.1</version>
</dependency>

``````

If your device supports GPU support, then use these dependencies:

``````<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>libtensorflow</artifactId>
<version>1.13.1</version>
</dependency>

<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>libtensorflow_jni_gpu</artifactId>
<version>1.13.1</version>
</dependency>

``````

You can check the version of TensorFlow currently installed by using the `TensorFlow` object:

``````System.out.println(TensorFlow.version());

``````

### TensorFlow Java API

The Java API TensorFlow offers is contained within the `org.tensorflow` package. It's currently experimental so it's not guaranteed to be stable.

Please note that the only fully supported language for TensorFlow is Python and that the Java API isn't nearly as functional.

### Graphs

As mentioned before, TensorFlow is based on computational graphs - where `org.tensorflow.Graph` is Java's implementation.

Note: Its instances are thread-safe, though we need to explicitly release resources used by the Graph after we're finished with it.

Let's start off with an empty graph:

``````Graph graph = new Graph();

``````

This graph doesn't mean much, it's empty. To do anything with it, we first need to load it up with `Operation`s.

To load it up with operations, we use the `opBuilder()` method, which returns an `OperationBuilder` object that'll add the operations to our graph once we call the `.build()` method.

#### Constants

Let's add a constant to our graph:

``````Operation x = graph.opBuilder("Const", "x")
.setAttr("dtype", DataType.FLOAT)
.setAttr("value", Tensor.create(3.0f))
.build();

``````

#### Placeholders

Placeholders are a "type" of variable that don't have a value at declaration. Their values will be assigned at a later date. This allows us to build graphs with operations without any actual data:

``````Operation y = graph.opBuilder("Placeholder", "y")
.setAttr("dtype", DataType.FLOAT)
.build();

``````

#### Functions

And now finally, to round this up, we need to add certain functions. These could be as simple as multiplication, division, or addition, or as complex as matrix multiplications. The same as before, we define functions using the `.opBuilder()` method:

``````Operation xy = graph.opBuilder("Mul", "xy")
.build();

``````

Note: We're using `output(0)` as a tensor can have more than one output.

#### Graph Visualisation

Sadly, the Java API doesn't yet include any tools that allow you to visualize graphs as you would in Python. When the Java API gets updated, so will this article.

### Sessions

As mentioned before, a `Session` is the driver for a `Graph`'s execution. It encapsulates the environment in which `Operation`s and `Graph`s are executed to compute `Tensor`s.

What this means is that the tensors in our graph that we constructed don't actually hold any value, as we didn't run the graph within a session.

Let's first add the graph to a session:

``````Session session = new Session(graph);

``````

Our computation simply multiples the `x` and `y` value. In order to run our graph and compute it, we `fetch()` the `xy` operation and feed it the `x` and `y` values:

``````Tensor tensor = session.runner().fetch("xy").feed("x", Tensor.create(5.0f)).feed("y", Tensor.create(2.0f)).run().get(0);
System.out.println(tensor.floatValue());

``````

Running this piece of code will yield:

``````10.0f

``````

This may sound a bit odd, but since Python is the only well-supported language, the Java API still doesn't have the functionality to save models.

This means that the Java API is meant only for the serving use-case, at least until it's fully supported by TensorFlow. At least, we can train and save models in Python and then load them in Java to serve them, using the `SavedModelBundle` class:

``````SavedModelBundle model = SavedModelBundle.load("./model", "serve");
Tensor tensor = model.session().runner().fetch("xy").feed("x", Tensor.create(5.0f)).feed("y", Tensor.create(2.0f)).run().get(0);

System.out.println(tensor.floatValue());

``````

### Conclusion

TensorFlow is a powerful, robust and widely-used framework. It's constantly being improved and lately introduced to new languages - including Java and JavaScript.

Although the Java API doesn't yet have nearly as much functionality as TensorFlow for Python, it can still serve as a good intro to TensorFlow for Java developers.

I hope this tutorial will surely help and you if you liked this tutorial, please consider sharing it with others.

Keep Visiting...

Originally published on stackabuse.com

## How to Use TensorFlow Using Java/JavaScript This article will help get experienced Java/JavaScript developers use machine learning/deep learning in the TensorFlow ecosystem without any prior knowledge of Python.

### Background

Nowadays, there is a great market for machine learning and deep learning enthusiasts, as it is a niche technology, but it also requires different programming skillset.

There are plenty of tools and libraries available for this like Numpy, Keras, and TensorFlow, but most of them play around R, MATLAB, and, of course, Python.

So, now the problem is reskilling the existing programmer community, which is typically trained on languages like C, C++, and Java.

You might also enjoy: Machine Learning In Node.js With TensorFlow.js

### Solution

There is lots of misconception that TensorFlow is made only for Python and its allies. Don't worry, if you are an experienced Java/JS programmer and want to get your hands dirty with ML/DL, you still can!

The solution is TensorFlow for Java and TensorFlow.js.

### Learn by Example

#### Java

``````<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow</artifactId>
<version>1.7.0</version>
</dependency>
``````

This is an enumeration of two libraries:

• libtensorflow
• libtensorflow_jni

This wraps the TensorFlow C++ library and JNI connector to access it through Java programs.

Additionally, you can add the following dependency to support GPU acceleration. By default, it works on CPU powers.

``````<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>libtensorflow_jni_gpu</artifactId>
<version>1.7.0</version>
</dependency>
``````

Now, you are all set to use TensorFlow features on Java. You can use following test program to check the environment.

``````import org.tensorflow.Graph;
import org.tensorflow.Session;
import org.tensorflow.Tensor;
import org.tensorflow.TensorFlow;

public class HelloTF {
public static void main(String[] args) throws Exception {
try (Graph g = new Graph()) {
final String value = "Hello from " + TensorFlow.version();

// Construct the computation graph with a single operation, a constant
// named "MyConst" with a value "value".
try (Tensor t = Tensor.create(value.getBytes("UTF-8"))) {
// The Java API doesn't yet include convenience functions for adding operations.
g.opBuilder("Const", "MyConst").setAttr("dtype", t.dataType()).setAttr("value", t).build();
}

// Execute the "MyConst" operation in a Session.
try (Session s = new Session(g);
Tensor output = s.runner().fetch("MyConst").run().get(0)) {
System.out.println(new String(output.bytesValue(), "UTF-8"));
}
}
}
}
``````

This is basically creating an operation graph with a const and assigning a value as the current version. For fetching it, you need to run it inside a session.

#### JavaScript

To start with JavaScript, you just need a single package available through CDN. It's available through an NPN package, as well, but it is easier to use CDN for working with a browser.

``````<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"> </script>
``````

That's it. Now, you can test TensorFlow features like training, re-training, and converting existing models into JavaScript-compatible models right into your browser.

To test, you can use following code.

``````// Define a model for linear regression.
const model = tf.sequential();

// Prepare the model for training: Specify the loss and the optimizer.
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

// Generate some synthetic data for training.
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

// Train the model using the data.
model.fit(xs, ys).then(() => {
// Use the model to do inference on a data point the model hasn't seen before:
result = model.predict(tf.tensor2d(, [1, 1]));
});
``````

This is a basic linear regression model trained with mean square error and gradient descent.

## Java Fundamentals: Learn Java for absolute beginners |Simpliv Java Fundamentals: Learn Java for absolute beginners

Description
This is the best course to learn to program in Java in Spanish from scratch and without any experience in this fabulous programming language. This is the first course where we will study the Fundamentals of Java, and we will take you step by step until you acquire the bases of the Java language and you can start to study more advanced Java topics.

The content is divided into perfectly structured levels, each level supported by the previous one, with the aim of adding Java knowledge incrementally and so you can focus on mastering the issues little by little and gradually. So ensure the success of your Java training.

We will also offer support for any doubts about the didactic material included in this Java Fundamentals course.

We manage a new teaching methodology that we have called Speed ​​Learning. This methodology consists of concise videos that go directly to the point to be studied, complemented by eBooks with explanations and step-by-step images (which you can print, or search for any text you need, or use for your offline study), since As we know we can not do text search within a video. In addition, our methodology includes perfectly structured and very didactic exercises that will allow you to accelerate your eLearning learning. No loss of time in videos where you have to watch the instructor codify an exercise, too much theory, little practice or anything like that. Our Speed ​​Learning methodology guarantees that in the shortest possible time you will acquire the necessary knowledge for the Java professional and working world.

The Java Fundamentals course includes the following topics for study:

Lesson 1 - Starting with Java Technology

The amazing world of Java programming

What is Java technology (from a practical approach)

Our first Java program from scratch

Lesson 2 - Variables and Operators in Java

Use of Variables in Java and what we use them for

Types of Data in Java and how they are classified

Management and Classification of operators in Java

Lesson 3 - Control statements in Java

Using the if-else structure and where to use it

Handling the switch structure and when applying it

Lesson 4 - Handling Loops in Java

Use of the for loop and its use

Using the while loop and how to apply it

Use of the do-while loop and when to use it

Lesson 5 - Object Oriented Programming

Introduction to Object Oriented Programming (OOP)

Handling Classes in Java

Use of Objects in Java

Lesson 6 - Functions in Java

Declaration of Methods or Functions in Java

Use and call of functions in Java

Lesson 7 - Data Management in Java

Using Arrays in Java

Management of Matrices in Java

Lesson 8 - Inheritance in Java

Inheritance Management in Java

Use of superclasses and subclasses in Java

Final Level Laboratory

Final Exercise where everything learned in this Level is integrated

At the end you get a certificate of having completed the Java Fundamentals course.

We wait for you on the other side.

Ing. Ubaldo Acosta

Founder of Global Mentoring

Passion for Java Technology

Who this course is for:

Anyone who wants to learn how to program in Java
Basic knowledge
Basic knowledge of PC use
Basic management of an operating system such as Windows, Mac or Linux
It is not necessary to know how to program, we will start from scratch !!!
The attitude and desire to start coding and learning Java once and for all from scratch!
What will you learn
Have the basics of the programming language with Java
You will know the basic syntax of the Java language
Manage the concept of Variables and Operators in Java
We will study Object Oriented Programming with Java
You will learn the Control Statements and Loops in Java
We will see the concept of Functions with Java
We will study the concept of Inheritance in Java
We will learn to use Arrays in java
We will handle the concept of Matrices in Java
We will learn to Design Classes in Java
We will make a final application with everything learned in the course
To know more: