Giter Site home page Giter Site logo

tensorswift's Introduction

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

tensorswift's People

Contributors

koher avatar robonich avatar t-ae avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tensorswift's Issues

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!

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.