TensorFlow中的基本概念

一个Operation (神经元节点)有零个或多个输入,零个或多个输出。这里的OP可以看作神经元,tensor作为输入的数据

An Operation is a node in a TensorFlow Graph that takes zero or more Tensor objects as input, and produces zero or more Tensor objects as output

Nodes in the graph are called ops (short for operations). 

 

Tensor是一个输出的符号句柄 ,不保存该操作的输出值,而是提供在TensorFlow中计算这些值的方法tf.Session

Tensor is a symbolic handle to one of the outputs of an Operation. It does not hold the values of that operation's output, but instead provides a means of computing those values in a TensorFlow tf.Session.

 

Session对象封装了执行操作对象和计算张量对象的环境

Session object encapsulates the environment in which Operation objects are executed, and Tensor objects are evaluated. 

# Build a graph.
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b

# Launch the graph in a session.
sess = tf.Session()

# Evaluate the tensor `c`.
print(sess.run(c))

 

variable用于维护图执行过程中的状态信息。通常会将一个统计模型中的参数表示为一组变量。 例如, 你可以将一个神经网络的权重作为一个tensor存储在某个变量中。在训练过程中, 通过重复运行训练图,更新这个 tensor。

A variable maintains state in the graph across calls to(调用) run(). You add a variable to the graph by constructing an instance(实例) of the class Variable.

The Variable() constructor requires an initial value for the variable, which can be a Tensor of any type and shape. The initial value defines the type and shape of the variable. After construction, the type and shape of the variable are fixed. The value can be changed using one of the assign methods.

If you want to change the shape of a variable later you have to use an assign Op with validate_shape(验证)=False.

import tensorflow as tf

# Create a variable.
w = tf.Variable(<initial-value>, name=<optional-name>)

# Use the variable in the graph like any Tensor.
y = tf.matmul(w, ...another variable or tensor...)

# The overloaded operators are available too.
z = tf.sigmoid(w + y)

# Assign a new value to the variable with `assign()` or a related method.
w.assign(w + 1.0)
w.assign_add(1.0)

 

constant 创建一个恒定的张量。

Creates a constant tensor.

tf.constant(
    value,
    dtype=None,
    shape=None,
    name='Const',
    verify_shape=False
)

The resulting tensor is populated with values of type dtype, as specified by arguments value and (optionally) shape (see examples below).

# Constant 1-D Tensor populated with value list.
tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) => [1 2 3 4 5 6 7]

# Constant 2-D tensor populated with scalar value -1.
tensor = tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.]
                                             [-1. -1. -1.]]

对于tensor、Constants、Variables三者的理解

tensor存储在Constants或者Variables。就像数据可以放在常量和变量中一样。放在变量中的数据是可以修改的,放在常量中的数据是不可以修改的。

常量op也算是op吧,只是比较简单而已。

posted @ 2018-12-17 16:40  clemente  阅读(192)  评论(0编辑  收藏  举报