This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"The answer to life is \(y)." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//define types inline | |
let a: Int = 1 | |
let b: Double = -2.0 | |
var c: Float = 3.0 | |
var d: Bool = true | |
var e: String = "Hello!" | |
// print w type | |
print(type(of: a)) // Prints Int | |
print(type(of: b)) // Prints Doube | |
print(type(of: c)) // Prints Float |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let x = 42.0 // immutable variable | |
// x = 21.5 this will throw an error | |
var y = 0 // can mutate | |
y = 42 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let pred = model(X) | |
// print(pred) | |
var predicted = [Bool]() | |
let npred = pred.makeNumpyArray() | |
for i in 0..<pred.shape[0] { | |
if npred[i][0] < 0.5 { | |
predicted.append(false) | |
} else { | |
predicted.append(true) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let epochCount = 30 // nbr of training steps/epochs | |
// TODO | |
// func accuracy(predictions: Tensor<Int32>, truths: Tensor<Int32>) -> Float { | |
// return Tensor<Float>(predictions .== truths).mean().scalarized() | |
// } | |
// let X: Tensor<Float> = [[0, 0], [0, 1], [1, 0], [1, 1]] | |
// let y: Tensor<Float> = [0, 1, 1, 0] | |
for _ in 1...epochCount { | |
let 𝛁model = gradient(at: model) { (model: NeuralNet) -> Tensor<Float> in | |
let ŷ = model(X).withDerivative { print("∂L/∂ŷ =", $0) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let optimizer = SGD(for: model, learningRate: 0.05) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import TensorFlow | |
let hiddenSize: Int = 10 | |
// Neural Network has a struct containing layers | |
struct NeuralNet: Layer { // Extends the Layer type provided by S4TF | |
var layer1 = Dense<Float>(inputSize: 2, outputSize: hiddenSize, activation: relu) | |
var layer2 = Dense<Float>(inputSize: hiddenSize, outputSize: 1) // hidden layer | |
@differentiable // tells swift compiler these function can be derived | |
func callAsFunction(_ input: Tensor<Float>) -> Tensor<Float> { | |
return input.sequenced(through: layer1, layer2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import TensorFlow | |
var X: Tensor<Float> = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]] | |
var y: Tensor<Float> = [0, 1, 1, 0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
No imports, just plain Swift 😎 | |
**/ | |
@differentiable | |
func power2(_ x:Double) -> Double{ | |
return x * x | |
} | |
gradient(at:0.5) {x in power2(x)} | |
// Prints 1.0 | |
// NB: grad(x*x) = 2*x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This cell is here to display plots inside a Jupyter Notebook. | |
// Do not copy it into another environment. | |
%include "EnableIPythonDisplay.swift" | |
IPythonDisplay.shell.enable_matplotlib("inline") |