Tensorflow eval() without session or move variable to an other session
<p>I'm using tensorflow-models like it is described in the <a href="https://www.tensorflow.org/versions/r0.9/tutorials/tflearn/index.html" rel="noreferrer">iris predict examples</a>. Because of that I do not have a session object. Now I want to convert the labels to a numpy-array with <code>.eval()</code>. Without a session there comes an error.</p>

<pre><code>Traceback (most recent call last):
 File "myfile.py", line 273, in &lt;module&gt;
   tf.app.run()
 File "/usr/local/lib/python3.4/site-packages/tensorflow/python/platform/app.py", line 30, in run
   sys.exit(main(sys.argv))
 File "myfile.py", line 270, in main
   train_and_eval()
 File "myfile.py", line 258, in train_and_eval
   label.eval()
 File "/usr/local/lib/python3.4/site-packages/tensorflow/python/framework/ops.py", line 559, in eval
   return _eval_using_default_session(self, feed_dict, self.graph, session)
 File "/usr/local/lib/python3.4/site-packages/tensorflow/python/framework/ops.py", line 3642, in _eval_using_default_session
   raise ValueError("Cannot evaluate tensor using `eval()`: No default "
ValueError: Cannot evaluate tensor using `eval()`: No default session is registered. Use `with sess.as_default()` or pass an explicit session to `eval(session=sess)`
</code></pre>

<p>Is there a possibility to access / get the session the model used in background? Or is there an other possibility to convert the tensor to a numpy-array?</p>

<p>If I create a new session, then it seems that tensorflow moves to this session but has no access to the variable. A python <code>print()</code> is displayed, but then it runs inifite. How can I parse a variable to this new session?</p>

<p>The other part of the net works well - it's only this special thing convert the tensor to a numpy-array</p>

<pre><code>    COLUMNS = ["col1", "col2", "col3", "target"]
    LABEL_COLUMN = "target"
    CATEGORICAL_COLUMNS = ["col1", "col2", "col3"]

    def build_estimator(model_dir):
        col1 = tf.contrib.layers.sparse_column_with_hash_bucket(
            "col1", hash_bucket_size=10000)
        col2........

        wide_columns = [col1, col2, col3]
        deep_columns = [
            tf.contrib.layers.embedding_column(col1, dimension=7),
            tf.contrib.layers.embedding_column(col2, dimension=7),
            tf.contrib.layers.embedding_column(col3, dimension=7)
        ]

        m = tf.contrib.learn.DNNLinearCombinedClassifier(...)
        return m

    def input_fn(file_names, batch_size):
        ...
        label = tf.string_to_number(examples_dict[LABEL_COLUMN], out_type=tf.int32)
        return feature_cols, label

    def train_and_eval():
        model_dir = "./model/"
        print(model_dir)

        m = build_estimator(model_dir)
        m.fit(input_fn=lambda: input_fn(train_file_name, batch_size), steps=steps)
        results = m.evaluate(input_fn=lambda: input_fn(test_file_name, batch_size),
            steps=1)
        pred_m = m.predict(input_fn=lambda: input_fn(test_file_name, batch_size))


        sess = tf.InteractiveSession()
        with sess.as_default():
            print("Is a session there?")
            _, label = input_fn(test_file_name, batch_size)
            label.eval()
            print(label)

    def main(_):
        train_and_eval()

    if __name__ == "__main__":
        tf.app.run()
</code></pre>

<p>The new session starts at the end of the code-snippet:</p>

<pre><code>        sess = tf.InteractiveSession()
        with sess.as_default():
            print("Is a session there?")
            _, label = input_fn(test_file_name, batch_size)
            label.eval()
            print(label)
</code></pre>