Kotlin and AI: What You Need to Know | Dice.com Career Advice

This post was originally published on this site.

Kotlin is a modern language built to target the Java Virtual Machine (JVM). People who have been coding in Java for years know that the language can be especially frustrating at times, such as having to explicitly write getters and setters, and dealing with null pointer exceptions. Kotlin solves many of those issues—but how well does it play with artificial intelligence (AI)?

First, it’s important to fully understand Kotlin in the context of Java. In Kotlin, for example, you can easily declare a class with data members without having to explicitly write your getters and setters. (The Kotlin compiler creates them automatically for you under the hood.) And like other languages, you can declare a statically-typed variable simply by assigning a constant or literal to a variable; Kotlin determines the type automatically.

Generally speaking, Kotlin is a huge improvement over Java while still targeting the JVM. (It’s so good that Google adopted it as the standard language for creating Android apps; previously they used Java.) Although Kotlin’s biggest codebase lives in Android apps, it’s been gaining traction in the AI world, largely thanks to its concise and expressive syntax that simplifies working with data. And because it targets the JVM, it can work with existing Java libraries. Let’s explore how you can get up to speed learning to code with AI and Kotlin.

Machine Learning and Deep Learning

Machine learning (ML) is often where people first start learning about AI. In its most modern form, machine learning is a process whereby software “learns” by analyzing existing data to develop new outputs. The typical example is how Amazon (which was a forerunner in developing machine learning technology) “learns” about you as you browse for products. It continues gathering a profile of your interests, and from there is able to generate suggestions for products you might like. (As a regular reader of novels, I can say that I’ve discovered many new authors from Amazon’s suggestions.)

ML includes many different techniques for learning, but there are three main ones you’ll want to learn about:

  • Unsupervised Learning: This is a type of ML where the system doesn’t really know initially what it’s supposed to learn. In the case of Amazon, over time and millions of purchases, the system will eventually notice patterns of behavior, especially related to suggestions. It will see that, after particular purchases (such as a laptop computer), certain suggestions (such as a wireless mouse) are likely to lead to more purchases, while other suggestions (perhaps a bluetooth speaker) don’t lead to the same outcome. Eventually, the system will have a strong understanding of what to suggest “next” after a purchase.
  • Supervised Learning: With this type of ML, there’s a “correct” answer to each question that the AI is fed. These answers are used to train the model. For example, a tech company could build a dataset that identifies emails as “spam” or “not spam,” and use it to train the model to successfully wwed out spam.
  • Reinforcement Learning: The AI system receives feedback on what worked and what didn’t. In the Amazon example, the model is “rewarded” when you purchase certain predicted items. If you decline to purchase those items, the system receives a “penalty” and adjusts its strategy.

Deep learning is like “next level” machine learning; it includes topics like image recognition and large language models. Deep learning uses neural networks to process data and learn from the data it’s given. One common use for deep learning is the ability to classify images: a human will feed in many photos of cats and dogs, with labels for each animal. Over time, the system will notice enough patterns until it can be fed an image without a label, and it will specify whether it’s a picture of a cat or a dog.

If you’re interested in learning about deep learning, you’ll want to explore a type of model called a Convolutional Neural Network (CNN). The CNN will notice patterns among the images, such as the shape of the ears on a cat compared to the shape of the ears on a dog.

Additionally, you’ll want to study:

  • Recurrent Neural Networks (RNNs): Whereas CNNs are great for images, noting edges, shapes, and features, RNNs are great for sequential data such as text, processing the data in order from left to right. They’re great for learning about language and speech.
  • Generative Adversarial Networks (GANs): AI can learn over time to distinguish generated images.

Here are some libraries and frameworks you’ll want to learn and practice both for ML and Deep Learning:

  • KotlinDL: This is a Kotlin library that helps with deep learning. Under the hood, it uses TensorFlow and ONNX Runtime (which are two tools you’ll want to explore, as we describe later). On the main page for KotlinDL, they include sample code right at the beginning for working with a CNN called LeNet. (And again, if you’re new to Kotlin, spend a bit of time looking at the syntax of this code as well, as it’s quite intriguing in its simplicity.)
  • DeepLearning4J is a set of open-source libraries for performing deep learning on the JVM. As it’s developed in Java, it’s fully compatible with Kotlin. DeepLearning4J is built from the ground up for large AI systems, and it even supports distributed training, meaning training in a cloud environment that’s scattered across multiple servers. Interestingly, it also supports working with existing Python libraries and models.
  • Smile: This is a machine learning system written in Java and therefore compatible with Kotlin. It’s incredibly fast and includes several built-in algorithms for machine learning and deep learning. Some of its features have terms you’ll want to study up on: Classification using decision trees; regression; clustering; dimensionality reduction. (We don’t have space to teach you about these here but Google and ChatGPT can help.) Also, note that Smile includes its own scripting language that you’ll want to learn.
  • TensorFlow API for Kotlin: This is an API that lets you use TensorFlow from within your Kotlin code. TensorFlow is one of the “old standards” in AI for deep learning. It was developed by Google, and it’s used a lot in research.
  • ONNX Runtime (Open Neural Network Exchange Runtime) is an engine for running ML and deep learning models. One of its highlights is that it can handle models built for many different frameworks, such as TensorFlow, PyTorch, and more. From there it allows you to export to its own format, which is somewhat of a standard format. ONNX includes a Java library that you can easily use from within a Kotlin app.

Large Language Models and Generative AI

After you’ve studied ML and Deep Learning, you’ll want to move on to generative AI and large language models (LLMs). LLMs are AI models trained on massive data sets and built so they can understand and generate human language. They use deep learning architectures and neural networks; they’re also great at conversational AI. They learn language patterns by analyzing billions of words and making connections between those words. This includes context, whereby a word might have a different meaning based on context.

ChatGPT is a great example of a tool that uses an LLM. For the most part, libraries for LLMs are built using Python. However, many of the above-mentioned libraries can also handle working with LLMs and similar language models, including:

  • DeepLearning4J: While this library can’t work directly with LLMs, it can process other language-based models called a BERT that can do tasks such as sentiment analysis (for example, deciding whether a review is positive or negative) and text classification (for example, decided if an email is spam or not).
  • ONNX Runtime: This library can work directly with LLMs, allowing you to load models such as those used by ChatGPT.
  • KotlinDL: This library can work with a limited type of LLM called a transformer-based model. It doesn’t work with full LLMs such as those used by ChatGPT.
  • LangChain4J: This is a Java library specifically built to work with LLMs directly from Java and Kotlin. (Note that there’s a similar library called LangChain.LangChain4J was built to be similar to LangChain, but is not a direct port, nor is it a set of bindings for LangChain. It’s its own product.) LangChain4J works with many different LLM models.

Android and AI

As mentioned earlier, Kotlin has become the primary language for Android development. And there are now many AI tools that run directly on Android.

Before we get into that, though, we need to talk about some architecture issues. Much of today’s AI requires an extreme level of processing. Today’s CPUs can handle some of it, but usually the big work runs on GPUs. While GPU stands for Graphics Processing Unit, modern CPUs have thousands of cores (compared to today’s CPUs which typically have under 20), which makes them well suited to the kind of processing power needed for AI.

Android devices typically have a handful of cores. They’re far from being able to handle large AI tasks. However, developers have come up with smaller AI tools and models that can actually run on Android devices. One such library is LiteRT (previously known as TensorFlow Lite). In addition to running on Android, this library can load smaller models built for mobile devices. While small, it can still do some great image and speech recognition, text processing, and real-time object detection.

Regarding images, LiteRT can perform facial and image recognition such as landmark detection, both of which are great applications for mobile devices. Another great example is gesture and pose recognition (perfect for yoga apps). For text recognition, it can convert images of documents to text, or translate signs and menus from one language to another.

Conclusion

Now is a great time to start learning about AI and Kotlin. Kotlin is considered by many people a beautiful programming language that’s easy to use and expressive, and AI fits well into it. While there aren’t as many libraries as there are for Python, the community of AI developers for Kotlin continues to grow.