How to pass random value (not tensor) in tensorflow function as a parameter?

I need help

Stackoverflow source: tensorflow2.0 - Passing random value in tensorflow function as a parameter - Stack Overflow

I have code in my augmentation tf.data pipeline…

    # BLURE
    filter_size = tf.random.uniform(shape=[], minval=0, maxval=5)
    image = tfa.image.mean_filter2d(image, filter_shape=filter_size)

But I’m constantly getting error…

 TypeError: The `filter_shape` argument must be a tuple of 2 integers. Received: Tensor("filter_shape:0", shape=(), dtype=int32)

I tried getting static value from random tensorflow like this…

    # BLURE
    filter_size = tf.get_static_value(tf.random.uniform(shape=[], minval=0, maxval=5))
    image = tfa.image.mean_filter2d(image, filter_shape=(filter_size, filter_size))

But I get error…

TypeError: The `filter_shape` argument must be a tuple of 2 integers. Received: None

And this errors makes me sad :frowning:

I want to create augmentation pipeline for tf.data btw…

Have you tried setting the type of the random values to integer?

filter_size = tf.random.uniform(shape=[2], minval=0, maxval=5, dtype=tf.int32)
image = tfa.image.mean_filter2d(image, filter_shape=filter_size)

or

filter_size = tf.get_static_value(tf.random.uniform(shape=[], minval=0, maxval=5, dtype=tf.int32))
image = tfa.image.mean_filter2d(image, filter_shape=(filter_size, filter_size))

Yes I did but I get this for the first code…

TypeError: The `filter_shape` argument must be a tuple of 2 integers.
Received: Tensor("filter_shape:0", shape=(2,), dtype=int32)

And for the second…

ValueError: The `filter_shape` argument must be a tuple of 2 integers.
Received: (None, None) including element None of type <class 'NoneType'>

First of all generate the value using a method like np.random from the Numpy library or any other suitable method then convert the value to tensorflow constant or placeholder. you can us tf.constant to create a constant or tf.placeholder() to create a placeholder.Constant are used for fixed the values, while placeholder are used for values which will be provided later during runtime. Regards.