DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Microservices With Apache Camel and Quarkus (Part 3)
  • Health Check Response Format for HTTP APIs
  • Step Into Serverless Computing
  • Performance Comparison — Thread Pool vs. Virtual Threads (Project Loom) In Spring Boot Applications

Trending

  • Microservices With Apache Camel and Quarkus (Part 3)
  • Health Check Response Format for HTTP APIs
  • Step Into Serverless Computing
  • Performance Comparison — Thread Pool vs. Virtual Threads (Project Loom) In Spring Boot Applications
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Getting Started With TensorFlow Using Java/JavaScript

Getting Started With TensorFlow Using Java/JavaScript

There is a great market for machine learning and deep learning enthusiasts. But unfortunately, it also requires different programming skillset than most developers have.

Vinod Pahuja user avatar by
Vinod Pahuja
·
Apr. 27, 18 · Opinion
Like (7)
Save
Tweet
Share
26.31K Views

Join the DZone community and get the full member experience.

Join For Free

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.

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

To start with Java, you need to first to set up a Maven project and add the following dependency to your pom.xml.

<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.

Add the below script tag to your HTML file.

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.10.0"> </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();
    model.add(tf.layers.dense({units: 1, inputShape: [1]}));

    // 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([5], [1, 1]));
    });

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

You can have a look at a complete example on GitHub.



If you enjoyed this article and want to learn more about TensorFlow, check out this collection of tutorials and articles on all things TensorFlow.

TensorFlow

Opinions expressed by DZone contributors are their own.

Trending

  • Microservices With Apache Camel and Quarkus (Part 3)
  • Health Check Response Format for HTTP APIs
  • Step Into Serverless Computing
  • Performance Comparison — Thread Pool vs. Virtual Threads (Project Loom) In Spring Boot Applications

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: