At the TensorFlow Developer Summit in March 2018, Swift was announced for TensorFlow as an open-source project on GitHub. Later in March 2019, Jeremy Howard founder of fastai announced that two lessons in their next course (co-taught with the inventor of Swift, Chris Lattner) will cover Swift for TensorFlow and show how to get started programming in Swift.

Naturally, we all started to worry if Python's days are numbered? and especially if a data scientist/ machine learning engineer should start learning Swift?. In this article, I will try answering both questions as thoroughly as possible.


✏️ Table of Contents

  • Python
  • Swift
  • Are python and swift similar?
  • What all deep learning libraries have in common?
  • Why C/C++ libraries are a problem?
  • Why Swift was chosen for TensorFlow?
  • Swift Advantages
  • Should you learn Swift?
  • Conclusion
  • References

🐍 Python

Python is a general-purpose programming language created by Guido Van Rossum in the late 1980s. Python has been around for a couple of decades and there are libraries for any use case that you may come up with. As stated in 2019 stackoverflow developer survey results,

"Python is the fastest-growing major programming language, has risen in the ranks of programming languages in our survey yet again, edging out Java this year and standing as the second most loved language (behind Rust)."

🍎 Swift

In the 6th place of the most loved language, you see Swift, which definitely rings a bell if you are an iOS app developer. Its development started in July 2010 (relatively new programming language) by Chris Lattner, with the eventual collaboration of many other programmers at Apple.

Chris Lattner is now working on one of the best AI, deep learning research groups in the world at Google Brain. This tells us that Swift language is probably a serious project. Some of iOS applications written in Swift can be seen in the picture below.

Its key advantages are:

  • Writing code is interactive and fun
  • The syntax is concise yet expressive
  • Apps run lightning-fast (Swift code works side-by-side with Objective-C)

πŸ€” Are python and swift similar?

Swift is largely influenced by several languages like Ruby, C# and particularly Python in an attempt to be modern, fast and interactive as stated by its creator. Worth mentioning that a lot of similarities are found between Swift and Python.

Check the link below if you interested to see a comparison between Python & Swift syntax:

https://blog.michaelckennedy.net/2014/11/26/comparison-of-python-and-apples-swift-programming-language-syntax/


❓ What all deep learning libraries like Theano, TensorFlow, Keras and PyTorch have in common?

The answer is simple all are written in Python which has been crowned as the go-to language for deep learning. Python is user-friendly and easy to learn language however it was not designed to be fast. To address those speed limitations libraries were written in other languages (generally C and C++), like numpy, PyTorch, and TensorFlow, which provide Python wrappers.


πŸ’š Why C/C++ libraries that are at the heart of nearly all Python numeric programming are a problem?

Researchers and educators can not easily modify/inspect the underlying code since it requires a whole different toolbox. This is due to the fact that the normal Python-based debugging and inspection approaches can not handle libraries in other languages.

For example, the performance achieved in pure CUDA C implementations of different architectures of RNN, cannot be compared even when using PyTorch’s fantastic new JIT compiler.


πŸš€ Why Swift was chosen for TensorFlow?

One of the key reasons is that it has a very powerful automatic differentiation system; which is one of the foundations of deep learning needed for calculating gradients. Note that automatic differentiation is not a core component of Python.

More details can be found in this document which describes:

  • Python drawbacks
  • Other languages considered
  • How eventually it was narrowed down to Swift.

πŸš€ For people who like video courses and want to kick-start a career in data science today, I highly recommend the below video course from Udemy:

Python for Statistical Analysis

πŸ“š While for book lovers:


πŸ’ͺ Swift Advantages

  • Swift is Fast. Unfortunately, speed is Python’s Achilles heel. Data is not getting smaller. In fact, quite the opposite. Swift runs as fast as C code without memory safety issues (in C someone has to worry for memory management) and it is easier to learn. This is achieved due to the LLVM compiler (behind Swift) which is very powerful.
  • Python Interoperability,using python with Swift. Not many ML libraries exist for Swift since it is a fairly new language. That caveat can be addressed by simply importing any Python, C and C++ library in Swift, and it just works.
  • Swift Can Go Very Low: Underneath the hood, deep learning Python framework have some C code. So when calling a function at some point you will hit some C code (i.e implementation of TF convolutions). On the contrary, Swift sits very close to the hardware, no C code in between allowing the developer to inspect code at a very low level.
  • Swift Automatic Memory Management: ARC, Automatic Reference Counting prevents memory leaks optimising performance. On the contrary, Python is characterized by high memory consumption fact which renders it unsuitable for intensive memory tasks.

Note that Python has been around for over 30 years having all of the advantages of a more mature language (i.e large user base, a plethora of libraries). As Swift is fairly new lacks talented Swift developers. But this drawback can easily become an advantage for those who want to be a Swift developer (demand for Swift coders is increasing).


πŸ‘¨β€πŸ’» Should you learn Swift?

The answer is Yes if and only if you are an experienced deep learning researcher. It is forecasting that Swift will eventually be the language of deep learning as Google invests heavily in making Swift a key component of its TensorFlow ML infrastructure.

Useful Resources to get started with Swift:

Hacking with Swift – learn to code iPhone and iPad apps with free Swift 5.1 tutorials
Learn Swift coding for iOS with these free Swift 5.1 tutorials

On the contrary, if you are just beginning your data science journey Python, which is most praised for its elegant syntax and readable code, suits you best. There is no doubt that Python will still be the king in the ML arena for many years to come but Swift is catching up.

Useful to note that both Python and Swift are open source tools.


πŸ€– Conclusion

This brings us to the end of this article. Hope you got your questions answered regarding Swift.

β€ŒThanks for reading, if you liked this article, please consider subscribing to my blog. That way I get to know that my work is valuable to you and also notify you for future articles.β€Œ

πŸ’ͺπŸ’ͺπŸ’ͺπŸ’ͺ As always keep studying, keep creating πŸ”₯πŸ”₯πŸ”₯πŸ”₯


β€ŒπŸ”˜ References