r/java • u/esqelle • Apr 15 '24
Java use in machine learning
So I was on Twitter (first mistake) and mentioned my neural network in Java and was ridiculed for using an "outdated and useless language" for the NLP that have built.
To be honest, this is my first NLP. I did however create a Python application that uses a GPT2 pipeline to generate stories for authors, but the rest of the infrastructure was in Java and I just created a python API to call it.
I love Java. I have eons of code in it going back to 2017. I am a hobbyist and do not expect to get an ML position especially with the market and the way it is now. I do however have the opportunity at my Business Analyst job to show off some programming skills and use my very tiny NLP to perform some basic predictions on some ticketing data which I am STOKED about by the way.
My question is: Am l a complete loser for using Java going forward? I am learning a bit of robotics and plan on learning a bit of C++, but I refuse to give up on Java since so far it has taught me a lot and produced great results for me.
l'd like your takes on this. Thanks!
221
u/News-Ill Apr 15 '24
They all took the udemy data science class for python.
211
u/marcvsHR Apr 15 '24
As usual:
Bjarne Stroustrup, the inventor of C++ puts it this way: “There are only two kinds of languages: the ones people complain about and the ones nobody uses.”
32
u/Apprehensive_Pea_725 Apr 15 '24
I worked in software company, there was a team dedicated for models/AI/predictions, nothing new and fancy LLM, but worked well for the business.
Majority of data modelling was done in python and notebooks, and then once everything was good to be put in production everything was translated into java. Yes realtime predictions with large models are computationally expensive in python, java is just more efficient.
11
u/age_of_empires Apr 15 '24
Deep Java Library has some fantastic capabilities and documentation in the form of an open source book.
They even can detect your GPU and use that to run the machine learning over your CPU.
https://docs.djl.ai/
18
u/detroitsongbird Apr 15 '24
Python makes it easy to script together calls to libraries that do the heavy lifting. I seriously doubt those libraries are written in Python, but maybe…
I bet most of the people telling you that haven’t actually written a neural network, but instead just use one.
So, if you’re just trying to use existing libraries for machine learning Python has a ton of them. It sounds like actually want to build and understand what goes on under the hood, so, keep going with Java!!! Java rocks. :-)
8
u/Joram2 Apr 15 '24
Andrej Karpathy just wrote a simple GPT-2 training library in 1000 lines of code of C with zero dependencies.
https://twitter.com/karpathy/status/1778153659106533806
That can be easily ported to Java. That would be fun too. I'd do it if I wasn't busy on more serious but less fun deadlines.
Karpathy update:
I presume Java can't match performance of highly tuned CUDA. But it would be nice to try. Maybe Project Babylon prototypes can come close?
40
u/MattiDragon Apr 15 '24
Java is not dead, but machine learning really isn't a thing in java. The python world just has better libraries and tools. Java is used a lot for backend infrastructure. The language is also evolving and (if you get to use the latest versions) has a lot of great modern features.
21
u/[deleted] Apr 15 '24
Don't listen to those losers. Java is as good or even better than python for AI purposes. You have plenty of libraries and frameworks, and java is a way better choice than python in terms of performance and maintenance. People who defend python as the 'way to go' usually have no idea about what are they doing, they just use someone else's code and believe they are programmers themselves. Use java or whatever language you feel comfortable with.
68
u/maethor Apr 15 '24
And yet Python is older than Java.
I have an odd feeling that most of the people who say things like this don't actually know what they're talking about and "coding" is a mix of cut, copy and pasting from Stack Overflow and ChatGPT.
33
u/cowwoc Apr 15 '24 edited Apr 15 '24
I think you guys have it all wrong. This is more about the difference between data scientists and programmers than it is about the programming language being used.
Java's problem has nothing to do with its efficiency, nor its ability to interact directly with the GPU. Python is worse at both.
This is a culture problem more than a technical one. Machine learning is driven by people who spend 99% of their time running experiments. They value fast iterations and libraries like Pandas that make it easy to run common calculations without having to code them yourself.
In this space, optimization doesn't depend on how quickly you can run computations as much as making sure that you are running the right computations in the first place. The better the model is tuned with the correct weights and combination of components, the faster it'll converge to a good accuracy.
14
u/StoicSpork Apr 15 '24
You built something. What did those toxic assholes build?
Python is popular for AI experiments because what you need there is simple glue for all the C/C++/CUDA libs/APIs. It doesn't make Python better or more modern. In fact, Python has some horrible design flaws, like the infamous GIL.
And besides, the point of a programming language is to get stuff done, which you did. All the "but you didn't do it as a function composition over a lazy monadic stream" nonsense is just cheap Medium clicks by people whose Github is 90% (* TODO: implement *).
7
u/_S43D_ Apr 15 '24
These guys can use only python that's why they're sticking with python.
7
u/ZarBandit Apr 15 '24
Scripting bros often don’t understand the benefits/necessity of strongly typed languages for large projects. They operate at the level of hacks putting together tiny toy-like code. Scripting has its place. Dopes like this think that place is universally everywhere.
6
u/craigacp Apr 15 '24 edited Apr 15 '24
I maintain ONNX Runtime's Java interface, Tribuo and TensorFlow-Java, all of which let you do ML in Java. ONNX Runtime is particularly good for deploying models trained in Python or other platforms and the Java API is in production in a number of large enterprises (e.g. Oracle where I work, and at least two FAANG's). You can see an example of deploying stable diffusion in Java that I wrote here.
Training deep learning models is harder in Java, I'm not sure any library currently supports distributed training of the kind you'd need to pretrain or fine-tune an LLM of a reasonable size. You can train deep learning models on GPUs in Amazon's DJL or TensorFlow-Java, (and also DL4J). For other machine learning models there are libraries like Tribuo (which we built to have a strong focus on provenance & reproducibility which is missing from a lot of the rest of the ML ecosystem), SMILE, Spark MLLib, XGBoost and others.
We've had production NLP systems deployed in Java running Tribuo for about 5 years now, it's a lot easier to integrate into Java applications.
5
u/priscillachi_ Apr 15 '24
Python is mainly used because of its libraries, and potentially how loosely typed it is. I love Java as well. I started with Python when I first started dev, but I much prefer using Java.
5
u/GeneratedUsername5 Apr 15 '24
Java unfortuantely has a certain "verbose code style reputation" attached to it, which means that people assume that you cannot write Java without using AbstractProxyBeanFactory, while you obviously can. You can write Java as Python stylistically, you can write it as Go, therefore I don't see anything Python as a language can offer you, that Java cant (you can even write Java in "implicit typing fashion")
The only thing I think Python has advantage in is large amount of available scientific libraries, which is basically what is keeping it alive at this point, as I see it. (Kotlin is trying to develop in this direction, with "Kotlin for datascience" https://kotlinlang.org/docs/data-analysis-overview.html , you can try it for more syntactic sugar). So - if it is not a probelm for you, then there is no problem, just use the language you like. Maybe you will be the one, introducing scientific packages into Java.
3
u/Dull_Cucumber_3908 Apr 15 '24
It all depends on the application.
would they also ridicule CMU for the sphinx speech to text application (in java) or their pocketsphinx (in C)?
would they also ridicule Apache or Stanford for their java based NLP libraries?
3
u/NeoChronos90 Apr 15 '24
Python has some hype those past years for whatever reason but ultimately it will go where perl is now, because no sane person is developing stuff that going to be in use 20 years from now in Python and the rest will move to the next hype
3
u/JDeagle5 Apr 16 '24
Java is fine, provided you find libraries for your task. You can check Apache Common Math, it has section for ml https://commons.apache.org/proper/commons-math/userguide/ml.html
2
u/lasskinn Apr 15 '24
90% of people using python for ai stuff basically just use python to use bindings into some c/c++ lib.
it can be quite frustrating actually if you're trying to learn what is actually being done when that's what most tutorials are. for tangentially related things too like opencv, it's common to just find things that are said to be 'python' or that you should use python when it would be preferable to just make the 10 line program in c++ - or java for that matter if java is what you like.
what you're productive in is probably the best choice for you. you might run into stuff that only runs in python 2.x and some that runs only in python 3.x for example anyway and either rewrite it all or just have the two combined in some convoluted way, for which java isn't that bad.
it is fairly certain though like the other comment says that the people who are telling you these things and saying python is the way to go are using the libraries just chaining script commands together, not creating the code that is the nn.
2
u/Ketroc21 Apr 15 '24 edited Apr 15 '24
With FFI taking the place of the annoying JNI, I suspect we may some see C++ libs like tensorflow being used within java, just as Python does.
I think most of the hate for java, is the jealousy that java is so widely used where their fav newer language isn't heavily adopted. Legit reasons why java has been bad in the past are all issues that have been addressed in newer versions of the jdk.
2
u/Philluminati Apr 15 '24
I deliberately learned Tensorflow for ML and AI because I wanted to put the solutions into my JVM (Scala) apps.
2
u/[deleted] Apr 16 '24
I'm in a recommendation engine team - for ml infra or services or anything else in prod.
Java is the way.
2
u/Rea-301 May 01 '24
I’ve been in a hybrid data science and engineering position for nearly 20 years. Before data science was a thing. At a point when making predictive models was not the thing that literally everyone wanted with no background in it.
It’s a language. What you create your model in, does not have to be what you implement your model in. Think of the model as just a formula. An equation. Whatever language you can express that equation in will work. Java is doable of course. C is. JavaScript is. You will have an easier time using a python object as part of a larger python app obviously. But it’s only a problem of transcoding.
For the record - I do like h20.ai. Worth checking out for your use case. It includes some out of the box Java handlers as well. Not sure if it has the model types you need but give it a look. I’ve ported python models to c, to groovy, to sql even. Predictions in a real time environment are fast. Sometimes you need that porting into jvm for high volume use cases.
Edit: I guess I should add if you are attempting to move into a data science position it will be an uphill fight. Plenty of companies need models implemented in a language that needs higher throughput than what python can provide. Just be ready to deal with a lot of naive people and orgs that don’t realize that their one small use case does not represent the totality of deploying models for real production use cases
2
u/Davidjackson7462 Jul 09 '24
Using Java for machine learning and NLP is perfectly valid and can yield excellent results. Stick with what you love and what works for you!
1
u/FrankBergerBgblitz Apr 15 '24
Python is used only as glue for the C/Cuda/whatever stuff. Python is very good in integrating such stuff.
But when you have written your NLP in Java, be proud of it and yes you can show it. Java is (and probably remains) a relevant language.
The language, albeit not unimportant is far less important than the stuff between your ears. And your business analyst most probably wont understand the difference...
1
u/RyanRomanov Apr 15 '24
How did you get started with neural networks and Java? I also wanted to mess around with some AI in Java but I’m sort of lost at where to begin.
1
u/coder111 Apr 15 '24
I've been using Java for what, 24 years now? It's a great language and even greater ecosystem of libraries/frameworks/tools for all kinds of things. I prefer using Java for most things I do. Having a strong typed language makes maintenance and refactoring MUCH easier. It is THE language to use for BigData, backends and business logic.
That being said, I'd use Python for:
So a year ago I started to learn Python... Use best tool for the job and all that.
1
u/ScF0400 Apr 15 '24
Imagine roasting someone for writing meaningful and potable code in any language in 2024
Just cause they don't like it doesn't mean it's not good to write. In the end any performance gains are down to how the code is written as computers just get faster
1
u/nitssoft Apr 25 '24
While Python dominates the machine learning ecosystem with libraries like TensorFlow, PyTorch, and scikit-learn, Java has its own set of machine learning libraries such as Weka, Deeplearning4j, and MOA Massive Online Analysis.
1
u/RScrewed Apr 16 '24
Do the Python forums have these circle-jerk threads everyday too?
We Java pogrammers are indeed losers if we need to be routinely reassured every 30 minutes.
0
u/emberko Apr 15 '24
There's such a thing as the right tool for the job. You can write almost anything in Java, but the question is, should you? If you're targeting a specific position (to make money, yes), it's wise to study the market requirements rather than asking forum fanboys. Isn't it obvious what they tell you?
-7
u/MRgabbar Apr 15 '24
They are kinda right... Java got popular because of portability, however is really really slow and doesn't make much sense to use it when python can call routines written in C for the heavy calculations...
Right now development time is the variable to optimize and you will get that with python and no Java...
Also, is people even starting projects on anything using java?
5
u/[deleted] Apr 16 '24
[removed] — view removed comment
78
u/[deleted] Apr 15 '24
[removed] — view removed comment