TensorFlow: trainable_variables() is empty I want to retrieve the list of trainable variables/weights in my model (wrapped in a tf.Estimator). However, tf.trainable_variables always returns an empty list, what am I doing wrong?

import numpy as np
import pandas as pd
import sys
import globals
import pkg_resources
import numpy as np

import tensorflow as tf


def cnn_model_fn(features, labels, mode):

    input_layer = tf.reshape(features["x"], [-1, 51, 13])
    input_layer = tf.cast(input_layer, tf.float32)

    # Convolutional Layer #1
    conv1 = tf.layers.conv1d(inputs=input_layer, filters=32, kernel_size=5, padding="same", activation=tf.nn.relu)

    # Pooling Layer #1
    pool1 = tf.layers.max_pooling1d(inputs=conv1, pool_size=2, strides=2)

    # Convolutional Layer #2 and Pooling Layer #2
    conv2 = tf.layers.conv1d(inputs=pool1, filters=64, kernel_size=5, padding="same", activation=tf.nn.relu)

    # Pooling layer #2
    pool2 = tf.layers.max_pooling1d(inputs=conv2, pool_size=2, strides=2)

    # flatten the feature map
    pool2_flat = tf.reshape(pool2, [-1, 12 * 64])

    # Dense Layer
    dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu)
    dropout = tf.layers.dropout(inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)

    # Logits Layer
    logits = tf.layers.dense(inputs=dropout, units=5)

    predictions = {
        # Generate predictions (for PREDICT and EVAL mode)
        "classes": tf.argmax(input=logits, axis=1),
        # Add `softmax_tensor` to the graph. It is used for PREDICT and by the `logging_hook`.
        "probabilities": tf.nn.softmax(logits, name="softmax_tensor")
    }

    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)

    # Calculate Loss (for both TRAIN and EVAL modes)
    loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)

    # Configure the Training Op (for TRAIN mode)
    if mode == tf.estimator.ModeKeys.TRAIN:
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
        train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())
        return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)

    # Add evaluation metrics (for EVAL mode)
    eval_metric_ops = {"accuracy": tf.metrics.accuracy(labels=labels, predictions=predictions["classes"])}

    return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)

def main(unused_argv):

    total = pd.read_feather('testfile.feather')
    labels = total['labels']
    features = total.iloc[:, 16:679]

    mnist_classifier = tf.estimator.Estimator(model_fn=cnn_model_fn, model_dir="/tmp/gait_convnet_model")

    # Log the values in the "Softmax" tensor with label "probabilities"
    tensors_to_log = {"probabilities": "softmax_tensor"}
    logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=50)    

    # Train the model
    train_input_fn = tf.estimator.inputs.numpy_input_fn(x={"x": np.array(features)}, y=np.array(labels), batch_size=100, num_epochs=None, shuffle=True)
    mnist_classifier.train(input_fn=train_input_fn, steps=1, hooks=[logging_hook])

    temp_list = tf.trainable_variables()
    print(temp_list)

if __name__ == '__main__':
    tf.app.run()
There are functions in the Estimator class that handle this, namely: get_variable_names and get_variable_value. 

I guess they want to avoid polluting the global scope. Note that you can pass a name parameter to your layers on creation to disambiguate them.