Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tensorflow/swift-apis
Swift for TensorFlow Deep Learning Library
https://github.com/tensorflow/swift-apis
deep-learning differentiable-programming swift tensorflow
Last synced: about 2 months ago
JSON representation
Swift for TensorFlow Deep Learning Library
- Host: GitHub
- URL: https://github.com/tensorflow/swift-apis
- Owner: tensorflow
- License: apache-2.0
- Archived: true
- Created: 2019-02-12T00:25:51.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2022-06-18T21:05:42.000Z (over 2 years ago)
- Last Synced: 2024-09-23T07:32:46.274Z (about 2 months ago)
- Topics: deep-learning, differentiable-programming, swift, tensorflow
- Language: Swift
- Homepage:
- Size: 22.8 MB
- Stars: 794
- Watchers: 44
- Forks: 133
- Open Issues: 63
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Swift for TensorFlow Deep Learning Library
Get a taste of *protocol-oriented differentiable programming*.
This repository hosts [Swift for TensorFlow][s4tf]'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][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**][blank_colab] to try out Swift,
TensorFlow, differentiable programming, and deep learning.> For detailed usage and troubleshooting, see [Usage][usage] on the Swift for
TensorFlow project homepage.#### Define a model
Simply import `TensorFlow` to get the full power of TensorFlow.
```swift
import TensorFlowlet 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: identity)
@differentiable
func callAsFunction(_ input: Tensor) -> Tensor {
return input.sequenced(through: layer1, layer2, layer3)
}
}
```#### Initialize a model and an optimizer
```swift
var classifier = Model()
let optimizer = SGD(for: classifier, learningRate: 0.02)
Context.local.learningPhase = .training
// Dummy data.
let x: Tensor = Tensor(randomNormal: [100, 4])
let y: Tensor = Tensor(randomUniform: [100])
```#### Run a training loop
One way to define a training epoch is to use the
[`gradient(at:in:)`][gradient] function.```swift
for _ in 0..<1000 {
let 𝛁model = gradient(at: classifier) { classifier -> Tensor in
let ŷ = classifier(x)
let loss = softmaxCrossEntropy(logits: ŷ, labels: y)
print("Loss: \(loss)")
return loss
}
optimizer.update(&classifier, along: 𝛁model)
}
```Another way is to make use of methods on `Differentiable` or `Layer` that
produce a backpropagation function. This allows you to compose your derivative
computation with great flexibility.```swift
for _ in 0..<1000 {
let (ŷ, backprop) = classifier.appliedForBackpropagation(to: x)
let (loss, 𝛁ŷ) = valueWithGradient(at: ŷ) { ŷ in softmaxCrossEntropy(logits: ŷ, labels: y) }
print("Model output: \(ŷ), Loss: \(loss)")
let (𝛁model, _) = backprop(𝛁ŷ)
optimizer.update(&classifier, along: 𝛁model)
}
```For more models, go to [**tensorflow/swift-models**][swift-models].
## Development
Documentation covering development can be found in the [Developer Guide](Documentation/Development.md).
## Bugs
Please report bugs and feature requests using GitHub issues in this repository.
## Community
Discussion about Swift for TensorFlow happens on the
[[email protected]][forum]
mailing list.## Contributing
We welcome contributions: please read the [Contributor Guide](CONTRIBUTING.md)
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](CODE_OF_CONDUCT.md), which we encourage everybody to read before
participating.[s4tf]: https://github.com/tensorflow/swift
[integrated]: https://github.com/apple/swift/tree/tensorflow#customize-tensorflow-support
[blank_colab]: https://colab.research.google.com/notebook#create=true&language=swift
[usage]: https://github.com/tensorflow/swift/blob/main/Usage.md
[gradient]: https://www.tensorflow.org/swift/api_docs/Functions#/s:10TensorFlow8gradient2at2in13TangentVectorQzx_AA0A0Vyq_GxXEtAA14DifferentiableRzAA0aB13FloatingPointR_r0_lF
[swift-models]: https://github.com/tensorflow/swift-models
[toolchain]: https://github.com/tensorflow/swift/blob/main/Installation.md
[forum]: https://groups.google.com/a/tensorflow.org/d/forum/swift