简体   繁体   English

TensorFlow:BasicRNNCell()从哪里获得权重和偏差?

[英]TensorFlow: Where does BasicRNNCell() get weights and biases from?

I don't really understand how TensorFlow's tf.contrib.rnn.BasicRNNCell works. 我真的不太明白TensorFlow的tf.contrib.rnn.BasicRNNCell是如何工作的。
We defined a dict of weights, which should be used in different places in the graph, but I don't get how I can tell the command above where it needs which weight, or in general how I can tell it to use especially said dict as weights. 我们定义了权重的字典,应该在图中的不同位置使用它,但是我不知道如何在上面的命令中告诉它需要权重的地方,或者总的来说,我如何告诉它使用特别是字典的字典作为重量。
Can anyone elaborate this? 有人可以详细说明吗?

When you call tf.contrib.rnn.BasicRNNCell , say, 当您调用tf.contrib.rnn.BasicRNNCell ,说,

cell = tf.contrib.rnn.BasicRNNCell(num_units=100)

its weights (in tensorflow it's called kernel ) and bias will not be created. 它的权重(在tensorflow中被称为kernel )和偏倚不会被创建。 At this point cell only knows the number of neurons it's gonna create and how to activate afterward. 在这一点上, cell只知道它将创建的神经元数量以及随后如何激活。 (Logically, you need to tell cell the input size to create its weights variable.) (逻辑上,您需要告诉cell输入大小以创建其weights变量。)

Then you probably will do: 然后,您可能会做:

rnn = tf.nn.dynamic_rnn(cell, rnn_inputs, dtype=tf.float32)

During tf.nn.dynamic_rnn , 2 important things will happen sequentially: tf.nn.dynamic_rnn期间,将依次发生2件重要的事情:

  1. cell.build will be called. cell.build将被调用。 Since input is specified, cell will create its weights (kernel) and bias. 由于指定了输入, cell将创建其权重(内核)和偏差。 After that you can access to them using cell._kernel and cell._bias . 之后,您可以使用cell._kernelcell._bias访问它们。 The corresponding nodes will be added to tensorflow's computation graph, but not linked to other nodes yet. 相应的节点将被添加到tensorflow的计算图中,但尚未链接到其他节点。

  2. cell.call will be called. cell.call将被调用。 The weight and bias nodes created in 1 are linked to some newly nodes to produce the RNN result. 在1中创建的权重和偏差节点链接到一些新节点以产生RNN结果。

So literally you don't need to tell the command how to do things exactly. 因此,从字面上看,您无需告诉命令确切的操作方法。 What you need to do is to use TensorFlow APIs in an appropriate way and most of the implementation details will be taken very good care of. 您需要做的是以适当的方式使用TensorFlow API,并且大多数实现细节都将得到很好的照顾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM