笔记:TensorFlow 的各种 out-of-the-box API。
1. tf.variable_scope
。规范命名
在Tensorflow 中,需要有 Graph 的观点。创建一个变量意味着往图中增加一个节点,也即要区分创建的变量是属于哪一层。 tf.variable_scope
允许你创建并共享已创建的变量。官方例子:
with tf.variable_scope("foo"):
with tf.variable_scope("bar"):
v = tf.get_variable("v", [1])
assert v.name == "foo/bar/v:0"
还可以重新进入:
with tf.variable_scope("foo") as vs:
pass
# Re-enter the variable scope.
with tf.variable_scope(vs, auxiliary_name_scope=False) as vs1:
# Restore the original name_scope.
with tf.name_scope(vs1.original_name_scope):
v = tf.get_variable("v", [1])
assert v.name == "foo/v:0"
c = tf.constant([1], name="c")
assert c.name == "foo/c:0"
以及共享功能:
def foo():
with tf.variable_scope("foo", reuse=tf.AUTO_REUSE):
v = tf.get_variable("v", [1])
return v
v1 = foo() # Creates v.
v2 = foo() # Gets the same, existing v.
assert v1 == v2
补充:
tf.placeholder() 占位符。* trainable==False *
tf.Variable() 一般变量用这种方式定义。 * 可以选择 trainable 类型 *
tf.get_variable() 一般都是和 tf.variable_scope() 配合使用,从而实现变量共享的功能。 * 可以选择 trainable 类型 *
tf.trainable_variables(), 将我们定义的所有的 trainable=True 的所有变量以一个list的形式返回
- Data formats refers to the structure of the Tensor passed to a given
op
. Tensorflow 的数据格式是4维张量:
- N refers to the number of images in a batch.
- H refers to the number of pixels in the vertical (height) dimension.
- W refers to the number of pixels in the horizontal (width) dimension.
- C refers to the channels. For example, 1 for black and white or grayscale and 3 for RGB.
Within TensorFlow there are two naming conventions representing the two most common data formats:
NCHW
orchannels_first
NHWC
orchannels_last
NHWC is the TensorFlow default and NCHW is the optimal format to use when training on NVIDIA GPUs using cuDNN.
3. tf.transpose
置换/共轭:
tf.transpose(
a,
perm=None,
name='transpose',
conjugate=False
)
例子:
x = tf.constant([[1, 2, 3], [4, 5, 6]])
tf.transpose(x) # [[1, 4]
# [2, 5]
# [3, 6]]
# Equivalently
tf.transpose(x, perm=[1, 0]) # [[1, 4]
# [2, 5]
# [3, 6]]
# If x is complex, setting conjugate=True gives the conjugate transpose
x = tf.constant([[1 + 1j, 2 + 2j, 3 + 3j],
[4 + 4j, 5 + 5j, 6 + 6j]])
tf.transpose(x, conjugate=True) # [[1 - 1j, 4 - 4j],
# [2 - 2j, 5 - 5j],
# [3 - 3j, 6 - 6j]]
# 'perm' is more useful for n-dimensional tensors, for n > 2
x = tf.constant([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]])
# Take the transpose of the matrices in dimension-0
# (this common operation has a shorthand `linalg.transpose`)
tf.transpose(x, perm=[0, 2, 1]) # [[[1, 4],
# [2, 5],
# [3, 6]],
# [[7, 10],
# [8, 11],
# [9, 12]]]
4. tf.layers.conv2d
创建卷积核。
tf.layers.conv2d(
inputs,
filters,
kernel_size,
strides=(1, 1),
padding='valid',
data_format='channels_last',
dilation_rate=(1, 1),
activation=None,
use_bias=True,
kernel_initializer=None,
bias_initializer=tf.zeros_initializer(),
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
trainable=True,
name=None,
reuse=None
)
5. tf.identity
新增一个节点(op 操作)。
Return a tensor with the same shape and contents as input.
tf.identity(
input,
name=None
)
有一个经典的例子:
没有使用 identify 时:
x = tf.Variable(0.0)
x_plus_1 = tf.assign_add(x, 1)
with tf.control_dependencies([x_plus_1]):
y = x
init = tf.initialize_all_variables()
with tf.Session() as session:
init.run()
for i in xrange(5):
print(y.eval())
输出:
2018-01-06 17:44:49.511158: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.2 AVX AVX2 FMA
0.0
0.0
0.0
0.0
0.0
有 identify 时候:
x = tf.Variable(0.0)
x_plus_1 = tf.assign_add(x, 1)
with tf.control_dependencies([x_plus_1]):
y = tf.identity(x)
init = tf.initialize_all_variables()
with tf.Session() as session:
init.run()
for i in xrange(5):
print(y.eval())
输出 1,2,3,4,5.
6 tf.control_dependencies
: 控制管理器。
Wrapper for Graph.control_dependencies() using the default graph.
tf.control_dependencies(control_inputs)
Args:
control_inputs
: A list of Operation or Tensor objects which must be executed or computed before running the operations defined in the context. Can also be None to clear the control dependencies. If eager execution is enabled, any callable object in the control_inputs list will be called.
7 tf.summary.scalar
日志打印
tf.summary.scalar(
name,
tensor,
collections=None,
family=None
)
Outputs a Summary protocol buffer containing a single scalar value.
The generated Summary has a Tensor.proto containing the input Tensor.
用法:
tf.summary.scalar('train_accuracy', accuracy)
则会打印出如下的日志:
INFO:tensorflow:cross_entropy = 2.6340933, learning_rate = 0.025, train_accuracy = 0
8 tf.no_op(name=None)
不做操作,仅作为占位符
Does nothing. Only useful as a placeholder for control edges.
Args:
name
: A name for the operation (optional).
Returns:
The created Operation.
9 tf.constant()
创建常量张量
tf.constant(
value,
dtype=None,
shape=None,
name='Const',
verify_shape=False
)
Creates a constant tensor.