Swiftpack.co - Package - huan/swift-MNIST

Swift MNIST Dataset

Build Status

MNIST dataset

Swift Module for MNIST database (Modified National Institute of Standards and Technology database)

This module is made by the purpose of easy to use for the Swift Chapter in Tensorflow Handbook.

Usage

Class MNIST will provide all you need.

We have example demo code that you can run directly. Run our demo by:

git clone git@github.com:huan/swift-MNIST.git
cd swift-MNIST

# if you need docker environment to run swift:
# make docker

make demo

Output:

$ make docker
docker run -ti --rm \
  --privileged \
  --userns=host \
  -v "$(pwd)":/notebooks \
  zixia/swift \
  bash

root@b07ae41e460c:/notebooks#

root@b07ae41e460c:/notebooks# make demo
(cd examples && swift run)
[2/2] Merging module s4tf
Reading data.
Constructing data tensors.
Test Accuracy: 0.93633336

main.swift

import MNIST

let mnist = MNIST()
let ((trainImages, trainLabels), (testImages, testLabels)) = mnist.loadData()

let imageBatch = Dataset(elements: trainImages).batched(32)
let labelBatch = Dataset(elements: trainLabels).batched(32)

for (X, y) in zip(imageBatch, labelBatch) {
  // Caculate the gradient
  let (_, grads) = valueWithGradient(at: model) { model -> Tensor<Float> in
    let logits = model(X)
    return softmaxCrossEntropy(logits: logits, labels: y)
  }

  // Update parameters by optimizer
  optimizer.update(&model.self, along: grads)
}

let logits = model(testImages)
let acc = mnist.getAccuracy(y: testLabels, logits: logits)

print("Test Accuracy: \(acc)" )

Learn more from Tensorflow Handbook for Swift

Package.swift

// swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "tensorflow-handbook-swift",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/huan/swift-MNIST.git", from: "0.4.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages which this package depends on.
        .target(
            name: "s4tf",
            dependencies: ["MNIST"]),
        .testTarget(
            name: "s4tfTests",
            dependencies: ["s4tf"]),
    ]
)

Talks

History

master

Add exmple code base in examples/, run the demo by make demo.

0.4.0 (12 Nov, 2019)

Fix the MNIST Dataset file 404 problem.

0.1.0 (28 Aug 2019)

First version, used from Tensorflow Handbook for Swift

Links

Author

Huan (李卓桓) zixia@zixia.net

Profile of Huan LI (李卓桓) on StackOverflow

Copyright & License

  • Code & Docs © 2019-now Huan LI (李卓桓) zixia@zixia.net
  • Code released under the Apache-2.0 License
  • Docs released under Creative Commons

Github

link
Stars: 8

Dependencies

Used By

Total: 0

Releases

MNIST Dataset for Swift - 2019-11-04 15:21:30

Swift Module for MNIST database (Modified National Institute of Standards and Technology database).

Initial Code & Dataset come from https://github.com/tensorflow/swift-models