TypeError:int()参数必须是字符串、类似字节的对象或数字,而不是“Tensor”

2024-09-23 04:24:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将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}$

Tags: sizegetparamtftensorflowdefrandomuniform
1条回答
网友
1楼 · 发布于 2024-09-23 04:24:18

问题似乎是gru_fw.params_size()返回的是Tensor("strided_slice_1:0", shape=(), dtype=int32),而不是它显然应该返回的int。tf.get_variable不接受张量作为shape参数。您的tf.Variable代码运行,但它生成一个形状为<unknown>的变量,当您尝试使用它时,可能会导致问题。在

不幸的是,我没有找到很多关于如何正确创建和使用CudnnGRU对象的文档。你是在听什么教程吗?另外,您正在使用什么版本的TensorFlow?在

相关问题 更多 >