47 Degrees joins forces with Xebia read more

Getting started with Swift for TensorFlow

Getting started with Swift for TensorFlow

TensorFlow is an open source library developed by the Google Brain team and widely used for Machine Learning purposes. The latest version is 2.0, and the main language used is Python; however, TensorFlow began using Swift recently. Although the APIs are still in beta and subject to improvements and changes, the current state of Swift for TensorFlow is good enough to start writing your first Machine Learning models.

Why the change to Swift?

When one thinks of Machine Learning, Python immediately comes to mind. It is a language with an easy learning curve, low boilerplate, a big set of libraries and tools for numerical computation and science, and an important community in the Artificial Intelligence and Machine Learning fields. If this is the situation, why change to a different programming language?

It turns out that there are also some drawbacks to using Python in TensorFlow, mainly related to performance, concurrency, ease of deployment, and building custom operations. These challenges led the design team to look at other alternatives and how they fit their requirements. From using Rust, C++, or Java, to making their own programming language, the team finally chose Swift as the candidate language to address the main problems that they had with Python.

But what do we do with all the Python libraries, like numpy or matplotlib, that are frequently used in this field? Do we need to rewrite all of them into Swift? Fortunately no. An integration of Python into Swift via the PythonKit module has been developed so that you can easily import your Python library of choice and use it with Swift syntax.

Trying Swift for TensorFlow

There is a straightforward way of trying Swift for TensorFlow without requiring any installation of tools on your computer, and regardless of the operating system of your choice. This is possible thanks to Google Colaboratory. You can create a notebook in the spirit of Jupyter notebooks in Python, but using Swift as your programming language. To do so, you just need to use this link and you are ready to get started.

If you prefer running things on your computer, you can also install Swift for TensorFlow. You can download it from here and follow the steps corresponding to your IDE and operating system. If you are using Xcode, remember to relaunch it after switching the toolchain for it to be used effectively; otherwise, even though it appears changed, you may not be able to use TensorFlow.


With Swift for TensorFlow, we can leverage the fantastic features of Swift as a programming language, addressing the main drawbacks that we had in Python, but still take advantage of Python libraries that have been thoroughly battle-tested. In the following weeks, we will publish some examples of what’s possible with Swift for TensorFlow using some sample datasets. Until then, make sure you get your setup ready and join us in this journey exploring Machine Learning with Swift!

Ensure the success of your project

47 Degrees can work with you to help manage the risks of technology evolution, develop a team of top-tier engaged developers, improve productivity, lower maintenance cost, increase hardware utilization, and improve product quality; all while using the best technologies.