Skip to content

Instantly share code, notes, and snippets.

View JacopoMangiavacchi's full-sized avatar

Jacopo Mangiavacchi JacopoMangiavacchi

View GitHub Profile
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)
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: ",") {
import Foundation
struct OrderedDict<K: Hashable, V> {
var _dict = [K : V]()
var _ordered = [K]()
var keys: [K] {
_ordered
}
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]()
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)
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@JacopoMangiavacchi
JacopoMangiavacchi / TensorFlowSimulation.swift
Last active April 6, 2018 18:32
TensorFlow Swift simulation
import Foundation
import TensorFlow
import numsw
var tf = TensorFlow()
let a = tf.constant(NDArray<Float>(shape: [3], elements: [5, 7, 10]))
let b = tf.constant(NDArray<Float>(shape: [3], elements: [2, 3, 21]))
let c = tf.constant(NDArray<Float>(shape: [3], elements: [3, 5, 7]))
@JacopoMangiavacchi
JacopoMangiavacchi / stdListTest.swift
Created March 16, 2018 04:22
Swift C++ std::list wrapper test
let list = SwiftListWrapper(dataSize: 1)
list.push_back(value: "1".data(using: .utf8)!)
list.push_back(value: "2".data(using: .utf8)!)
list.push_back(value: "3".data(using: .utf8)!)
list.push_front(value: "0".data(using: .utf8)!)
XCTAssertEqual(list.size(), 4)
XCTAssertEqual(String.init(data: list.front()!, encoding: .utf8), "0")
@JacopoMangiavacchi
JacopoMangiavacchi / stdListTest.cpp
Created March 16, 2018 04:19
C++ std::list test
#include <list>
int main(int argc, const char * argv[]) {
std::list<void*> list;
list.push_back((void*)"1");
list.push_back((void*)"2");
list.push_back((void*)"3");
list.push_front((void*)"0");
public final class SampleStruct {
public var key: Int32
public var value: String
}
public protocol TestService {
func Hello(HelloString: String) throws -> String
func GetSampleStruct(key: Int32, value: String) throws -> SampleStruct
}