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

时间:2018-02-12 15:10:36

标签: python tensorflow

我正在尝试将tf.Variable的初始化形式转换为tf.get_variable Cudnn_GRU,但我一直收到此错误。我必须转换因为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)

回溯如下:

Traceback (most recent call last):
    File "test_run_error.py", line 16, in <module>
        result = tf.cond(tf.equal(i, tf.constant(0)),func,func2)
    File "/home/search/snetP/snet/lib/python3.5/site-packages/tensorflow/python/util/deprecation.py", line 316, in new_func
        return func(*args, **kwargs)
    File "/home/search/snetP/snet/lib/python3.5/site-packages/tensorflow/python/ops/control_flow_ops.py", line 1855, in cond
        orig_res_t, res_t = context_t.BuildCondBranch(true_fn)
    File "/home/search/snetP/snet/lib/python3.5/site-packages/tensorflow/python/ops/control_flow_ops.py", line 1725, in BuildCondBranch
        original_result = fn()
    File "test_run_error.py", line 9, in func
        param_fw = tf.get_variable("abcd", shape=[gru_fw.params_size()],initializer=e, validate_shape=False)
    File "/home/search/snetP/snet/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 1203, in get_variable
        constraint=constraint)
    File "/home/search/snetP/snet/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 1092, in get_variable
        constraint=constraint)
    File "/home/search/snetP/snet/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 425, in get_variable
        constraint=constraint)
    File "/home/search/snetP/snet/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 394, in _true_getter
        use_resource=use_resource, constraint=constraint)
    File "/home/search/snetP/snet/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 730, in _get_single_variable
        shape = tensor_shape.as_shape(shape)
    File "/home/search/snetP/snet/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py", line 849, in as_shape
        return TensorShape(shape)
    File "/home/search/snetP/snet/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py", line 455, in __init__
        self._dims = [as_dimension(d) for d in dims_iter]
    File "/home/search/snetP/snet/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py", line 455, in <listcomp>
        self._dims = [as_dimension(d) for d in dims_iter]
    File "/home/search/snetP/snet/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py", line 397, in as_dimension
        return Dimension(value)
    File "/home/search/snetP/snet/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py", line 32, in __init__
        self._value = int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Tensor'

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

此方法对我有用,请尝试一下。

layers1= tf.compat.v1.layers.dense(tf_x, 10, tf.nn.relu)
output= tf.compat.v1.layers.dense(layers1, 1)