tf.train.range_input_producer(limit, num_epochs=None, shuffle=True, seed=None, capacity=32, shared_name=None, name=None)
在阅读reader.py中的ptb_producer()函数时看到的这个函数,该函数的功能为生成一个队列,其中为从0到limit-1的整数,然后我想独自运行一下这行代码,发现程序会一直死在这句话这里,不退出也不报错,百度后发现原因是tf的数据线程没有启动,导致数据流图没办法计算,整个程序就卡在那里。
https://blog.csdn.net/lujiandong1/article/details/53373650
这里有更详细的解答,我随后在sess = tf.Session()
后面添加了下面的代码:
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
以及在结尾添加了
coord.request_stop()
coord.join(threads)
sess.close()
但是报了错
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value input_producer/limit_epochs/epochs
随后注意到在该函数的官方注释中写道:
Note: if num_epochs
is not None
, this function creates local counterepochs
. Use local_variables_initializer()
to initialize local variables.
我在开始时添加了sess.run(tf.local_variables_initializer())
就没问题了,程序开始自动生成一些样本。
strided_slice(input_, begin, end, strides=None, …)
在阅读reader.py中的ptb_producer()函数时看到的这个函数,该函数的功能为从input_中生成一个张量切片
x = tf.strided_slice(data, [0, i * num_steps], [batch_size, (i + 1) * num_steps])
x.set_shape([batch_size, num_steps])
y = tf.strided_slice(data, [0, i * num_steps + 1],[batch_size, (i + 1) * num_steps + 1])
y.set_shape([batch_size, num_steps])
如图第一次i=0时x的值即为绿色覆盖的矩阵,y则往后错一格。
tf.device(“/cpu:0”)
该函数用于指定运行设备,tensorflow可以运行在GPU或CPU上,我们把训练数据Tensor存在显存中使用是最好的,但是往往我们的训练数据过大,显存装不下,此时我们就可以将Tensor指定存在内存中,然而将数据从内存写入显存也很费时。
tf.nn.xw_plus_b(x, weights, biases,…)
计算 matmul(x, weights) + biases.
tf.contrib.rnn.BasicLSTMCell()
建立一层基本的LSTM
def __init__(self, num_units, forget_bias=1.0, state_is_tuple=True, activation=None, reuse=None)
初始化参数中只有单元数需要输入,其他都可以默认,reuse参数可以在训练时使用False,在测试时重载checkpoint时使用True
tf.contrib.rnn.DropoutWrapper()
所谓dropout,就是指网络中每个单元在每次有数据流入时以一定的概率(keep prob)正常工作,否则输出0值。这是是一种有效的正则化方法,可以有效防止过拟合。
def __init__(self, cell, input_keep_prob=1.0, output_keep_prob=1.0,
state_keep_prob=1.0, variational_recurrent=False,
input_size=None, dtype=None, seed=None)
初始化时cell需要输入上个类返回的对象,然后在训练时将output_keep_prob改为合适的值,推荐0.5。
tf.contrib.rnn.MultiRNNCell()
这个类用来形成多层RNN结构
def __init__(self, cells, state_is_tuple=True)
初始化时需要输入cells,这里的cells可以是个列表,列表中有几个tf.contrib.rnn.BasicLSTMCell()生成的对象就有几层
随后需要初始化状态矩阵,使用类中的zero_state(self, batch_size, dtype)
函数生成,输入训练时的batch_size和类型可以生成全0的张量。
def call(self, inputs, state)
将类变为一个可调用对象,输入为RNN的输入和上一个时刻的状态,输出为当前的输出和状态
tf.get_variable_scope().reuse_variables()
共享当前scope下的同名变量,在这个程序中用来保证每次输入字符后更新相同的张量的参数
tf.concat()
with tf.variable_scope("RNN"):
for time_step in range(self.num_steps):
if time_step > 0:
tf.get_variable_scope().reuse_variables()
(cell_output, state) = cell(inputs[:, time_step, :], state)
outputs.append(cell_output)
output = tf.reshape(tf.concat(outputs, 1), [-1, config.hidden_size])
这里outputs是一个长度为num_steps的列表,其中每一项为[batch,hidden_size]大小的张量
tf.concat(outputs,1)
这里用来将num_step个[batch,hidden_size]大小的张量变为一个[batch,hidden_size*num_step]的张量,然后经过reshape就变为[batch*num_step,hidden_size]大小的张量
tf.contrib.seq2seq.sequence_loss()
logits,
targets,
weights,
average_across_timesteps=True,
average_across_batch=True,
softmax_loss_function=None,
name=None
上面为函数传入的参数,logits为softmax层输出的张量,targets为实际label值,这里不需要进行one-hot转换,weights用来衡量targets中各字符的权重
average_across_timesteps: If set, divide the returned cost by the total label weight.官方例程将这个值设为了False,如果全设置为True,函数输出为一个标量,将其中一个值设为False则输出为在该方向的一个一维张量。
tf.trainable_variables()
返回的需要训练的变量列表
而tf.all_variables()返回的是所有变量的列表
clip_by_global_norm(t_list, clip_norm, use_norm=None, name=None):
grandient clipping 是用来防止梯度爆炸或梯度消失的手段,是通过限制梯度更新的范围实现的
t_list内容为梯度张量,clip_norm是截取的比率,该函数返回处理后的梯度张量和一个所有张量的全局范数。
具体可以看这里https://blog.csdn.net/u013713117/article/details/56281715
tf.gradients(ys,xs,…)
该函数用来计算梯度,ys和xs必须要相关,ys可以填入loss,xs则可以填入由tf.trainable_variables()返回的变量列表
tf.assign(A, new_number)
将A的值变为new_number