Fast.ai Lesson 14 notes — Part 2 v3

Even making two-dimensional matrix calculation to go fast is really hard. In our case, we need to do more dimensions more different kind of operations and that is even harder. There is a compiler doing this and it is called XLA. This only works with graphs which is kind of a problem right now. There are two ways to transfer the normal code into graphs, tracing and staging. These methods although have weird side effects and currently there is research happening around this area to make these work. Probably when this video is published in June there will be a solution for this.

All these things are coming in the future but because we don’t have those yet now we should focus on things we have. C is a fast programming language and next, we will learn how to use it in Swift.

c_interop_examples.ipynb

There are no great audio processing libraries in Swift. There are C libraries that are doing this job well so why not use those.

To use C library Jeremy did the following things: First, he created a directory called SwiftSox, then inside that directory, he created a file called Package.swift that has the following code.

// swift-tools-version:4.2import PackageDescriptionlet package = Package(
name: "SwiftSox",
products: [ .library( name: "SwiftSox", targets: ["SwiftSox"]), ],
targets: [
.target( name: "SwiftSox", dependencies: ["sox"]),
.testTarget( name: "SwiftSoxTests", dependencies: ["SwiftSox"]),
.systemLibrary( name: "sox", pkgConfig: "sox")
]
)

Use this as a base when you create another C library folder because the structure is the same.

Then Jeremy also created the following directories ./Sources/sox and a file called module.modulemap inside sox directory.

module sox [system] {
umbrella header "soxu.h"
link "sox"
export *
}

Again these lines are mostly the same for every C library.

The final thing is soxu.h file inside the same directory than previous.

#include <sox.h>

After all these steps you can just import it in Swift by writing import sox Now you can use all of the functions that are defined in the library.

Jeremy showed techniques to use different languages in Swift. What’s the point of all this? He showed that sometimes using e.g. C library can make the code faster. It’s not just about what we can do twice as fast computing in some things but more like that this opens us new doors that were closed previously. Chris highlighted that this is not something rookies should do because it’s kind of complicated and there is other things you should learn first. He even said that ignoring all this (using other languages) stuff could be a good idea.

Datablock API

08c_data_block_generic.ipynb

There is a couple of problems in the current implementation. Firstly we need to run everything in a particular order. Also if the code misses a step it might cause an error.

The rest of the notebook shows how data block API can be implemented to Swift.

Protocols in Swift

aka interfaces, aka type classes similar to abstract classes

Fully connected model

02_fully_connected.ipynb

Basic Architecture

I didn’t want to just copy the full notebook so go check rest of it to understand how loss and the backward pass is working.

Classes in Swift

Previously we have used a struct that looks like class.

value (struct) vs. reference (class)

Automatic Differentiation in Swift

This is not something we should do every time but in some cases, gradient can be really complex and using approximation can speed up the calculation.

In the end, Jeremy showed how to build image classifier from scratch. There wasn’t any new theory but just using the old things we already should know.

In the end I watched the videos again 1.75x speed on Youtube. That is fast enough to hear what Jeremy is saying but watching takes a lot less time than normal speed. I recommend doing that and going through these notes one more time. Personally I realized how easy some things were and I also saw a little bit better things when I had the structure of these lessons in my mind. That’s my tip to recall everything and really learn these things. I often times find myself learning probably 50% of the things first time but when I recall later I learn probably 70–80%.

This has nothing to do with the course but I just want to add a link to the project I have been working on from early 2019. TrimmedNews is the fastest way to discuss news. You can join or create private groups where users can comment interesting news. I’m using a lot of deep learning almost everywhere and one cool feature is summarized news. I realized how much time I spend reading news that aren’t something I expected. With this app, I can find interesting news much more easily than just reading the headlines.

lesson 8 notes
lesson 9 notes
lesson 10 notes
lesson 11 notes
lesson 12 notes
lesson 13 notes
lesson 14 notes

~Lankinen

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store