tf.stack和tf.unstack
解析:
tf.stack
stack(values, axis=0, name='stack')
#假如'x'为[1, 4],'y'为[2, 5],'z'为[3, 6],
#那么:
tf.stack([x, y, z]) => [[1, 4], [2, 5], [3,6]],
tf.stack([x, y, z], axis=1) => [[1, 2, 3], [4, 5, 6]]。
tf.stack([x, y, z]) = np.asarray([x, y, z])。
tf.unstack
unstack(value, num=None, axis=0, name='unstack')
tf.unstack(x, n) = list(x)。
举个例子,如下所示:
import tensorflow as tf
a = tf.constant([1,2,3])
b = tf.constant([4,5,6])
c = tf.stack([a,b],axis=1)
d = tf.unstack(c,axis=0)
e = tf.unstack(c,axis=1)
print(c.get_shape())
with tf.Session() as sess:
print(sess.run(c))
print(sess.run(d))
print(sess.run(e))
# 结果输出,如下所示:
#[[1 4][2 5][3 6]]
#[array([1, 4], dtype=int32), array([2, 5], dtype=int32), #array([3, 6], dtype=int32)]
#[array([1, 2, 3], dtype=int32), array([4, 5, 6], dtype=int32)]
tf.split
split(value, num_or_size_splits, axis=0, num=None, name='split')
假设value的shape为[5, 30],如下所示:
(1)如果split0, split1, split2 = tf.split(value, [4, 15, 11], 1),那么
tf.shape(split0) ==> [5, 4]
tf.shape(split1) ==> [5, 15]
tf.shape(split2) ==> [5, 11]
(2)如果split0, split1, split2 = tf.split(value, num_or_size_splits=3, axis=1),那么
tf.shape(split0) ==> [5, 10]
举个例子,如下所示:
import tensorflow as tf
A = [[1, 2, 3], [4, 5, 6]]
x0 = tf.split(A, num_or_size_splits=3, axis=1)
x1 = tf.unstack(A, num=3, axis=1)
x2 = tf.split(A, num_or_size_splits=2, axis=0)
x3 = tf.unstack(A, num=2, axis=0)
with tf.Session() as sess:
print(sess.run(x0))
print(sess.run(x1))
print(sess.run(x2))
print(sess.run(x3))
# 结果输出,如下所示:
[array([[1],[4]]), array([[2],[5]]), array([[3],[6]])]
[array([1, 4]), array([2, 5]), array([3, 6])]
[array([[1, 2, 3]]), array([[4, 5, 6]])]
[array([1, 2, 3]), array([4, 5, 6])]
# 说明:axis=0表示按行操作,axis=1表示按列操作。
tf.nn.rnn_cell.BasicLSTMCell和tf.contrib.rnn.BasicLSTMCell
state_is_tuple
解析:如果state_is_tuple=True,那么state是元组形式,state=(c,h)。如果state_is_tuple=False,那么state是一个由
c和h拼接起来的张量,state=tf.concat(1,[c,h])。
tf.subtract
解析:
subtract(x, y, name=None)
如下所示:
(1)x: A Tensor. Must be one of the following types: half, float32, float64, int32, int64, complex64, complex128.
(2)y: A Tensor. Must have the same type as x.
(3)name: A name for the operation (optional).
tf.multiply和tf.matmul区别
解析:
(1)tf.multiply是点乘,即Returns x * y element-wise.
(2)tf.matmul是矩阵乘法,即Multiplies matrix a by matrix b, producing a * b.
tf.contrib.layers.xavier_initializer
解析:xavier_initializer(uniform=True, seed=None, dtype=tf.float32)
:An initializer for a weight matrix.
1.uniform
: Whether to use uniform or normal distributed random initialization.
2.seed
: A Python integer. Used to create random seeds. See tf.set_random_seed for behavior.
3.dtype
: The data type. Only floating point types are supported.
tf.concat
解析:
concat(values, axis, name='concat')
假设t1 = [[1, 2, 3], [4, 5, 6]],t2 = [[7, 8, 9], [10, 11, 12]],那么
tf.concat([t1, t2], 0) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],tf.concat([t1, t2], 1) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]
dynamic_rnn与static_rnn [1]
解析:可以分别静态和动态构建RNN模型,如下所示:
rnn_outputs, final_state = tf.nn.static_rnn(cell, rnn_inputs, initial_state=init_state)
rnn_outputs, final_state = tf.nn.dynamic_rnn(cell, rnn_inputs, initial_state=init_state)
两者的区别,如下所示:
使用static_rnn时,输入为一个列表,len(list)=num_step,list[0].shape=[batch_size, input_feature],输出为一个列表,len(list)=num_step,list[0].shape=[batch_size, num_hidden]。
使用dynamic_rnn时,输入为[batch_size, num_step, input_feature]的三维Tensor,输出为[batch_size, num_step, num_hidden]的三维Tensor。
说明:dynamic_rnn可以让不同迭代传入的batch是长度不同的数据,但同一次迭代一个batch内部的所有数据长度是固定的。但是,static_rnn不能这样,它要求每一时刻传入的数据为[batch_size, max_seq],并且在每次迭代过程中都保持不变。
tf.get_default_graph和tf.reset_default_graph
解析:
tf.get_default_graph()
:返回返回当前线程的默认图。
tf.reset_default_graph()
:清除默认图的堆栈,并设置全局图为默认图。
双曲正切函数(tanh)
解析:
f(z)=tanh(z)=ez−e−zez+e−z f ( z ) = tanh ( z ) = e z − e − z e z + e − z
说明:tanh值域的范围为(-1,1)。
TensorFlow中的常量、序列、随机数
解析:
tf.ones([2, 3], int32) ==> [[1, 1, 1], [1, 1, 1]]
tf.zeros([2, 3], int32) ==> [[0, 0, 0], [0, 0, 0]]
假设tensor为[[1, 2, 3], [4, 5, 6]],那么
tf.ones_like(tensor) ==> [[1, 1, 1], [1, 1, 1]]
假设tensor为[[1, 2, 3], [4, 5, 6]],那么
tf.zeros_like(tensor) ==> [[0, 0, 0], [0, 0, 0]]
tf.fill([2, 3], 9) ==> [[9, 9, 9], [9, 9, 9]]
tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.] [-1. -1. -1.]
tf.linspace(10.0, 12.0, 3, name="linspace") => [ 10.0 11.0 12.0]
tf.range(start,limit=None,delta=1,name='range')
:返回一个tensor等差数列,该tensor中的数值在start到limit之间,不包括limit,delta是等差数列的差值。
tf.random_normal(shape,mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name=None)
:返回一个tensor其`中的元素的值服从正态分布。
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
:返回一个`tensor其中的元素服从截断正态分布。
tf.random_uniform(shape,minval=0,maxval=None,dtype=tf.float32,seed=None,name=None)
:返回一个形状`为shape的tensor,其中的元素服从minval和maxval之间的均匀分布。
tf.random_shuffle(value,seed=None,name=None)
:对value(是一个tensor)的第一维进行随机化。比如,
[[1,2], [2,3],[3,4]] ==> [[2,3], [1,2], [3,4]]
。
tf.set_random_seed(seed)
:设置产生随机数的种子。
softmax(logits, dim=-1, name=None)
解析:
logits
: A non-empty Tensor. Must be one of the following types: half, float32, float64.
dim
: The dimension softmax would be performed on. The default is -1 which indicates the last dimension.
name
: A name for the operation (optional).
tf.shape(x)和x.get_shape()区别
解析:
都可以得到tensor x的尺寸。
tf.shape()中x数据类型可以是tensor,list,array,而x.get_shape()中x的数据类型只能是tensor,且返回的是一个元组。
tf.slice
解析:slice(input_, begin, size, name=None)
:Extracts a slice from a tensor.
假设input为[[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 6, 6]]],如下所示:
(1)tf.slice(input, [1, 0, 0], [1, 1, 3]) ==> [[[3, 3, 3]]]
(2)tf.slice(input, [1, 0, 0], [1, 2, 3]) ==> [[[3, 3, 3], [4, 4, 4]]]
(3)tf.slice(input, [1, 0, 0], [2, 1, 3]) ==> [[[3, 3, 3]], [[5, 5, 5]]]
- FLAGS = tf.app.flags.FLAGS
解析:
(1)tf.app.flags.DEFINE_string()
(2)tf.app.flags.DEFINE_integer()
(3)tf.app.flags.DEFINE_boolean()
(4)tf.app.flags.DEFINE_integer()
说明:TensorFlow通过设置flags来传递tf.app.run()所需的参数。
tf.get_default_graph()
解析:得到默认的图。
Graph.as_default()
解析:with tf.Graph().as_default():…:使用Graph.as_default()上下文管理器可以覆盖默认的图。
多个Session和Graph
解析:
import tensorflow as tf
g1 = tf.Graph()
with g1.as_default():
c1 = tf.constant([1.0])
with tf.Graph().as_default() as g2:
c2 = tf.constant([2.0])
with tf.Session(graph=g1) as sess1:
print sess1.run(c1)
with tf.Session(graph=g2) as sess2:
print sess2.run(c2)