a

Lorem ipsum dolor sit amet, consectetur adicing elit ut ullamcorper. leo, eget euismod orci. Cum sociis natoque penati bus et magnis dis.Proin gravida nibh vel velit auctor aliquet. Leo, eget euismod orci. Cum sociis natoque penati bus et magnis dis.Proin gravida nibh vel velit auctor aliquet.

  /  Project   /  Blog: Fast.ai Lesson 14 notes — Part 2 v3

Blog: Fast.ai Lesson 14 notes — Part 2 v3


These notes are from Part 2 v3 lessons which are not released yet. Some of the material might not make sense without the notebooks and therefore I don’t recommend to read these if you are not in the course. I tried to add all things that weren’t clear from v2 or that didn’t exist then.

This is what we will cover today.
Last time we saw this video.

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.2
import PackageDescription
let 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.

This is the basic structure.
This is how we can use it with ImageNette.

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

Protocol defines behavior of structs
Like when we use Layer with Dense we need to define the call
This way we can implement isOdd to all different integers because they use BinaryInteger protocol.
This shows how the idea of protocols is to make them group things.

Fully connected model

02_fully_connected.ipynb

A neat trick that makes it plausible to create own names for the types -> less typing NOT recommended to use in codes that other people read. Might be confusing.
We can use these things just like in Python.
Tests.

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)

As we can see the values are things that are copied to a new variable but the references are something that is kept somewhere else and there is only reference to it that is also copied to a new variable. When we change a new variable that has value it doesn’t change the original but if we change the reference value, the original will change too.
Copying is a solution that kind of works. One big problem is that this makes the code slower.
In Swift, this works automatically.
Some cases you might want to use the reference version which happens this way.
This shows how struct works.
This shows how class works.

Automatic Differentiation in Swift

This is what we want to build. It’s calculating gradients.
This is true because of chain rule.
Using previous codes we can build function that calculates the forward pass and backward pass for MSE.
This generalize better for different tasks
We can use our knowledge to calculate gradients for basic neural network.

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.

~Lankinen

Source: Artificial Intelligence on Medium

(Visited 34 times, 1 visits today)
Post a Comment

Newsletter