`get_variable()` doesn't recognize existing variables for tf.estimator












6















This question has been asked here, difference is my problem is focused on Estimator.



Some context: We have trained a model using estimator and get some variable defined within Estimator input_fn, this function preprocesses data to batches. Now, we are moving to prediction. During the prediction, we use the same input_fn to read in and process the data. But got error saying variable (word_embeddings) does not exist (variables exist in the chkp graph), here's the relevant bit of code in input_fn:



with tf.variable_scope('vocabulary', reuse=tf.AUTO_REUSE):
if mode == tf.estimator.ModeKeys.TRAIN:
word_to_index, word_to_vec = load_embedding(graph_params["word_to_vec"])
word_embeddings = tf.get_variable(initializer=tf.constant(word_to_vec, dtype=tf.float32),
trainable=False,
name="word_to_vec",
dtype=tf.float32)
else:
word_embeddings = tf.get_variable("word_to_vec", dtype=tf.float32)


basically, when it's in prediction mode, else is invoked to load up variables in checkpoint. Failure of recognizing this variable indicates a) inappropriate usage of scope; b) graph is not restored. I don't think scope matters that much here as long as reuse is set properly.



I suspect that is because the graph is not yet restored at input_fn phase. Usually, the graph is restored by calling saver.restore(sess, "/tmp/model.ckpt") reference. Investigation of estimator source code doesn't get me anything relating to restore, the best shot is MonitoredSession, a wrapper of training. It's already been stretch so much from the original problem, not confident if I'm on the right path, I'm looking for help here if anyone has any insights.



One line summary of my question: How does graph get restored within tf.estimator, via input_fn or model_fn?










share|improve this question

























  • There definitely seems to be a difference between how a session is normally run vs using an Estimator that is afaik not in any of the Estimator documentation.

    – SantoshGupta7
    Dec 3 '18 at 8:49











  • Graph will be restored by setting scaffold=estimator_spec.scaffold within MonitorSession. The MonitorSession is called by estimator.predict.

    – GabrielChu
    Dec 7 '18 at 1:16
















6















This question has been asked here, difference is my problem is focused on Estimator.



Some context: We have trained a model using estimator and get some variable defined within Estimator input_fn, this function preprocesses data to batches. Now, we are moving to prediction. During the prediction, we use the same input_fn to read in and process the data. But got error saying variable (word_embeddings) does not exist (variables exist in the chkp graph), here's the relevant bit of code in input_fn:



with tf.variable_scope('vocabulary', reuse=tf.AUTO_REUSE):
if mode == tf.estimator.ModeKeys.TRAIN:
word_to_index, word_to_vec = load_embedding(graph_params["word_to_vec"])
word_embeddings = tf.get_variable(initializer=tf.constant(word_to_vec, dtype=tf.float32),
trainable=False,
name="word_to_vec",
dtype=tf.float32)
else:
word_embeddings = tf.get_variable("word_to_vec", dtype=tf.float32)


basically, when it's in prediction mode, else is invoked to load up variables in checkpoint. Failure of recognizing this variable indicates a) inappropriate usage of scope; b) graph is not restored. I don't think scope matters that much here as long as reuse is set properly.



I suspect that is because the graph is not yet restored at input_fn phase. Usually, the graph is restored by calling saver.restore(sess, "/tmp/model.ckpt") reference. Investigation of estimator source code doesn't get me anything relating to restore, the best shot is MonitoredSession, a wrapper of training. It's already been stretch so much from the original problem, not confident if I'm on the right path, I'm looking for help here if anyone has any insights.



One line summary of my question: How does graph get restored within tf.estimator, via input_fn or model_fn?










share|improve this question

























  • There definitely seems to be a difference between how a session is normally run vs using an Estimator that is afaik not in any of the Estimator documentation.

    – SantoshGupta7
    Dec 3 '18 at 8:49











  • Graph will be restored by setting scaffold=estimator_spec.scaffold within MonitorSession. The MonitorSession is called by estimator.predict.

    – GabrielChu
    Dec 7 '18 at 1:16














6












6








6


1






This question has been asked here, difference is my problem is focused on Estimator.



Some context: We have trained a model using estimator and get some variable defined within Estimator input_fn, this function preprocesses data to batches. Now, we are moving to prediction. During the prediction, we use the same input_fn to read in and process the data. But got error saying variable (word_embeddings) does not exist (variables exist in the chkp graph), here's the relevant bit of code in input_fn:



with tf.variable_scope('vocabulary', reuse=tf.AUTO_REUSE):
if mode == tf.estimator.ModeKeys.TRAIN:
word_to_index, word_to_vec = load_embedding(graph_params["word_to_vec"])
word_embeddings = tf.get_variable(initializer=tf.constant(word_to_vec, dtype=tf.float32),
trainable=False,
name="word_to_vec",
dtype=tf.float32)
else:
word_embeddings = tf.get_variable("word_to_vec", dtype=tf.float32)


basically, when it's in prediction mode, else is invoked to load up variables in checkpoint. Failure of recognizing this variable indicates a) inappropriate usage of scope; b) graph is not restored. I don't think scope matters that much here as long as reuse is set properly.



I suspect that is because the graph is not yet restored at input_fn phase. Usually, the graph is restored by calling saver.restore(sess, "/tmp/model.ckpt") reference. Investigation of estimator source code doesn't get me anything relating to restore, the best shot is MonitoredSession, a wrapper of training. It's already been stretch so much from the original problem, not confident if I'm on the right path, I'm looking for help here if anyone has any insights.



One line summary of my question: How does graph get restored within tf.estimator, via input_fn or model_fn?










share|improve this question
















This question has been asked here, difference is my problem is focused on Estimator.



Some context: We have trained a model using estimator and get some variable defined within Estimator input_fn, this function preprocesses data to batches. Now, we are moving to prediction. During the prediction, we use the same input_fn to read in and process the data. But got error saying variable (word_embeddings) does not exist (variables exist in the chkp graph), here's the relevant bit of code in input_fn:



with tf.variable_scope('vocabulary', reuse=tf.AUTO_REUSE):
if mode == tf.estimator.ModeKeys.TRAIN:
word_to_index, word_to_vec = load_embedding(graph_params["word_to_vec"])
word_embeddings = tf.get_variable(initializer=tf.constant(word_to_vec, dtype=tf.float32),
trainable=False,
name="word_to_vec",
dtype=tf.float32)
else:
word_embeddings = tf.get_variable("word_to_vec", dtype=tf.float32)


basically, when it's in prediction mode, else is invoked to load up variables in checkpoint. Failure of recognizing this variable indicates a) inappropriate usage of scope; b) graph is not restored. I don't think scope matters that much here as long as reuse is set properly.



I suspect that is because the graph is not yet restored at input_fn phase. Usually, the graph is restored by calling saver.restore(sess, "/tmp/model.ckpt") reference. Investigation of estimator source code doesn't get me anything relating to restore, the best shot is MonitoredSession, a wrapper of training. It's already been stretch so much from the original problem, not confident if I'm on the right path, I'm looking for help here if anyone has any insights.



One line summary of my question: How does graph get restored within tf.estimator, via input_fn or model_fn?







tensorflow tensorflow-estimator






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 24 at 8:02







GabrielChu

















asked Nov 26 '18 at 11:26









GabrielChuGabrielChu

2,11061326




2,11061326













  • There definitely seems to be a difference between how a session is normally run vs using an Estimator that is afaik not in any of the Estimator documentation.

    – SantoshGupta7
    Dec 3 '18 at 8:49











  • Graph will be restored by setting scaffold=estimator_spec.scaffold within MonitorSession. The MonitorSession is called by estimator.predict.

    – GabrielChu
    Dec 7 '18 at 1:16



















  • There definitely seems to be a difference between how a session is normally run vs using an Estimator that is afaik not in any of the Estimator documentation.

    – SantoshGupta7
    Dec 3 '18 at 8:49











  • Graph will be restored by setting scaffold=estimator_spec.scaffold within MonitorSession. The MonitorSession is called by estimator.predict.

    – GabrielChu
    Dec 7 '18 at 1:16

















There definitely seems to be a difference between how a session is normally run vs using an Estimator that is afaik not in any of the Estimator documentation.

– SantoshGupta7
Dec 3 '18 at 8:49





There definitely seems to be a difference between how a session is normally run vs using an Estimator that is afaik not in any of the Estimator documentation.

– SantoshGupta7
Dec 3 '18 at 8:49













Graph will be restored by setting scaffold=estimator_spec.scaffold within MonitorSession. The MonitorSession is called by estimator.predict.

– GabrielChu
Dec 7 '18 at 1:16





Graph will be restored by setting scaffold=estimator_spec.scaffold within MonitorSession. The MonitorSession is called by estimator.predict.

– GabrielChu
Dec 7 '18 at 1:16












1 Answer
1






active

oldest

votes


















1














Hi I think that you error comes simply because you didn't specify the shape in the tf.get_variable (at predict) , it seems that you need to specify the shape even if the variable is going to be restored.



I've made the following test with a simple linear regressor estimator that simply needs to predict x + 5



def input_fn(mode):
def _input_fn():
with tf.variable_scope('all_input_fn', reuse=tf.AUTO_REUSE):
if mode == tf.estimator.ModeKeys.TRAIN:
var_to_follow = tf.get_variable('var_to_follow', initializer=tf.constant(20))
x_data = np.random.randn(1000)
labels = x_data + 5
return {'x':x_data}, labels
elif mode == tf.estimator.ModeKeys.PREDICT:
var_to_follow = tf.get_variable("var_to_follow", dtype=tf.int32, shape=)
return {'x':[0,10,100,var_to_follow]}
return _input_fn

featcols = [tf.feature_column.numeric_column('x')]
model = tf.estimator.LinearRegressor(featcols, './outdir')


This code works perfectly fine, the value of the const is 20 and also for fun use it in my test set to confirm :p



However if you remove the shape= , it breaks, you can also give another initializer such as tf.constant(500) and everything will work and 20 will be used.



By running



model.train(input_fn(tf.estimator.ModeKeys.TRAIN), max_steps=10000)


and



preds = model.predict(input_fn(tf.estimator.ModeKeys.PREDICT))
print(next(preds))


You can visualize the graph and you'll see that a) the scoping is normal and b) the graph is restored.



Hope this will help you.






share|improve this answer


























  • Thanks! Within a tf.graph you do not necessarily to specify the shape, tested it locally, if needed I can upload the code as well. Weird thing is you have to do that in Estimator.

    – GabrielChu
    Dec 12 '18 at 2:26








  • 1





    I guess that in the case of estimators the graph is created each time when you call train, evaluate or predict, then the weights are restored. It can explain this behavior. As you can see in github.com/tensorflow/tensorflow/blob/r1.12/tensorflow/python/… , you first call input_fn , then model_fn (build the graph) and finally you'll restore the weights through the Monitored Session.

    – abcdaire
    Dec 12 '18 at 12:35












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53480116%2fget-variable-doesnt-recognize-existing-variables-for-tf-estimator%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Hi I think that you error comes simply because you didn't specify the shape in the tf.get_variable (at predict) , it seems that you need to specify the shape even if the variable is going to be restored.



I've made the following test with a simple linear regressor estimator that simply needs to predict x + 5



def input_fn(mode):
def _input_fn():
with tf.variable_scope('all_input_fn', reuse=tf.AUTO_REUSE):
if mode == tf.estimator.ModeKeys.TRAIN:
var_to_follow = tf.get_variable('var_to_follow', initializer=tf.constant(20))
x_data = np.random.randn(1000)
labels = x_data + 5
return {'x':x_data}, labels
elif mode == tf.estimator.ModeKeys.PREDICT:
var_to_follow = tf.get_variable("var_to_follow", dtype=tf.int32, shape=)
return {'x':[0,10,100,var_to_follow]}
return _input_fn

featcols = [tf.feature_column.numeric_column('x')]
model = tf.estimator.LinearRegressor(featcols, './outdir')


This code works perfectly fine, the value of the const is 20 and also for fun use it in my test set to confirm :p



However if you remove the shape= , it breaks, you can also give another initializer such as tf.constant(500) and everything will work and 20 will be used.



By running



model.train(input_fn(tf.estimator.ModeKeys.TRAIN), max_steps=10000)


and



preds = model.predict(input_fn(tf.estimator.ModeKeys.PREDICT))
print(next(preds))


You can visualize the graph and you'll see that a) the scoping is normal and b) the graph is restored.



Hope this will help you.






share|improve this answer


























  • Thanks! Within a tf.graph you do not necessarily to specify the shape, tested it locally, if needed I can upload the code as well. Weird thing is you have to do that in Estimator.

    – GabrielChu
    Dec 12 '18 at 2:26








  • 1





    I guess that in the case of estimators the graph is created each time when you call train, evaluate or predict, then the weights are restored. It can explain this behavior. As you can see in github.com/tensorflow/tensorflow/blob/r1.12/tensorflow/python/… , you first call input_fn , then model_fn (build the graph) and finally you'll restore the weights through the Monitored Session.

    – abcdaire
    Dec 12 '18 at 12:35
















1














Hi I think that you error comes simply because you didn't specify the shape in the tf.get_variable (at predict) , it seems that you need to specify the shape even if the variable is going to be restored.



I've made the following test with a simple linear regressor estimator that simply needs to predict x + 5



def input_fn(mode):
def _input_fn():
with tf.variable_scope('all_input_fn', reuse=tf.AUTO_REUSE):
if mode == tf.estimator.ModeKeys.TRAIN:
var_to_follow = tf.get_variable('var_to_follow', initializer=tf.constant(20))
x_data = np.random.randn(1000)
labels = x_data + 5
return {'x':x_data}, labels
elif mode == tf.estimator.ModeKeys.PREDICT:
var_to_follow = tf.get_variable("var_to_follow", dtype=tf.int32, shape=)
return {'x':[0,10,100,var_to_follow]}
return _input_fn

featcols = [tf.feature_column.numeric_column('x')]
model = tf.estimator.LinearRegressor(featcols, './outdir')


This code works perfectly fine, the value of the const is 20 and also for fun use it in my test set to confirm :p



However if you remove the shape= , it breaks, you can also give another initializer such as tf.constant(500) and everything will work and 20 will be used.



By running



model.train(input_fn(tf.estimator.ModeKeys.TRAIN), max_steps=10000)


and



preds = model.predict(input_fn(tf.estimator.ModeKeys.PREDICT))
print(next(preds))


You can visualize the graph and you'll see that a) the scoping is normal and b) the graph is restored.



Hope this will help you.






share|improve this answer


























  • Thanks! Within a tf.graph you do not necessarily to specify the shape, tested it locally, if needed I can upload the code as well. Weird thing is you have to do that in Estimator.

    – GabrielChu
    Dec 12 '18 at 2:26








  • 1





    I guess that in the case of estimators the graph is created each time when you call train, evaluate or predict, then the weights are restored. It can explain this behavior. As you can see in github.com/tensorflow/tensorflow/blob/r1.12/tensorflow/python/… , you first call input_fn , then model_fn (build the graph) and finally you'll restore the weights through the Monitored Session.

    – abcdaire
    Dec 12 '18 at 12:35














1












1








1







Hi I think that you error comes simply because you didn't specify the shape in the tf.get_variable (at predict) , it seems that you need to specify the shape even if the variable is going to be restored.



I've made the following test with a simple linear regressor estimator that simply needs to predict x + 5



def input_fn(mode):
def _input_fn():
with tf.variable_scope('all_input_fn', reuse=tf.AUTO_REUSE):
if mode == tf.estimator.ModeKeys.TRAIN:
var_to_follow = tf.get_variable('var_to_follow', initializer=tf.constant(20))
x_data = np.random.randn(1000)
labels = x_data + 5
return {'x':x_data}, labels
elif mode == tf.estimator.ModeKeys.PREDICT:
var_to_follow = tf.get_variable("var_to_follow", dtype=tf.int32, shape=)
return {'x':[0,10,100,var_to_follow]}
return _input_fn

featcols = [tf.feature_column.numeric_column('x')]
model = tf.estimator.LinearRegressor(featcols, './outdir')


This code works perfectly fine, the value of the const is 20 and also for fun use it in my test set to confirm :p



However if you remove the shape= , it breaks, you can also give another initializer such as tf.constant(500) and everything will work and 20 will be used.



By running



model.train(input_fn(tf.estimator.ModeKeys.TRAIN), max_steps=10000)


and



preds = model.predict(input_fn(tf.estimator.ModeKeys.PREDICT))
print(next(preds))


You can visualize the graph and you'll see that a) the scoping is normal and b) the graph is restored.



Hope this will help you.






share|improve this answer















Hi I think that you error comes simply because you didn't specify the shape in the tf.get_variable (at predict) , it seems that you need to specify the shape even if the variable is going to be restored.



I've made the following test with a simple linear regressor estimator that simply needs to predict x + 5



def input_fn(mode):
def _input_fn():
with tf.variable_scope('all_input_fn', reuse=tf.AUTO_REUSE):
if mode == tf.estimator.ModeKeys.TRAIN:
var_to_follow = tf.get_variable('var_to_follow', initializer=tf.constant(20))
x_data = np.random.randn(1000)
labels = x_data + 5
return {'x':x_data}, labels
elif mode == tf.estimator.ModeKeys.PREDICT:
var_to_follow = tf.get_variable("var_to_follow", dtype=tf.int32, shape=)
return {'x':[0,10,100,var_to_follow]}
return _input_fn

featcols = [tf.feature_column.numeric_column('x')]
model = tf.estimator.LinearRegressor(featcols, './outdir')


This code works perfectly fine, the value of the const is 20 and also for fun use it in my test set to confirm :p



However if you remove the shape= , it breaks, you can also give another initializer such as tf.constant(500) and everything will work and 20 will be used.



By running



model.train(input_fn(tf.estimator.ModeKeys.TRAIN), max_steps=10000)


and



preds = model.predict(input_fn(tf.estimator.ModeKeys.PREDICT))
print(next(preds))


You can visualize the graph and you'll see that a) the scoping is normal and b) the graph is restored.



Hope this will help you.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 12 '18 at 12:47

























answered Dec 11 '18 at 23:23









abcdaireabcdaire

720219




720219













  • Thanks! Within a tf.graph you do not necessarily to specify the shape, tested it locally, if needed I can upload the code as well. Weird thing is you have to do that in Estimator.

    – GabrielChu
    Dec 12 '18 at 2:26








  • 1





    I guess that in the case of estimators the graph is created each time when you call train, evaluate or predict, then the weights are restored. It can explain this behavior. As you can see in github.com/tensorflow/tensorflow/blob/r1.12/tensorflow/python/… , you first call input_fn , then model_fn (build the graph) and finally you'll restore the weights through the Monitored Session.

    – abcdaire
    Dec 12 '18 at 12:35



















  • Thanks! Within a tf.graph you do not necessarily to specify the shape, tested it locally, if needed I can upload the code as well. Weird thing is you have to do that in Estimator.

    – GabrielChu
    Dec 12 '18 at 2:26








  • 1





    I guess that in the case of estimators the graph is created each time when you call train, evaluate or predict, then the weights are restored. It can explain this behavior. As you can see in github.com/tensorflow/tensorflow/blob/r1.12/tensorflow/python/… , you first call input_fn , then model_fn (build the graph) and finally you'll restore the weights through the Monitored Session.

    – abcdaire
    Dec 12 '18 at 12:35

















Thanks! Within a tf.graph you do not necessarily to specify the shape, tested it locally, if needed I can upload the code as well. Weird thing is you have to do that in Estimator.

– GabrielChu
Dec 12 '18 at 2:26







Thanks! Within a tf.graph you do not necessarily to specify the shape, tested it locally, if needed I can upload the code as well. Weird thing is you have to do that in Estimator.

– GabrielChu
Dec 12 '18 at 2:26






1




1





I guess that in the case of estimators the graph is created each time when you call train, evaluate or predict, then the weights are restored. It can explain this behavior. As you can see in github.com/tensorflow/tensorflow/blob/r1.12/tensorflow/python/… , you first call input_fn , then model_fn (build the graph) and finally you'll restore the weights through the Monitored Session.

– abcdaire
Dec 12 '18 at 12:35





I guess that in the case of estimators the graph is created each time when you call train, evaluate or predict, then the weights are restored. It can explain this behavior. As you can see in github.com/tensorflow/tensorflow/blob/r1.12/tensorflow/python/… , you first call input_fn , then model_fn (build the graph) and finally you'll restore the weights through the Monitored Session.

– abcdaire
Dec 12 '18 at 12:35




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53480116%2fget-variable-doesnt-recognize-existing-variables-for-tf-estimator%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno