CMPM147
Generative Design
TensorFlow.js
Lucas N. Ferreira
University of California, Santa Cruz
Summer 19
UC Santa Cruz
Announcements
Last lecture we talked about:
Upcoming deadlines:
Assignment 5: Due this Saturday
UC Santa Cruz
2
TensorFlow.js
Motivation
Neural Networks are essentially matrices:
2x1
1x1
Input
Output
Wih
2x2
Who
1x2
UC Santa Cruz
4
Motivation
Feedforwarding to calculate an input y requires:
Backpropagation to adjust the weights of a NN requires:
UC Santa Cruz
5
TensorFlow
TensorFlow is a free and open-source library that offers three main features (among many others):
UC Santa Cruz
6
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:
UC Santa Cruz
7
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
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
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
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
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
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
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
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
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
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
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
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
References
TensorFlow
UC Santa Cruz
20