Giter Site home page Giter Site logo

tensorflow / swift-models Goto Github PK

View Code? Open in Web Editor NEW
645.0 45.0 145.0 64.76 MB

Models and examples built with Swift for TensorFlow

License: Apache License 2.0

Swift 39.88% Python 0.15% Dockerfile 0.03% Jupyter Notebook 48.79% CMake 1.02% C 10.09% Shell 0.04%

swift-models's Introduction

Swift for TensorFlow Models

This repository contains many examples of how Swift for TensorFlow can be used to build machine learning applications, as well as the models, datasets, and other components required to build them. These examples are intended to demonstrate best practices for the use of Swift for TensorFlow APIs and act as end-to-end tests to validate the function and performance of those APIs.

Active development occurs on the main branch, and that is kept current against the main branch of the Swift compiler and the main branch of the Swift for TensorFlow APIs.

For stable snapshots, use the tensorflow-xx branch that corresponds to the toolchain you are using from the Swift for TensorFlow releases. For example, for the 0.12 release, use the tensorflow-0.12 branch.

To learn more about Swift for TensorFlow development, please visit tensorflow/swift.

Examples

The examples within this repository are all designed to be run as standalone applications. The easiest way to do this is to use Swift Package Manager to build and run individual examples. This can be accomplished by changing to the root directory of the project and typing something like

swift run -c release [Example] [Options]

For Windows, an additional flag may be required:

swift run -Xswiftc -use-ld=lld -c release [Example] [Options]

This will build and run a specific example in the release configuration. Due to significant performance differences between debug and release builds in Swift, we highly recommend running the examples from a release build. Some examples have additional command-line options, and those will be described in the example's README.

The following is a catalog of the current examples, grouped by subject area, with links to their location within the project. Each example should have documentation for what it is demonstrating and how to use it.

Image classification

Text

Generative models

Reinforcement learning

Standalone

Components

Beyond examples that use Swift for TensorFlow, this repository also contains reusable components for constructing machine learning applications. These components reside in modules that can be imported into separate Swift projects and used by themselves.

These components provide standalone machine learning models, datasets, image loading and saving, TensorBoard integration, and a training loop abstraction, among other capabilities.

The Swift for TensorFlow models repository has acted as a staging ground for experimental capabilities, letting us evaluate new components and interfaces before elevating them into the core Swift for TensorFlow APIs. As a result, the design and interfaces of these components may change regularly.

Models

Several modules are provided that contain reusable Swift models for image classification, text processing, and more. These modules are used within the example applications to demonstrate the capabilities of these models, but they can also be imported into many other projects.

Image classification

Many common image classification models are present within the ImageClassificationModels module. To use them within a Swift project, add ImageClassificationModels as a dependency and import the module:

import ImageClassificationModels

These models include:

  • DenseNet121
  • EfficientNet
  • LeNet-5
  • MobileNetV1
  • MobileNetV2
  • MobileNetV3
  • ResNet
  • ResNetV2
  • ShuffleNetV2
  • SqueezeNet
  • VGG
  • WideResNet
  • Xception

Recommendation

Several recommendation models are present within the RecommendationModels module. To use them within a Swift project, add RecommendationModels as a dependency and import the module:

import RecommendationModels

These models include:

  • DLRM
  • MLP
  • NeuMF

Text

Several text models are present within the TextModels module. To use them within a Swift project, add TextModels as a dependency and import the module:

import TextModels

These models include:

Datasets

In addition to the machine learning model itself, a dataset is usually required when training. Swift wrappers have been built for many common datasets to ease their use within machine learning applications. Most of these use the Epochs API that provides a generalized abstraction of common dataset operations.

The Datasets module provides these wrappers. To use them within a Swift project, add Datasets as a dependency and import the module:

import Datasets

These are the currently provided dataset wrappers:

Model checkpoints

Model saving and loading is provided by the Checkpoints module. To use the model checkpointing functionality, add Checkpoints as a dependency and import the module:

import Checkpoints

Image loading and saving

The ModelSupport module contains many shared utilites that are needed within the Swift machine learning examples. This includes the loading, saving, and processing of still images via the stb_image library. Animated images can also be written out as GIF files from multiple tensors.

Experimental support for libjpeg-turbo as an accelerated image loader is present, but has not yet been incorporated into the main image loading capabilities.

Generalized training loop

A generalized training loop that can be customized via callbacks is provided within the TrainingLoop module. All of the image classification examples use this training loop, with the exception of the Custom-CIFAR10 example that demonstrates how to define your own training loop from scratch. Other examples are being gradually converted to use this training loop.

TensorBoard integration

TensorBoard integration is provided in the TensorBoard module as a callback for the generalized training loop. TensorBoard lets you visualize the progress of your model as it trains by plotting model statistics as they update, or to review the training process afterward.

The GPT2-WikiText2 example demonstrates how this can be used when training your own models.

Benchmarks and tests

A core goal of this repository is to validate the proper function of the Swift for TensorFlow APIs. In addition to the models and end-to-end applications present within this project, a suite of benchmarks and unit tests reside here.

The benchmarks are split into a core of functionality, the SwiftModelsBenchmarksCore module, and a Benchmarks command-line application for running these benchmarks. Refer to the documentation for how to run the benchmarks on your system.

The unit tests verify functionality within models, datasets and other components. To run them using Swift Package Manager on macOS or Linux:

swift test

and to run them on Windows:

swift test -Xswiftc -use-ld=lld -c debug

Using CMake for Development

In addition to Swift Package Manager, CMake can be used to build and run Swift for TensorFlow models.

Experimental CMake Support

There is experimental support for building with CMake. This can be used to cross-compile the models and the demo programs.

It is highly recommended that you use CMake 3.16 or newer to ensure that -B and parallel builds function properly in the example commands below. To install this version on Ubuntu, we recommend following the instructions at Kitware's apt repo.

Prerequisite: Ninja build tool. Find installation commands for your favorite package manager here.

macOS:

# Configure
cmake                                                              \
  -B /BinaryCache/tensorflow-swift-models                          \
  -D BUILD_TESTING=YES                                             \
  -D CMAKE_BUILD_TYPE=Release                                      \
  -D CMAKE_Swift_COMPILER=$(TOOLCHAINS=tensorflow xcrun -f swiftc) \
  -G Ninja                                                         \
  -S /SourceCache/tensorflow-swift-models
# Build
cmake --build /BinaryCache/tensorflow-swift-models
# Test
cmake --build /BinaryCache/tensorflow-swift-models --target test

Linux:

# Configure
cmake                                     \
  -B /BinaryCache/tensorflow-swift-models \
  -D BUILD_TESTING=NO                     \
  -D CMAKE_BUILD_TYPE=Release             \
  -D CMAKE_Swift_COMPILER=$(which swiftc) \
  -G Ninja                                \
  -S /SourceCache/tensorflow-swift-models
# Build
cmake --build /BinaryCache/tensorflow-swift-models

Windows:

set DEVELOPER_LIBRARY_DIR=%SystemDrive%/Library/Developer/Platforms/Windows.platform/Developer/Library
:: Configure
"%ProgramFiles%\CMake\bin\cmake.exe"                                                                                                                                                   ^
  -B %SystemDrive%/BinaryCache/tensorflow-swift-models                                                                                                                                 ^
  -D BUILD_SHARED_LIBS=YES                                                                                                                                                             ^
  -D BUILD_TESTING=YES                                                                                                                                                                 ^
  -D CMAKE_BUILD_TYPE=Release                                                                                                                                                          ^
  -D CMAKE_Swift_COMPILER=%SystemDrive%/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/bin/swiftc.exe                                                        ^
  -D CMAKE_Swift_FLAGS="-sdk %SDKROOT% -I %DEVELOPER_LIBRARY_DIR%/XCTest-development/usr/lib/swift/windows/x86_64 -L %DEVELOPER_LIBRARY_DIR%/XCTest-development/usr/lib/swift/windows" ^
  -G Ninja                                                                                                                                                                             ^
  -S %SystemDrive%/SourceCache/tensorflow-swift-models
:: Build
"%ProgramFiles%\CMake\bin\cmake.exe" --build %SystemDrive%/BinaryCache/tensorflow-swift-models
:: Test
"%ProgramFiles%\CMake\bin\cmake.exe" --build %SystemDrive%/BinaryCache/tensorflow-swift-models --target test

Bugs

Please report model-related bugs and feature requests using GitHub issues in this repository.

Community

Discussion about Swift for TensorFlow happens on the [email protected] mailing list.

Contributing

We welcome contributions: please read the Contributor Guide to get started. It's always a good idea to discuss your plans on the mailing list before making any major submissions.

We have labeled some issues as "good first issue" or "help wanted" to provide some suggestions for where new contributors might be able to start.

Code of Conduct

In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.

The Swift for TensorFlow community is guided by our Code of Conduct, which we encourage everybody to read before participating.

swift-models's People

Contributors

andr0id100 avatar bradlarson avatar brettkoonce avatar compnerd avatar dan-zheng avatar inukshukdeveloper avatar jekbradbury avatar johndpope avatar kongzii avatar leoxzhao avatar mikowals avatar mlsawyer avatar pschuh avatar rahulbhalley avatar rickwierenga avatar rxwei avatar saeta avatar sgugger avatar shabalind avatar shadaj avatar shashi456 avatar sumansudhir avatar t-ae avatar texasmichelle avatar valeriyvan avatar vballoli avatar vojtamolda avatar volodymyrpavliukevych avatar xiejw avatar xihui-wu 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swift-models's Issues

Inside `FeedForward`, input goes through `dense1`, `dropout`, `dense2`. Is it intentional?

    func applied(to input: Tensor<Float>, in context: Context) -> Tensor<Float> {
        return input.sequenced(in: context, through: dense1, dropout, dense2)
    }

return input.sequenced(in: context, through: dense1, dropout, dense2)

I did not find reference code in Python with dropout. I had the impression that dropout normally is added to the exit as the last operation before produces the final output. In this case, it would be dense1 -> dense2 -> dropout. This looks like a typo. But I don't possess the knowledge to judge. So I open an issue.

How does GPU support work?

In PyTorch I am able to cast some tensor from cpu to gpu by calling special method .to(device) like x.to(t.device("cuda:2")) to cast x to gpu with id 2.

It's unclear how does GPU/TPU support work in swift for tensorflow. I noticed that I could change env variable in collab from None to GPU and all computations would be performed on GPU, but how could I do something like that using compiler?

Error compiling MNIST.swift

MNIST.swift:35:13: error: value of type 'Tensor' has no member 'toAccelerator'
return (imagesTensor.toAccelerator(), labelsTensor.toAccelerator())
^~~~~~~~~~~~ ~~~~~~~~~~~~~

swift-tensorflow-DEVELOPMENT-2018-06-01-a.xctoolchain
Swift version 4.2-dev (LLVM 04bdb56f3d, Clang b44dbbdf44, Swift 70260a59ef)
Target: x86_64-apple-darwin17.6.0

UNet Example

I'd love to contribute an example of using UNet for image segmentation - would this be useful?

MNIST prediction output

I wanted to analyse the results of the predictor so wrote some code to output the predictions outside the loop (after the print loss in the MNIST file):

let z1 = images ⊗ w1 + b1
let h1 = sigmoid(z1)
let z2 = h1 ⊗ w2 + b2
let predictions = sigmoid(z2)
for prediction in predictions.toDevice().array {
print(prediction)
}

for v in h1.toDevice().array {
print(v)
}

The h1 print is always outputting [1.0,1.0...1.0]
and the prediction outputs for each sample on my machine:
[0.0987168, 0.112367, 0.0993, 0.102183, 0.0973667, 0.0903497, 0.0986334, 0.104417, 0.0975165, 0.09915]
Indicates only the bias is optimised?

I'm running: swift-tensorflow-DEVELOPMENT-2018-05-10-a.xctoolchain
using: swift -O MNIST.swift in terminal in macos

Transformer example crashes

Hello, I'm trying to run Transformer example, but getting this crash trace.

~/tensorflow/swift-models/Transformer$ ./GPT2 0.5 "Introducing Swift for TensorFlow"
Fatal error: Input to reshape is a tensor with 768 values, but the requested shape has 7: file /swift-base/tensorflow-swift-apis/Sources/TensorFlow/Bindings/EagerExecution.swift, line 300
Current stack trace:
0    libswiftCore.so                    0x00007f5f0d297780 swift_reportError + 50
1    libswiftCore.so                    0x00007f5f0d306780 _swift_stdlib_reportFatalErrorInFile + 115
2    libswiftCore.so                    0x00007f5f0d2301d8 <unavailable> + 3793368
3    libswiftCore.so                    0x00007f5f0d230367 <unavailable> + 3793767
4    libswiftCore.so                    0x00007f5f0cff1c78 <unavailable> + 1440888
5    libswiftCore.so                    0x00007f5f0d20069e <unavailable> + 3597982
6    libswiftCore.so                    0x00007f5f0cff1399 <unavailable> + 1438617
7    libswiftTensorFlow.so              0x00007f5f0d6e7f20 dumpTensorContent<A>(_:_:) + 0
8    libswiftTensorFlow.so              0x00007f5f0d540f30 checkOk(_:file:line:) + 434
9    libswiftTensorFlow.so              0x00007f5f0d5480a0 TFE_Op.evaluateUnsafe() + 506
10   libswiftTensorFlow.so              0x00007f5f0d548790 TFE_Op.execute<A>(_:) + 132
11   libswiftTensorFlow.so              0x00007f5f0d551434 <unavailable> + 1279028
12   libswiftTensorFlow.so              0x00007f5f0d639e50 static Raw.reshape<A, B>(_:shape:) + 1057
13   libswiftTensorFlow.so              0x00007f5f0d70a3f0 Tensor.reshaped(toShape:) + 114
14   libswiftTensorFlow.so              0x00007f5f0d706420 Tensor.reshaped(to:) + 113
15   libswiftTensorFlow.so              0x00007f5f0d753c10 LayerNorm.callAsFunction(_:) + 744
16   GPT2                               0x0000563cd54275e7 <unavailable> + 251367
17   GPT2                               0x0000563cd544e475 <unavailable> + 410741
18   GPT2                               0x0000563cd53fce38 <unavailable> + 77368
19   libc.so.6                          0x00007f5f02434ab0 __libc_start_main + 231
20   GPT2                               0x0000563cd53fb19a <unavailable> + 70042
Illegal instruction

I'm using this S4TF build on Ubuntu 18.10 LTS

Welcome to Swift version 5.1-dev (LLVM 200186e28b, Swift 28eb536).

MNIST & Autoencoder can't be run with latest toolchain

OS: macOS Mojava Beta
Toolchain: Xcode 10/7-24
Command: swift -O xxx
Output:

SESE FIXME: Imperfect loop exits not handled yet!
UNREACHABLE executed at /usr/local/src/swift-build/swift/lib/SILOptimizer/Mandatory/TFCanonicalizeCFG.cpp:802!
0  swift                    0x00000001066426d8 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x0000000106641917 llvm::sys::RunSignalHandlers() + 39
2  swift                    0x0000000106642d52 SignalHandler(int) + 258
3  libsystem_platform.dylib 0x00007fff66d37d7a _sigtramp + 26
4  libsystem_platform.dylib 0x00007ffeecefd120 _sigtramp + 2250003392
5  libsystem_c.dylib        0x00007fff66bf81c9 abort + 127
6  swift                    0x00000001065e1d24 llvm::llvm_unreachable_internal(char const*, char const*, unsigned int) + 532
7  swift                    0x0000000103706d03 (anonymous namespace)::SESERegionBuilder::processLoop(swift::SILLoop*) + 787
8  swift                    0x00000001037065ab swift::tf::canonicalizeCFGForXLA(swift::SILFunction*) + 1115
9  swift                    0x00000001037340e5 lowerTFGraphOrFunction(llvm::StringRef, swift::SILFunction*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool, swift::tf::GraphFunctionDeviceInfo const&, llvm::DenseMap<llvm::StringRef, std::__1::unique_ptr<swift::tf::LoweredGraphFunction, std::__1::default_delete<swift::tf::LoweredGraphFunction> >, llvm::DenseMapInfo<llvm::StringRef>, llvm::detail::DenseMapPair<llvm::StringRef, std::__1::unique_ptr<swift::tf::LoweredGraphFunction, std::__1::default_delete<swift::tf::LoweredGraphFunction> > > >&) + 69
10 swift                    0x0000000103736e26 swift::tf::lowerTFGraph(llvm::StringRef, swift::SILFunction*, swift::tf::GraphFunctionDeviceInfo const&, llvm::DenseMap<llvm::StringRef, std::__1::unique_ptr<swift::tf::LoweredGraphFunction, std::__1::default_delete<swift::tf::LoweredGraphFunction> >, llvm::DenseMapInfo<llvm::StringRef>, llvm::detail::DenseMapPair<llvm::StringRef, std::__1::unique_ptr<swift::tf::LoweredGraphFunction, std::__1::default_delete<swift::tf::LoweredGraphFunction> > > >&) + 262
11 swift                    0x000000010374d2d2 (anonymous namespace)::TFPartition::run() + 578
12 swift                    0x0000000103784e9a swift::SILPassManager::runModulePass(unsigned int) + 346
13 swift                    0x0000000103785827 swift::SILPassManager::execute() + 759
14 swift                    0x0000000102eb7bdb swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 187
15 swift                    0x000000010378dd33 swift::runSILTFPartitionPass(swift::SILModule&) + 99
16 swift                    0x0000000102d4ce9e performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 13742
17 swift                    0x0000000102d48878 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2936
18 swift                    0x0000000102d025b8 main + 1128
19 libdyld.dylib            0x00007fff66b3b429 start + 1
20 libdyld.dylib            0x000000000000000b start + 2571914211
Stack dump:
0.	Program arguments: /Library/Developer/Toolchains/swift-tensorflow-DEVELOPMENT-2018-07-24-a.xctoolchain/usr/bin/swift -frontend -interpret MNIST.swift -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -O -color-diagnostics -module-name MNIST
1.	While running pass #0 SILModuleTransform "TFPartition".
[1]    39469 abort       -O MNIST.swift

Error when running MNIST

I am having the following error trying to run the MNIST algorithm and I am not sure what is causing
it.

Reading data.
Constructing data tensors.
Precondition failed: The number of scalars does not match the shape.: file /swift-base/swift/stdlib/public/TensorFlow/Tensor.swift, line 101
Current stack trace:
0    libswiftCore.so                    0x00007f9df09fbf40 _swift_stdlib_reportFatalErrorInFile + 115
1    libswiftCore.so                    0x00007f9df09443dc <unavailable> + 3003356
2    libswiftCore.so                    0x00007f9df09444ce <unavailable> + 3003598
3    libswiftCore.so                    0x00007f9df078be12 <unavailable> + 1199634
4    libswiftCore.so                    0x00007f9df090e6b2 <unavailable> + 2782898
5    libswiftCore.so                    0x00007f9df078b259 <unavailable> + 1196633
6    libswiftTensorFlow.so              0x00007f9dede7fd50 __tf_tensor_from_scalars + 942
Current stack trace:
	frame #4: 0x00007f9ddfd4563a $__lldb_expr377`specialized readMNIST(imagesFile:labelsFile:) at <Cell 12>:36
	frame #6: 0x00007f9ddfd3b0ed $__lldb_expr377`main at <REPL>:60

Recommendations example

It'd be great to have an example for neural recommendations using embeddings incorporated into our model repository.

ResNet variants currently cause a segmentation fault during training

I'm still working on a smaller reproducer for this, but as of the latest nightly builds (since at least 7/25), the various ResNets (tested on 18, 34, and 50 depths) trigger a segfault during training on CIFAR-10. This happens even with ResNet-18 and a batch size of 1.

The following is what I see in the first 10 frames after the crash:

* thread #1, name = 'ResNet', stop reason = signal SIGSEGV: invalid address (fault address: 0x7fffff7fefe8)
  * frame #0: 0x00007ffff768c110 libswiftCore.so`swift_getTupleTypeMetadata + 80
    frame #1: 0x00007ffff768ca3a libswiftCore.so`swift_getTupleTypeMetadata2 + 26
    frame #2: 0x00007ffff73f9252 libswiftCore.so`Swift.Range.init(uncheckedBounds: (lower: A, upper: A)) -> Swift.Range<A> + 50
    frame #3: 0x00007ffff74a166d libswiftCore.so`Swift.Range.relative<A where A == A1.Index, A1: Swift.Collection>(to: A1) -> Swift.Range<A> + 125
    frame #4: 0x00007ffff74a168c libswiftCore.so`protocol witness for Swift.RangeExpression.relative<A where A1: Swift.Collection, A.Bound == A1.Index>(to: A1) -> Swift.Range<A.Bound> in conformance Swift.Range<A> : Swift.RangeExpression in Swift + 12
    frame #5: 0x00007ffff74a4bc5 libswiftCore.so`(extension in Swift):Swift.RangeReplaceableCollection.replaceSubrange<A, B where A1: Swift.Collection, B1: Swift.RangeExpression, A.Element == A1.Element, A.Index == B1.Bound>(_: B1, with: __owned A1) -> () + 213
    frame #6: 0x00007ffff7bbd88f libswiftTensorFlow.so`protocol witness for Swift.RangeReplaceableCollection.replaceSubrange<A where A1: Swift.Collection, A.Element == A1.Element>(_: Swift.Range<A.Index>, with: __owned A1) -> () in conformance (extension in Swift):Swift.Array<A><A where A: Swift.Differentiable>.DifferentiableView : Swift.RangeReplaceableCollection in Swift + 111
    frame #7: 0x00007ffff74a4be2 libswiftCore.so`(extension in Swift):Swift.RangeReplaceableCollection.replaceSubrange<A, B where A1: Swift.Collection, B1: Swift.RangeExpression, A.Element == A1.Element, A.Index == B1.Bound>(_: B1, with: __owned A1) -> () + 242
    frame #8: 0x00007ffff7bbd88f libswiftTensorFlow.so`protocol witness for Swift.RangeReplaceableCollection.replaceSubrange<A where A1: Swift.Collection, A.Element == A1.Element>(_: Swift.Range<A.Index>, with: __owned A1) -> () in conformance (extension in Swift):Swift.Array<A><A where A: Swift.Differentiable>.DifferentiableView : Swift.RangeReplaceableCollection in Swift + 111
    frame #9: 0x00007ffff74a4be2 libswiftCore.so`(extension in Swift):Swift.RangeReplaceableCollection.replaceSubrange<A, B where A1: Swift.Collection, B1: Swift.RangeExpression, A.Element == A1.Element, A.Index == B1.Bound>(_: B1, with: __owned A1) -> () + 242

This appears to be localized to the ResNets, so hoping to narrow this down soon.

GAN example

It'd be great to have a generative model incorporated as part of our models repository.

ReLU implementation in Swift (for TensorFlow / S4TF)

@rxwei your ReLU implementation in Swift 4 TF from forums.fast.ai is great. Unfortunately, it still returns an error with v0.4 in Colab—could it be the compiler issue? I have run the script to upgrade the Colab to the latest S4TF (it should be as of 07/22/19). If there is a current workaround let me know, maybe I missed something.

The issue was first raised here: https://forums.fast.ai/t/implementing-generalrelu-in-swift/44754/11 and I think @marcrasi has looked into it here https://forums.fast.ai/t/s4tf-in-colab-error/44734/6?u=marcrasi

Thanks in advance and great work @rxwei and team

cc @dynamicwebpaige

import TensorFlow

struct GeneralReLU: Layer {
    var leak: Float // The slope for X if it's negative
    var subtract: Float  // Used to substract from the final result
    var maxValue: Float // Maximum value

    init(_ leak:Float = 0.0, _ subtract:Float = 0.0, _ maxValue:Float = Float.greatestFiniteMagnitude) {
        self.leak = leak
        subtract = subtract
        maxValue = maxValue
    }
    @differentiable
    func call(_ input: Tensor<Float>) -> Tensor<Float> {
        let positives = max(input, Tensor(0))
        let leaked = min(input, Tensor(0)) * self.leak
        return min(positives + leaked - subtract, Tensor(maxValue))
    }
}

Object detection examples

It'd be great to have object detection models (e.g. RetinaNet and MaskRCNN) incorporated as part of our models repository.

MNIST.swift:41:26: MNist.swift:23:17: MNist.swift:98:13: MNist.swift:114:50:

MNIST.swift is failing on macOS:

$ swift
Welcome to Swift version 5.0-dev (LLVM dcb9eb74a7, Clang 95cdf7c9af, Swift 3fd7afd).

$ swift -O MNIST.swift
MNIST.swift:41:26: error: use of undeclared type 'ParameterGroup'
struct MNISTParameters : ParameterGroup {
^~~~~~~~~~~~~~
MNist.swift:23:17: error: incorrect argument label in call (have 'numpyArray:', expected 'arrayLiteral:')
return Array(numpyArray: np.frombuffer(d, dtype: np.uint8))!
^~~~~~~~~~~
arrayLiteral
MNist.swift:98:13: error: value of type 'MNISTParameters' has no member 'update'
parameters.update(withGradients: gradients) { param, grad in
^~~~~~~~~~ ~~~~~~
MNist.swift:114:50: error: value of type 'Tensor' has no member 'elementsEqual'
let correctPredictions = predictions.argmax(squeezingAxis: 1).elementsEqual(numericLabels)
~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~

MNIST.swift:113:50: error: value of type 'Tensor<Int32>' has no member 'elementsEqual'

swift -O MNIST.swift is failing on Linux - Ubuntu 16.04 - AArch64.

Swift version 5.0-dev (LLVM dcb9eb74a7, Clang 95cdf7c9af, Swift ce00e6b128)
Target: aarch64-unknown-linux-gnu
MNIST.swift:113:50: error: value of type 'Tensor<Int32>' has no member 'elementsEqual'
            let correctPredictions = predictions.argmax(squeezingAxis: 1).elementsEqual(numericLabels)
                                     ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~

The same problem also occurs in Swift version 4.2-dev built for AArch64

swift -O MNIST.swift - errors param -= grad * learningRate ~~~~~~^~~~~~~~~~~~~~~~~~~~~~ statement type not handled! UNREACHABLE executed at /usr/local/src/swift-build/swift/lib/AST/Stmt.cpp:120!

MNIST/
swift -O MNIST.swift

           ~~~~~~^~~~~~~~~~~~~~~~~~~~~~

statement type not handled!
UNREACHABLE executed at /usr/local/src/swift-build/swift/lib/AST/Stmt.cpp:120!
Stack dump:
0. Program arguments: /Library/Developer/Toolchains/swift-tensorflow-DEVELOPMENT-2018-12-04-a.xctoolchain/usr/bin/swift -frontend -interpret MNIST.swift -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -O -color-diagnostics -module-name MNIST

  1. While emitting IR SIL function "@main".
    0 swift 0x0000000110f561d8 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
    1 swift 0x0000000110f55455 llvm::sys::RunSignalHandlers() + 85
    2 swift 0x0000000110f567e2 SignalHandler(int) + 258
    3 libsystem_platform.dylib 0x00007fff6d36bb3d _sigtramp + 29
    4 libsystem_platform.dylib 0x000000011da307c7 _sigtramp + 2959887527
    5 libsystem_c.dylib 0x00007fff6d22a1c9 abort + 127
    6 swift 0x0000000110ef1144 llvm::llvm_unreachable_internal(char const*, char const*, unsigned int) + 532
    7 swift 0x000000010e7465a3 swift::Stmt::getStartLoc() const + 451
    8 swift 0x000000010e1a0d2f swift::SILLocation::getDebugSourceLoc() const + 95
    9 swift 0x000000010d3fdf63 (anonymous namespace)::IRGenDebugInfoImpl::setCurrentLoc(swift::irgen::IRBuilder&, swift::SILDebugScope const*, swift::SILLocation) + 387
    10 swift 0x000000010d3fddcd swift::irgen::IRGenDebugInfo::setCurrentLoc(swift::irgen::IRBuilder&, swift::SILDebugScope const*, swift::SILLocation) + 29
    11 swift 0x000000010d41f9a0 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 2576
    12 swift 0x000000010d32d0d2 swift::irgen::IRGenerator::emitGlobalTopLevel(bool) + 674
    13 swift 0x000000010d3f8f8b performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_deleteswift::SILModule >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, swift::SourceFile*, llvm::GlobalVariable**) + 1067
    14 swift 0x000000010d3f7aba swift::performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_deleteswift::SILModule >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > >, llvm::GlobalVariable**) + 714
    15 swift 0x000000010d2d58f7 swift::RunImmediately(swift::CompilerInstance&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > > > const&, swift::IRGenOptions&, swift::SILOptions const&) + 311
    16 swift 0x000000010d28787f performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 13327
    17 swift 0x000000010d2833dd swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3021
    18 swift 0x000000010d2351ce main + 686
    19 libdyld.dylib 0x00007fff6d182085 start + 1
    [2] 45563 abort swift -O MNIST.swift

screen shot 2019-01-03 at 4 14 38 pm

Transformer sample fails with regex/ macos /miniconda

./main 0.5 "Introducing Swift for TensorFlow"
Fatal error: 'try!' expression unexpectedly raised an error: Python exception: No module named 'regex'
Traceback:
  File "./encoder.py", line 9, in <module>
    import regex as re
: file /usr/local/src/swift-build/swift/stdlib/public/Python/Python.swift, line 681
[1]    87050 illegal hardware instruction  ./main 0.5 "Introducing Swift for TensorFlow"
(base) ➜  Transformer git:(master) ✗ pip install regex
Requirement already satisfied: regex in /Users/admin/miniconda3/lib/python3.7/site-packages (2019.4.14)
```n

How to export to model?

Hi, i've checked out the repository and tried to run command follow to README.md guide in MNIST example. It worked perfectly, but i could not find any exported model. When i looked into source code, seem that there is no exporting function.
My question is, how to export the result as a model ? I cannot find any documents for this feature (I want something similar to tf.saved_model in Python)

Some known problems of the MNIST implmentation

tensorflow/swift#260 (comment)

An improvement list for the MNIST dataset module, from @BradLarson:

  1. One big problem is that the current MNIST implementation in the repository lacks shuffling for the batches. The CIFAR10 dataset example has this, for example.
  2. Another is that it currently relies on hardcoded paths, which will break if you take the dataset outside of that project and try to use it in something like Colab.
    • Update: Fixed in #215.
  3. Also, there isn't a consistent API in the image classification datasets we have so far, so it would be nice to align the CIFAR10 and MNIST datasets such that they could be closer in that regard.
  4. I don't know how much we want to develop here (vs. in a framework like SwiftAI), but maybe there could be a protocol that image classification datasets could comply to.

GPU perf is much worse than CPU for MNIST

With s4tf version 5.0-dev (built from utils/build-toolchain-tensorflow -g), MNIST sample is running much slower with GPU than with CPU. For each epoch, CPU training takes 16 seconds, but GPU training takes 31 seconds.

From nvprof, we can see that the GPU utilization is very low, and there are many GPU/CPU data copies.

s4tfperf

Note that MNIST sample is modified as below to make it runnable.

            // Update number of correct/total guesses.
            let correctPredictions = predictions.argmax(squeezingAxis: 1) .== numericLabels
            for i in 0...correctPredictions.scalarCount - 1 {
                if correctPredictions.scalars[Int(i)] {
                    correctGuesses += 1
                }
            }

Another issue is that the launching time for the first run of GPU code is very slow, takes about 5 minutes. After the first run, there will be no such problems. We are running in ubuntu docker container.

enhancement - a generative model / GAN

to foster community involvement - some richer sample code beyond MNIST should be tackled.
Generative Adversarial Networks is a hot topic amongst ML - and some sample code using swift should help encourage researchers to pick up swift / a list of GAN projects attached below.

thinking out loud - consider google swift team - could have a budget with bounties that community could vote on and award to spur development ahead.

python reference -
https://github.com/hwalsuklee/tensorflow-generative-model-collections

https://github.com/hindupuravinash/the-gan-zoo

3D-ED-GAN - Shape Inpainting using 3D Generative Adversarial Network and Recurrent Convolutional Networks
3D-GAN - Learning a Probabilistic Latent Space of Object Shapes via 3D Generative-Adversarial Modeling (github)
3D-IWGAN - Improved Adversarial Systems for 3D Object Generation and Reconstruction (github)
3D-RecGAN - 3D Object Reconstruction from a Single Depth View with Adversarial Learning (github)
ABC-GAN - ABC-GAN: Adaptive Blur and Control for improved training stability of Generative Adversarial Networks (github)
ABC-GAN - GANs for LIFE: Generative Adversarial Networks for Likelihood Free Inference
AC-GAN - Conditional Image Synthesis With Auxiliary Classifier GANs
acGAN - Face Aging With Conditional Generative Adversarial Networks
ACGAN - Coverless Information Hiding Based on Generative adversarial networks
ACtuAL - ACtuAL: Actor-Critic Under Adversarial Learning
AdaGAN - AdaGAN: Boosting Generative Models
AdvGAN - Generating adversarial examples with adversarial networks
AE-GAN - AE-GAN: adversarial eliminating with GAN
AEGAN - Learning Inverse Mapping by Autoencoder based Generative Adversarial Nets
AF-DCGAN - AF-DCGAN: Amplitude Feature Deep Convolutional GAN for Fingerprint Construction in Indoor Localization System
AffGAN - Amortised MAP Inference for Image Super-resolution
AL-CGAN - Learning to Generate Images of Outdoor Scenes from Attributes and Semantic Layouts
ALI - Adversarially Learned Inference (github)
AlignGAN - AlignGAN: Learning to Align Cross-Domain Images with Conditional Generative Adversarial Networks
AM-GAN - Activation Maximization Generative Adversarial Nets
AmbientGAN - AmbientGAN: Generative models from lossy measurements (github)
AnoGAN - Unsupervised Anomaly Detection with Generative Adversarial Networks to Guide Marker Discovery
APE-GAN - APE-GAN: Adversarial Perturbation Elimination with GAN
ARAE - Adversarially Regularized Autoencoders for Generating Discrete Structures (github)
ARDA - Adversarial Representation Learning for Domain Adaptation
ARIGAN - ARIGAN: Synthetic Arabidopsis Plants using Generative Adversarial Network
ArtGAN - ArtGAN: Artwork Synthesis with Conditional Categorial GANs
ATA-GAN - Attention-Aware Generative Adversarial Networks (ATA-GANs)
Attention-GAN - Attention-GAN for Object Transfiguration in Wild Images
AttGAN - Arbitrary Facial Attribute Editing: Only Change What You Want (github)
AttnGAN - AttnGAN: Fine-Grained Text to Image Generation with Attentional Generative Adversarial Networks (github)
B-DCGAN - B-DCGAN:Evaluation of Binarized DCGAN for FPGA
b-GAN - Generative Adversarial Nets from a Density Ratio Estimation Perspective
BAGAN - BAGAN: Data Augmentation with Balancing GAN
Bayesian GAN - Deep and Hierarchical Implicit Models
Bayesian GAN - Bayesian GAN (github)
BCGAN - Bayesian Conditional Generative Adverserial Networks
BCGAN - Bidirectional Conditional Generative Adversarial networks
BEGAN - BEGAN: Boundary Equilibrium Generative Adversarial Networks
BGAN - Binary Generative Adversarial Networks for Image Retrieval (github)
BicycleGAN - Toward Multimodal Image-to-Image Translation (github)
BiGAN - Adversarial Feature Learning
BranchGAN - Branched Generative Adversarial Networks for Multi-Scale Image Manifold Learning
BS-GAN - Boundary-Seeking Generative Adversarial Networks
C-GAN - Face Aging with Contextual Generative Adversarial Nets
C-RNN-GAN - C-RNN-GAN: Continuous recurrent neural networks with adversarial training (github)
CA-GAN - Composition-aided Sketch-realistic Portrait Generation
CaloGAN - CaloGAN: Simulating 3D High Energy Particle Showers in Multi-Layer Electromagnetic Calorimeters with Generative Adversarial Networks (github)
CAN - CAN: Creative Adversarial Networks, Generating Art by Learning About Styles and Deviating from Style Norms
CapsuleGAN - CapsuleGAN: Generative Adversarial Capsule Network
CatGAN - Unsupervised and Semi-supervised Learning with Categorical Generative Adversarial Networks
CatGAN - CatGAN: Coupled Adversarial Transfer for Domain Generation
CausalGAN - CausalGAN: Learning Causal Implicit Generative Models with Adversarial Training
CC-GAN - Semi-Supervised Learning with Context-Conditional Generative Adversarial Networks (github)
CDcGAN - Simultaneously Color-Depth Super-Resolution with Conditional Generative Adversarial Network
CFG-GAN - Composite Functional Gradient Learning of Generative Adversarial Models
CGAN - Conditional Generative Adversarial Nets
CGAN - Controllable Generative Adversarial Network
Chekhov GAN - An Online Learning Approach to Generative Adversarial Networks
CipherGAN - Unsupervised Cipher Cracking Using Discrete GANs
CM-GAN - CM-GANs: Cross-modal Generative Adversarial Networks for Common Representation Learning
CoAtt-GAN - Are You Talking to Me? Reasoned Visual Dialog Generation through Adversarial Learning
CoGAN - Coupled Generative Adversarial Networks
ComboGAN - ComboGAN: Unrestrained Scalability for Image Domain Translation (github)
ConceptGAN - Learning Compositional Visual Concepts with Mutual Consistency
Conditional cycleGAN - Conditional CycleGAN for Attribute Guided Face Image Generation
constrast-GAN - Generative Semantic Manipulation with Contrasting GAN
Context-RNN-GAN - Contextual RNN-GANs for Abstract Reasoning Diagram Generation
CorrGAN - Correlated discrete data generation using adversarial training
Coulomb GAN - Coulomb GANs: Provably Optimal Nash Equilibria via Potential Fields
Cover-GAN - Generative Steganography with Kerckhoffs' Principle based on Generative Adversarial Networks
Cramèr GAN - The Cramer Distance as a Solution to Biased Wasserstein Gradients
Cross-GAN - Crossing Generative Adversarial Networks for Cross-View Person Re-identification
crVAE-GAN - Channel-Recurrent Variational Autoencoders
CS-GAN - Improving Neural Machine Translation with Conditional Sequence Generative Adversarial Nets
CVAE-GAN - CVAE-GAN: Fine-Grained Image Generation through Asymmetric Training
CycleGAN - Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks (github)
D-GAN - Differential Generative Adversarial Networks: Synthesizing Non-linear Facial Variations with Limited Number of Training Data
D-WCGAN - I-vector Transformation Using Conditional Generative Adversarial Networks for Short Utterance Speaker Verification
D2GAN - Dual Discriminator Generative Adversarial Nets
D2IA-GAN - Tagging like Humans: Diverse and Distinct Image Annotation
DA-GAN - DA-GAN: Instance-level Image Translation by Deep Attention Generative Adversarial Networks (with Supplementary Materials)
DAGAN - Data Augmentation Generative Adversarial Networks
DAN - Distributional Adversarial Networks
DBLRGAN - Adversarial Spatio-Temporal Learning for Video Deblurring
DCGAN - Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks (github)
DeblurGAN - DeblurGAN: Blind Motion Deblurring Using Conditional Adversarial Networks (github)
Defense-GAN - Defense-GAN: Protecting Classifiers Against Adversarial Attacks Using Generative Models
DeliGAN - DeLiGAN : Generative Adversarial Networks for Diverse and Limited Data (github)
DF-GAN - Learning Disentangling and Fusing Networks for Face Completion Under Structured Occlusions
DiscoGAN - Learning to Discover Cross-Domain Relations with Generative Adversarial Networks
DistanceGAN - One-Sided Unsupervised Domain Mapping
DM-GAN - Dual Motion GAN for Future-Flow Embedded Video Prediction
DNA-GAN - DNA-GAN: Learning Disentangled Representations from Multi-Attribute Images
Domain and Geometry Agnostic CNNs for Left Atrium Segmentation in 3D Ultrasound - https://arxiv.org/abs/1805.00357 (github)
dp-GAN - Differentially Private Releasing via Deep Generative Model
DP-GAN - DP-GAN: Diversity-Promoting Generative Adversarial Network for Generating Informative and Diversified Text
DPGAN - Differentially Private Generative Adversarial Network
DR-GAN - Representation Learning by Rotating Your Faces
DRAGAN - How to Train Your DRAGAN (github)
DRPAN - Discriminative Region Proposal Adversarial Networks for High-Quality Image-to-Image Translation
DSP-GAN - Depth Structure Preserving Scene Image Generation
DTN - Unsupervised Cross-Domain Image Generation
DualGAN - DualGAN: Unsupervised Dual Learning for Image-to-Image Translation
Dualing GAN - Dualing GANs
Dynamics Transfer GAN - Dynamics Transfer GAN: Generating Video by Transferring Arbitrary Temporal Dynamics from a Source Video to a Single Target Image
E-GAN - Evolutionary Generative Adversarial Networks
EBGAN - Energy-based Generative Adversarial Network
ecGAN - eCommerceGAN : A Generative Adversarial Network for E-commerce
ED//GAN - Stabilizing Training of Generative Adversarial Networks through Regularization
EGAN - Enhanced Experience Replay Generation for Efficient Reinforcement Learning
ELEGANT - ELEGANT: Exchanging Latent Encodings with GAN for Transferring Multiple Face Attributes
EnergyWGAN - Energy-relaxed Wassertein GANs (EnergyWGAN): Towards More Stable and High Resolution Image Generation
ExGAN - Eye In-Painting with Exemplar Generative Adversarial Networks
ExposureGAN - Exposure: A White-Box Photo Post-Processing Framework (github)
ExprGAN - ExprGAN: Facial Expression Editing with Controllable Expression Intensity
f-CLSWGAN - Feature Generating Networks for Zero-Shot Learning
f-GAN - f-GAN: Training Generative Neural Samplers using Variational Divergence Minimization
FBGAN - Feedback GAN (FBGAN) for DNA: a Novel Feedback-Loop Architecture for Optimizing Protein Functions
FF-GAN - Towards Large-Pose Face Frontalization in the Wild
Fictitious GAN - Fictitious GAN: Training GANs with Historical Models
FIGAN - Frame Interpolation with Multi-Scale Deep Loss Functions and Generative Adversarial Networks
Fila-GAN - Synthesizing Filamentary Structured Images with GANs
First Order GAN - First Order Generative Adversarial Networks (github)
Fisher GAN - Fisher GAN
Flow-GAN - Flow-GAN: Bridging implicit and prescribed learning in generative models
FSEGAN - Exploring Speech Enhancement with Generative Adversarial Networks for Robust Speech Recognition
FTGAN - Hierarchical Video Generation from Orthogonal Information: Optical Flow and Texture
FusedGAN - Semi-supervised FusedGAN for Conditional Image Generation
FusionGAN - Learning to Fuse Music Genres with Generative Adversarial Dual Learning
G2-GAN - Geometry Guided Adversarial Facial Expression Synthesis
GAAN - Generative Adversarial Autoencoder Networks
GAGAN - GAGAN: Geometry-Aware Generative Adverserial Networks
GAMN - Generative Adversarial Mapping Networks
GAN - Generative Adversarial Networks (github)
GAN-ATV - A Novel Approach to Artistic Textual Visualization via GAN
GAN-CLS - Generative Adversarial Text to Image Synthesis (github)
GAN-RS - Towards Qualitative Advancement of Underwater Machine Vision with Generative Adversarial Networks
GAN-sep - GANs for Biological Image Synthesis (github)
GAN-VFS - Generative Adversarial Network-based Synthesis of Visible Faces from Polarimetric Thermal Faces
GANCS - Deep Generative Adversarial Networks for Compressed Sensing Automates MRI
GANDI - Guiding the search in continuous state-action spaces by learning an action sampling distribution from off-target samples
GANG - GANGs: Generative Adversarial Network Games
GANosaic - GANosaic: Mosaic Creation with Generative Texture Manifolds
GAP - Context-Aware Generative Adversarial Privacy
GAWWN - Learning What and Where to Draw (github)
GC-GAN - Geometry-Contrastive Generative Adversarial Network for Facial Expression Synthesis
GeneGAN - GeneGAN: Learning Object Transfiguration and Attribute Subspace from Unpaired Data (github)
GeoGAN - Generating Instance Segmentation Annotation by Geometry-guided GAN
Geometric GAN - Geometric GAN
GLCA-GAN - Global and Local Consistent Age Generative Adversarial Networks
GMAN - Generative Multi-Adversarial Networks
GMM-GAN - Towards Understanding the Dynamics of Generative Adversarial Networks
GoGAN - Gang of GANs: Generative Adversarial Networks with Maximum Margin Ranking
GONet - GONet: A Semi-Supervised Deep Learning Approach For Traversability Estimation
GP-GAN - GP-GAN: Towards Realistic High-Resolution Image Blending (github)
GP-GAN - GP-GAN: Gender Preserving GAN for Synthesizing Faces from Landmarks
GPU - A generative adversarial framework for positive-unlabeled classification
GRAN - Generating images with recurrent adversarial networks (github)
Graphical-GAN - Graphical Generative Adversarial Networks
GraspGAN - Using Simulation and Domain Adaptation to Improve Efficiency of Deep Robotic Grasping
HAN - Chinese Typeface Transformation with Hierarchical Adversarial Network
HP-GAN - HP-GAN: Probabilistic 3D human motion prediction via GAN
HR-DCGAN - High-Resolution Deep Convolutional Generative Adversarial Networks
IAN - Neural Photo Editing with Introspective Adversarial Networks (github)
IcGAN - Invertible Conditional GANs for image editing (github)
ID-CGAN - Image De-raining Using a Conditional Generative Adversarial Network
IdCycleGAN - Face Translation between Images and Videos using Identity-aware CycleGAN
IFcVAEGAN - Conditional Autoencoders with Adversarial Information Factorization
iGAN - Generative Visual Manipulation on the Natural Image Manifold (github)
Improved GAN - Improved Techniques for Training GANs (github)
In2I - In2I : Unsupervised Multi-Image-to-Image Translation Using Generative Adversarial Networks
InfoGAN - InfoGAN: Interpretable Representation Learning by Information Maximizing Generative Adversarial Nets (github)
IRGAN - IRGAN: A Minimax Game for Unifying Generative and Discriminative Information Retrieval models
Iterative-GAN - Two Birds with One Stone: Iteratively Learn Facial Attributes with GANs (github)
IVE-GAN - IVE-GAN: Invariant Encoding Generative Adversarial Networks
iVGAN - Towards an Understanding of Our World by GANing Videos in the Wild (github)
IWGAN - On Unifying Deep Generative Models
KBGAN - KBGAN: Adversarial Learning for Knowledge Graph Embeddings
KGAN - KGAN: How to Break The Minimax Game in GAN
l-GAN - Representation Learning and Adversarial Generation of 3D Point Clouds
LAC-GAN - Grounded Language Understanding for Manipulation Instructions Using GAN-Based Classification
LAGAN - Learning Particle Physics by Example: Location-Aware Generative Adversarial Networks for Physics Synthesis
LAPGAN - Deep Generative Image Models using a Laplacian Pyramid of Adversarial Networks (github)
LB-GAN - Load Balanced GANs for Multi-view Face Image Synthesis
LD-GAN - Linear Discriminant Generative Adversarial Networks
LDAN - Label Denoising Adversarial Network (LDAN) for Inverse Lighting of Face Images
LeakGAN - Long Text Generation via Adversarial Training with Leaked Information
LeGAN - Likelihood Estimation for Generative Adversarial Networks
LGAN - Global versus Localized Generative Adversarial Nets
LR-GAN - LR-GAN: Layered Recursive Generative Adversarial Networks for Image Generation
LS-GAN - Loss-Sensitive Generative Adversarial Networks on Lipschitz Densities
LSGAN - Least Squares Generative Adversarial Networks
MAD-GAN - Multi-Agent Diverse Generative Adversarial Networks
MAGAN - MAGAN: Margin Adaptation for Generative Adversarial Networks
MAGAN - MAGAN: Aligning Biological Manifolds
MalGAN - Generating Adversarial Malware Examples for Black-Box Attacks Based on GAN
MaliGAN - Maximum-Likelihood Augmented Discrete Generative Adversarial Networks
manifold-WGAN - Manifold-valued Image Generation with Wasserstein Adversarial Networks
MARTA-GAN - Deep Unsupervised Representation Learning for Remote Sensing Images
MaskGAN - MaskGAN: Better Text Generation via Filling in the ______
MC-GAN - Multi-Content GAN for Few-Shot Font Style Transfer (github)
McGAN - McGan: Mean and Covariance Feature Matching GAN
MD-GAN - Learning to Generate Time-Lapse Videos Using Multi-Stage Dynamic Generative Adversarial Networks
MDGAN - Mode Regularized Generative Adversarial Networks
MedGAN - Generating Multi-label Discrete Electronic Health Records using Generative Adversarial Networks
MelanoGAN - MelanoGANs: High Resolution Skin Lesion Synthesis with GANs
memoryGAN - Memorization Precedes Generation: Learning Unsupervised GANs with Memory Networks
MGAN - Precomputed Real-Time Texture Synthesis with Markovian Generative Adversarial Networks (github)
MGGAN - Multi-Generator Generative Adversarial Nets
MGGAN - MGGAN: Solving Mode Collapse using Manifold Guided Training
MIL-GAN - Multimodal Storytelling via Generative Adversarial Imitation Learning
MIX+GAN - Generalization and Equilibrium in Generative Adversarial Nets (GANs)
MLGAN - Metric Learning-based Generative Adversarial Network
MMD-GAN - MMD GAN: Towards Deeper Understanding of Moment Matching Network (github)
MMGAN - MMGAN: Manifold Matching Generative Adversarial Network for Generating Images
MoCoGAN - MoCoGAN: Decomposing Motion and Content for Video Generation (github)
ModularGAN - Modular Generative Adversarial Networks
MPM-GAN - Message Passing Multi-Agent GANs
MS-GAN - Temporal Coherency based Criteria for Predicting Video Frames using Deep Multi-stage Generative Adversarial Networks
MTGAN - MTGAN: Speaker Verification through Multitasking Triplet Generative Adversarial Networks
MuseGAN - MuseGAN: Symbolic-domain Music Generation and Accompaniment with Multi-track Sequential Generative Adversarial Networks
MV-BiGAN - Multi-view Generative Adversarial Networks
NAN - Understanding Humans in Crowded Scenes: Deep Nested Adversarial Learning and A New Benchmark for Multi-Human Parsing
NCE-GAN - Dihedral angle prediction using generative adversarial networks
ND-GAN - Novelty Detection with GAN
NetGAN - NetGAN: Generating Graphs via Random Walks
OCAN - One-Class Adversarial Nets for Fraud Detection
OptionGAN - OptionGAN: Learning Joint Reward-Policy Options using Generative Adversarial Inverse Reinforcement Learning
ORGAN - Objective-Reinforced Generative Adversarial Networks (ORGAN) for Sequence Generation Models
ORGAN - 3D Reconstruction of Incomplete Archaeological Objects Using a Generative Adversary Network
OT-GAN - Improving GANs Using Optimal Transport
PacGAN - PacGAN: The power of two samples in generative adversarial networks
PAN - Perceptual Adversarial Networks for Image-to-Image Transformation
PassGAN - PassGAN: A Deep Learning Approach for Password Guessing
Perceptual GAN - Perceptual Generative Adversarial Networks for Small Object Detection
PGAN - Probabilistic Generative Adversarial Networks
PGD-GAN - Solving Linear Inverse Problems Using GAN Priors: An Algorithm with Provable Guarantees
PGGAN - Patch-Based Image Inpainting with Generative Adversarial Networks
Pip-GAN - Pipeline Generative Adversarial Networks for Facial Images Generation with Multiple Attributes
pix2pix - Image-to-Image Translation with Conditional Adversarial Networks (github)
pix2pixHD - High-Resolution Image Synthesis and Semantic Manipulation with Conditional GANs (github)
PixelGAN - PixelGAN Autoencoders
PN-GAN - Pose-Normalized Image Generation for Person Re-identification
Pose-GAN - The Pose Knows: Video Forecasting by Generating Pose Futures
PPAN - Privacy-Preserving Adversarial Networks
PPGN - Plug & Play Generative Networks: Conditional Iterative Generation of Images in Latent Space
PrGAN - 3D Shape Induction from 2D Views of Multiple Objects
ProGanSR - A Fully Progressive Approach to Single-Image Super-Resolution
Progressive GAN - Progressive Growing of GANs for Improved Quality, Stability, and Variation (github)
PS-GAN - Pedestrian-Synthesis-GAN: Generating Pedestrian Data in Real Scene and Beyond
PSGAN - Learning Texture Manifolds with the Periodic Spatial GAN
PS²-GAN - High-Quality Facial Photo-Sketch Synthesis Using Multi-Adversarial Networks
RadialGAN - RadialGAN: Leveraging multiple datasets to improve target-specific predictive models using Generative Adversarial Networks
RAN - RAN4IQA: Restorative Adversarial Nets for No-Reference Image Quality Assessment (github)
RankGAN - Adversarial Ranking for Language Generation
RCGAN - Real-valued (Medical) Time Series Generation with Recurrent Conditional GANs
RefineGAN - Compressed Sensing MRI Reconstruction with Cyclic Loss in Generative Adversarial Networks
RenderGAN - RenderGAN: Generating Realistic Labeled Data
ResGAN - Generative Adversarial Network based on Resnet for Conditional Image Restoration
RNN-WGAN - Language Generation with Recurrent Generative Adversarial Networks without Pre-training (github)
RPGAN - Stabilizing GAN Training with Multiple Random Projections (github)
RTT-GAN - Recurrent Topic-Transition GAN for Visual Paragraph Generation
RWGAN - Relaxed Wasserstein with Applications to GANs
SAD-GAN - SAD-GAN: Synthetic Autonomous Driving using Generative Adversarial Networks
SAGA - Generative Adversarial Learning for Spectrum Sensing
SalGAN - SalGAN: Visual Saliency Prediction with Generative Adversarial Networks (github)
SAR-GAN - Generating High Quality Visible Images from SAR Images Using CNNs
SBADA-GAN - From source to target and back: symmetric bi-directional adaptive GAN
SCH-GAN - SCH-GAN: Semi-supervised Cross-modal Hashing by Generative Adversarial Network
SD-GAN - Semantically Decomposing the Latent Spaces of Generative Adversarial Networks
Sdf-GAN - Sdf-GAN: Semi-supervised Depth Fusion with Multi-scale Adversarial Networks
SEGAN - SEGAN: Speech Enhancement Generative Adversarial Network
SeGAN - SeGAN: Segmenting and Generating the Invisible
SegAN - SegAN: Adversarial Network with Multi-scale L1 Loss for Medical Image Segmentation
SeqGAN - SeqGAN: Sequence Generative Adversarial Nets with Policy Gradient (github)
SG-GAN - Semantic-aware Grad-GAN for Virtual-to-Real Urban Scene Adaption (github)
SGAN - Texture Synthesis with Spatial Generative Adversarial Networks
SGAN - Stacked Generative Adversarial Networks (github)
SGAN - Steganographic Generative Adversarial Networks
SGAN - SGAN: An Alternative Training of Generative Adversarial Networks
sGAN - Generative Adversarial Training for MRA Image Synthesis Using Multi-Contrast MRI
SimGAN - Learning from Simulated and Unsupervised Images through Adversarial Training
SisGAN - Semantic Image Synthesis via Adversarial Learning
SketchGAN - Adversarial Training For Sketch Retrieval
SketchyGAN - SketchyGAN: Towards Diverse and Realistic Sketch to Image Synthesis
SL-GAN - Semi-Latent GAN: Learning to generate and modify facial images from attributes
SN-GAN - Spectral Normalization for Generative Adversarial Networks (github)
Sobolev GAN - Sobolev GAN
Social GAN - Social GAN: Socially Acceptable Trajectories with Generative Adversarial Networks
Softmax GAN - Softmax GAN
Spike-GAN - Synthesizing realistic neural population activity patterns using Generative Adversarial Networks
Splitting GAN - Class-Splitting Generative Adversarial Networks
SRGAN - Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network
SRPGAN - SRPGAN: Perceptual Generative Adversarial Network for Single Image Super Resolution
SS-GAN - Semi-supervised Conditional GANs
ss-InfoGAN - Guiding InfoGAN with Semi-Supervision
SSGAN - SSGAN: Secure Steganography Based on Generative Adversarial Networks
SSL-GAN - Semi-Supervised Learning with Context-Conditional Generative Adversarial Networks
ST-CGAN - Stacked Conditional Generative Adversarial Networks for Jointly Learning Shadow Detection and Shadow Removal
ST-GAN - Style Transfer Generative Adversarial Networks: Learning to Play Chess Differently
ST-GAN - ST-GAN: Spatial Transformer Generative Adversarial Networks for Image Compositing
StackGAN - StackGAN: Text to Photo-realistic Image Synthesis with Stacked Generative Adversarial Networks (github)
StainGAN: Stain Style Transfer for Digital Histological Images - https://arxiv.org/abs/1804.01601 (github)
StarGAN - StarGAN: Unified Generative Adversarial Networks for Multi-Domain Image-to-Image Translation (github)
SteinGAN - Learning Deep Energy Models: Contrastive Divergence vs. Amortized MLE
Super-FAN - Super-FAN: Integrated facial landmark localization and super-resolution of real-world low resolution faces in arbitrary poses with GANs
SVSGAN - SVSGAN: Singing Voice Separation via Generative Adversarial Network
SWGAN - Solving Approximate Wasserstein GANs to Stationarity
SyncGAN - SyncGAN: Synchronize the Latent Space of Cross-modal Generative Adversarial Networks
S^2GAN - Generative Image Modeling using Style and Structure Adversarial Networks
TAC-GAN - TAC-GAN - Text Conditioned Auxiliary Classifier Generative Adversarial Network (github)
TAN - Outline Colorization through Tandem Adversarial Networks
tempoGAN - tempoGAN: A Temporally Coherent, Volumetric GAN for Super-resolution Fluid Flow
Text2Shape - Text2Shape: Generating Shapes from Natural Language by Learning Joint Embeddings
textGAN - Generating Text via Adversarial Training
TextureGAN - TextureGAN: Controlling Deep Image Synthesis with Texture Patches
TGAN - Temporal Generative Adversarial Nets
TGAN - Tensorizing Generative Adversarial Nets
TGAN - Tensor-Generative Adversarial Network with Two-dimensional Sparse Coding: Application to Real-time Indoor Localization
tiny-GAN - Analysis of Nonautonomous Adversarial Systems
TP-GAN - Beyond Face Rotation: Global and Local Perception GAN for Photorealistic and Identity Preserving Frontal View Synthesis
Triple-GAN - Triple Generative Adversarial Nets
tripletGAN - TripletGAN: Training Generative Model with Triplet Loss
TV-GAN - TV-GAN: Generative Adversarial Network Based Thermal to Visible Face Recognition
UGACH - Unsupervised Generative Adversarial Cross-modal Hashing
UGAN - Enhancing Underwater Imagery using Generative Adversarial Networks
Unim2im - Unsupervised Image-to-Image Translation with Generative Adversarial Networks (github)
UNIT - Unsupervised Image-to-image Translation Networks (github)
Unrolled GAN - Unrolled Generative Adversarial Networks (github)
UV-GAN - UV-GAN: Adversarial Facial UV Map Completion for Pose-invariant Face Recognition
VAE-GAN - Autoencoding beyond pixels using a learned similarity metric
VariGAN - Multi-View Image Generation from a Single-View
VAW-GAN - Voice Conversion from Unaligned Corpora using Variational Autoencoding Wasserstein Generative Adversarial Networks
VEEGAN - VEEGAN: Reducing Mode Collapse in GANs using Implicit Variational Learning (github)
VGAN - Generating Videos with Scene Dynamics (github)
VGAN - Generative Adversarial Networks as Variational Training of Energy Based Models (github)
VGAN - Text Generation Based on Generative Adversarial Nets with Latent Variable
ViGAN - Image Generation and Editing with Variational Info Generative Adversarial Networks
VIGAN - VIGAN: Missing View Imputation with Generative Adversarial Networks
VoiceGAN - Voice Impersonation using Generative Adversarial Networks
VOS-GAN - VOS-GAN: Adversarial Learning of Visual-Temporal Dynamics for Unsupervised Dense Prediction in Videos
VRAL - Variance Regularizing Adversarial Learning
WaterGAN - WaterGAN: Unsupervised Generative Network to Enable Real-time Color Correction of Monocular Underwater Images
WaveGAN - Synthesizing Audio with Generative Adversarial Networks
weGAN - Generative Adversarial Nets for Multiple Text Corpora
WGAN - Wasserstein GAN (github)
WGAN-GP - Improved Training of Wasserstein GANs (github)
WS-GAN - Weakly Supervised Generative Adversarial Networks for 3D Reconstruction
XGAN - XGAN: Unsupervised Image-to-Image Translation for many-to-many Mappings
ZipNet-GAN - ZipNet-GAN: Inferring Fine-grained Mobile Traffic Patterns via a Generative Adversarial Neural Network
α-GAN - Variational Approaches for Auto-Encoding Generative Adversarial Networks (github)
β-GAN - Annealed Generative Adversarial Networks
Δ-GAN - Triangle Generative Adversarial Networks

Speech to text example

It'd be great to have an example speech to text model incorporated into our example models.

macOS troubleshooting

macOS Troubleshooting

If you see TensorFlow not found when building samples from command line or

<unknown>:0: error: cannot load underlying module for 'Darwin'
<unknown>:0: note: did you forget to set an SDK using -sdk or SDKROOT?
<unknown>:0: note: use "xcrun swiftc" to select the default macOS SDK installed with Xcode

make sure you've added the correct version of swift to your path.
eg.

which swift
/usr/bin/swift # This is the wrong path!

The swift binary should be referencing the most updated toolchain bin path eg.

export PATH="/Library/Developer/Toolchains/swift-latest/usr/bin/:$PATH"
which swift
/Library/Developer/Toolchains/swift-latest/usr/bin/swift

Transformer model can not handle batch_size bigger than 1 during inference.

When trying to do inference with more than one seed sentences or simply duplicate the input token like this:

    var tokarr: [Int32] = Array<Int>(pytok)!.map { Int32($0) }
    tokarr = Array<[Int32]>(repeating: tokarr, count: 2).flatMap { $0 }
    tokens = Tensor<Int32>(shape: [2, Int32(tokarr.count/2)], scalars: tokarr)

It failed at line 113 of Model.swift

key: state.key.concatenated(with: input.key, alongAxis: 1)

This is the error message:

Thread 1: Fatal error: ConcatOp : Dimensions of inputs should match: shape[0] = [12,0,64] vs. shape[1] = [24,2,64]

Python version supports batch_size bigger than 1.

Branch stable and Swift for Tensorflow 0.2 release 2019-03-02.
Xcode Version 10.1 (10B61)
macOS High Sierra (Version 10.13.6)

Fails to install on Google Colab

I would like to install this framework in Google Colab on the blank_swift.ipynb notebook.

My code:

%install-location $cwd/swift-install
%install '.package(url: "https://github.com/tensorflow/swift-models", from: "0.16.1")' SwiftModels

It gives the following error:

Installing packages:
	.package(url: "https://github.com/tensorflow/swift-models", from: "0.16.1")
		SwiftModels
With SwiftPM flags: []
Working in: /tmp/tmpezinnxut/swift-install
Updating https://github.com/tensorflow/swift-models
error: the package dependency graph could not be resolved; unable to find any available tag for the following requirements:
    https://github.com/tensorflow/swift-models @ 0.16.1..<1.0.0
Install Error: swift-build returned nonzero exit code 1.

Please let me know if you need additional information.

Autoencoder's activation should be sigmoid

It's using tanh in output layer.

var decoder4 = Dense<Float>(inputSize: 128, outputSize: imageHeight * imageWidth,
activation: tanh)

But input image is [0, 1] ranged.

images: Tensor(shape: [rowCount, imageHeight * imageWidth], scalars: images) / 255.0,

In v0.4.0-rc2, if we use sigmoid, this model doesn't learn maybe because there's a bug I fixed but not included yet.
tensorflow/swift-apis#341

It should be replaced in next release.

Internal error when `public`s are removed from the catch model

This is the catch model with all publics removed. The original version runs ok, but it triggers an internal error when publics are removed.

import TensorFlow

// Note: This is a work in progress and training doesn't quite work.
// Here are areas for improvement:
// - Adopt a more principled reinforcement learning algorithm (e.g. policy
//   gradients). The algorithm should perform some tensor computation (not a
//   purely table-based approach).
// - `CatchAgent.step` calculates loss from the wrong reward. It uses the reward
//   at time `t+1`, but should actually use the reward from time `t`. This
//   requires saving the previous reward somehow.
// - The current back-propagation calculation may be incorrect.
// - It may be better to use a different initialization scheme for the layers of
//   `CatchAgent`.

extension Sequence {
  /// Returns elements' descriptions joined by a separator.
  func description(joinedBy separator: String) -> String {
    return map{"\($0)"}.joined(separator: separator)
  }
}

typealias Observation = ShapedArray<Float>
typealias Reward = Float

protocol Environment {
  associatedtype Action : Equatable

  mutating func step(
    with action: Action
  ) -> (observation: Observation, reward: Reward)
  mutating func reset() -> Observation
}

protocol Agent {
  associatedtype Action : Equatable

  mutating func step(
    with state: (observation: Observation, reward: Reward)
  ) -> Action
}

struct CatchAgent : Agent {
  typealias Action = CatchAction

  @_versioned var layer1: FullyConnectedLayer<Float>
  @_versioned var layer2: FullyConnectedLayer<Float>
  @_versioned let learningRate: Float
}

extension CatchAgent {
  init(learningRate: Float) {
    layer1 = FullyConnectedLayer(inputCount: 3, outputCount: 50)
    layer2 = FullyConnectedLayer(inputCount: 50, outputCount: 3)
    self.learningRate = learningRate
  }

  /// Performs one "step" (or parameter update) based on the specified
  /// observation and reward.
  @inline(never)
  mutating func step(
     with state: (observation: Observation, reward: Reward)
  ) -> Action {
    // NOTE: using `self.layer1` directly causes a send error. This is likely
    // because the function is mutating so referencing `self.layer1` produces a
    // load.
    // The workaround here is to:
    // - Bind `self.layer1` to a local variable.
    // - Perform tensor computations using the local variable.
    // - After all computations, set `self.layer1` to the local variable.

    // Initial setup.
    let (observation, reward) = state
    var layer1 = self.layer1
    var layer2 = self.layer2
    let learningRate = self.learningRate

    // Inference.
    let input = Tensor<Float>(observation).rankLifted()
    let pred1 = layer1.applied(to: input)
    let output1 = sigmoid(pred1)
    let pred2 = layer2.applied(to: output1)
    let output2 = sigmoid(pred2)
    let maxIndex = output2.argmax()

    // Back-propagation.
    let dOutput2 = output2 * (1 - output2)
    let (_, dParameters2) = layer2.gradient(for: output1,
                                            backpropagating: dOutput2)
    let dOutput1 = output1 * (1 - output1)
    let (_, dParameters1) = layer1.gradient(for: input,
                                            backpropagating: dOutput1)

    // Negative log loss.
    // FIXME: Loss is calculated from the wrong reward! It should be calculated
    // from the previous state. Fixing this is *most likely* to improve
    // training.
    // FIXME: indexing with `maxIndex` directly causes a send.
    // let loss = -log(output2[maxIndex]) * reward
    // FIXME: This infers to the variadic `max` function, which acts as a no-op.
    // let loss = -log(output2.max()) * reward
    let maxValue: Float = output2.max()
    let loss = -log(Tensor(maxValue)) * reward

    layer1.parameters.update(with: dParameters1,
                             by: { $0 -= learningRate * loss * $1 })
    layer2.parameters.update(with: dParameters2,
                             by: { $0 -= learningRate * loss * $1 })

    self.layer1 = layer1
    self.layer2 = layer2
    let action = CatchAction(rawValue: Int(maxIndex))!
    return action
  }

  /// Returns the perfect action, given an observation.
  /// If the ball is left of the paddle, returns `left`.
  /// If the ball is right of the paddle, returns `right`.
  /// Otherwise, returns `none`.
  /// Note: This function is for reference and is not used by `CatchAgent`.
  func perfectAction(for observation: Observation) -> Action {
    let paddleX = observation.scalars[0]
    let ballX = observation.scalars[1]
    if paddleX > ballX {
      return .right
    } else if paddleX < ballX {
      return .left
    }
    return .none
  }

  /// Returns a random action.
  /// Note: This function is for reference and is not used by `CatchAgent`.
  func randomAction() -> Action {
    let id = Int(RandomState.global.generate()) % 3
    return CatchAction(rawValue: id)!
  }
}

enum CatchAction : Int {
  case none
  case left
  case right
}

struct Position : Equatable, Hashable {
  var x: Int
  var y: Int
}

struct CatchEnvironment : Environment {
  typealias Action = CatchAction
  let rowCount: Int
  let columnCount: Int
  var ballPosition: Position
  var paddlePosition: Position
}

extension CatchEnvironment {
  init(rowCount: Int, columnCount: Int, seed: UInt32? = nil) {
    self.rowCount = rowCount
    self.columnCount = columnCount
    self.ballPosition = Position(x: 0, y: 0)
    self.paddlePosition = Position(x: 0, y: 0)
    reset()
  }

  mutating func step(
    with action: CatchAction
  ) -> (observation: Observation, reward: Float) {
    // Update state.
    switch action {
    case .left where paddlePosition.x > 0:
      paddlePosition.x -= 1
    case .right where paddlePosition.x < columnCount - 1:
      paddlePosition.x += 1
    default:
      break
    }
    ballPosition.y += 1
    // Get reward.
    let currentReward = reward()
    // Return observation and reward.
    if ballPosition.y == rowCount {
      return (reset(), currentReward)
    }
    return (observation(), currentReward)
  }

  /// Resets the ball to be in a random column in the first row, and resets the
  /// paddle to be in the middle column of the bottom row.
  @discardableResult
  mutating func reset() -> Observation {
    let randomColumn = Int(RandomState.global.generate()) % columnCount
    ballPosition = Position(x: randomColumn, y: 0)
    paddlePosition = Position(x: columnCount / 2, y: rowCount - 1)
    return observation()
  }

  /// If the ball is in the bottom row:
  /// - Return 1 if the horizontal distance from the ball to the paddle is less
  ///   than or equal to 1.
  /// - Otherwise, return -1.
  /// If the ball is not in the bottom row, return 0.
  func reward() -> Float {
    if ballPosition.y == rowCount {
      return abs(ballPosition.x - paddlePosition.x) <= 1 ? 1 : -1
    }
    return 0
  }

  /// Returns an obeservation of the game grid.
  func observation() -> Observation {
    return ShapedArray<Float>(
      shape: [3],
      scalars: [Float(ballPosition.x) / Float(columnCount),
                Float(ballPosition.y) / Float(rowCount),
                Float(paddlePosition.x) / Float(columnCount)]
    )
  }

  /// Returns the game grid as a 2D matrix where all scalars are 0 except the
  /// positions of the ball and paddle, which are 1.
  var grid: ShapedArray<Float> {
    var result = ShapedArray<Float>(shape: [rowCount, columnCount], repeating: 0)
    result[ballPosition.y][ballPosition.x] = ShapedArraySlice(1 as Float)
    result[paddlePosition.y][paddlePosition.x] = ShapedArraySlice(1 as Float)
    return result
  }
}

extension CatchEnvironment : CustomStringConvertible {
  var description: String {
    return grid.description(joinedBy: "\n")
  }
}

func main() {
  // Set global seed.
  RandomState.global.seed(with: 42)

  // Setup environment and agent.
  var environment = CatchEnvironment(rowCount: 5, columnCount: 5)
  var action: CatchAction = .none
  var agent = CatchAgent(learningRate: 0.01)

  var gameCount = 0
  var winCount = 0
  var totalWinCount = 0
  let maxIterations = 1000
  repeat {
    // NOTE: the next line is the only one running tensor code.
    let state = environment.step(with: action)
    action = agent.step(with: state)

    if !state.reward.isZero {
      print("Game \(gameCount)", state.reward)
      gameCount += 1
      if state.reward > 0 {
        winCount += 1
        totalWinCount += 1
      }
      if gameCount % 20 == 0 {
        print("Win rate (last 20 games): \(Float(winCount) / 20)")
        print("""
          Win rate (total): \(Float(totalWinCount) / Float(gameCount)) \
          [\(totalWinCount)/\(gameCount)]
          """)
        winCount = 0
      }
    }
  } while gameCount < maxIterations
  print("""
    Win rate (final): \(Float(totalWinCount) / Float(gameCount)) \
    [\(totalWinCount)/\(gameCount)]
    """)
}
main()

Running this using swift -O catch.swift gives an internal error. The _$S5catch8PositionVMn symbol is not linked. I haven't tested the compiler mode. This may be related to JIT only.

<unknown>:0: error: fatal error encountered during compilation; please file a bug report with your project and the crash log
<unknown>:0: note: Program used external function '_$S5catch8PositionVMn' which could not be resolved!
0  swift                    0x000000010b1e9d78 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x000000010b1ea486 SignalHandler(int) + 694
2  libsystem_platform.dylib 0x00007fff77a10f5a _sigtramp + 26
3  swift                    0x000000010bf1eb40 (anonymous namespace)::DarwinX86AsmBackend::getCompactUnwindRegNum(unsigned int) const::CU64BitRegs + 229990
4  libsystem_c.dylib        0x00007fff777ae1ae abort + 127
5  swift                    0x00000001079f6a67 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*)::$_0::__invoke(void*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool) + 519
6  swift                    0x000000010b18574c llvm::report_fatal_error(llvm::Twine const&, bool) + 252
7  swift                    0x0000000108556f2c llvm::RuntimeDyldImpl::resolveExternalSymbols() + 2956
8  swift                    0x0000000108555a09 llvm::RuntimeDyldImpl::resolveRelocations() + 201
9  swift                    0x0000000108545111 llvm::MCJIT::finalizeObject() + 433
10 swift                    0x0000000107a395c9 swift::RunImmediately(swift::CompilerInstance&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, swift::IRGenOptions&, swift::SILOptions const&) + 3017
11 swift                    0x00000001079f5f94 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 15188
12 swift                    0x00000001079f142e swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3310
13 swift                    0x00000001079a7f83 main + 2051
14 libdyld.dylib            0x00007fff77702015 start + 1
15 libdyld.dylib            0x000000000000000e start + 2291130362
Stack dump:

The emitted IR has this symbol, however.

@"$S5catch8PositionVMn" = hidden constant <{ i32, i32, i32, i32, i32, i32 }> <{ i32 262225, i32 trunc (i64 sub (i64 ptrtoint (<{ i32, i32, i32 }>* @"$S5catchMXM" to i64), i64 ptrtoint (i32* getelementptr
 inbounds (<{ i32, i32, i32, i32, i32, i32 }>, <{ i32, i32, i32, i32, i32, i32 }>* @"$S5catch8PositionVMn", i32 0, i32 1) to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([9 x i8]* @19 to i64), i64 pt
rtoint (i32* getelementptr inbounds (<{ i32, i32, i32, i32, i32, i32 }>, <{ i32, i32, i32, i32, i32, i32 }>* @"$S5catch8PositionVMn", i32 0, i32 2) to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (%sw
ift.type* ()* @"$S5catch8PositionVMa" to i64), i64 ptrtoint (i32* getelementptr inbounds (<{ i32, i32, i32, i32, i32, i32 }>, <{ i32, i32, i32, i32, i32, i32 }>* @"$S5catch8PositionVMn", i32 0, i32 3) to
 i64)) to i32), i32 2, i32 2 }>, section "__TEXT,__const", align 4

Adding @_versioned on CatchAgent, Position and CatchEnvironment makes it run normally. We should take a look at the frontend and see what's going on with linking.

Enhancement - MNIST example - showcase protocol Learnable/Optimizer for gradient / loss / learningRate

https://www.tensorflow.org/api_docs/swift/Protocols/Learnable
screen shot 2018-06-11 at 10 03 20

People picking up this sample repo will likely sprawl the wrong style of code unless good guidance is provided.

screen shot 2018-06-11 at 10 06 55

was there more sample code associated with this and other protocols (optimizer) that we can cherry pick?

here there's a bunch of code that seems messy lakes the DifferentiableModule inheritance compared to perceptron sample.
screen shot 2018-06-11 at 10 11 05

0.3 models pass

@rxwei Did a quick pass:

  • Transformer has a bunch of Int32 calls that will need to be updated v2: #113 thanks @leoxzhao!
  • MiniGo will require some ❤️ -> #114
  • Autoencoder: see #93

Rest seem to be working!

Transformer - errors - expected '(' in 'differentiable' attribute

I'm on stable branch
10.14.4 Beta (18E215a)
Xcode - Version 10.1 (10B61)
I'm 0.2 Release toolchain
I'm on python3 / miniconda environment

As per instructions
swiftc -O -ltensorflow Operators.swift Model.swift PythonCheckpointReader.swift main.swift

  • below I've captured two different errors running in terminal

Operators.swift:7:1: error: expected '(' in 'differentiable' attribute
func gelu<Scalar: TensorFlowFloatingPoint>(_ x: Tensor) -> Tensor {
^
Operators.swift:87:1: error: expected ')' in 'differentiable' attribute

^
Operators.swift:87:1: error: expected declaration

^
:0: error: cannot load underlying module for 'Darwin'
:0: note: did you forget to set an SDK using -sdk or SDKROOT?
:0: note: use "xcrun swiftc" to select the default macOS SDK installed with Xcode
:1:9: note: in file included from :1:
#import "include/Python.h"

Screen Shot 2019-03-19 at 00 59 24

When I use the AlexNet - it successfully compiles & runs
https://github.com/BradLarson/AlexNet-Swift/tree/master/AlexNet

I created the xcode proj file for macos ->

/ one in xcodeproject using these parameters (+ XcodeGen to spit out respective xcode project

toolchain_path: "/Library/Developer/Toolchains/swift-latest/usr/lib/swift/macosx/"


name: SwiftTensorflow
options:
  bundleIdPrefix: com.swiftTensorflow

targets:
  SwiftTensorflow:
    type: tool
    platform: macOS
    deploymentTarget: "10.13"
    sources: 
        - Sources
    dependencies:
      - framework: /Library/Developer/Toolchains/swift-latest/usr/lib/swift/macosx/libtensorflow_framework.so
      - framework: /Library/Developer/Toolchains/swift-latest/usr/lib/swift/macosx//libtensorflow.so
      - framework: libpython2.7.tbd
    settings:
      SWIFT_OPTIMIZATION_LEVEL: -O
      LD_RUNPATH_SEARCH_PATHS: /Library/Developer/Toolchains/swift-latest/usr/lib/swift/macosx/
      OTHER_LDFLAGS:  -lpython 
    

When I build I get these errors / Undefined symbols for architecture x86_64:

Screen Shot 2019-03-19 at 01 01 55

Showing Recent Messages
Ld /Users/johndpope/Library/Developer/Xcode/DerivedData/SwiftTensorflow-aosvcycpbojketffpbxvrqywhhyg/Build/Products/Debug/SwiftTensorflow normal x86_64
cd /Users/johndpope/Documents/mlWorkspace/swift-models/Transformer
export MACOSX_DEPLOYMENT_TARGET=10.13
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -L/Users/johndpope/Library/Developer/Xcode/DerivedData/SwiftTensorflow-aosvcycpbojketffpbxvrqywhhyg/Build/Products/Debug -F/Users/johndpope/Library/Developer/Xcode/DerivedData/SwiftTensorflow-aosvcycpbojketffpbxvrqywhhyg/Build/Products/Debug -F. -F/Library/Developer/Toolchains/swift-latest/usr/lib/swift/macosx -filelist /Users/johndpope/Library/Developer/Xcode/DerivedData/SwiftTensorflow-aosvcycpbojketffpbxvrqywhhyg/Build/Intermediates.noindex/SwiftTensorflow.build/Debug/SwiftTensorflow.build/Objects-normal/x86_64/SwiftTensorflow.LinkFileList -Xlinker -rpath -Xlinker /Library/Developer/Toolchains/swift-latest/usr/lib/swift/macosx/ -mmacosx-version-min=10.13 -Xlinker -object_path_lto -Xlinker /Users/johndpope/Library/Developer/Xcode/DerivedData/SwiftTensorflow-aosvcycpbojketffpbxvrqywhhyg/Build/Intermediates.noindex/SwiftTensorflow.build/Debug/SwiftTensorflow.build/Objects-normal/x86_64/SwiftTensorflow_lto.o -Xlinker -export_dynamic -Xlinker -no_deduplicate -fobjc-link-runtime -L/Library/Developer/Toolchains/swift-tensorflow-RELEASE-0.2.xctoolchain/usr/lib/swift_static/macosx -Xlinker -force_load_swift_libs -lc++ -framework Foundation -Xlinker -add_ast_path -Xlinker /Users/johndpope/Library/Developer/Xcode/DerivedData/SwiftTensorflow-aosvcycpbojketffpbxvrqywhhyg/Build/Intermediates.noindex/SwiftTensorflow.build/Debug/SwiftTensorflow.build/Objects-normal/x86_64/SwiftTensorflow.swiftmodule -lpython /Library/Developer/Toolchains/swift-latest/usr/lib/swift/macosx/libtensorflow_framework.so /Library/Developer/Toolchains/swift-latest/usr/lib/swift/macosx/libtensorflow.so -lpython2.7 -Xlinker -dependency_info -Xlinker /Users/johndpope/Library/Developer/Xcode/DerivedData/SwiftTensorflow-aosvcycpbojketffpbxvrqywhhyg/Build/Intermediates.noindex/SwiftTensorflow.build/Debug/SwiftTensorflow.build/Objects-normal/x86_64/SwiftTensorflow_dependency_info.dat -o /Users/johndpope/Library/Developer/Xcode/DerivedData/SwiftTensorflow-aosvcycpbojketffpbxvrqywhhyg/Build/Products/Debug/SwiftTensorflow

Undefined symbols for architecture x86_64:
"_TFE_AddEagerOpToGraph", referenced from:
TensorFlow.(TraceContext in _F1EB29DDC3964F4495E8056659BC5464).addEagerOpToGraph(Swift.OpaquePointer, Swift.UnsafeMutablePointer<Swift.OpaquePointer?>, Swift.UnsafeMutablePointer<Swift.Int32>, Swift.OpaquePointer) -> () in libswiftTensorFlow.a(TensorFlow.o)
"_TFE_ConsumeInputConcreteTensorFromTraceContext", referenced from:
TensorFlow.(TraceContext in _F1EB29DDC3964F4495E8056659BC5464).execute(traceeInputs: [TensorFlow._AnyTensorHandle]) -> [Swift.OpaquePointer] in libswiftTensorFlow.a(TensorFlow.o)
TensorFlow.(TraceContext in _F1EB29DDC3964F4495E8056659BC5464).specializeTFFunction(with: [Swift.OpaquePointer]) -> Swift.String in libswiftTensorFlow.a(TensorFlow.o)
"_TFE_DeleteTraceContext", referenced from:
TensorFlow.(TraceContext in _F1EB29DDC3964F4495E8056659BC5464).deinit in libswiftTensorFlow.a(TensorFlow.o)
"_TFE_DequeueNamedTensorFromCtx", referenced from:
static TensorFlow.TensorHandle.receiveFromAccelerator(TensorFlow._TensorComputation, Swift.Int) -> TensorFlow.TensorHandle in libswiftTensorFlow.a(TensorFlow.o)
static TensorFlow.ResourceHandle.receiveFromAccelerator(TensorFlow._TensorComputation, Swift.Int) -> TensorFlow.ResourceHandle in libswiftTensorFlow.a(TensorFlow.o)
static TensorFlow.VariantHandle.receiveFromAccelerator(TensorFlow._TensorComputation, Swift.Int) -> TensorFlow.VariantHandle in libswiftTensorFlow.a(TensorFlow.o)
"_TFE_EnqueueNamedTensorFromCtx", referenced from:
TensorFlow.TensorHandle.sendToAccelerator(TensorFlow._TensorComputation, Swift.Int) -> () in libswiftTensorFlow.a(TensorFlow.o)
TensorFlow.ResourceHandle.sendToAccelerator(TensorFlow._TensorComputation, Swift.Int) -> () in libswiftTensorFlow.a(TensorFlow.o)
TensorFlow.VariantHandle.sendToAccelerator(TensorFlow._TensorComputation, Swift.Int) -> () in libswiftTensorFlow.a(TensorFlow.o)
"_TFE_FinalizeInputTensorsFromTraceContext", referenced from:
TensorFlow.(TraceContext in _F1EB29DDC3964F4495E8056659BC5464).finalize(traceeBasicName: Swift.String) -> () in libswiftTensorFlow.a(TensorFlow.o)
"_TFE_GetInputGraphNodeFromTraceContext", referenced from:
TensorFlow.(TraceContext in _F1EB29DDC3964F4495E8056659BC5464).finalize(traceeBasicName: Swift.String) -> () in libswiftTensorFlow.a(TensorFlow.o)
"_TFE_GetServerDef", referenced from:
TensorFlow._ExecutionContext.init() -> TensorFlow._ExecutionContext in libswiftTensorFlow.a(TensorFlow.o)
"_TFE_GetTFOutputFromTensorHandle", referenced from:
TensorFlow.(TraceContext in _F1EB29DDC3964F4495E8056659BC5464).finalize(traceeBasicName: Swift.String) -> () in libswiftTensorFlow.a(TensorFlow.o)
"_TFE_NewTensorHandleFromTFOutput", referenced from:
closure #1 (__C.TF_Output) -> Swift.OpaquePointer in TensorFlow.(_trace in _F1EB29DDC3964F4495E8056659BC5464)(with: [__C.TF_DataType], in: ([Swift.OpaquePointer]) -> [Swift.OpaquePointer]) -> TensorFlow.(TraceContext in _F1EB29DDC3964F4495E8056659BC5464) in libswiftTensorFlow.a(TensorFlow.o)
"_TFE_NewTraceContext", referenced from:
TensorFlow.(TraceContext in _F1EB29DDC3964F4495E8056659BC5464).init(dtypes: [__C.TF_DataType]) -> TensorFlow.(TraceContext in _F1EB29DDC3964F4495E8056659BC5464) in libswiftTensorFlow.a(TensorFlow.o)
"_TFE_OpPrintDebugString", referenced from:
TensorFlow._TFCEagerExecute(Swift.OpaquePointer, Swift.UnsafeMutablePointer<Swift.OpaquePointer?>, Swift.UnsafeMutablePointer<Swift.Int32>, Swift.OpaquePointer) -> () in libswiftTensorFlow.a(TensorFlow.o)
"_TFE_OpSetAttrFunctionName", referenced from:
closure #1 (Swift.UnsafeBufferPointer<Swift.Int8>) -> () in TensorFlow._TFCOpSetAttrFunctionName(Swift.OpaquePointer, Swift.UnsafePointer<Swift.Int8>, Swift.String) -> () in libswiftTensorFlow.a(TensorFlow.o)
"_TFE_TensorHandleCopySharingTensor", referenced from:
TensorFlow.(TraceContext in _F1EB29DDC3964F4495E8056659BC5464).execute(traceeInputs: [TensorFlow._AnyTensorHandle]) -> [Swift.OpaquePointer] in libswiftTensorFlow.a(TensorFlow.o)
(extension in TensorFlow):TensorFlow.TensorArrayProtocol.(cTensorHandles in _F1EB29DDC3964F4495E8056659BC5464).getter : [Swift.OpaquePointer] in libswiftTensorFlow.a(TensorFlow.o)
(extension in TensorFlow):TensorFlow.TensorGroup.init<A where A1: Swift.Collection, A1.Element == Swift.OpaquePointer>(_copying: A1) -> A in libswiftTensorFlow.a(TensorFlow.o)
"_TFE_TensorHandleIsConcrete", referenced from:
TensorFlow.(TraceContext in _F1EB29DDC3964F4495E8056659BC5464).finalize(traceeBasicName: Swift.String) -> () in libswiftTensorFlow.a(TensorFlow.o)
TensorFlow.(TraceContext in _F1EB29DDC3964F4495E8056659BC5464).execute(traceeInputs: [TensorFlow._AnyTensorHandle]) -> [Swift.OpaquePointer] in libswiftTensorFlow.a(TensorFlow.o)
TensorFlow.(TraceContext in _F1EB29DDC3964F4495E8056659BC5464).specializeTFFunction(with: [Swift.OpaquePointer]) -> Swift.String in libswiftTensorFlow.a(TensorFlow.o)
(extension in TensorFlow):TensorFlow.TensorArrayProtocol.(cTensorHandles in _F1EB29DDC3964F4495E8056659BC5464).getter : [Swift.OpaquePointer] in libswiftTensorFlow.a(TensorFlow.o)
TensorFlow.dumpTensorContent(Swift.OpaquePointer, A.Type) -> () in libswiftTensorFlow.a(TensorFlow.o)
(extension in TensorFlow):TensorFlow.ShapedArray.init(cTensorHandle: Swift.OpaquePointer) -> TensorFlow.ShapedArray in libswiftTensorFlow.a(TensorFlow.o)
TensorFlow.dumpCTensorHandleContent(Swift.Int, Swift.OpaquePointer) -> () in libswiftTensorFlow.a(TensorFlow.o)
...
"_TF_FunctionDebugString", referenced from:
TensorFlow.(TraceContext in _F1EB29DDC3964F4495E8056659BC5464).finalize(traceeBasicName: Swift.String) -> () in libswiftTensorFlow.a(TensorFlow.o)
TensorFlow.(TraceContext in _F1EB29DDC3964F4495E8056659BC5464).specializeTFFunction(with: [Swift.OpaquePointer]) -> Swift.String in libswiftTensorFlow.a(TensorFlow.o)
"_TF_MakeInternalErrorStatus", referenced from:
_swift_tfc_CreateIntTensor in libswiftTensorFlow.a(ctensorflow_init.cpp.o)
_swift_tfc_CreateFloatTensor in libswiftTensorFlow.a(ctensorflow_init.cpp.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Translation example

It'd be great to have a neural machine translation model incorporated into our models repository.

Convert trivial models to use 'Sequential'

After #199, there are some other trivial models that should be converted to use Sequential.

struct Net: Layer {

struct Model: Layer {

struct Autoencoder: Layer {

Models other than these are no longer so trivial, so let's please hold off on considering a conversion for them until unit tests are added.

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.