Machine Learning in XOJO

If you want to parallelize CPU threads, you can build a console app (like the one this thread is making) and use Folderitem.launch to instantiate multiple items across multiple cores as needed, assuming they communicate data efficiently.

I would try to persuade XOJO to focus on GPU integration before multicore. For the same reason. If you have to discretize information for multicore, than you can discretize it for GPU. Then you get thousands of cores working independently on the same problem. Check out a cuda-neural-network on my GitHub if you are on windows with Nvidia.

1 Like

Hello,
in the example, is there a way to specify values for a new wine? and I would then get a value for the quality back?

thanks Norbert

I have found better results with a network topology of: 11 20 5 1
So change that at the top of the winedata file. Here is the code to input manual data:

Function Run(args() as String) As Integer
#Pragma Unused args

	  Var inputfilename As String = "winedata.txt" '"trainingdata.txt"
	  
	  Var trainData As New trainingData(inputfilename)
	  //check trainingData constructor to change path to file
	  //default is desktop
	  
	  trainData.getTopology
	  
	  var myNet as new ai.Net(trainData.topology)
	  
	  var resultVals() as double
	  var trainingPass as integer = 0
	  var consoleprintremainder as integer = 99   //print to console every x operations
	  
	  var d1 as DateTime = DateTime.now
	  
	  while not trainData.isEof
	    trainingPass = trainingPass + 1
	    
	    if trainingPass Mod consoleprintremainder = 0 then
	      Print( "Pass " + trainingPass.ToString)
	    end if
	    
	    if trainData.getNextInputs <> trainData.topology(0) then
	      break
	    end if
	    
	    If trainingPass Mod consoleprintremainder = 0 Then
	      showVectorVals("Inputs :",trainData.inputVals)
	    end if
	    
	    myNet.feedForward(trainData.inputVals)
	    
	    resultVals = myNet.getResults
	    If trainingPass Mod consoleprintremainder = 0 Then
	      showVectorVals("Outputs:", myNet.getResults)
	    end if
	    
	    trainData.getTargetOutputs(trainData.targetOutputVals)
	    If trainingPass Mod consoleprintremainder = 0 Then
	      showVectorVals("Targets:", trainData.targetOutputVals)
	    end if
	    
	    myNet.backProp(trainData.targetOutputVals)
	    
	    if trainingPass Mod consoleprintremainder = 0 then
	      print("Net recent average error: " + myNet.getRecentAverageError.ToString + EndOfLine)
	    end if
	    
	  Wend
	  
	  var d2 as datetime = datetime.now
	  
	  print(EndOfLine + "DONE")
	  
	  var elapsed as double = d2.SecondsFrom1970 - d1.SecondsFrom1970
	  print("ELAPSED " + elapsed.ToString)
	  
	  Print "processed file: " + inputfilename
	  
	  //pause terminal
	  
	  //new code
	  //Print("Input New Wine Data (11 parameters")
	  print("input new wine parameters:")
	  Var s As String = stdin.ReadLine
	  var newstringentry() as string
	  var newentry() as double
	  newstringentry = s.Split(" ")
	  for i as integer = 0 to newstringentry.Count - 1
	    newentry.add( Val(newstringentry(i)) )
	  next
	  
	  trainData.inputVals = newentry
	  myNet.feedForward(trainData.inputVals)
	  resultVals = myNet.getResults
	  showVectorVals("Outputs:", myNet.getResults)
	  
	  s = stdin.ReadLine
	  
	  #Pragma Unused s
	  
	  
	End Function
2 Likes

Hello Alexander Kostyak,

Thanks for the example, a question about the model.
When I enter the following input I should get 0.7 but I get 0.606:
ā€œ0.28 0.16 0.4 0.015 0.044 0.48 0.3575 0.9912 0.82 0.52 0.44ā€

When I enter the following input I should get 0.3 but I also get 0.606:
ā€œ0.47 0.26 0.21 0.162 0.074 0.41 0.4925 0.998 0.3 0.5 0.18ā€

Are the outputs incorrect because there is not enough data for 0.7 and 0.3 in the windata.txt? Or can the model also be adapted so that one can also find values of which only a few samples are previous?

Thanks & greetings Norbert

Great question Norbert,

There may not be enough data, and there are possibly better hyperparameters for the dataset. This includes the eta and alpha values in the neuron class, and the network topology specified at the top of the trainingdata file. The winedata instance was used mostly to test the speed. I am not sure if the wine qualities themselves are totally accurate. They reflect the opinion of a wine tester. Maybe there is a combination of parameters that can predict it accurately.

If you want to see the neural network work successfully on raw logic, try the other dataset (trainingData.txt). For the wines, maybe someone is willing to develop computational hyperparameter optimization to see if they can be accurately predicted…

1 Like

Hello Alexander,
I have now used the file: traningData.txt. Here, however, I already have an error of around 0.5 when learning. Am I doing something wrong here?

Thanks Norbert

No, I have the same issue. I have moved the newer code to a beta folder on the GitHub and uploaded the original projects. Try it again once with trainingData.txt

I found the bug and reverted the code, the new repository should be stable and accurate

2 Likes

XOJO Forum Users,
The YouTube Link at the top of this thread has been updated, the new video demonstrates that XOJO is competitive with C++ for machine learning applications when utilizing aggressive built optimization, pragma statements, and loop boundary caching.
https://youtu.be/na4lY2B7uco

5 Likes

Very nice comparison Alexander.

Xojo definitely has a lot of potential in the machine learning space!

1 Like

I just found this thread because I would like to use a Machine Learning model in Xojo.

My training data consists of thousands of rows in a CSV file with many numeric columns.
I want to be able to predict good outcomes.

outcome value1 value2 value3 …
good 0.02 1.3 2.5 …
good 0.02 0.3 2.5 …
bad 3.0 2.3 1.5 …

ChatGPT suggested to build a Maching Learning detection model using Python. But I would like to learn how to do this in Xojo.

I don’t really understand how to use this project and how to format my training data.

Then, how do I test new data against the model?

2 Likes

I’ve used xojo since realbasic days and it’s great for many tasks. I’ve also been doing a lot of ML in the last few years. My advice: do not re-invent the wheel in xojo (I too wanted to do it all in xojo at first). The python codebase for ML is absolutely massive and it quickly became apparent that learning python and doing ML in python is the way to go.

And this is not as hard as it seems: I knew 0 python at first, and now write multi-threaded and GPU based scripts that will run circles around xojo e.g. a recent benchmark on my new 5090OC gpu card and a cuda script clocked in at >200 Tflops using half-precision floats. The secret is to use AIs for help: I use Perplexity and Claude 4 as the back-end. It would take me a week to get a massively parallel GPU-based script working. With Claude 4 I had the above benchmark running in <5 min. The AIs are particularly good at python (not so much with xojo as they often hallucinate, Claude is the best behaved in my experience, Gemini is the worst).

And don’t be fooled by the fact that python is interpreted (I too had a bias: interpreted = 100x slower). Yes and no: if you hand code a loop in python it will be much slower. But keep in mind that python’s numerical libraries like numpy are vectorized and written in highly optimized C/C++ under the hood. So a large matrix multiplication requires a single line of python:

result = X @ Y

and while this single line pre se is interpreted, the actual operation uses highly optimized linear algebra libraries (BLAS/LAPACK) that have been refined over decades, and can be shifted to the GPU if u wish (Claude writes nvidia/cuda and apple silicon versions that select what’s available automatically, oh, and default to cpu if no gpu. quite amazing actually)

In the end, I have my cake and can eat it at the same time: I wrote a front-end app in xojo that encapsulates 8GB of x86 and arm64 python libraries, allowing user interaction, graphics display, etc to be done by xojo and its nice GUI, while the heavy lifting is done by myriad python libraries that are added as needed. I do my prototyping in JupyterLab, then incorporate the working scripts into my xojo-based app. Best of both worlds, allowing less sophisticated users to run complex ML algorithms with a friendly GUI.

3 Likes

You are totally right.

I just tried the Neural Network solution in Xojo, after figuring out how it works…
Confidence level was way too low and predictions were mostly wrong.

@Peter Stys: Interesting. How do you make the Python app since Python isn’t installed anymore on macOS? I remember that being a PITA some 10ish years ago when I dabbled with Python.

On macOS, I installed python using homebrew.

ChatGPT gave me all the steps to setup a python script that reads JSON data and outputs JSON data using the Machine Learning model it helped me create.

Now I need to figure out how to run all this on a Debian 12 VPS as I want it to run automatically every 6 hours.

How do you make the Python app since Python isn’t installed anymore on macOS?

Even if it were, it wouldn’t solve the problem because you (or users) would manually have to pip install the required libs. What i ended up doing is use miniconda to create 2 parallel python installations, one for x86 the other for arm64, then a shell script copies this installation (hence the 8GB size) into the app bundle after compilation. This way I maintain control over the python version and all required libs to ensure it all works on any machine the user happens to be running on.

2 Likes

@Jeremie_L - I’ve really tried integrating Python with a xojo app, but maybe @Bjƶrn_EirĆ­ksson’s plugin would help?

Just a thought,
Anthony

possibly, but from the docs, where is python supposed to be installed? if in the system, then full circle back to the same problem of different versions on different machines and missing libraries. I wanted the entire thing self-contained in the app bundle, otherwise no end of compatibility complaints: copy the app bundle to wherever you want and it will always work, with no ancillary files written/required anywhere else. I think this is the single biggest hurdle in getting python & libs deployed for ā€œnormalā€ users.

@Peter_Stys - I got plugin libraries mixed up. Bjƶrn’s is to make a Xojo app accessible to a Python script.

It’s @Christian_Schmitz’s plugin which lets you use Python in your app.

The documentation mentions that you can point the plugin at Python.

Sample project is here: Monkeybread Xojo plugin - Example: /Python/Python

Beyond that, questions should probably be directed to Christian. He’s pretty responsive. :slight_smile:

That’s what a Python app is for. It was just annoying to make. And the source code was accessible.

I’ll check out the Anaconda thing.