Getting started with Deep Learning in TensorFlow and Swift

Abhimanyu Aryan | November 22, 2019 · 2 min read

Edit on GithubEdit on Github

There are 3 ways to get started with coding Swift & TensorFlow:

  • Google Colab (Basic: Windows/Mac/Linux)
  • Command Line (Advanced: Mac/Linux)
  • REPL Playground XCode (Basic: Mac | Coming Soon…)

Note: I’ll cover first two approaches today in today’s blogpost ie. google colab & command line. I’ll keep 3rd approach for separate video where I’ll also teach you more about Swift & it’s playground

1. Google Colab

First we need to create an empty swift.ipynb notebook. You can do this by going to terminal and typing

> touch swift.ipynb

Now .ipynb files contains json. To make it a swift, all notebook we need is some json inside the notebook

Open the notebook in your favourite code editor. I prefer VSCode

> code swift.ipynb

should open VSCode from terminal. Now copy paste below json in swift.ipynb

{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "swift_notebook.ipynb",
      "version": "0.3.2",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "swift",
      "display_name": "Swift"
    }
  },
  "cells": [
    {
      "metadata": {
        "id": "icDfXRlHRYvE",
        "colab_type": "code",
        "outputId": "a1489762-9fe2-4fac-8e24-e3c969f51c8c",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 55
        }
      },
      "cell_type": "code",
      "source": [
        "let x = 2\n",
        "let y = 2\n",
        "print(\"Hello world, this is Swift! \\(x + y)\")"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Hello world, this is Swift! 4\r\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}

Now go to https://colab.research.google.com/notebooks/welcome.ipynb and then click on File > Upload Notebook

Upload your Swift.ipynb & you are done. You can now write Swift & TensorFlow code in there

tensorflowSwift

2. Command Line

Download Swift-TensorFlow for Mac or Ubuntu from here: https://github.com/tensorflow/swift/blob/master/Installation.md

Since I’m on mac so I’ll download XCode10 .pkg file

Installation for Mac & XCode are given here: https://github.com/tensorflow/swift/blob/master/Installation.md#installation

Once you have everything setup you can use Swift just like Python in terminal

  • Comping `.swift` files

basics.swift

print("Tensorflow Basics Tutorial")

import TensorFlow

let x = Tensor<Float>([[2,2,], [2,2]])
print(x)

Now we will compile basics.swift using swift compiler. Open Terminal

❯ swift basics.swift
Tensorflow Basics Tutorial
[[2.0, 2.0], [2.0, 2.0]]
  • REPL

Swift has Python like REPL since swift compiler is based on LLVM Infrastructure

REPL

What's next?

  • I want to discuss why TensorFlow & Swift
  • Swift Compiler Technology. How is it better than competition
  • Using Python libraries with Swift-TensorFlow

Written by

Abhimanyu Aryan

Hi, I'm Abhimanyu Aryan, Deep Learning practitioner. Click here if you want know more about me or checkout my portfolio.