我试图将Cudnn_GRU
的初始值设定项形式从tf.Variable
转换为tf.get_variable
,但我一直得到这个错误。我必须进行转换,因为tensorflow不允许初始化循环/控制流函数,只允许lambda初始化器或通过tf.get_variable
我将问题简化为以下最小示例:
import tensorflow as tf
e = tf.random_uniform_initializer(-0.1, 0.1)
i = tf.constant(0)
def func():
gru_fw = tf.contrib.cudnn_rnn.CudnnGRU(num_layers=1, num_units=75, input_size=25)
# original line: commented out and working if not under a control flow mechanism
# param_fw = tf.Variable(tf.random_uniform([gru_fw.params_size()], -0.1, 0.1), validate_shape=False)
# converted line
param_fw = tf.get_variable("abcd", shape=[gru_fw.params_size()],initializer=e, validate_shape=False)
return param_fw
def func2():
### repeat the same thing from func1
pass
result = tf.cond(tf.equal(i, tf.constant(0)),func,func2)
回溯如下:
^{pr2}$
问题似乎是
gru_fw.params_size()
返回的是Tensor("strided_slice_1:0", shape=(), dtype=int32)
,而不是它显然应该返回的int。tf.get_variable
不接受张量作为shape
参数。您的tf.Variable
代码运行,但它生成一个形状为<unknown>
的变量,当您尝试使用它时,可能会导致问题。在不幸的是,我没有找到很多关于如何正确创建和使用
CudnnGRU
对象的文档。你是在听什么教程吗?另外,您正在使用什么版本的TensorFlow?在相关问题 更多 >
编程相关推荐