5

I'm trying to convert a the initializer form from tf.Variable to tf.get_variable for Cudnn_GRU but I keep getting this error. I have to convert because tensorflow does not allow initializing in loop/control-flow functions and only allow lambda initializers or through tf.get_variable

I have reduced the problem into the following minimal example:

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)

The traceback is as follows:

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
  • 1
    Could you include the full traceback resulting from running this code? Commented Feb 12, 2018 at 15:14
  • @glibdud Done and added! Commented Feb 12, 2018 at 15:29

2 Answers 2

2

The problem seems to be that gru_fw.params_size() is returning Tensor("strided_slice_1:0", shape=(), dtype=int32) instead of the int it's apparently supposed to return. tf.get_variable won't accept a tensor as the shape argument. Your tf.Variable code runs but it produces a variable with a shape of <unknown>, which would likely cause problems when you try to use it.

Unfortunately I'm not finding a lot of documentation on how to properly create and use a CudnnGRU object. Are you following a tutorial from somewhere? Also, what version of TensorFlow are you using?

Sign up to request clarification or add additional context in comments.

1 Comment

The tf.Variable code works perfectly as the code progresses further. I'm following a project. Tensorflow version is 1.4.1 github.com/HKUST-KnowComp/R-Net/blob/master/func.py
0

This method works for me, Try it out.

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

1 Comment

Can you be more clear on where this code should be incorporated. Is this replacing an existing code or is this in addition to an existing code. Also, I would recommend you read how to write a good answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.