Skip to content

Swift for TensorFlow Deep Learning Library

License

Notifications You must be signed in to change notification settings

pschuh/swift-apis

 
 

Repository files navigation

Swift for TensorFlow Deep Learning Library

Get a taste of protocol-oriented differentiable programming.

This repository hosts Swift for TensorFlow's deep learning library, available both as a part of Swift for TensorFlow toolchains and as a Swift package.

Usage

This library is being automatically integrated in Swift for TensorFlow toolchains. You do not need to add this library as a Swift Package Manager dependency.

Use Google Colaboratory

Open an empty Colaboratory now to try out Swift, TensorFlow, differentiable programming, and deep learning.

For detailed usage and troubleshooting, see Usage on the Swift for TensorFlow project homepage.

Define a model

Simply import TensorFlow to get the full power of TensorFlow.

import TensorFlow

let hiddenSize: Int = 10

struct Model: Layer {
    var layer1 = Dense(inputSize: 4, outputSize: hiddenSize, activation: relu)
    var layer2 = Dense(inputSize: hiddenSize, outputSize: hiddenSize, activation: relu)
    var layer3 = Dense(inputSize: hiddenSize, outputSize: 3, activation: {$0})
    
    @differentiable(wrt: (self, input))
    func applied(to input: Tensor<Float>) -> Tensor<Float> {
        let l1 = layer1.applied(to: input)
        let l2 = layer2.applied(to: l1)
        return layer3.applied(to: l2)
    }
}

Run a training loop

let optimizer = SGD<Model, Float>(learningRate: 0.02)
var classifier = Model()
let x: Tensor<Float> = ...
let y: Tensor<Float> = ...

for _ in 0..<1000 {
    let 𝛁model = classifier.gradient { classifier -> Tensor<Float> in
        let ŷ = classifier.applied(to: x)
        let loss = softmaxCrossEntropy(logits: ŷ, labels: y)
        print("Loss: \(loss)")
        return loss
    }
    optimizer.update(&classifier.allDifferentiableVariables, along: 𝛁model)
}

For more tutorials and models, go to tensorflow/swift-tutorials and tensorflow/swift-models.

Development

Requirements

Building and testing

$ swift build
$ swift test

Bugs

Please report bugs and feature requests using GitHub issues in this repository.

Community

Discussion about Swift for TensorFlow happens on the [email protected] mailing list.

Contributing

We welcome contributions: please read the Contributor Guide to get started. It's always a good idea to discuss your plans on the mailing list before making any major submissions.

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.

About

Swift for TensorFlow Deep Learning Library

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Swift 98.0%
  • Dockerfile 2.0%