Building AI Applications With Java and Gradle
Explore AI development with Java and Gradle using popular libraries like Deeplearning4j, Weka, and Encog through practical code examples.
Join the DZone community and get the full member experience.
Join For FreeArtificial intelligence (AI) is transforming various industries and changing the way businesses operate. Although Python is often regarded as the go-to language for AI development, Java provides robust libraries and frameworks that make it an equally strong contender for creating AI-based applications. In this article, we explore using Java and Gradle for AI development by discussing popular libraries, providing code examples, and demonstrating end-to-end working examples.
Java Libraries for AI Development
Java offers several powerful libraries and frameworks for building AI applications, including:
- Deeplearning4j (DL4J) - A deep learning library for Java that provides a platform for building, training, and deploying neural networks, DL4J supports various neural network architectures and offers GPU acceleration for faster computations.
- Weka - A collection of machine learning algorithms for data mining tasks, Weka offers tools for data pre-processing, classification, regression, clustering, and visualization.
- Encog - A machine learning framework supporting various advanced algorithms, including neural networks, support vector machines, genetic programming, and Bayesian networks
Setting up Dependencies With Gradle
To begin AI development in Java using Gradle, set up the required dependencies in your project by adding the following to your build.gradle file:
dependencies {
implementation 'org.deeplearning4j:deeplearning4j-core:1.0.0-M1.1'
implementation 'nz.ac.waikato.cms.weka:weka-stable:3.8.5'
implementation 'org.encog:encog-core:3.4'
}
Code Examples
Building a Simple Neural Network With DL4J
This example demonstrates creating a basic neural network using the Deeplearning4j (DL4J) library. The code sets up a two-layer neural network architecture consisting of a DenseLayer
with 4 input neurons and 10 output neurons, using the ReLU activation function, and an OutputLayer
with 10 input neurons and 3 output neurons, using the Softmax activation function and Negative Log Likelihood as the loss function. The model is then initialized and can be further trained on data and used for predictions.
import org.deeplearning4j.nn.api.OptimizationAlgorithm;
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.layers.DenseLayer;
import org.deeplearning4j.nn.conf.layers.OutputLayer;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.nn.weights.WeightInit;
import org.nd4j.linalg.activations.Activation;
import org.nd4j.linalg.learning.config.Sgd;
import org.nd4j.linalg.lossfunctions.LossFunctions;
public class SimpleNeuralNetwork {
public static void main(String[] args) {
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.seed(123)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.updater(new Sgd(0.01))
.list()
.layer(0, new DenseLayer.Builder().nIn(4).nOut(10)
.weightInit(WeightInit.XAVIER)
.activation(Activation.RELU)
.build())
.layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
.nIn(10).nOut(3)
.weightInit(WeightInit.XAVIER)
.activation(Activation.SOFTMAX)
.build())
.pretrain(false).backprop(true)
.build();
MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
}
}
Classification Using Weka
This example shows how to use the Weka library for classification on the Iris dataset. The code loads the dataset from an ARFF file, sets the class attribute (the attribute we want to predict) to be the last attribute in the dataset, builds a Naive Bayes classifier using the loaded data, and classifies a new instance.
import weka.classifiers.bayes.NaiveBayes;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
public class WekaClassification {
public static void main(String[] args) throws Exception {
DataSource source = new DataSource("data/iris.arff");
Instances data = source.getDataSet();
data.setClassIndex(data.numAttributes() - 1);
NaiveBayes nb = new NaiveBayes();
nb.buildClassifier(data);
Instance newInstance = data.instance(0);
double result = nb.classifyInstance(newInstance);
System.out.println("Predicted class: " + data.classAttribute().value((int) result));
}
}
Conclusion
Java, with its rich ecosystem of libraries and frameworks for AI development, is a viable choice for building AI-based applications. By leveraging popular libraries like Deeplearning4j, Weka, and Encog, and using Gradle as the build tool, developers can create powerful AI solutions using the familiar Java programming language.
The provided code examples demonstrate the ease of setting up and configuring AI applications using Java and Gradle. The DL4J example shows how to create a basic deep learning model that can be applied to tasks such as image recognition or natural language processing. The Weka example demonstrates how to use Java and the Weka library for machine learning tasks, specifically classification, which can be valuable for implementing machine learning solutions in Java applications, such as predicting customer churn or classifying emails as spam or not spam.
Happy Learning!!
Opinions expressed by DZone contributors are their own.
Comments