简体   繁体   English

Ada + 机器学习(Python 框架)

[英]Ada + Machine Learning (Python Framework)

I'm trying to write a simple machine learning application in Ada, and also trying to find a good framework to use.我正在尝试用 Ada 编写一个简单的机器学习应用程序,还试图找到一个好的框架来使用。 My knowledge of one thing is extremely minimal, and of the other is somewhat minimal.我对一件事的了解极少,对另一件事的了解也很少。

There are several nifty machine learning frameworks out there, and I'd like to leverage one for use with an Ada program, but I guess I'm just...at a loss.那里有几个漂亮的机器学习框架,我想利用一个与 Ada 程序一起使用,但我想我只是……不知所措。 Can I use an existing framework written in Python, for instance and wrap (or I guess, bind?) the API calls in Ada?例如,我可以使用一个用 Python 编写的现有框架并包装(或者我猜是绑定?)Ada 中的 API 调用吗? Should I just pass off the scripting capabilities?我应该放弃脚本功能吗? I'm trying to figure it out.我想弄明白。

Case in point: Scikit (sklearn) https://scikit-learn.org/stable/tutorial/text_analytics/working_with_text_data.html#例证:Scikit(sklearn) https://scikit-learn.org/stable/tutorial/text_analytics/working_with_text_data.html#

This does some neat stuff, and I'd like to be able to leverage this, but with an Ada program.这做了一些巧妙的事情,我希望能够利用它,但使用 Ada 程序。 Does anyone have advice from a similar experience?有没有人有类似经验的建议?

I am just researching, so I have tried finding information.我只是在研究,所以我尝试查找信息。

http://www.inspirel.com/articles/Ada_Python_Binding.html https://scikit-learn.org/stable/tutorial/text_analytics/working_with_text_data.html# http://www.inspirel.com/articles/Ada_Python_Binding.html https://scikit-learn.org/stable/tutorial/text_analytics/working_with_text_data.html#

The inspirel solution is based on python2.7. inspirel解决方案基于python2.7。 If you're using anything from python3.5 onwards a few mods need to be made如果您使用的是 python3.5 以上版本,则需要制作一些模组

python_module.py python_模块.py

#print 'Hello from Python module'
print('Hello from Python module')

#print 'Python adding:', a, '+', b
print('Python adding:', a, '+', b)

ada_main.adb ada_main.adb

-- Python.Execute_String("print 'Hello from Python!'");
Python.Execute_String("print('Hello from Python!')");

Some routines have been deprecated so the linkage has to change一些例程已被弃用,因此必须更改链接

python.adb python.adb

--pragma Import(C, PyInt_AsLong, "PyInt_AsLong");
pragma Import(C, PyInt_AsLong, "PyLong_AsLong");

--pragma Import(C, PyString_FromString, PyString_FromString");
pragma Import(C, PyString_FromString, "PyUnicode_FromString");

Now the fun bit.现在有趣的是。 On Linux, changing to say python 3.9, you'd just change在 Linux 上,更改为 python 3.9,您只需更改

--for Default_Switches ("Ada") use ("-lpython2.7");
for Default_Switches ("Ada") use ("-lpython3.7");

but on windows, the libraries aren't dumped in a community lib.但在 windows 上,这些库不会转储到社区库中。 All the packages are kept separately.所有包裹都分开存放。 The -L has to be added to tell the linker where to find the library.必须添加 -L 以告诉 linker 在哪里可以找到库。 In my case, I did a non-admin install of python, so it looks something like就我而言,我进行了 python 的非管理员安装,所以它看起来像

for Default_Switches ("Ada") use ("-L\Users\StdUser\AppData\Local\Programs\Python\Python37-32\libs", "-lpython37");

Use gprbuild instead of gnatmake -p, which has been deprecated.使用 gprbuild 而不是 gnatmake -p,后者已被弃用。 If you do all your mods correctly如果你正确地做所有的模组

gprbuild ada_main.gpr

should give you an executable in obj\ada_main.exe.应该在 obj\ada_main.exe 中给你一个可执行文件。 Running the executable should give运行可执行文件应该给

C:\Users\StdUser\My Documents\ada-python>gprbuild ada_main.gpr
Compile
   [Ada]          ada_main.adb
Bind
   [gprbind]      ada_main.bexch
   [Ada]          ada_main.ali
Link
   [link]         ada_main.adb

C:\Users\StdUser\My Documents\ada-python>obj\ada_main.exe
executing Python directly from Ada:
Hello from Python!

loading external Python module and calling functions from that module:
Hello from Python module!

asking Python to add two integers:
Python adding: 10 + 2
Ada got result from Python: 12

we can try other operations, too:
subtract: 8
multiply: 20
divide  : 5

Remember to put the pythonxx.dll somewhere on your path otherwise it won't be able to find the library wen it starts executing.请记住将 pythonxx.dll 放在路径的某个位置,否则它将无法在开始执行时找到库。

It is possible to use an existing machine learning framework written in Python, such as scikit-learn, with an Ada program.可以使用Python编写的现有机器学习框架,例如scikit-learn,带有Ada程序。 One way to do this is to use Ada's foreign function interface (FFI) to bind to the Python API calls, so that you can call the Python functions from within your Ada program.一种方法是使用 Ada 的外部 function 接口 (FFI) 绑定到 Python API 调用,这样您就可以从 Ada 程序中调用 Python 函数。 This can be done using a library such as GPRBind or SWIG, which can generate the necessary bindings.这可以使用 GPRBind 或 SWIG 等库来完成,它们可以生成必要的绑定。

Another option is to use the Python script to perform the machine learning tasks and pass the data to and from the Ada program using the command line or a file.另一种选择是使用 Python 脚本执行机器学习任务,并使用命令行或文件将数据传入和传出 Ada 程序。 This way you can leverage the capabilities of the Python framework without having to integrate it directly into your Ada program.这样您就可以利用 Python 框架的功能,而不必将其直接集成到您的 Ada 程序中。

It's worth noting that, as Ada is a systems programming language, it tends to be used more often in embedded systems, real-time systems and safety-critical systems.值得注意的是,由于 Ada 是一种系统编程语言,它往往更常用于嵌入式系统、实时系统和安全关键系统。 It's less popular for ML.它对 ML 来说不太受欢迎。 Therefore, it might be more challenging to find Ada libraries or Ada-specific tutorials for ML tasks.因此,为 ML 任务找到 Ada 库或特定于 Ada 的教程可能更具挑战性。

It's recommended that you try out different ways of integrating with Python and see which one works best for your specific use case.建议您尝试与 Python 集成的不同方式,看看哪种方式最适合您的特定用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM