pangdan
pangdan

Reputation: 61

How to generate a static random constant in Tensorflow?

I want to generate a constant tensor in Tensorflow, which will be initialized with a specified mechanism, eg, random_uniform, random_normal.

I know that I can generate a random numpy array according to these mechanisms, say random_uniform, random_normal, etc; Then we feed the resulted numpy array as value argument in tf.constant.

However, the question is that we must give a shape when using numpy version of random mechanism. However, I don't want to pre-specify the shape, and I hope the shape is resilient, just like we write Tensorflow code shape = tf.shape(some_previous_tensor)

Way1 I tried: There is not a must to pre-specify the concrete shape of the constant in the graph construction phase. However, the generated tensor is random rather than static. That is not I expected.

var = tf.random.normal(
    [2,2], mean=0.0, stddev=0.5, dtype=tf.float32, 
)

with tf.Session() as sess:
    print('var:', sess.run(var))
    print('var:', sess.run(var))

Output:
 var: [[ 0.21260215  0.13721827]
 [ 0.7704196  -0.48304045]]

var: [[-0.63397115 -0.0956466 ]
 [ 0.0761982   0.54037064]]

Way2 I tried: I can get static constant, but it is necessary to give a size in np.random.normal, which is not I expected.

var_np = np.random.normal(0,0.5, size=(2,2))
var = tf.constant(value=var_np)

with tf.Session() as sess:
    print('var:', sess.run(var))
    print('var:', sess.run(var))

Output:
var: [[-0.73357953 -0.10277695]
 [ 0.57406473  0.32157612]]
var: [[-0.73357953 -0.10277695]
 [ 0.57406473  0.32157612]]

Upvotes: 1

Views: 375

Answers (2)

javidcf
javidcf

Reputation: 59701

You can use tf.Variable / tf.get_variable with trainable=False and validate_shape=False. You can use a value depending on a placeholder for the shape as initial value. Then, when you initialize the variable (either using the initializer attribute or something more common like tf.global_variables_initializer), you just have to give the shape for initialization. After the initialization, the value of the variable will be kept the same for the whole session, as long as it is not initialized again or assigned a different value.

import tensorflow as tf

shape = tf.placeholder(tf.int32, [None])
var_init = tf.random.normal(
    shape, mean=0.0, stddev=0.5, dtype=tf.float32, 
)
var = tf.Variable(var_init, validate_shape=False, trainable=False, name='Var')
with tf.Session() as sess:
    tf.random.set_random_seed(0)
    sess.run(var.initializer, feed_dict={shape: [2, 3]})
    print('var:', sess.run(var), sep='\n')
    print('var:', sess.run(var), sep='\n')

Output:

var:
[[-0.4055751   0.7597851  -0.04810145]
 [ 0.92776746 -0.3747548  -0.03715562]]
var:
[[-0.4055751   0.7597851  -0.04810145]
 [ 0.92776746 -0.3747548  -0.03715562]]

Upvotes: 1

Tobias Feil
Tobias Feil

Reputation: 2696

Just run tf.shape(t) for a tensor t in whose shape you want your static random tensor to be. Feed the output value as size argument to np.random.normal and you're all set.

Upvotes: 0

Related Questions