tensorFlow官网学习

来源:互联网 发布:qt matlab混合编程 编辑:程序博客网 时间:2024/05/29 12:47

TensorFlow 编程基础知识:

Tensors:

The central unit of data in TensorFlow is the tensor【张量】. A tensor consists of a set of primitive values(原始值) shaped into an array【数组】 of any number of dimensions. A tensor's rank is its number of dimensions. 
3 # a rank 0 tensor; this is a scalar with shape []
[1., 2., 3.] # a rank 1 tensor; this is a vector with shape [3]
[[1., 2., 3.], [4., 5., 6.]] # a rank 2 tensor; a matrix with shape [2, 3]
[[[1., 2., 3.]], [[7., 8., 9.]]] # a rank 3 tensor with shape [2, 1, 3]

TensorFlow Core【核】 tutorial【学习指南】

Importing TensorFlow

The canonical import statement for TensorFlow programs is as follows:

import tensorflow as tf

This gives Python access to all of TensorFlow's classes, methods, and symbols.


The Computational Graph

You might think of TensorFlow Core programs as consisting of two discrete sections:

  1. Building the computational graph.
  2. Running the computational graph.

computational graph is a series of TensorFlow operations arranged into a graph of nodes

Let's build a simple computational graph. Each node takes zero or more tensors as inputs and produces a tensor as an output.
 
One type of node is a constant. Like all TensorFlow constants, it takes no inputs, and it outputs a value it stores internally【内部】.

We can create two floating point Tensors node1 and node2 as follows:

node1 = tf.constant(3.0, dtype=tf.float32)
node2
= tf.constant(4.0) # also tf.float32 implicitly【隐式地】
print(node1, node2)

The final print statement produces

Tensor("Const:0", shape=(), dtype=float32) Tensor("Const_1:0", shape=(), dtype=float32)
Notice that printing the nodes does not output the values 3.0 and 4.0 as you might expect. 
Instead, they are nodes that, when evaluated, would produce 3.0 and 4.0, respectively.
To actually evaluate the nodes, we must run the computational graph within a session.
A session encapsulates the control and state of the TensorFlow runtime.
The following code creates a Session object and then invokes its run method to run enough of the computational graph to evaluate node1 and node2
By running the computational graph in a session as follows:
sess = tf.Session()
print(sess.run([node1, node2]))

we see the expected values of 3.0 and 4.0:

[3.0, 4.0]
We can build more complicated computations by combining Tensor nodes with operations (Operations are also nodes).
For example, we can add our two constant nodes and produce a new graph as follows:
node3 = tf.add(node1, node2)
print("node3:", node3)
print("sess.run(node3):", sess.run(node3))
The last two print statements produce
node3: Tensor("Add:0", shape=(), dtype=float32)
sess
.run(node3): 7.0
TensorFlow provides a utility called TensorBoard that can display a picture of the computational graph. Here is a screenshot showing how TensorBoard visualizes the graph:
先往下走。。。tensorboard回头再看
As it stands, this graph is not especially interesting because it always produces a constant result. 
 A graph can be parameterized to accept external inputs, known as placeholders. 
placeholder is a promise to provide a value later.
a = tf.placeholder(tf.float32)
b
= tf.placeholder(tf.float32)
adder_node
= a + b  # + provides a shortcut for tf.add(a, b)
The preceding three lines are a bit like a function or a lambda in which we define two input parameters (a and b) and then an operation on them.
We can evaluate this graph with multiple inputs by using the feed_dict argument to the run method to feed(提供) concrete(具体的) values to the placeholders:
In TensorBoard, the graph looks like this:



We can make the computational graph more complex by adding another operation. For example,

add_and_triple = adder_node * 3.
print(sess.run(add_and_triple, {a: 3, b: 4.5}))

produces the output

22.5
The preceding computational 【前向计算图】graph would look as follows in TensorBoard:


In machine learning we will typically want a model that can take arbitrary【随机的】 inputs, such as the one above. 

To make the model trainable, we need to be able to modify the graph【主要指的是权值和偏差】 to get new outputs with the same input.

 Variables allow us to add trainable parameters to a graph. They are constructed with (初始值)a type and initial

 value:

W = tf.Variable([.3], dtype=tf.float32)
b
= tf.Variable([-.3], dtype=tf.float32)
x
= tf.placeholder(tf.float32)
linear_model
= W * x + b
Constants are initialized when you call tf.constant, and their value can never change.

By contrast, variables are not initialized when you call tf.Variable

To initialize all the variables in a TensorFlow program, you must explicitly【显式地】 call a special operation【操作符】 as follows:

init = tf.global_variables_initializer()   //在老版本中,是tf.initialize_all_variable(
sess.run(init)
It is important to realize init is a handle to the TensorFlow sub-graph that initializes all the global variables
Until we call sess.run, the variables are uninitialized.

Since x is a placeholder, we can evaluate linear_model for several values of xsimultaneously[同时地] as follows:

print(sess.run(linear_model, {x: [1, 2, 3, 4]}))

to produce the output

[ 0.          0.30000001  0.60000002  0.90000004]
We've created a model, but we don't know how good it is yet.To evaluate the model on training data, we need a y placeholder to provide the desired values, and we need to write a loss function.A loss function measures how far apart the current model is from the provided data. We'll use a standard loss model forlinear regression, which sums the squares of the deltas【标准差】 between the current model and the provided data.

linear_model - y creates a vector where each element is the corresponding example's error delta. 

We call tf.square to square that error. Then, we sum all the squared errors to create asingle scalar that abstracts the error of all examples using tf.reduce_sum:

producing the loss value

23.66
We could improve this manually by reassigning the values of W and b to the perfect values of -1 and 1. A variable is initialized to the value provided to tf.Variable but can be changed using operations like tf.assign. For example, W=-1 and b=1 are the optimal parameters for our model. We can change W and b accordingly:

fixW = tf.assign(W, [-1.])
fixb
= tf.assign(b, [1.])
sess
.run([fixW, fixb])
print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))

The final print shows the loss now is zero.

0.0
We guessed【猜测】 the "perfect" values of W and b, but the whole point【全部的意义,重心】 of machine learning is to find the correct model parameters automatically. We will show how to accomplish this in the next section.

tf.train API

A complete discussion of machine learning is out of the scope of this tutorial. However, TensorFlow provides optimizers【优化器】 thatslowly change each variablein order to minimize the loss function

The simplest optimizer is gradient descent It modifies each variable according to the magnitude of the derivativeof loss【损失函数的导数】 with respect to that variable. In general, computing symbolic derivatives manually is tedious【冗长乏味】 and error-prone【容易出错】. Consequently, TensorFlow can automatically produce derivatives given only a description of the model using the function tf.gradients. For simplicity, optimizers typically do this for you. For example,

optimizer = tf.train.GradientDescentOptimizer(0.01)
train
= optimizer.minimize(loss)
sess.run(init) # reset values to incorrect defaults.
for i in range(1000):
  sess
.run(train, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]})

print(sess.run([W, b]))

results in the final model parameters:

[array([-0.9999969], dtype=float32), array([ 0.99999082],
 dtype
=float32)]
Now we have doneactual machine learning! Although doing this simple linear regression doesn't require much TensorFlow core code, more complicated models and methods to feed data into your model necessitate【需要】 more code. Thus TensorFlow provides higher level abstractions【更高级的抽象】 for common patterns, structures, and functionality【功能】. We will learn how to use some of these abstractions in the next section.


Complete program

The completed trainable linear regression model is shown here:

import tensorflow as tf

# Model parameters
W
= tf.Variable([.3], dtype=tf.float32)
b
= tf.Variable([-.3], dtype=tf.float32)
# Model input and output
x
= tf.placeholder(tf.float32)
linear_model
= W * x + b
y
= tf.placeholder(tf.float32)

# loss
loss
= tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
# optimizer
optimizer
= tf.train.GradientDescentOptimizer(0.01)
train
= optimizer.minimize(loss)

# training data
x_train
= [1, 2, 3, 4]
y_train
= [0, -1, -2, -3]
# training loop
init
= tf.global_variables_initializer()
sess
= tf.Session()
sess
.run(init) # reset values to wrong
for i in range(1000):
  sess
.run(train, {x: x_train, y: y_train})

# evaluate training accuracy
curr_W
, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))

When run, it produces

W: [-0.9999969] b: [ 0.99999082] loss: 5.69997e-11

Notice that the loss is a very small number (very close to zero). If you run this program, your loss may not be the exact same because the model is initialized with pseudorandom【伪随机】 values.

This more complicated program can still be visualized in TensorBoard 






原创粉丝点击