2

我想知道如果在下面的代码的两个堆叠单元的权共享:Tensorflow RNN细胞重共享

cell = rnn_cell.GRUCell(hidden_dim) 
stacked_cell = tf.nn.rnn_cell.MultiRNNCell([cell] * 2) 

如果他们不共享,如何强制任何RNNs共享?

注意: 我可能更希望在嵌套的输入到输出连接的RNN配置中共享权重,其中第一层针对第二层的每个输入被克隆多次(例如,第一层代表字母和第二层代表遍历第一层的输出聚集字)

回答

5

你可以看到,权重不通过执行以下脚本共享:

import tensorflow as tf 

with tf.variable_scope("scope1") as vs: 
    cell = tf.nn.rnn_cell.GRUCell(10) 
    stacked_cell = tf.nn.rnn_cell.MultiRNNCell([cell] * 2) 
    stacked_cell(tf.Variable(np.zeros((100, 100), dtype=np.float32), name="moo"), tf.Variable(np.zeros((100, 100), dtype=np.float32), "bla")) 
    # Retrieve just the LSTM variables. 
    vars = [v.name for v in tf.all_variables() 
        if v.name.startswith(vs.name)] 
    print vars 

你会看到,除了虚拟变量返回两套GRU的权重:那些“Cell1”和那些“Cell0 ”。

为了让他们共享,可以实现从GRUCell继承并始终始终使用相同的变量范围的方式重新使用权自己的电池类:

import tensorflow as tf 

class SharedGRUCell(tf.nn.rnn_cell.GRUCell): 
    def __init__(self, num_units, input_size=None, activation=tf.nn.tanh): 
     tf.nn.rnn_cell.GRUCell.__init__(self, num_units, input_size, activation) 
     self.my_scope = None 

    def __call__(self, a, b): 
     if self.my_scope == None: 
      self.my_scope = tf.get_variable_scope() 
     else: 
      self.my_scope.reuse_variables() 
     return tf.nn.rnn_cell.GRUCell.__call__(self, a, b, self.my_scope) 

with tf.variable_scope("scope2") as vs: 
    cell = SharedGRUCell(10) 
    stacked_cell = tf.nn.rnn_cell.MultiRNNCell([cell] * 2) 
    stacked_cell(tf.Variable(np.zeros((20, 10), dtype=np.float32), name="moo"), tf.Variable(np.zeros((20, 10), dtype=np.float32), "bla")) 
    # Retrieve just the LSTM variables. 
    vars = [v.name for v in tf.all_variables() 
        if v.name.startswith(vs.name)] 
    print vars 

这样两者之间的变量GRUCells是共享的。请注意,您需要小心形状,因为同一个单元需要同时处理原始输入和输出。