Python porting to Julia resources

I’m trying to port an Machine Learning app that was written in Python using Pytorch to Julia and I’m a newbie in Julia.
Is there a resource,cheat sheet that show how the same thing work in Julia for examples
Python Julia
class ?
init ?
call ?
iter ?
**kwargs ?
etc…
I’ve been reading Julia doc but would take too long I want to get this done a little faster

1 Like

Are you asking for help woth those specific things or a resource for the machine learning functions?

  • Classes don’t exist in Julia
  • Init isn’t necessary to create structures. Loook at https://docs.julialang.org/en/v1/manual/constructors/
  • Iterators are pretty broad and what you want to do will depend on the type of iteration.
  • You can just write methodname(; kwargs...)
1 Like

If you intend to actually use Julia besides porting one application, you’ll probably be better off learning Julia properly. The documentation and several available tutorials are available for that. It quickly pays off when you don’t have to consult online forums for simple things.

7 Likes

I thought some one have done this before porting their Python app over to Julia
they might have some resource that they used willing to share on the net.

1 Like

I’m sure similar things have been ported over but it’s unclear what you would need. There’s not a general “How to convert Python to Julia” guide (although I’m sure there’s some sort of numpy to julia guide out there).

There’s not an exact PyTorch equivalent, but there are several neural net libraries. You can get a lot done with Flux.jl. I’m not personally familiar with Knet.jl, but I’ve heard it’s well implemented and easy to use. You could also use Tensorflow.jl which you might prefer if you’re familiar with Tensorflow but want the benefits of Julia.

Those libraries will give you a good idea of how to port standard layers that you’re probably use to in other ML libraries, but Julia takes an entirely different approach than the Python mega libraries out there. With Julia you can have many libraries be interoperable but still be fast. In Python each library has to reinvent a lot of stuff for their own use. In other words, you will likely need lots of tools that are much less involved than learning all of PyTorch.

1 Like

Thank for the suggestion.

No problem. Feel free to ask more questions here. The Julia community is very friendly to newcomers.

1 Like

You may find this useful: https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-Python-1

1 Like

I have recently worked through porting Lark (a Python parsing package) to Julia. Some little things to be prepared for:

  • A single character ‘a’ and a single character string “a” are different in Julia, unlike Python. Equality tests in Julia will therefore fail where they succeed in Python. In general, make sure you know when a Julia function returns a character. You can add a method in Julia to make them equal, of course.
  • Do you know how the syntax a,=b works in Python? In Julia? Check it out!
  • Make sure you know the difference between push!, append! in Julia and Python’s append, extend, +=. Hint: append works differently.
  • Python code loves things like if b:, where None and empty both count as False. nothing and an empty string/list/tuple are not false in Julia.
  • Make sure that both Julia hash and == behave the same way as Python for your complex structures. I spent the most debugging time discovering that functions like unique!, Set(), Dict() had slightly different ideas as to the contents of lists and sets than I had.
  • The Julia “join” function takes the joining string as the final argument, not the first.

To answer your original question, I do not know of a cheatsheet, and I suspect each Python codebase will be approached differently depending on the particular coding philosophy.

The strategy I took with __init__ was to define one or more outer constructors for that type.

**kwargs are handled by “splatting”" and “slurping”.

The only use of __call__ in my source codebase appeared to be a way of passing around a closure in an object…so I just created a closure in Julia.

I didn’t have to work with __iter__. Python generator functions (containing yield) can be modeled using unbuffered Julia Channels. But beware: as Python is single-threaded, the code after the yield is not executed until whichever code was yielded to calls back into the generator. However, in Julia code following a put! is executed as soon as the value is read, and runs right up until the next put!. So, if the Python code calling the generator sees fit to mutate any structures that the generator is using after it has received the value but before it calls for the next value, then the Julia code may diverge in behaviour, as it will be running in parallel with the generator, unlike the Python code. The way I solved this was to have a two-way channel so that the calling function had to send a dummy value back to allow calculations to proceed.

None of this is to imply that it might not be better to restructure the Julia code to use more natural Julia structures. As this codebase was not my own, I didn’t feel confident in getting too fancy until I had all the tests passing.

7 Likes

Thank, this is what I’m looking for you should put this on cheat sheet or web site,so those of us that come after you don’t have to spend hour debugging them to find out that it works a little differently. May be later when I have more time and better understanding of Julia I’ll rewrite it in Julia

Yes, It’s been helpful, thank.

I am in about the same position as @vphom - I just started to learn Julia, and I have a small application or two, which I would probably like to port from Python and further develop it in Julia. “Probably” in my case because whether we will really need the applications depends on many circumstances.

So in one case I started with the Python application and tried to port the most computational expensive part of it to Julia, than call it from Python through pyjulia. That actually worked on one of the computers, while on another one I couldn’t properly install it. Thus I could port the module function by function and check results against the original program.

To the list of the things to be prepared for (@frtps , thank you for your post), I would like to add one more - the scoping rules as discussed here: Dicussion on scoping rules . The nasty thing is that in many cases your try blocks and loops would work in Julia just as in Python - because you just happend to initialize the corresponding variable in some way.

As a general remark - from my recent experience: Julia may look familiar on the first glance, but pretty soon you will find out you need to invest time in reading at least a big part of the manual anyway.

Also: