{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import Foundation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import Glibc" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dlopen(\"/home/ubuntu/swift/usr/lib/swift/linux/libPath.so\", RTLD_NOW)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import Path" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(Path.home)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dlopen(\"/home/ubuntu/swift/usr/lib/swift/linux/libJust.so\", RTLD_NOW)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import Just" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "func shell(_ launchPath: String, _ arguments: [String]) -> String?\n", "{\n", " let task = Process()\n", " task.launchPath = launchPath\n", " task.arguments = arguments\n", "\n", " let pipe = Pipe()\n", " task.standardOutput = pipe\n", " task.launch()\n", "\n", " let data = pipe.fileHandleForReading.readDataToEndOfFile()\n", " let output = String(data: data, encoding: String.Encoding.utf8)\n", "\n", " return output\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var from = \"http://yann.lecun.com/exdb/mnist\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "func get_file(_ from:String, _ fn:String) {\n", " let cts = Just.get(\"\\(from)/\\(fn).gz\").content!\n", " try! cts.write(to: URL.init(fileURLWithPath: \"\\(fn).gz\"))\n", " shell(\"/bin/gunzip\", [\"-fq\", (Path.cwd/\"\\(fn).gz\").string])\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "protocol ConvertableFromByte {\n", " init(_ d:UInt8)\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "extension Float : ConvertableFromByte{}\n", "extension Int32 : ConvertableFromByte{}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import TensorFlow" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "func get_data(_ fn:String, _ skip:Int) -> Tensor {\n", " let data = try! Data.init(contentsOf: URL.init(fileURLWithPath: fn)).dropFirst(skip)\n", " return Tensor(data.map(T.init))\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var trn_fn = \"train-images-idx3-ubyte\"\n", "var trn_lab_fn = \"train-labels-idx1-ubyte\"\n", "var val_fn = \"t10k-images-idx3-ubyte\"\n", "var val_lab_fn = \"t10k-labels-idx1-ubyte\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "get_file(from, trn_fn)\n", "get_file(from, trn_lab_fn)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "get_file(from, val_fn)\n", "get_file(from, val_lab_fn)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var trn_arr:Tensor = get_data(trn_fn, 16)/255.0\n", "var trn_lab:Tensor = get_data(trn_lab_fn, 8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var val_arr:Tensor = get_data(val_fn, 16)/255.0\n", "var val_lab:Tensor = get_data(val_lab_fn, 8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(val_lab[100..<300])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "val_lab.scalarCount" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Swift", "language": "swift", "name": "swift" } }, "nbformat": 4, "nbformat_minor": 2 }