一个 TensorFlow 的运算可以看作是一个数据流图
一个图呢则由一组操作和数据集组成
操作 (operation) 代表运算单元
数据(tensor) 代表在各运算单元流动的数据单元
要想使用一个数据流图, 必须把它注册为默认的图
注意: 图这个 class 并不是线程安全的, 它所有的方法也不是
将一个图设为默认的图的方法:
- g = tf.Graph()
- with g.as_default(): # 将图设为默认
- # define operations and tensors in g
- c = tf.constant(30.0)
- as_default(self)
返回一个将 Graph 设为默认的 context manager 如果你想要在一个进程中创建多个图考虑使用如果你不显示调用, 一个全局默认图将被提供
默认图所属当前线程, 如果你创建了一个新的线程, 你必须在那个线程中显示调用 as_default 方法
创建常量 tensor
- constant(value, dtype=None, shape=None, name='Const', verify_shape=False)
- '''
- Args:
- value: A constant value (or list) of output type `dtype`.
- dtype: The type of the elements of the resulting tensor.
- shape: Optional dimensions of resulting tensor.
- name: Optional name for the tensor.
- verify_shape: Boolean that enables verification of a shape of values.
- Returns:
- A Constant Tensor.
- Raises:
- TypeError: if shape is incorrectly specified or unsupported.
- '''
传入 value 创建常量 tensorvalue 可以是一个常量值或常量列表 参数 shape 是可选的, 如果不提供默认为传入 value 的 shape
同理, dtype 也是可选的, 不提供则默认为传入 value 的 dtype
- ```python
- # 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
w = tf.Variable(<initial-value>, name=<optional-name>)
一个变量维持图的状态 变量在定义的时候要声明它的类型和 shape, 在创建之后虽然值是可以改变的, 但类型和 shape 是固定的
和其他 tensor 一样, 被创建的变量可以被图中的操作当作输入使用
当你启用已经定义好的图时, 所有变量必须显示的被初始化, 之后你才可以运行操作使用他们的值你也可以从文件中读取完成初始化步骤
创建 placeholder
placeholder(dtype, shape=None, name=None)
给一个 tensor 插入 placeholder, 并且会在之后补充进去
通过 `Session.run()` 的备选参数 `feed_dict` 将之前的 placeholder 的值补进去, 如果你没有喂进任何数据就运行会话对象将报错
- ```python
- x = tf.placeholder(tf.float32, shape=(1024, 1024))
- y = tf.matmul(x, x)
- with tf.Session() as sess:
- print(sess.run(y)) # ERROR: will fail because x was not fed.
- rand_array = np.random.rand(1024, 1024)
- print(sess.run(y, feed_dict={x: rand_array})) # Will succeed.
- ```
计算 logits tensor
logits = tf.matmul(tf_train_dataset, weights) + biases
计算 softmax cross entropy,loss tensor
- loss = tf.reduce_mean(
- tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits))
- softmax_cross_entropy_with_logits_v2(_sentinel=None, labels=None, logits=None, dim=-1, name=None)
- Args:
- _sentinel: Used to prevent positional parameters. Internal, do not use.
- labels: Each row `labels[i]` must be a valid probability distribution.
- logits: Unscaled log probabilities.
- dim: The class dimension. Defaulted to -1 which is the last dimension.
- name: A name for the operation (optional).
- Returns:
- A 1-D `Tensor` of length `batch_size` of the same type as `logits` with the
- softmax cross entropy loss.
计算 probability errors 对于每一个想要预测的内容, 它必须有且只有一个标签
参数为 logits 和 labels, 这个函数内部会自动处理 soft max
softmax: 将 score 转化成 probability
- def softmax(x):
- """Compute softmax values for each sets of scores in x."""
- return np.exp(x) / np.sum(np.exp(x), axis = 0)
one-hot-encoding: 正确的 label 标为 1, 其他均为 0. 比如有 a,b,c 三个 label,a 为正确的 label 标为 1,b 和 c 标为 0
cross-entropy(用来衡量 performance):
计算所有样本的综合 loss:
优化处理 Optimizer: Gradient Descent Optimizer, 定义 operation
使用梯度下降法找到最小的 loss 这个 operation 会更新涉及到的 tensor 的值
- class GradientDescentOptimizer(tensorflow.python.training.optimizer.Optimizer)
- __init__(self, learning_rate, use_locking=False, name='GradientDescent')
- Construct a new gradient descent optimizer.
- Args:
- learning_rate: A Tensor or a floating point value. The learning rate to use.
- minimize(self, loss, global_step=None, var_list=None, gate_gradients=1,
- aggregation_method=None, colocate_gradients_with_ops=False, name=None,
- grad_loss=None)
- Add operations to minimize `loss` by updating `var_list`.
- This method simply combines calls `compute_gradients()` and `apply_gradients()`.
- If you want to process the gradient before applying them call `compute_gradients()` and `apply_gradients()` explicitly instead of using this function.
- Args:
- loss: A `Tensor` containing the value to minimize.
- global_step: Optional `Variable` to increment by one after the variables have been updated.
- Returns:
- An Operation that updates the variables in `var_list`. If `global_step` was not `None`, that operation also increments `global_step`.
- Raises:
- ValueError: If some of the variables are not `Variable` objects.
什么是梯度下降?
误差方程 (Cost Function). 用来计算预测出来的和我们实际中的值有多大差别. 在预测数值的问题中. W 是我们神经网络中的参数, 假设我们初始化的 W 在一个位置. 而这个位置的斜率也就是梯度下降中的梯度. loss 误差最小的时候正是这条曲线最低的地方, 不过 W 却不知道这件事情, 他目前所知道的就是梯度线为自己在这个位置指出的一个下降方向, 我们就要朝着这个梯度的方向下降一点点. 在做一条切线, 发现我还能下降, 那我就朝着梯度的方向继续下降, 这时, 再展示出现在的梯度, 因为梯度线已经躺平了, 我们已经指不出哪边是下降的方向了, 所以这时我们就找到了 W 参数的最理想值. 简而言之, 就是找到梯度线躺平的点.
Session(会话)
用来运行 tensorflow operation 的类
会话对象封装了一个供 ops 类执行并且可以评估 tensor 的环境
会话类会拥有自己的资源, 为确保在使用后这些资源被释放, 可以使用 context manager
- # Using the context manager.
- with tf.Session() as sess:
- sess.run(...)
在初始化创建一个会话对象的时候, 如果没有指明图那么一个默认的图会被启用如果你拥有多个图, 那么你需要为每一个图初始化一个会话对象
在会话类中, 首先要初始化所有变量使用 global_variables_initializer()返回一个 op 并运行来初始化
- global_variables_initializer()
- Returns an Op that initializes global variables.
- Returns:
- An Op that initializes global variables in the graph.
来源: https://www.cnblogs.com/duwenlu/p/8621223.html