user4225701
user4225701

Reputation:

How to dynamically initialize Variables in Tensorflow?

I want to run some optimization procedure in Tensorflow for a batch of examples, and I already have some raw estimation of these variables to optimize. So I want to initialize the variables with these estimated values, instead of some random numbers or zero.

So I wonder how can I make it? Please note here the initialization value is sample-dependent. My plan is to feed the initialization to some placeholder, then initialize the variable from this placeholder, but that doesn't work.

Upvotes: 2

Views: 1083

Answers (2)

jeandut
jeandut

Reputation: 2524

Define the operation update_estimates = tf.assign(variable,estimated_value), where estimated_value is a tf.placeholder that will contain your guess in the form of numpy arrays.

You then do a simple sess.run(update_estimates, feed_dict={estimated_value:numpy_array}).

tf.get_variable() can be very useful, but for beginners I would advise against it.

Upvotes: 1

prometeu
prometeu

Reputation: 689

I belive that this could be a good start for your problem:

import numpy as np
import tensorflow as tf

#This should be your raw estimation for the variables.
#Here I am using random numers as an example.
estimated_raw = np.random.uniform(-1,1,[2,3])

#This trainable variable will be initialized with estimated_raw
var = tf.get_variable('var', initializer=estimated_raw)

# Testing if everything is ok
with tf.Session() as sess:
  var.initializer.run()
  print(var.eval())

In this way you have initialized a variable with your estimation. The optimizer will take it further.

Upvotes: 1

Related Questions