【问题标题】:internal variables in BasicRNNCellBasicRNNCell 中的内部变量
【发布时间】:2017-12-25 02:23:37
【问题描述】:

我有以下示例代码来测试BasicRNNCell。我想得到它的内部矩阵,这样我就可以使用我自己的代码计算output_resnewstate_res 的值,以确保我可以重现output_resnewstate_res 的值。

在 tensorflow 源代码中,它显示为 output = new_state = act(W * input + U * state + B)。有谁知道我如何获得WU? (我尝试访问cell._kernel,但无法访问。)

$ cat ./main.py
#!/usr/bin/env python
# vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8:

import tensorflow as tf
import numpy as np

batch_size = 4
vector_size = 3

inputs = tf.placeholder(
        tf.float32
        , [batch_size, vector_size]
        )

num_units = 2
state = tf.zeros([batch_size, num_units], tf.float32)

cell = tf.contrib.rnn.BasicRNNCell(num_units=num_units)
output, newstate = cell(inputs = inputs, state = state)

X = np.zeros([batch_size, vector_size])
#X = np.ones([batch_size, vector_size])
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    output_res, newstate_res = sess.run([output, newstate], feed_dict = {inputs: X})
    print(output_res)
    print(newstate_res)
sess.close()

$ ./main.py
[[ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]]
[[ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]]

【问题讨论】:

标签: python tensorflow rnn


【解决方案1】:

简答:你知道你在追cell._kernel。下面是一些使用大多数 TensorFlow RNN 中的 variables 属性获取内核(和偏差)的代码:

import tensorflow as tf
import numpy as np

batch_size = 4
vector_size = 3
inputs = tf.placeholder(tf.float32, [batch_size, vector_size])

num_units = 2
state = tf.zeros([batch_size, num_units], tf.float32)

cell = tf.contrib.rnn.BasicRNNCell(num_units=num_units)
output, newstate = cell(inputs=inputs, state=state)

print("Output of cell.variables is a list of Tensors:")
print(cell.variables)
kernel, bias = cell.variables

X = np.zeros([batch_size, vector_size])
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    output_, newstate_, k_, b_ = sess.run(
        [output, newstate, kernel, bias], feed_dict = {inputs: X})
    print("Output:")
    print(output_)
    print("New State == Output:")
    print(newstate_)
    print("\nKernel:")
    print(k_)
    print("\nBias:")
    print(b_)

输出

Output of cell.variables is a list of Tensors:
[<tf.Variable 'basic_rnn_cell/kernel:0' shape=(5, 2) dtype=float32_ref>, 
<tf.Variable 'basic_rnn_cell/bias:0' shape=(2,) dtype=float32_ref>]
Output:
[[ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]]
New State == Output:
[[ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]]

Kernel:
[[ 0.41417515 -0.64997244]
 [-0.40868729 -0.90995187]
 [ 0.62134564 -0.88962835]
 [-0.35878009 -0.25680023]
 [ 0.35606658 -0.83596271]]

Bias:
[ 0.  0.]

长答案:你也问如何得到W和U。让我复制call的实现并讨论W和U在哪里。

def call(self, inputs, state):
     """Most basic RNN: output = new_state = act(W * input + U * state + B)."""

    gate_inputs = math_ops.matmul(
        array_ops.concat([inputs, state], 1), self._kernel)
    gate_inputs = nn_ops.bias_add(gate_inputs, self._bias)
    output = self._activation(gate_inputs)
    return output, output

看起来没有 W 和 U,但它们确实存在。本质上,内核的前vector_size 行是W,内核的下num_units 行是U。也许看看LaTeX 中的元素数学会有所帮助:

我使用 m 作为通用批次索引,vvector_sizennum_units,以及 b 为batch_size。还有 [ ; ] 表示连接。由于 TensorFlow 以批处理为主,因此实现通常使用右乘矩阵。

因为这是一个非常基本的 RNN,output == new_state。下一次迭代的“历史”只是当前迭代的输出。

【讨论】:

  • 没用的sess.close()?
猜你喜欢
  • 2019-07-08
  • 2018-04-12
  • 1970-01-01
  • 2017-07-11
  • 1970-01-01
  • 2016-04-03
  • 1970-01-01
  • 2017-11-04
  • 2022-11-27
相关资源
最近更新 更多