Getting Started with Deep Learning in Swift and TensorFlow

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

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

Note: I’ll cover the first two approaches today — Google Colab & command line. The 3rd approach (XCode Playground) will be a separate post.


1. Google Colab

First, create an empty swift.ipynb notebook:

touch swift.ipynb
code swift.ipynb

Open it in VSCode and paste this JSON to make it a Swift kernel notebook:

{
  "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"
      },
      "cell_type": "code",
      "source": ["let x = 2\n", "let y = 2\n", "print(\"Hello world, this is Swift! \\(x + y)\")"],
      "execution_count": 0,
      "outputs": []
    }
  ]
}

Then go to colab.research.google.comFile > Upload Notebook → upload your Swift.ipynb. You can now write Swift & TensorFlow in Colab!


2. Command Line

Download Swift-TensorFlow for Mac or Ubuntu from the official installation guide.

Once set up, create basics.swift:

print("Tensorflow Basics Tutorial")

import TensorFlow

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

Compile and run:

swift basics.swift

Output:

Tensorflow Basics Tutorial
[[2.0, 2.0], [2.0, 2.0]]

Swift also has a Python-like REPL since it’s built on the LLVM infrastructure.


What’s Next?

  • Why TensorFlow & Swift?
  • Swift Compiler Technology — how it compares to the competition
  • Using Python libraries with Swift-TensorFlow



Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • How to Redesign the Dyad Agent
  • How to Build the Harness
  • How to Build the Control Room for Your Agent
  • Agent Architectures: From Single Agent to Hybrid MAS
  • Building a Single Agent System: From Formal Foundations to Working Code