Skip to content

Commit

Permalink
Merge pull request #12 from qoncept/dev-0.2.3
Browse files Browse the repository at this point in the history
Update for Swift 4
  • Loading branch information
koher authored Oct 24, 2017
2 parents 326c37a + b4060ae commit f4616d0
Show file tree
Hide file tree
Showing 42 changed files with 269 additions and 443 deletions.
16 changes: 0 additions & 16 deletions MNISTTests/MNISTTests.swift

This file was deleted.

22 changes: 0 additions & 22 deletions MNISTUITests/Info.plist

This file was deleted.

36 changes: 0 additions & 36 deletions MNISTUITests/MNISTUITests.swift

This file was deleted.

25 changes: 24 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "TensorSwift"
name: "TensorSwift",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "TensorSwift",
targets: ["TensorSwift"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.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: "TensorSwift",
dependencies: []),
.testTarget(
name: "TensorSwiftTests",
dependencies: ["TensorSwift"]),
]
)
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# TensorSwift

_TensorSwift_ is a lightweight library to calculate tensors, which has similar APIs to [_TensorFlow_](https://www.tensorflow.org/)'s. _TensorSwift_ is useful to simulate calculating tensors in Swift __using models trained by _TensorFlow___.
_TensorSwift_ is a lightweight library to calculate tensors, which has similar APIs to [_TensorFlow_](https://www.tensorflow.org/)'s. _TensorSwift_ is useful to simulate calculating tensors in Swift **using models trained by _TensorFlow_**.

```swift
let a = Tensor(shape: [2, 3], elements: [1, 2, 3, 4, 5, 6])
Expand Down Expand Up @@ -31,15 +31,15 @@ public struct Classifier {
public let b_fc1: Tensor
public let W_fc2: Tensor
public let b_fc2: Tensor

public func classify(x_image: Tensor) -> Int {
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, 7 * 7 * 64])
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()
Expand All @@ -54,7 +54,7 @@ public struct Classifier {
### Swift Package Manager

```swift
.Package(url: "[email protected]:qoncept/TensorSwift.git", majorVersion: 0, minor: 2),
.Package(url: "[email protected]:qoncept/TensorSwift.git", from: "0.2.0"),
```

### CocoaPods
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions MNIST/CanvasView.swift → Sources/MNIST/CanvasView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CanvasView: UIView {
let context = UIGraphicsGetCurrentContext()
for line in canvas.lines {
context?.setLineWidth(20.0)
context?.setStrokeColor(UIColor(colorLiteralRed: 0.0, green: 0.0, blue: 0.0, alpha: 1.0).cgColor)
context?.setStrokeColor(UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0).cgColor)
context?.setLineCap(.round)
context?.setLineJoin(.round)
for (index, point) in line.points.enumerated() {
Expand All @@ -36,7 +36,7 @@ class CanvasView: UIView {
context?.strokePath()
}

func onPanGesture(_ gestureRecognizer: UIPanGestureRecognizer) {
@objc func onPanGesture(_ gestureRecognizer: UIPanGestureRecognizer) {
canvas.draw(gestureRecognizer.location(in: self))
if gestureRecognizer.state == .ended {
canvas.newLine()
Expand Down
4 changes: 2 additions & 2 deletions MNIST/Classifier.swift → Sources/MNIST/Classifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public struct Classifier {
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, 7 * 7 * 64])
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()
Expand All @@ -33,7 +33,7 @@ extension Classifier {
b_conv1 = Tensor(shape: [32], elements: loadFloatArray(path, file: "b_conv1"))
W_conv2 = Tensor(shape: [5, 5, 32, 64], elements: loadFloatArray(path, file: "W_conv2"))
b_conv2 = Tensor(shape: [64], elements: loadFloatArray(path, file: "b_conv2"))
W_fc1 = Tensor(shape: [7 * 7 * 64, 1024], elements: loadFloatArray(path, file: "W_fc1"))
W_fc1 = Tensor(shape: [Dimension(7 * 7 * 64), 1024], elements: loadFloatArray(path, file: "W_fc1"))
b_fc1 = Tensor(shape: [1024], elements: loadFloatArray(path, file: "b_fc1"))
W_fc2 = Tensor(shape: [1024, 10], elements: loadFloatArray(path, file: "W_fc2"))
b_fc2 = Tensor(shape: [10], elements: loadFloatArray(path, file: "b_fc2"))
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ViewController: UIViewController {
let rect = CGRect(x: 0.0, y: 0.0, width: CGFloat(inputSize), height: CGFloat(inputSize))
context.draw(cgImage, in: rect)

input = Tensor(shape: [Dimension(inputSize), Dimension(inputSize), 1], elements: pixels.map { -(Float($0) / 255.0 - 0.5) + 0.5 })
input = Tensor(shape: [Dimension(inputSize), Dimension(inputSize), 1], elements: pixels.map { (pixel: UInt8) -> Float in -(Float(pixel) / 255.0 - 0.5) + 0.5 })
}

let estimatedLabel = classifier.classify(input)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit f4616d0

Please sign in to comment.