Slide 1

Slide 1 text

Building Artificial Intelligence Units in Rust

Slide 2

Slide 2 text

About Me. 2 I am Vigneshwer Author of Rust CookBook moz://a tech speakers

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Objective 4 Demystifying Artificial Intelligence - Fundamental units for AI algos - Learn the math behind AI Rust is great for building AI - Properties of Rust - Overview of different AI crates Learn to build real world AI applications - Code snippets for Mathematical models - Implementing web services

Slide 5

Slide 5 text

Workshop Overview Hacker’s guide to Rust Programming 1. Ownership & Borrowing Concept 2. Rust tools, library & community support ecosystem 3. Packaging and shipping Rust applications 5 Implementing ML Apps in Rust 1. Building Web services in Rust 2. Training your first ML model in Rust The Math behind Machine Learning 1. Gradient descent optimization technique 2. Vectors & Matrix Operations 3. Neural Networks 4. Hyperparameter Optimization

Slide 6

Slide 6 text

Hacker's guide to Rust Programming

Slide 7

Slide 7 text

What is Rust? System programming language that has great control like C/C++, delivers productivity like in Python, and is super safe

Slide 8

Slide 8 text

What is Rust? “Systems programming without fear

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

where are we with Rust?

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

2016 -17 Rust is the Most Loved Language by Developers

Slide 13

Slide 13 text

Friends of Rust Organizations running Rust in production. (https://www.rust-lang.org/en-US/friends.html)

Slide 14

Slide 14 text

Why should one consider Rust? ➔ State of art programming language ➔ Solves a lot of common system programming bugs ➔ Cargo : Rust Package manager ➔ Improving your toolkit ➔ Self learning ➔ It's FUN ...

Slide 15

Slide 15 text

Basic Terminologies ➔ Low and high level language ➔ System programming ➔ Stack and heap ➔ Concurrency and parallelism ➔ Compile time and run time ➔ Type system ➔ Garbage collector ➔ Mutability ➔ Scope

Slide 16

Slide 16 text

Segmentation Fault ➔ Dereference a null pointer ➔ Try to write to a portion of memory that was marked as read-only

Slide 17

Slide 17 text

Buffer OverFlow ➔ Writing and reading the past end of buffer

Slide 18

Slide 18 text

Sample Error Outputs ➔ Segmentation fault ➔ Buffer OverFlow

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Hack without Fear ➔ Strong type system ◆ Reduces a lot of common bugs ➔ Borrowing and Ownership ◆ Memory safety ◆ Freedom from data races ➔ Abstraction without overhead ➔ Stability without stagnation ➔ Libraries & tools ecosystem

Slide 21

Slide 21 text

Installing Rust Ubuntu / MacOS ● Open your terminal (Ctrl + Alt +T) ● curl -sSf https://static.rust-lang.org/rustup.sh | sh

Slide 22

Slide 22 text

Installing Rust rustc --version cargo --version Windows : ● Go to https://win.rustup.rs/ ○ This will download rustup-init.exe ● Double click and start the installation

Slide 23

Slide 23 text

Features of rustup tool -> Update to latest version: rustup update stable -> Update the rustup tool to the latest version rustup self update -> Install the nightly toolkit version of the Rust compiler: rustup install nightly -> Change the default version of the Rust compiler to nightly version: rustup default nightly

Slide 24

Slide 24 text

Type System

Slide 25

Slide 25 text

Hello World // Execution starts here fn main() { let greet = “world”; println!("Hello {}!”, greet); }

Slide 26

Slide 26 text

Variable Bindings let x = 5; let (x, y) = (1, 2); // patterns let x: i32 = 5; // Type annotations let x = 5; // By default, bindings are immutable. x = 10; let mut x = 5; // mut x: i32 x = 10;

Slide 27

Slide 27 text

Function in Rust fn main() { print_sum(5, 6); } fn print_sum(x: i32, y: i32) { println!("sum is: {}", x + y); }

Slide 28

Slide 28 text

Identify the error fn main() { print_sum(5, 6); } fn print_sum(x , y ) { println!("sum is: {}", x + y); }

Slide 29

Slide 29 text

Returning a value fn add_one(x: i32) -> i32 { x + 1 } fn add_one(x: i32) -> i32 { x + 1; }

Slide 30

Slide 30 text

Expressions vs Statements x = y = 5 let x = (let y = 5); // Expected identifier, found keyword `let`. Rust : Expression -based language

Slide 31

Slide 31 text

Primitive Types

Slide 32

Slide 32 text

bool let bool_val: bool = true; println!("Bool value is {}", bool_val); let bool_val: bool = false;

Slide 33

Slide 33 text

char let x_char: char = 'a'; // Printing the character println!("x char is {}", x_char);

Slide 34

Slide 34 text

i8/i16/i32/i64/isize let num =10; println!("Num is {}", num); let age: i32 =40; println!("Age is {}", age); println!("Max i32 {}",i32::MAX); println!("Max i32 {}",i32::MIN);

Slide 35

Slide 35 text

Arrays let name: [type; size] = [elem1, elem2, elem3, elem4]; let array: [i32; 5] = [0, 1, 2, 3, 4]; let rand_array = [1,2,3]; // Defining an array println!("random array {:?}",rand_array ); println!("random array 1st element {}",rand_array[0] ); // indexing starts with 0 println!("random array length {}",rand_array.len() );

Slide 36

Slide 36 text

Tuples // Declaring a tuple let rand_tuple = ("DevFest Siberia", 2017); let rand_tuple2 : (&str, i8) = ("Viki",4); // tuple operations println!(" Name : {}", rand_tuple2.0); println!(" Lucky no : {}", rand_tuple2.1);

Slide 37

Slide 37 text

slice let array: [i32; 5] = [0, 1, 2, 3, 4]; println!("random array {:?}",&rand_array[0..3] ); // last three elements

Slide 38

Slide 38 text

String let rand_string = "Devfest Yangon 2017"; // declaring a random string println!("length of the string is {}",rand_string.len() ); // printing the length of the string let (first,second) = rand_string.split_at(7); // Splits in string let count = rand_string.chars().count(); // Count using iterator count println!(rand_string)

Slide 39

Slide 39 text

Ownership In Rust, every value has an “owning scope” and passing or returning a value means transferring ownership (“moving” it) to a new scope

Slide 40

Slide 40 text

Example 1 fn foo{ let v = vec![1,2,3]; let x = v; println!(“{:?}”,v); // ERROR : use of moved value: “v” }

Slide 41

Slide 41 text

Example 2 fn print(v : Vec) { println!(“{:?}”, v); } fn make_vec() { let v = vec![1,2,3]; print(v); print(v); // ERROR : use of moved value: “v” }

Slide 42

Slide 42 text

Example 3

Slide 43

Slide 43 text

Aliasing Aliasing -> More than one pointer to the same memory The key problem to most memory problems out there is when mutation and aliasing both happens at the same time. Ownership concepts avoids Aliasing

Slide 44

Slide 44 text

Borrowing If you have access to a value in Rust, you can lend out that access to the functions you call

Slide 45

Slide 45 text

Types of Borrowing There is two type of borrowing in Rust, both the cases aliasing and mutation do not happen simultaneously ● Shared Borrowing (&T) ● Mutable Borrow (&mut T)

Slide 46

Slide 46 text

&mut T fn add_one(v: &mut Vec ) { v.push(1) } fn foo() { let mut v = Vec![1,2,3]; add_one(&mut v); }

Slide 47

Slide 47 text

Lifetimes let outer; { let v = 1; outer = &v; // ERROR: ‘v’ doesn’t live long } println!(“{}”, outer);

Slide 48

Slide 48 text

Mutability Rules

Slide 49

Slide 49 text

Mutability Rules All variables are immutable by default Only one mutable reference at a time But as many immutable &’s as you want Mutable references block all other access The &mut must go out of scope before using other &’s

Slide 50

Slide 50 text

A bit complex example fn avg(list: &[f64]) -> f64 { let mut total = 0; for el in list{ total += *el; } total/list.len() as f64 }

Slide 51

Slide 51 text

HLL version fn avg(list: &[f64]) -> f64 { list.iter().sum::() / list.len() as f64 }

Slide 52

Slide 52 text

Parallel Version (Rayon) fn avg(list: &[f64]) -> f64 { list.par_iter().sum::() / list.len() as f64 }

Slide 53

Slide 53 text

Demos ➔ Vectors, Pointers, closures, typecasting ➔ Complex Data Structures : Structs, enum, impl , trait ➔ Decision making and looping statements ➔ Crates and Modules ➔ Cargo features ➔ Introduction to Rust library ecosystem ➔ Error handling ➔ Understanding Macros ➔ FFI Examples ➔ Building REST API’s

Slide 54

Slide 54 text

Vectors & Std datatypes ➔ Sample arithmetic operations (arithmetic.rs) ➔ Arrays (array.rs) ➔ Assignment operations (assignment.rs) ➔ Boolean types (boolean.rs) ➔ Calculator example (calculator.rs) ➔ Decimal (decimal.rs) ➔ Mutability example (mutuable.rs) ➔ String operations (string.rs) ➔ Tuples (tuple.rs) ➔ Vector (vector.rs)

Slide 55

Slide 55 text

User defined structs, Pointers, closures, typecasting ➔ Closure (closures.rs) ➔ Conditions (condition.rs) ➔ Constants (constant.rs) ➔ Enum types (enum.rs) ➔ block example (expressions.rs) ➔ Looping (looping.rs) ➔ Pointer example (pointers.rs) ➔ struct operations (string.rs) ➔ Methods (implement.rs) ➔ Functionality (trait.rs) ➔ Typecasting (typecasting.rs)

Slide 56

Slide 56 text

Cargo Features ➔ Creating a project ➔ Building & Running a project ➔ Installing dependencies ➔ Deployment of projects ➔ Publishing a package to crates.io ➔ Testing and benchmarking ➔ CI using travis CI

Slide 57

Slide 57 text

Crates and Modules ➔ Defining a module in Rust (sample_mod.rs) ➔ Building a nested module (sample_nested.rs) ➔ Creating a module with struct (sample_struct.rs) ➔ Controlling modules (sample_control.rs) ➔ Accessing modules (sample_access.rs) ➔ Creating a file hierarchy (sample_split.rs) ➔ Building libraries in Rust (sample_lib.rs) ➔ Calling external crates (sample_exec.rs)

Slide 58

Slide 58 text

Deep Dive into Parallelism ➔ Creating a thread in Rust (sample_move.rs) ➔ Spawning multiple threads (sample_multiple_thread.rs) ➔ Holding threads in a vector (sample_thread_expt.rs) ➔ Sharing data between threads using channels (sample_channel.rs) ➔ Implementing safe mutable access (sample_lock.rs) ➔ Creating child processes (sample_child_process.rs) ➔ Waiting for a child process (sample_wait.rs) ➔ Making sequential code parallel (sample_rayon.rs)

Slide 59

Slide 59 text

Efficient Error Handling ➔ Implementing panic (sample_panic.rs) ➔ Implementing Option (sample_option.rs) ➔ Creating map combinator (sample_map.rs) ➔ Creating and_then combinator (sample_and_then.rs) ➔ Creating map for the Result type (sample_map_results.rs) ➔ Implementing aliases (sample_alias.rs) ➔ Handling multiple errors (sample_multi_err.rs) ➔ Implementing early returns (sample_erl_ret.rs) ➔ Implementing the try! Macro (sample_try.rs)

Slide 60

Slide 60 text

Hacking Macros ➔ Building macros in Rust (sample_macro.rs) ➔ Implementing matching in macros (sample_match.rs) ➔ Implementing designators (sample_designator.rs) ➔ Overloading macros (sample_overloading.rs)

Slide 61

Slide 61 text

Integrating Rust with other languages ➔ Calling C operations from Rust ➔ Calling Rust commands from C ➔ Calling Rust operations from Node.js apps ➔ Calling Rust operations from Python ➔ Writing a Python module in Rust

Slide 62

Slide 62 text

Web Development with Rust ➔ Setting up a web server (nickel-demo) ➔ Creating endpoints (nickel-routing) ➔ Handling JSONRequests (nickel-jsonhandling) ➔ Building a custom error handler (nickel-errorhandling) ➔ Hosting templates (nickel-template)

Slide 63

Slide 63 text

RESTful API web service ➔ Setting up the API (sample_app) ➔ Saving user data in MongoDB (sample_post) ➔ Fetching user data (sample_get) ➔ Deleting user data (sample_rest_api)

Slide 64

Slide 64 text

Get /users

Slide 65

Slide 65 text

Post /users/new

Slide 66

Slide 66 text

DELETE /users/object_id

Slide 67

Slide 67 text

Changes to Rust

Slide 68

Slide 68 text

Rust Trailheads Follow us on Twitter: @rustlang @ThisWeekInRust Join Rust IRC Channels: #rust #rust-community #rust-machine-learning #tensorflow-rust Join Rust Websites: rust-lang.org rustbyexample.com rustaceans.org reddit.com/r/rust/ 68

Slide 69

Slide 69 text

The Math behind Machine Intelligence

Slide 70

Slide 70 text

Predictive Maintenance

Slide 71

Slide 71 text

Complex Games

Slide 72

Slide 72 text

Computer Vision

Slide 73

Slide 73 text

Voice Search

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

2.5 x 10^8 Bytes Data created daily

Slide 76

Slide 76 text

90 % of the World’s Data is created 2 years ago! 80% of it is unstructured

Slide 77

Slide 77 text

Fundamental units of AI system Data/ Sensor Mathematical Model + Training Actuator Basic AI System

Slide 78

Slide 78 text

Maths Behind AI Without mathematics, there's nothing you can do. Everything around you is mathematics. Everything around you is numbers. - Shakuntala Devi

Slide 79

Slide 79 text

Classification in General

Slide 80

Slide 80 text

Challenges

Slide 81

Slide 81 text

Neural Networks ➔ Attempt to make a computer model of the brain

Slide 82

Slide 82 text

Learning Process

Slide 83

Slide 83 text

Activation Functions Activation functions add nonlinearity to our network’s function

Slide 84

Slide 84 text

Training Neural networks Loss is a function of the model’s parameters - Aim is to minimize loss and find the best parameters

Slide 85

Slide 85 text

Optimization ➔ Random search ➔ Random Local Search ➔ Following the Gradient

Slide 86

Slide 86 text

SVM vs Softmax

Slide 87

Slide 87 text

Evaluation metrics Accuracy: Overall, how often is the classifier correct? (TP+TN)/total = (100+50)/165 = 0.91 True Positive Rate (Recall) : When it's actually yes, how often does it predict yes? TP/actual yes = 100/105 = 0.95 Precision: When it predicts yes, how often is it correct? TP/predicted yes = 100/110 = 0.91

Slide 88

Slide 88 text

Inspection

Slide 89

Slide 89 text

Summarize ➔ Assign random weights ➔ Calculated the activation rates between the different layers ➔ Find the error rate at output node ➔ Re-calibrate the linkage weights by back propagating ➔ Continue process until convergence criteria

Slide 90

Slide 90 text

Implementing ML Apps in Rust

Slide 91

Slide 91 text

Preparing the Training Dataset

Slide 92

Slide 92 text

Preparing the Labels

Slide 93

Slide 93 text

Building a SVM model in Rustlearn

Slide 94

Slide 94 text

Building a ANN using juggernaut extern crate juggernaut; use juggernaut::nl::NeuralLayer; use juggernaut::nn::NeuralNetwork; use juggernaut::activation::Sigmoid; use juggernaut::sample::Sample; use juggernaut::matrix::MatrixTrait;

Slide 95

Slide 95 text

Creating a sample dataset let dataset = vec![ Sample::new(vec![0f64, 0f64, 1f64], vec![0f64]), Sample::new(vec![0f64, 1f64, 1f64], vec![0f64]), Sample::new(vec![1f64, 0f64, 1f64], vec![1f64]), Sample::new(vec![1f64, 1f64, 1f64], vec![1f64]), ];

Slide 96

Slide 96 text

Creating the ANN architecture let mut test = NeuralNetwork::new(); let sig_activation = Sigmoid::new(); // 1st layer = 2 neurons - 3 inputs test.add_layer(NeuralLayer::new(2, 3, sig_activation)); test.add_layer(NeuralLayer::new(1, 2, sig_activation));

Slide 97

Slide 97 text

Train & Test test.on_error(|err| { println!("error({})", err.to_string()); }); test.train(dataset, 1000, 0.1f64); let think = test.evaluate(&Sample::predict(vec![1f64, 0f64, 1f64])); println!("Evaluate [1, 0, 1] = {:?}", think.get(0, 0));

Slide 98

Slide 98 text

What’s next? - Leaf (https://github.com/autumnai/leaf) - Tensorflow(https://github.com/tensorflow/rust) - Rustml (https://github.com/daniel-e/rustml)

Slide 99

Slide 99 text

What’s next? Collenchyma: Portable, HPC-Framework on any hardware with CUDA, OpenCL, Rust https://github.com/autumnai/collenchyma

Slide 100

Slide 100 text

Thanks! 100 Twitter @dvigneshwer Instagram @dvigneshwer Email [email protected] Website dvigneshwer.github.io/