Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Commit 86bc5a2

Browse files
authored
Update introduction and code snippet.
1 parent b62213a commit 86bc5a2

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

README.md

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# [Swift for TensorFlow](https://github.com/tensorflow/swift) APIs
1+
# Swift for TensorFlow APIs
22

3-
Deep learning library for Swift for TensorFlow.
3+
This repository hosts [Swift for TensorFlow](https://github.com/tensorflow/swift)'s deep learning library, available both as a part of the Swift for TensorFlow toolchain and as a Swift package.
44

55
## Requirements
66

7-
* A latest Swift for TensorFlow toolchain.
7+
* A latest [Swift for TensorFlow toolchain](https://github.com/tensorflow/swift/blob/master/Installation.md).
88

99
## Usage
1010

@@ -13,7 +13,7 @@ is required to use this package. Add the following to your Swift package manifes
1313

1414
```swift
1515
packages: [
16-
.package(url: "https://github.com/tensorflow/swift-apis.git")
16+
.package(url: "https://github.com/tensorflow/swift-apis.git")
1717
]
1818
```
1919

@@ -22,27 +22,32 @@ To get started, simply import `TensorFlow` in your Swift code.
2222
```swift
2323
import TensorFlow
2424

25-
// Define a model.
26-
struct Classifier: Layer {
25+
struct Model: Layer {
2726
var l1, l2: Dense<Float>
28-
27+
init(hiddenSize: Int) {
28+
l1 = Dense<Float>(inputSize: 2, outputSize: hiddenSize, activation: relu)
29+
l2 = Dense<Float>(inputSize: hiddenSize, outputSize: 1, activation: relu)
30+
}
31+
@differentiable(wrt: (self, input))
2932
func applied(to input: Tensor<Float>) -> Tensor<Float> {
3033
let h1 = l1.applied(to: input)
3134
return l2.applied(to: h1)
3235
}
3336
}
3437

35-
var model = Classifier(...)
3638
let optimizer = SGD<Classifier, Float>(learningRate: 0.02)
39+
var classifier = Model(hiddenSize: 4)
40+
let x: Tensor<Float> = ...
41+
let y: Tensor<Float> = ...
42+
3743
for _ in 0..<1000 {
38-
let (loss, 𝛁model) = model.valueWithGradient { model in
39-
let ŷ = model.applied(to: x)
40-
print("Prediction: \(ŷ)")
41-
return (y - ŷ).squared().mean()
44+
let 𝛁model = classifier.gradient { classifier -> Tensor<Float> in
45+
let ŷ = classifier.applied(to: x)
46+
let loss = meanSquaredError(predicted: ŷ, expected: y)
47+
print("Loss: \(loss)")
48+
return loss
4249
}
43-
print("Loss: \(loss)")
44-
optimizer.update(&model.allDifferentiableVariables,
45-
along: 𝛁model)
50+
optimizer.update(&classifier.allDifferentiableVariables, along: 𝛁model)
4651
}
4752
```
4853

0 commit comments

Comments
 (0)