Convert Figma logo to code with AI

tensorflow logoswift

Swift for TensorFlow

6,130
608
6,130
37

Top Related Projects

88,135

Tensors and Dynamic neural networks in Python with strong GPU acceleration

18,872

Open standard for machine learning interoperability

ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator

Quick Overview

TensorFlow Swift is an open-source project that integrates the Swift programming language with TensorFlow, Google's machine learning framework. It aims to provide a high-performance, strongly-typed interface for building and training machine learning models, leveraging Swift's language features and compiler optimizations.

Pros

  • Combines the expressiveness and safety of Swift with the power of TensorFlow
  • Offers automatic differentiation capabilities, simplifying gradient computation
  • Provides strong type checking and compile-time optimizations
  • Enables seamless integration with existing Swift and Objective-C codebases

Cons

  • Limited community support compared to Python-based TensorFlow
  • Fewer available resources, tutorials, and third-party libraries
  • Still in early stages of development, with potential API changes
  • May require learning Swift for developers not familiar with the language

Code Examples

  1. Creating a simple neural network:
import TensorFlow

let model = Sequential {
  Dense(units: 64, activation: relu)
  Dense(units: 10, activation: softmax)
}
  1. Training a model:
let optimizer = SGD(learningRate: 0.01)
let (loss, grads) = valueWithGradient(at: model) { model -> Tensor<Float> in
  let logits = model(images)
  return softmaxCrossEntropy(logits: logits, labels: labels)
}
optimizer.update(&model, along: grads)
  1. Performing inference:
let input = Tensor<Float>(shape: [1, 28, 28, 1], scalars: imageData)
let prediction = model(input)
let label = prediction.argmax().scalar!

Getting Started

To get started with TensorFlow Swift:

  1. Install Swift for TensorFlow:

    brew install swift
    
  2. Create a new Swift file (e.g., main.swift) and import TensorFlow:

    import TensorFlow
    
  3. Write your TensorFlow code using Swift syntax.

  4. Compile and run your code:

    swiftc -O -emit-executable main.swift -o myprogram
    ./myprogram
    

For more detailed instructions and documentation, visit the official TensorFlow Swift repository on GitHub.

Competitor Comparisons

88,135

Tensors and Dynamic neural networks in Python with strong GPU acceleration

Pros of PyTorch

  • More intuitive and Pythonic API, easier for beginners to learn
  • Dynamic computational graphs allow for more flexible model architectures
  • Stronger community support and ecosystem for research applications

Cons of PyTorch

  • Generally slower deployment in production environments
  • Less comprehensive support for mobile and edge devices
  • Smaller ecosystem for non-research industrial applications

Code Comparison

PyTorch:

import torch

x = torch.tensor([1, 2, 3])
y = torch.tensor([4, 5, 6])
z = torch.add(x, y)

Swift for TensorFlow:

import TensorFlow

let x = Tensor<Int32>([1, 2, 3])
let y = Tensor<Int32>([4, 5, 6])
let z = x + y

Summary

PyTorch offers a more intuitive API and flexibility for research, while Swift for TensorFlow aims to provide better performance and integration with Apple's ecosystem. PyTorch has a larger community and more extensive documentation, but Swift for TensorFlow may offer advantages in terms of compile-time optimizations and seamless integration with Swift-based applications.

18,872

Open standard for machine learning interoperability

Pros of ONNX

  • Broader ecosystem support and compatibility across multiple frameworks
  • More mature and established project with wider industry adoption
  • Focuses on interoperability and standardization of neural network models

Cons of ONNX

  • Less tightly integrated with a specific programming language
  • May require additional steps for model conversion and optimization
  • Potentially slower development cycle for cutting-edge features

Code Comparison

ONNX example:

import onnx

model = onnx.load("model.onnx")
onnx.checker.check_model(model)
print(onnx.helper.printable_graph(model.graph))

Swift for TensorFlow example:

import TensorFlow

let model = Sequential {
  Dense(units: 64, activation: relu)
  Dense(units: 10, activation: softmax)
}
let optimizer = SGD(learningRate: 0.01)

Summary

ONNX is a more established project focused on interoperability across frameworks, while Swift for TensorFlow aims to integrate deep learning directly into the Swift programming language. ONNX offers broader ecosystem support but may require additional steps for model conversion. Swift for TensorFlow provides a more native integration with Swift but has a smaller ecosystem and is less mature. The choice between them depends on specific project requirements and the preferred development environment.

ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator

Pros of ONNX Runtime

  • Broader ecosystem support: Works with models from various frameworks, not limited to Swift
  • More mature and production-ready: Widely used in industry with extensive optimizations
  • Cross-platform compatibility: Supports multiple programming languages and operating systems

Cons of ONNX Runtime

  • Less tightly integrated with Swift: May require additional steps for Swift developers
  • Potentially larger footprint: More comprehensive runtime compared to Swift for TensorFlow

Code Comparison

ONNX Runtime (Python):

import onnxruntime as ort
session = ort.InferenceSession("model.onnx")
input_name = session.get_inputs()[0].name
output = session.run(None, {input_name: input_data})

Swift for TensorFlow:

import TensorFlow
let model = try MLModel(contentsOf: modelURL)
let prediction = try model.prediction(from: input)

Summary

ONNX Runtime offers broader compatibility and production-readiness, while Swift for TensorFlow provides tighter integration with Swift and potentially a smaller footprint. ONNX Runtime is more versatile across frameworks and languages, whereas Swift for TensorFlow focuses on Swift-specific optimizations and ease of use for Swift developers.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Swift for TensorFlow (Archived)

Swift for TensorFlow was an experiment in the next-generation platform for machine learning, incorporating the latest research across machine learning, compilers, differentiable programming, systems design, and beyond. It was archived in February 2021. Some significant achievements from this project include:

This site will not receive further updates. The API documentation and binary downloads will continue to be accessible as well as the Open Design Review meeting recordings.

Getting started

Using Swift for TensorFlow

Tutorials

TutorialLast Updated
A Swift TourMarch 2019
Protocol-Oriented Programming & GenericsAugust 2019
Python InteroperabilityMarch 2019
Custom DifferentiationMarch 2019
Sharp Edges in DifferentiabilityNovember 2020
Model Training WalkthroughMarch 2019
Raw TensorFlow OperatorsDecember 2019
Introducing X10, an XLA-Based BackendMay 2020

Resources

Forums

The discussions happened on the swift@tensorflow.org mailing list.

Why Swift for TensorFlow?

Swift for TensorFlow is a new way to develop machine learning models. It gives you the power of TensorFlow directly integrated into the Swift programming language. We believe that machine learning paradigms are so important that they deserve first-class language and compiler support.

A fundamental primitive in machine learning is gradient-based optimization: computing function derivatives to optimize parameters. With Swift for TensorFlow, you can easily differentiate functions using differential operators like gradient(of:), or differentiate with respect to an entire model by calling method gradient(in:). These differentiation APIs are not just available for Tensor-related concepts—they are generalized for all types that conform to the Differentiable protocol, including Float, Double, SIMD vectors, and your own data structures.

// Custom differentiable type.
struct Model: Differentiable {
    var w: Float
    var b: Float
    func applied(to input: Float) -> Float {
        return w * input + b
    }
}

// Differentiate using `gradient(at:_:in:)`.
let model = Model(w: 4, b: 3)
let input: Float = 2
let (𝛁model, 𝛁input) = gradient(at: model, input) { model, input in
    model.applied(to: input)
}

print(𝛁model) // Model.TangentVector(w: 2.0, b: 1.0)
print(𝛁input) // 4.0

Beyond derivatives, the Swift for TensorFlow project comes with a sophisticated toolchain to make users more productive. You can run Swift interactively in a Jupyter notebook, and get helpful autocomplete suggestions to help you explore the massive API surface of a modern deep learning library. You can get started right in your browser in seconds!

Migrating to Swift for TensorFlow is really easy thanks to Swift's powerful Python integration. You can incrementally migrate your Python code over (or continue to use your favorite Python libraries), because you can easily call your favorite Python library with a familiar syntax:

import TensorFlow
import Python

let np = Python.import("numpy")

let array = np.arange(100).reshape(10, 10)  // Create a 10x10 numpy array.
let tensor = Tensor<Float>(numpy: array)  // Seamless integration!

Documentation

Beware: the project is moving very quickly, and thus some of these documents are slightly out of date as compared to the current state-of-the-art.

Overview

DocumentLast UpdatedStatus
Why Swift for TensorFlow?April 2018Current
Swift for TensorFlow Design OverviewApril 2018Outdated
Supported BackendsMay 2020Current

Technology deep dive

The Swift for TensorFlow project builds on top of powerful theoretical foundations. For insight into some of the underlying technologies, check out the following documentation.

DocumentLast UpdatedStatus
Swift Differentiable Programming ManifestoJanuary 2020Current
Swift Differentiable Programming Implementation OverviewAugust 2019Current
Swift Differentiable Programming Design OverviewJune 2019Outdated
Differentiable TypesMarch 2019Outdated
Differentiable Functions and Differentiation APIsMarch 2019Outdated
Dynamic Property Iteration using Key PathsMarch 2019Current
Hierarchical Parameter Iteration and OptimizationMarch 2019Current
First-Class Automatic Differentiation in Swift: A ManifestoOctober 2018Outdated
Automatic Differentiation WhitepaperApril 2018Outdated
Python InteroperabilityApril 2018Current
Graph Program ExtractionApril 2018Outdated

Source code

Compiler and standard library development happens on the main branch of the apple/swift repository.

Additional code repositories that make up the core of the project include:

Swift for TensorFlow is no longer a fork of the official Swift language; development was previously done on the tensorflow branch of the apple/swift repository. Language additions were designed to fit with the direction of Swift and are going through the Swift Evolution process.

Jupyter Notebook support

Jupyter Notebook support for Swift is under development at google/swift-jupyter.

Model garden

tensorflow/swift-models is a repository of machine learning models built with Swift for TensorFlow. It intended to provide examples of how to use Swift for TensorFlow, to allow for end-to-end tests of machine learning APIs, and to host model benchmarking infrastructure.

SwiftAI

fastai/swiftai is a high-level API for Swift for TensorFlow, modeled after the fastai Python library.

Community

Swift for TensorFlow discussions happen on the swift@tensorflow.org mailing list.

Bugs reports and feature requests

Before reporting an issue, please check the Frequently Asked Questions to see if your question has already been addressed.

For questions about general use or feature requests, please send an email to the mailing list or search for relevant issues in the JIRA issue tracker.

For the most part, the core team's development is also tracked in JIRA.

Contributing

We welcome contributions from everyone. Read the contributing guide for information on how to get started.

Code of conduct

In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.

The Swift for TensorFlow community is guided by our Code of Conduct, which we encourage everybody to read before participating.