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 Foundation | |
import CoreML | |
func compileCoreML(path: String) -> (MLModel, URL) { | |
let modelUrl = URL(fileURLWithPath: path) | |
let compiledUrl = try! MLModel.compileModel(at: modelUrl) | |
print("Compiled Model Path: \(compiledUrl)") | |
return try! (MLModel(contentsOf: compiledUrl), compiledUrl) | |
} |
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 Foundation | |
import CoreML | |
func generateData(sampleSize: Int = 100) -> ([Float], [Float]) { | |
let a: Float = 2.0 | |
let b: Float = 1.5 | |
var X = [Float]() | |
var Y = [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
import Foundation | |
struct OrderedDict<K: Hashable, V> { | |
var _dict = [K : V]() | |
var _ordered = [K]() | |
var keys: [K] { | |
_ordered | |
} | |
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
func prepareBatchProvider() -> MLBatchProvider { | |
var featureProviders = [MLFeatureProvider]() | |
var count = 0 | |
errno = 0 | |
let trainFilePath = Bundle.main.url(forResource: "mnist_train", withExtension: "csv")! | |
if freopen(trainFilePath.path, "r", stdin) == nil { | |
print("error opening file") | |
} | |
while let line = readLine()?.split(separator: ",") { |
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
public func prepareModel() { | |
let coremlModel = Model(version: 4, | |
shortDescription: "MNIST-Trainable", | |
author: "Jacopo Mangiavacchi", | |
license: "MIT", | |
userDefined: ["SwiftCoremltoolsVersion" : "0.0.12"]) { | |
Input(name: "image", shape: [1, 28, 28]) | |
Output(name: "output", shape: [10], featureType: .float) | |
TrainingInput(name: "image", shape: [1, 28, 28]) | |
TrainingInput(name: "output_true", shape: [1], featureType: .int) |
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 Foundation | |
import MLCompute | |
import PlaygroundSupport | |
let page = PlaygroundPage.current | |
page.needsIndefiniteExecution = true | |
let tensor1 = MLCTensor(descriptor: MLCTensorDescriptor(shape: [6, 1], dataType: .float32)!) | |
let tensor2 = MLCTensor(descriptor: MLCTensorDescriptor(shape: [6, 1], dataType: .float32)!) | |
let tensor3 = MLCTensor(descriptor: MLCTensorDescriptor(shape: [6, 1], dataType: .float32)!) |
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
private func readDataSet(fileName: String, updateStatus: @escaping (Int) -> Void) -> ([Float], [Float]) { //}(MLCTensor, MLCTensor) { | |
guard let filePath = Bundle.main.path(forResource: fileName, ofType: "csv") else { | |
fatalError("CSV file not found") | |
} | |
let serialQueue = DispatchQueue(label: "MNIST.serial.queue.\(fileName)") | |
var count = 0 | |
var X = [Float]() | |
var Y = [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
// Load in memory and split is not performant | |
private func getFileLine(filePath: String, process: (String) -> Void) { | |
guard let filePointer:UnsafeMutablePointer<FILE> = fopen(filePath,"r") else { | |
preconditionFailure("Could not open file at \(filePath)") | |
} | |
defer { | |
fclose(filePointer) | |
} |
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
private func initializeTensors() { | |
device = MLCDevice(type: .cpu)! | |
inputTensor = MLCTensor(descriptor: MLCTensorDescriptor(shape: [batchSize, imageSize, 1, 1], dataType: .float32)!) | |
dense1WeightsTensor = MLCTensor(descriptor: MLCTensorDescriptor(shape: [1, imageSize*dense1LayerOutputSize, 1, 1], dataType: .float32)!, | |
randomInitializerType: .glorotUniform) | |
dense1BiasesTensor = MLCTensor(descriptor: MLCTensorDescriptor(shape: [1, dense1LayerOutputSize, 1, 1], dataType: .float32)!, | |
randomInitializerType: .glorotUniform) | |
dense2WeightsTensor = MLCTensor(descriptor: MLCTensorDescriptor(shape: [1, dense1LayerOutputSize*numberOfClasses, 1, 1], dataType: .float32)!, |
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
private func buildTrainingGraph() { | |
trainingGraph = MLCTrainingGraph(graphObjects: [graph], | |
lossLayer: MLCLossLayer(descriptor: MLCLossDescriptor(type: .softmaxCrossEntropy, | |
reductionType: .mean)), | |
optimizer: MLCAdamOptimizer(descriptor: MLCOptimizerDescriptor(learningRate: 0.001, | |
gradientRescale: 1.0, | |
regularizationType: .none, | |
regularizationScale: 0.0), | |
beta1: 0.9, | |
beta2: 0.999, |