Please excuse me and redirect me to the appropriate forum to ask this question if necessary.
My question concerns the swift-4-tensorflow framework.
I recently started reading David Foster's "Generative Deep Learning" book. I want to follow the examples, but I prefer swift over Python, and additionaly I prefer running my code in xcode instead of jupyter notebook.
I successfully converted his first notebook example python code to Swift using the "TensorFlow" swift library and I can run it in xcode. However, when training the network the training speed is dramatically slower (more than 20x) than when running the same example in notebook/python.
Any idea why? I'm pretty sure in both cases the training is only using the CPU.
EDIT: I do get the following warning when running in xcode right before the training starts:
020-03-07 14:03:32.859407: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
It's hard to debug performance without more information. Could you share your Python and Swift code?
It sounds like you're comparing the performance of Swift for TensorFlow vs Keras in Python? S4TF and Keras have different programming models, so it might not be an apples-to-apples comparison.
Swift for TensorFlow toolchains are currently built without these CPU extensions for maximum compatibility with different CPUs. We could consider building with some of these extensions by default though, especially on macOS which is more homogeneous.
Also: you'll have more visibility asking questions on the Swift for TensorFlow mailing list!
The Swift for TensorFlow team will definitely see your questions there.
In particular, the last macOS that supported hardware without SSE4.1 and 4.2 was 10.13, so those seem pretty safe for S4TF. We should also investigate better support for building fat Swift libraries with x86_64h support, which would let you use F16C, AVX2 and FMA while still having a fallback in the x86_64 slice.
I haven't tried running or profiling your code, but I imagine calling Python (C APIs) via Swift has overhead compared with directly using CPython: calling Python via Swift involves at least running additional Swift code.
Minimizing Python API calls (e.g. by moving Python code from Swift to Python, and importing the resulting Python code) should reduce some overheads.
But really, I think you should try learning Swift if you have time and interest! Here's a Swift ML model training walkthrough that shows you how to define and train a model in Swift. Models written in Swift are more idiomatic and should be more concise and performant.
The library here (TensorFlow) is actually a C++ library rather than a Swift library, which, specifically for macOS I suppose would be possible to build in a fat configuration (although may become computationally expensive at ~4hrs per slice).
My fear was, reading everywhere that Keras is essentialy a high level API of tensorflow, that I would be having to go to low-level in code, while Keras has all the benefits to offer for someone just starting out with deep learning.
But maybe I'm wrong and the same thing can be done with (S4-)TensorfFlow?
Stil it would be nice to see some official Keras integration to swfit in the future imho, as most resources I have come across use Keras.
Thanks for the info. I will try to convert all the Python code to Swift then (i use swift on a daily basis since I am primarily an iOS developer, so maybe the effort makes sense :p)
Are there also examples available focused on S4TF in a macOS xcode project environment?
Most of them are assuming using swift in the command line or with Notebooks.
The reason I am asking is because I want to build a user-friendly macOS app with a UI so my client does not have to deal with commands etc.
git clone https://github.com/tensorflow/swift-models.git
cd swift-models
swift package generate-xcodeproj
open TensorFlowModels.xcodeproj # opens Xcode