在实现“快速权重”时在自定义层中手动更新矩阵的问题

时间:2019-10-17 13:42:24

标签: tensorflow tensorflow2.0

我真的是Tensorflow的新手,但是我的最终目标是实现Tensorflow 2.0中“使用快速权重参加最新的过去”中描述的体系结构。 我首先从Github复制了SimpleRNNCell,我的想法是简单地将这个SimpleRNN(Layer)扩展到我需要的内容中。此扩展不会花费很多精力,因为它仅包含
a)在创建单元(带有零)时初始化矩阵,

b)在call()函数中手动更新此矩阵。 (这可以通过简单地构建循环图层激活的输出乘积并将其添加到相关矩阵中来完成),

c)将其添加到正常的储层活动中。

我的问题是,当尝试手动更新此矩阵时,出现以下错误:

TypeError: An op outside of the function building code is being passed
a "Graph" tensor. It is possible to have Graph tensors
leak out of the function building context by including a
tf.init_scope in your function building code.
For example, the following function will fail:
  @tf.function
  def has_init_scope():
    my_constant = tf.constant(1.)
    with tf.init_scope():
      added = my_constant * 2
The graph tensor has name: add_1

我尝试的东西基本上可以归结为初始化的矩阵类型。我尝试将其初始化为 a)不可训练的重量(self.add_weight()) b)常数(tensorflow.python.keras.backend.constant()) c)张量(tensorflow.python.keras.backend.variable()

  • 矩阵的初始化:
self.A = K.variable(value=tf.zeros((self.units, self.units)),
        dtype='float32', name='fast_weights')
  • 手动更新
self.A = self.l * self.A + self.e * K.dot(K.transpose(h), h)

,其中h是隐藏的循环层的激活。这是引发错误的地方。

  • 添加更新步骤:
h = self.activation(K.bias_add(K.dot(inputs, self.kernel), self.bias) + K.dot(h, self.recurrent_kernel)) + K.dot(h, self.A)

其中K.dot(h, self.A)是我添加的。

我有(并且希望)感觉到我缺少明显的东西。预先谢谢你。

1 个答案:

答案 0 :(得分:0)

您可以使用旧的tf.assign来解决此问题。

self.A = tf.compat.v1.assign(self.A, self.l * self.A + self.e * K.dot(K.transpose(h), h))