# Help, Convert from python to Fortran

**URL:** <https://fortran-lang.discourse.group/t/help-convert-from-python-to-fortran/6512>\
**Category:** Uncategorized\
**Created:** [September 14, 2023, 8:04pm UTC](https://fortran-lang.discourse.group/t/help-convert-from-python-to-fortran/6512 "2023-09-14T20:04:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![eikeakanam](https://avatars.discourse-cdn.com/v4/letter/e/3d9bf3/32.png) [@eikeakanam](https://fortran-lang.discourse.group/u/eikeakanam)\
**Post date:** [September 14, 2023, 8:04pm UTC](https://fortran-lang.discourse.group/t/help-convert-from-python-to-fortran/6512/1 "2023-09-14T20:04:02Z")

</div>

I’ve got python implementation for a simple neural network using mainly numpy and trying to convert to fortran but been stuck for days. any suggestions or assistance will be appreciated

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [September 14, 2023, 8:23pm UTC](https://fortran-lang.discourse.group/t/help-convert-from-python-to-fortran/6512/2 "2023-09-14T20:23:01Z")

</div>

That tutorial could help:

> **[Python Fortran Rosetta Stone — Fortran Programming Language](https://fortran-lang.org/learn/rosetta_stone/)**
>
> Fortran : High-performance parallel programming language

---

<div class="post-metadata">

**Author:** ![nbehrnd](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nbehrnd](https://fortran-lang.discourse.group/u/nbehrnd)\
**Post date:** [September 14, 2023, 9:05pm UTC](https://fortran-lang.discourse.group/t/help-convert-from-python-to-fortran/6512/3 "2023-09-14T21:05:57Z")

</div>

While preparing his book [Modern Fortran](https://www.manning.com/books/modern-fortran), Milan Courcic retained a dedicated GitHub repository [neural-fortran](https://github.com/modern-fortran/neural-fortran). Beliavsky’s survey of Fortran projects on GitHub equally features a section dedicated to [this topic](https://github.com/Beliavsky/Fortran-code-on-GitHub#neural-networks-and-machine-learning). (It is difficult to provide a specific answer if the question remains vague and somewhat fuzzy.)

---

<div class="post-metadata">

**Author:** ![piotr](https://avatars.discourse-cdn.com/v4/letter/p/839c29/32.png) [@piotr](https://fortran-lang.discourse.group/u/piotr)\
**Post date:** [September 14, 2023, 10:03pm UTC](https://fortran-lang.discourse.group/t/help-convert-from-python-to-fortran/6512/4 "2023-09-14T22:03:29Z")

</div>

Your question lacks specificity. However, some time ago, I translated a Rust-based neural network into Fortran primarily using ChatGPT and manually correcting errors.

Given the improvement in this tool, ChatGPT may be your optimal choice. Nevertheless, if I were in your position, I would first modify the Python code to eliminate any external libraries.

Additionally, as pointed out by @nbehrnd, you might want to consider trying out [neural-fortran](https://github.com/modern-fortran/neural-fortran). Unfortunately, my Fortran code is closed-source. Nevertheless, a few days ago, I initiated the development of a Lua-based deep learning program (view it [here](https://github.com/piotrbajdek/neurosculptor) – it may contain bugs since the code is only several days old). Perhaps, having this in Fortran would be intriguing as well, but do we really need more options when neural-fortran is already well-established within the community?

---

<div class="post-metadata">

**Author:** ![aerosayan](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@aerosayan](https://fortran-lang.discourse.group/u/aerosayan)\
**Post date:** [September 15, 2023, 2:42am UTC](https://fortran-lang.discourse.group/t/help-convert-from-python-to-fortran/6512/5 "2023-09-15T02:42:38Z")

</div>

Could you please share more about your code?

Since you mentioned that you’re mainly using numpy, I’m blindly assuming (and kindly correct me if I’m wrong) that your code is pedagogical in nature, or if it’s for industrial applications, you’re not using advanced Neural Network libraries for Python.

If your code is mainly simple linear algebra code, then yes, it can be easily converted to Fortran, but if you’re using advanced Neural Network libraries in Python like Tensorflow, Keras, etc. then it will require more efforts from your side to convert the code to Fortran.

---

<div class="post-metadata">

**Author:** ![rbitr](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rbitr](https://fortran-lang.discourse.group/u/rbitr)\
**Post date:** [September 17, 2023, 8:53pm UTC](https://fortran-lang.discourse.group/t/help-convert-from-python-to-fortran/6512/6 "2023-09-17T20:53:31Z")

</div>

If it helps, I have the same transformer implemented in python (with numpy) and in Fortran. See [https://github.com/rbitr/llama2.ipynb/blob/master/llama2.ipynb](https://github.com/rbitr/llama2.ipynb/blob/master/llama2.ipynb) and [https://github.com/rbitr/llama2.f90/blob/original\_master/llama2.f90](https://github.com/rbitr/llama2.f90/blob/original_master/llama2.f90) the transformer function in each. They should be pretty close translations, but it really depends on what you’re stuck on.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [September 18, 2023, 10:19pm UTC](https://fortran-lang.discourse.group/t/help-convert-from-python-to-fortran/6512/7 "2023-09-18T22:19:10Z")

</div>

And here is how to implement GPT-2 transformer in Fortran: [GitHub - certik/fastGPT: Fast GPT-2 inference written in Fortran](https://github.com/certik/fastGPT/), translated from a Python NumPy code (picoGPT).

---

<div class="post-metadata">

**Author:** ![ebourne](https://avatars.discourse-cdn.com/v4/letter/e/df705f/32.png) [@ebourne](https://fortran-lang.discourse.group/u/ebourne)\
**Post date:** [September 25, 2023, 8:55am UTC](https://fortran-lang.discourse.group/t/help-convert-from-python-to-fortran/6512/8 "2023-09-25T08:55:03Z")

</div>

If you are only using basic NumPy functionalities (array, array operations, etc) then you may be able to use Pyccel to do this automatically:

> **[GitHub - pyccel/pyccel: Python extension language using accelerators](https://github.com/pyccel/pyccel/)**
>
> Python extension language using accelerators. Contribute to pyccel/pyccel development by creating an account on GitHub.

Pyccel also creates an interface which will let you call your Fortran code from Python which should allow you to do the work bit-by-bit if necessary

Disclaimer: I am one of the main developers of Pyccel
