1 of 20

CMPM147

Generative Design

TensorFlow.js

Lucas N. Ferreira

University of California, Santa Cruz

Summer 19

UC Santa Cruz

2 of 20

Announcements

Last lecture we talked about:

  • Multilayer Perceptron

Upcoming deadlines:

Assignment 5: Due this Saturday

UC Santa Cruz

2

3 of 20

TensorFlow.js

4 of 20

Motivation

Neural Networks are essentially matrices:

2x1

1x1

Input

Output

Wih

2x2

Who

1x2

UC Santa Cruz

4

5 of 20

Motivation

Feedforwarding to calculate an input y requires:

  • Matrix multiplication

Backpropagation to adjust the weights of a NN requires:

  • Matrix multiplication
  • Matrix transposition
  • Several elementwise operations: add, sub, hadamard, etc.
  • Calculating the gradient of a error function in respect to every weight matrix W.

UC Santa Cruz

5

6 of 20

TensorFlow

TensorFlow is a free and open-source library that offers three main features (among many others):

  • A complete set of functions to operate with multidimensional matrices (tensors)
  • A set of functions for differentiable calculus
  • A very optimized way of performing these functions using GPUs

UC Santa Cruz

6

7 of 20

TensorFlow

TensorFlow core library is written with a combination of highly-optimized C++ and CUDA (Nvidia's language for programming GPUs).

TensorFlow has bindings/ports for many languages:

  • Python (mainly used for industrial/scientific applications)
  • Javascript (mainly used for artistic applications)
  • Java
  • Go
  • Swift

UC Santa Cruz

7

8 of 20

TensorFlow.js

TensorFlow.js is a javascript library that implements all the major features of the original TensorFlow.

It is a port and not a binding!

UC Santa Cruz

8

9 of 20

Brief History

TensorFlow was developed by the Google Brain team for internal Google use.

It was released under the Apache License 2.0 in November 2015.

In March 2018, Google announced TensorFlow.js

10 of 20

Tensor: The building blocks of NNs

Tensors are the core data structure of TensorFlow. They are a generalization of vectors and matrices to potentially higher dimensions.

1, 2, 3

1 2 3

4 5 6

7 8 9

1

Scalar

Vector

Matrix

Tensor

UC Santa Cruz

10

11 of 20

Tensor

The central unit of data in TensorFlow is the tf.Tensor: a set of values shaped into an array of one or more dimensions:

https://js.tensorflow.org/api/latest/#Tensors

UC Santa Cruz

11

12 of 20

Tensor Operations

While tensors allow you to store data, operations allow you to manipulate that data:

https://js.tensorflow.org/api/latest/#Operations

UC Santa Cruz

12

13 of 20

Tensor Operations

While tensors allow you to store data, operations allow you to manipulate that data:

https://js.tensorflow.org/api/latest/#Operations

tf.Tensor is immutable! So every operation returns a new tf.Tensor.

UC Santa Cruz

13

14 of 20

TensorFlow Memory Management

TensorFlow was designed to take advantage of the GPU for processing tensor operations faster, thus:

Reading data from tensors is asynchronous:

https://www.tensorflow.org/js/guide/tensors_operations#getting_values_from_a_tensor

Memory must be managed explicitly:

https://www.tensorflow.org/js/guide/tensors_operations#memory

UC Santa Cruz

14

15 of 20

Changing data in Tensors

tf.Tensor is immutable! If you need to change the values of a tensor you need to create a tf.Variable:

https://js.tensorflow.org/api/latest/#variable

UC Santa Cruz

15

16 of 20

Creating a Feedforward Network

In TensorFlow, a feedforward network (e.g. Multilayer Perceptron) can be created with the tf.Sequential class:

https://www.tensorflow.org/js/guide/models_and_layers#creating_models_with_the_layers_api

A tf.Sequential instance can have different types of layers, for example tf.dense:

https://js.tensorflow.org/api/latest/#layers.dense

UC Santa Cruz

16

17 of 20

Creating a Feedforward Neural Network

After defining the architecture of a NN, you have to compile it with an optimizer and loss function, for example:

Gradient Descent Optimizer with " meanSquaredError" loss function:

https://js.tensorflow.org/api/latest/#train.sgd

Compiling a sequential NN:

https://js.tensorflow.org/api/latest/#class:Sequential

UC Santa Cruz

17

18 of 20

Training a Neural Network

After compiling the architecture of a NN, you can train it with a dataset using the function fit:

https://js.tensorflow.org/api/latest/#tf.LayersModel.fit

UC Santa Cruz

18

19 of 20

Making Predictions with a Neural Network

After training the NN, you can run predictions with the function predict:

https://js.tensorflow.org/api/latest/#tf.GraphModel.predict

UC Santa Cruz

19

20 of 20

References

TensorFlow

UC Santa Cruz

20