Tensorflow基础

我把用Tensorflow搭建网络常见到的一些方法进行了整理。

一、variable_scope

它是一个定义了创建变量的操作的管理器,包含以下参数:

  • name_or_scopestring or VariableScope: the scope to open.
  • default_name: The default name to use if the name_or_scope argument is None, this name will be uniquified. If name_or_scope is provided it won't be used and therefore it is not required and can be None.
  • values: The list of Tensor arguments that are passed to the op function.
  • initializer: default initializer for variables within this scope.
  • regularizer: default regularizer for variables within this scope.
  • caching_device: default caching device for variables within this scope.
  • partitioner: default partitioner for variables within this scope.
  • custom_getter: default custom getter for variables within this scope.
  • reuseTrue, None, or tf.AUTO_REUSE; if True, we go into reuse mode for this scope as well as all sub-scopes; if tf.AUTO_REUSE, we create variables if they do not exist, and return them otherwise; if None, we inherit the parent scope's reuse flag. When eager execution is enabled, this argument is always forced to be tf.AUTO_REUSE.
  • dtype: type of variables created in this scope (defaults to the type in the passed scope, or inherited from parent scope).
  • use_resource: If False, all variables will be regular Variables. If True, experimental ResourceVariables with well-defined semantics will be used instead. Defaults to False (will later change to True). When eager execution is enabled this argument is always forced to be True.
  • constraint: An optional projection function to be applied to the variable after being updated by an Optimizer (e.g. used to implement norm constraints or value constraints for layer weights). The function must take as input the unprojected Tensor representing the value of the variable and return the Tensor for the projected value (which must have the same shape). Constraints are not safe to use when doing asynchronous distributed training.
  • auxiliary_name_scope: If True, we create an auxiliary name scope with the scope. If False, we don't touch name scope.

创建新变量如下:

with tf.variable_scope("foo"):    with tf.variable_scope("bar"):        v = tf.get_variable("v", [1])        assert v.name == "foo/bar/v:0"

将reuse设置为tf.AUTO_REUSE,变量名会被搜索,从而不需要重新建立。

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

二、placeholder

参数包括:
  • dtype: The type of elements in the tensor to be fed.
  • shape: The shape of the tensor to be fed (optional). If the shape is not specified, you can feed a tensor of any shape.
  • name: A name for the operation (optional).
placeholder指定变量内部放置什么类型的元素,以及变量的结构。其本身充当占位符使用,不能直接求值。需要通过Session.run(),Tensor.eval(),Operation.run()中feed_dict参数。
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.

三、tf.nn.rnn_cell

它主要包含以下的类:

class BasicLSTMCell: Basic LSTM recurrent network cell.

class BasicRNNCell: The most basic RNN cell.

class DeviceWrapper: Operator that ensures an RNNCell runs on a particular device.

class DropoutWrapper: Operator adding dropout to inputs and outputs of the given cell.

class GRUCell: Gated Recurrent Unit cell (cf. http://arxiv.org/abs/1406.1078).

class LSTMCell: Long short-term memory unit (LSTM) recurrent network cell.

class LSTMStateTuple: Tuple used by LSTM Cells for state_sizezero_state, and output state.

class MultiRNNCell: RNN cell composed sequentially of multiple simple cells.

class RNNCell: Abstract object representing an RNN cell.

class ResidualWrapper: RNNCell wrapper that ensures cell inputs are added to the outputs.

GRU(Gated Recurrent Unit Cell)的调用有两种形式:

(1)tf.contrib.rnn.GRUCell

(2)tf.nn.rnn_cell.GRUCell

其参数包含:

num_units:GRUcell中的单元数量;

activation:使用的非线性激活函数,默认为tanh

reuse

Kernel_initializer:初始化权重和投影矩阵

bias_initializer:偏差量初始化

name:层的名字,相同名字的层共享权重。(共享利用reuse = true也可以实现)

四、tf.nn.rnn_cell.DropoutWrapper

同上,有两种调用方式。

是对指定层的输入输出进行dropout的操作。

五、tf.nn.rnn_cell.MultiRNNCell

RNN很多简单层顺序结合得到。

包含两个参数:

(1)cells:RNNCells列表

(2)states_is_tuple:如果设置为true,接受和返回状态为n-tuples,n是cells的长度,如果设置为false,状态是列上的联合。

方法之一:zero_state

生成全0状态,有两个参数batch_size、dtype(状态数据类型)

六、tf.concat

包含三个参数:values、axis、name=‘concat’

函数功能:在某一维度上对tensor连接。

比如对于二维张量:

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]] # 一维上连接 由2*3到4*3
tf.concat([t1, t2], 1)  # [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]     # 二维上连接 由2*3到2*6

七、tf.reshape

例子:

t = [1,2,3,4,5,6,7,8,9]reshape(t, [3, 3]) ==> [[1, 2, 3],[4, 5, 6],[7, 8, 9]]

八、tf.reverse 

# 't' is [[[[ 0,  1,  2,  3],
#                  [ 4,  5,  6,  7],
#                  [ 8,  9, 10, 11]],
#                 [[12, 13, 14, 15],
#                  [16, 17, 18, 19],
#                  [20, 21, 22, 23]]]]
# 'dims' is [3] or 'dims' is [-1]reverse(t, dims) ==> [[[[ 3,  2,  1,  0],
                        [ 7,  6,  5,  4],
                       
[ 11, 10, 9, 8]],
                       
[[15, 14, 13, 12],
                       
[19, 18, 17, 16],
                       
[23, 22, 21, 20]]]]

# 'dims' is '[2]' (or 'dims' is '[-2]') (2*3*4)
reverse
(t, dims) ==> [[[[8, 9, 10, 11],
                       
[4, 5, 6, 7],
                       
[0, 1, 2, 3]]
                       
[[20, 21, 22, 23],
                       
[16, 17, 18, 19],
                       
[12, 13, 14, 15]]]]
# 'dims' is '[1]' (or 'dims' is '[-3]')
reverse
(t, dims) ==> [[[[12, 13, 14, 15],
                       
[16, 17, 18, 19],
                       
[20, 21, 22, 23]
                       
[[ 0,  1,  2,  3],
                       
[ 4,  5,  6,  7],
                       
[ 8,  9, 10, 11]]]]

九、tf.matmul,tf.batch_matmul

等价,在之后的tf版本本,tf.batch_matmul被重命名为tf.matmul,表示的是张量的乘积。

十、tf.get_variable

如果已有该变量,则使用,否则创建

例如:

#创建word_embedding向量,并用word_embeddings(预训练词嵌入或者预先定义的词嵌入)进行初始化。

word_embedding = tf.get_variable(initializer=word_embeddings,name = 'word_embedding')








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值