什么是Tensorflow?
- Tensorflow是google发布的深度学习开源框架
- Tensorflow提供了定义张量的函数,而且可以自动的计算导数
TensorFlow VS. Numpy
- Few people make this comparison, but TensorFlow and
Numpy are quite similar. (Both are N-d array libraries!) - Numpy has Ndarray support, but doesn’t offer methods to
create tensor functions and automatically compute
derivatives (+ no GPU support).
Simple Numpy Recap
In [23]: import numpy as np
In [24]: a = np.zeros((2,2)); b = np.ones((2,2))
In [25]: np.sum(b, axis=1)
Out[25]: array([ 2., 2.])
In [26]: a.shape
Out[26]: (2, 2)
In [27]: np.reshape(a, (1,4))
Out[27]: array([[ 0., 0., 0., 0.]])
Repeat in TensorFlow
In [31]: import tensorflow as tf
In [32]: tf.InteractiveSession() ##More on Session
soon
In [33]: a = tf.zeros((2,2)); b = tf.ones((2,2))
In [34]: tf.reduce_sum(b, reduction_indices=1).eval()
Out[34]: array([ 2., 2.], dtype=float32)
In [35]: a.get_shape() ##TensorShape behaves
like a python tuple.
Out[35]: TensorShape([Dimension(2), Dimension(2)])
In [36]: tf.reshape(a, (1, 4)).eval()
Out[36]: array([[ 0., 0., 0., 0.]], dtype=float32)
● tf.InteractiveSession() is just convenient syntactic sugar for keeping a default session open in ipython.
● sess.run(c) is an example of a TensorFlow Fetch. Will say more on this soon
Tensorflow Computation Graph
- TensorFlow programs are usually structured into a construction phase, that assembles a graph, and an execution phase that uses a session to execute ops in the graph.
- All computations add nodes to global default graph
Tensorflow Variables
-“When you train a model you use variables to hold and update parameters. Variables are in-memory buffers containing tensors
- All tensors we’ve used previously have been constant tensors, not variables
Updating Variable State
Fetch Variable State
Inputing Data
Variable Scope
Understanding get_variable()
Tensorflow实战:线性回归
计算流图如下所示: