【发布时间】:2017-12-25 02:23:37
【问题描述】:
我有以下示例代码来测试BasicRNNCell。我想得到它的内部矩阵,这样我就可以使用我自己的代码计算output_res、newstate_res 的值,以确保我可以重现output_res、newstate_res 的值。
在 tensorflow 源代码中,它显示为 output = new_state = act(W * input + U * state + B)。有谁知道我如何获得W 和U? (我尝试访问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.]]
【问题讨论】:
-
您可能正在寻找
cell.variablesproperty。 This 答案可能有用。如果这不是你要找的,你能链接源代码吗?或者解释一下W和U是什么?
标签: python tensorflow rnn