Skip to content

Instantly share code, notes, and snippets.

View MicrexIT's full-sized avatar

Michele Rexha MicrexIT

View GitHub Profile
@MicrexIT
MicrexIT / SwiftBasicsStrings.swift
Created May 1, 2020 09:09
SwiftBasicsStrings.swift
"The answer to life is \(y)."
@MicrexIT
MicrexIT / SwiftBasicsTypes.swift
Created May 1, 2020 09:08
SwiftBasicsTypes.swift
//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
@MicrexIT
MicrexIT / SwiftBasicsConstantsVars.swift
Created May 1, 2020 09:07
SwiftBasicsConstantsVars.swift
let x = 42.0 // immutable variable
// x = 21.5 this will throw an error
var y = 0 // can mutate
y = 42
@MicrexIT
MicrexIT / s4tfXORPredict.swift
Created May 1, 2020 08:50
s4tfXORPredict.swift
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)
}
@MicrexIT
MicrexIT / s4tfXORTraining.swift
Created May 1, 2020 08:49
s4tfXORTraining.swift
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) }
@MicrexIT
MicrexIT / s4tfXOROptimizer.swift
Created May 1, 2020 08:49
s4tfXOROptimizer.swift
let optimizer = SGD(for: model, learningRate: 0.05)
@MicrexIT
MicrexIT / s4tfXORModel.swift
Created May 1, 2020 08:48
s4tfXORModel.swift
// 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)
@MicrexIT
MicrexIT / s4tfXORTensor.swift
Created May 1, 2020 08:47
s4tfXORTensor.swift
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]
@MicrexIT
MicrexIT / SwiftBasicsDifferentiable.swift
Created May 1, 2020 08:46
SwiftBasicsDifferentiable.swift
/**
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
@MicrexIT
MicrexIT / SwiftBasicsColabDisplayPlot.swift
Created May 1, 2020 08:45
SwiftBasicsColabDisplayPlot.swift
// 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")