A lightweight library to calculate tensors in Swift, which has similar APIs to TensorFlow's

Overview

TensorSwift

TensorSwift is a lightweight library to calculate tensors, which has similar APIs to TensorFlow's. TensorSwift is useful to simulate calculating tensors in Swift using models trained by TensorFlow.

let a = Tensor(shape: [2, 3], elements: [1, 2, 3, 4, 5, 6])
let b = Tensor(shape: [2, 3], elements: [7, 8, 9, 10, 11, 12])
let sum = a + b // Tensor(shape: [2, 3], elements: [8, 10, 12, 14, 16, 18])
let mul = a * b // Tensor(shape: [2, 3], elements: [7, 16, 27, 40, 55, 72])

let c = Tensor(shape: [3, 1], elements: [7, 8, 9])
let matmul = a.matmul(c) // Tensor(shape: [2, 1], elements: [50, 122])

let zeros = Tensor(shape: [2, 3, 4])
let ones = Tensor(shape: [2, 3, 4], element: 1)

Deep MNIST for Experts

deep-mnist.gif

The following code shows how to simulate Deep MNIST for Experts, a tutorial of TensorFlow, by TensorSwift.

public struct Classifier {
    public let W_conv1: Tensor
    public let b_conv1: Tensor
    public let W_conv2: Tensor
    public let b_conv2: Tensor
    public let W_fc1: Tensor
    public let b_fc1: Tensor
    public let W_fc2: Tensor
    public let b_fc2: Tensor
    
    public func classify(_ x_image: Tensor) -> Int {
        let h_conv1 = (x_image.conv2d(filter: W_conv1, strides: [1, 1, 1]) + b_conv1).relu()
        let h_pool1 = h_conv1.maxPool(kernelSize: [2, 2, 1], strides: [2, 2, 1])
        
        let h_conv2 = (h_pool1.conv2d(filter: W_conv2, strides: [1, 1, 1]) + b_conv2).relu()
        let h_pool2 = h_conv2.maxPool(kernelSize: [2, 2, 1], strides: [2, 2, 1])
        
        let h_pool2_flat = h_pool2.reshaped([1, Dimension(7 * 7 * 64)])
        let h_fc1 = (h_pool2_flat.matmul(W_fc1) + b_fc1).relu()
        
        let y_conv = (h_fc1.matmul(W_fc2) + b_fc2).softmax()

        return y_conv.elements.enumerated().max { $0.1 < $1.1 }!.0
    }
}

Installation

Swift Package Manager

.Package(url: "[email protected]:qoncept/TensorSwift.git", from: "0.2.0"),

CocoaPods

pod 'TensorSwift', '~> 0.2'

Carthage

github "qoncept/TensorSwift" ~> 0.2

License

The MIT License

Comments
  • fatal error: `elements.count` must be greater than or equal to `shape.volume`: elements.count = 1024, shape.volume = 3136:

    fatal error: `elements.count` must be greater than or equal to `shape.volume`: elements.count = 1024, shape.volume = 3136:

    I have used my own images to trained by Tensorflow's DeepMnist example code. And I used the way you give in the other issue to output the Models. But when I replace your Models with mine and run, I meet the error:

    fatal error: elements.count must be greater than or equal to shape.volume: elements.count = 1024, shape.volume = 3136:

    I think maybe there is something wrong with my models. But the DeepMnist training is successful: 2016-07-13 10 26 21 And the output of models is successful, too.

    What can I do to solve this problem? In order to replace the models successfully, should I do something with the Classifier Struct?

    opened by LinShiwei 7
  • swift 3 - migration problems / tensorflow fyi

    swift 3 - migration problems / tensorflow fyi

    screen shot 2017-05-29 at 2 59 06 pm

    fyi - I'm getting close to having tensorflow via c_api in swift working. could use some help though. I'm a bit of a newb when it comes to tensors. checkout my branch - https://github.com/johndpope/tensorflow/tree/swift/tensorflow/swift

    opened by johndpope 2
  • Integration with Numpy

    Integration with Numpy

    Implement new feature: Load tensor from Numpy array file (.npy)

    Supported npy file:

    • ~~Little endian~~
    • np.float32 or np.float64 array
    • "fortran_order" is False
    opened by t-ae 2
  • How to export weights from Python?

    How to export weights from Python?

    This is a pretty awesome project. I've trained some models on Tensorflow and want to export to the format you use loadFloatArray with. Any chance you can post some example Python code to export those weights?

    Thanks for building this btw!

    opened by ivanmkc 2
  • Update for Swift3, etc.

    Update for Swift3, etc.

    This PR includes

    • Syntax update for Swift3
    • Support Swift Package Manager
    • Separate MNIST sample project
    • Update podspec file (Sources directory & version)
    opened by t-ae 1
Releases(0.2.3)
Owner
Qoncept, Inc.
Qoncept, Inc.
A simple Swift package which acts as an OpenAI client for the GPT-3 API.

SwiftyGPT3 A simple Swift package which acts as an OpenAI client for GPT-3 brought to you by the Airgift Crew. Supports GPT-3 Codex! Requirements iOS

Airgift 23 Dec 25, 2022
This is an open-source project for the aesthetic evaluation of images based on the deep learning-caffe framework, which we completed in the Victory team of Besti.

This is an open-source project for the aesthetic evaluation of images based on the deep learning-caffe framework, which we completed in the Victory team of Besti.

The Victory Group of Besti 102 Dec 15, 2022
A Swift library for creating and exporting CoreML Models in Swift

SwiftCoreMLTools A Swift Library for creating CoreML models in Swift. Work in progress This library expose a (function builder based) DSL as well as a

Jacopo Mangiavacchi 140 Dec 5, 2022
The Swift machine learning library.

Swift AI is a high-performance deep learning library written entirely in Swift. We currently offer support for all Apple platforms, with Linux support

Swift AI 5.9k Jan 2, 2023
A Swift deep learning library with Accelerate and Metal support.

Serrano Aiming to offering popular and cutting edge techs in deep learning area on iOS devices, Serrano is developed as a tool for developers & resear

pcpLiu 51 Nov 17, 2022
A simple deep learning library for estimating a set of tags and extracting semantic feature vectors from given illustrations.

Illustration2Vec illustration2vec (i2v) is a simple library for estimating a set of tags and extracting semantic feature vectors from given illustrati

Masaki Saito 661 Dec 12, 2022
Accelerated tensor operations and dynamic neural networks based on reverse mode automatic differentiation for every device that can run Swift - from watchOS to Linux

DL4S provides a high-level API for many accelerated operations common in neural networks and deep learning. It furthermore has automatic differentiati

Palle 87 Dec 29, 2022
BrainCore is a simple but fast neural network framework written in Swift.

BrainCore is a simple but fast neural network framework written in Swift. It uses Metal which makes it screamin' fast. If you want to see it

Alejandro Isaza 377 Jun 29, 2022
MLKit is a simple machine learning framework written in Swift.

MLKit (a.k.a Machine Learning Kit) ?? MLKit is a simple machine learning framework written in Swift. Currently MLKit features machine learning algorit

Guled 152 Nov 17, 2022
Artificial intelligence/machine learning data structures and Swift algorithms for future iOS development. bayes theorem, neural networks, and more AI.

Swift Brain The first neural network / machine learning library written in Swift. This is a project for AI algorithms in Swift for iOS and OS X develo

Vishal 331 Oct 14, 2022
A toolbox of AI modules written in Swift: Graphs/Trees, Support Vector Machines, Neural Networks, PCA, K-Means, Genetic Algorithms

AIToolbox A toolbox of AI modules written in Swift: Graphs/Trees, Linear Regression, Support Vector Machines, Neural Networks, PCA, KMeans, Genetic Al

Kevin Coble 776 Dec 18, 2022
A framework for building fast genetic algorithms in Swift.

Revolver is a framework for building fast genetic algorithms in Swift 3.0. Features Chromosomes: strings, trees Genetic operators: reproduction, mutat

Petr Mánek 27 Nov 17, 2022
Running Swift automatic differentiation on iOS

Differentiation Demo This is an example of Swift's automatic differentiation running on iOS. It is a modified version of the game from ARHeadsetKit tu

Philip Turner 7 Apr 27, 2022
Track List Detail App With Swift

TrackListDetailApp TrackListDetailApp is master-detail application that containsa a list of items obtained from a iTunes Search API and show a detaile

Karlo Manguiat 1 Dec 12, 2021
A Swift package for working with probability-driven randomness

ControlledChaos ControlledChaos is a simple Swift package for working with proba

B.T. Franklin 2 Dec 31, 2022
This repo contains beginner examples to advanced in swift. Aim to create this for learning native iOS development.

iOS-learning-with-swift-22 This repo contains beginner examples to advanced in swift. Aim to create this for learning native iOS development. Oh, you

Umesh Jangid 0 Jan 9, 2022
NGram: a Swift implementation to generate N-grams (all word combinations) from an input string

nGram nGram is a Swift implementation to generate N-grams (all word combinations

Jeff Seibert 1 Apr 27, 2022
TensorFlow C API Class Wrapper in Server Side Swift.

Perfect TensorFlow 简体中文 This project is an experimental wrapper of TensorFlow C API which enables Machine Learning in Server Side Swift. This package

PerfectlySoft Inc. 169 Dec 11, 2022
Swift for TensorFlow

Swift for TensorFlow (Archived) Swift for TensorFlow was an experiment in the next-generation platform for machine learning, incorporating the latest

null 6.1k Dec 31, 2022