1 of 17

Session 4d

ACM AI + ACM TeachLA

Linear Regression

w/ TensorFlow

2 of 17

while z is not equal to func_min:

#when do we want to stop?

x = x + calculate_step(0.25, calculate_gradient(x)) #calculate our new x based on the gradient at the old x

y = y + calculate_step(0.25, calculate_gradient(y)) #basically the same for y!

z = f(x, y) #now calculate our new z!

Homework Recap

3 of 17

Recap:

ML Math

4 of 17

How do we Multiply Matrices?

  • We can multiply matrices by taking the dot product of their row and column vectors.

x

=

2

2

1

3

6

0

4

3

2

2

8

10

12

12

11

14

5 of 17

Today:

Tensorflow

6 of 17

What is a Computation Graph?

  • A computational graph is a directed graph where the nodes correspond to operations or variables.

7 of 17

IMPORTANT: scalars, row vectors, column vectors, and matrices are ALL tensors. A scalar is a single-entry row vector, which is a 1-by-1 matrix, which is a 2D tensor. For linear regression, we will only use matrices. We will use 3D+ tensors when we get to convolutional neural networks.

8 of 17

Linear Regression Computation Graph

X

Z = W * X

X: n-by-m tensor

W: 1-by-n tensor,

Z: 1-by-m tensor

Ypred = Z + b

b: 1-by-1 tensor (scalar),

Ypred: 1-m tensor

9 of 17

  • It is a math library and is used for machine learning applications such as neural networks.
  • We will use it today to help up set up our computation graphs, multiply matrices, and train our model to optimize our Weights and Biases

What is Tensorflow?

10 of 17

TensorFlow Steps

  1. Make the computation graph with placeholders (values you will feed in) and variables (model parameters) -- this tells TensorFlow what exactly to expect when you train your model or run your session!
  2. Tell TensorFlow what loss or error you want to use
  3. Tell TensorFlow what optimizer you want to use (i.e. how do you want to perform gradient descent?)
  4. Create a session and initialize global variables (these are the variables in your computation graph)
  5. Run your session! Make sure to feed in the appropriate training data

11 of 17

TensorFlow Code Walkthrough

Import Tensorflow library, use name alias so we don’t have to type out whole name

every time

Clears the default graph and resets the global default graph.

Placeholders: values we will feed in when we run our session

12 of 17

TensorFlow Code Walkthrough

Variables: model parameters

Build computation graph

Specify loss metric and optimizer

13 of 17

TensorFlow Code Walkthrough

Print MSE on training set every 50 iterations -- should only decrease!

Train for 1000 iterations by running session 1000 times and using optimizer to adjust W and b so that MSE is minimized

Initialize global variables in computation graph

14 of 17

Homework!

  • Use this dataset, write TensorFlow code to perform linear regression to predict the popularity of a song based on variables of your choosing. Please print out the MSE during training to make sure that your error is decreasing (and that the gradient descent is not diverging).
  • Plot how the MSE changed during training
  • Feel free to email us if you have any doubts/problems!

15 of 17

Submission Instructions

  • In Google Colab, go to “File” → “Download .ipynb” and email it to uclaacmai@gmail.com with the subject having the following format: “Last Name, First Name: Linear Regression Assignment”
  • You can also submit your Python (.py) files if you choose not to use colab.

16 of 17

17 of 17

Thanks!

Fill out our Feedback Form:

Check out our Github

Read our Blog

ucla.acm.ai@gmail.com

ACM AI