A Quick Start
Yychi Fyu @SIST, ShanghaiTech
Computation Graph: The structure of TF.
|
|
Tensor("add_5:0", shape=(1, 1), dtype=int32)
The above codes “describe” a computation graph:
How can we actually see a tensor
?
To evaluate a tensor, we need
tf.Session()
|
|
[[16]]
This is so-called lazy-evaluation!
Keep in mind:
tensor
is most fundamental object in TF. Almost all TF operations return the reference of tensor
.
Create constant tensor
|
|
Tensor("Const_15:0", shape=(), dtype=int32)
Tensor("Const_16:0", shape=(2,), dtype=int32)
Tensor("Const_17:0", shape=(2, 1), dtype=int32)
Mind the shape!
Interactive tensor evaluation
|
|
|
|
Tensor addition and scaling
|
|
|
|
|
|
Tensor data types
|
|
Tensor shapes!
|
|
[0 1 2 3 4 5 6 7]
[[0 1 2 3 4 5 6 7]]
[[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]]
[[0 1]
[2 3]
[4 5]
[6 7]]
Any computation in TF is represented as an instance of a tf.Graph
object.
A tf.Session()
object stores the context under which a computation is performed.
Use an explicit context
|
|
Here comes tf.Variable()
.
Creating a variable is easy enough.
|
|
What about to evaluate the variable a
?
|
|
As we said before, TF code just describes computation, a variable should be initialized before using it.
How to use a variable? Just initialize it!
|
|
A variable is mutable, and statful, we can assign a new value to it!
Assigning values to variables
|
|
The shape should match!
Since we already have variables, why do we need placeholders?
A placeholder is a way to input information into a TF computation graph.
*"Think of placeholders as the input nodes through which information enters TF."*
tf.Tensor
$\mapsto$ np.ndarray
|
|
Linear Regression
Use synthetic toy data for the regression task. The data is generated by $$ y = wx + b + \epsilon, $$ where $\epsilon \sim N(0,1)$.
Generate the data
|
|
Plot the data
Generate TF graph
|
|
Perform training
|
|
Thank you!