# Buggy code
def transform(self, inputs, mode):
    try:
        embeddings = tf.get_variable(
            "w_embs", dtype=self.dtype, trainable=self.trainable
        )
    except ValueError:
        # Variable does not exist yet.
        if self.embedding_file:
            pretrained = load_pretrained_embeddings(
                self.embedding_file,
                self.vocabulary_file,
                num_oov_buckets=self.num_oov_buckets,
                with_header=self.embedding_file_with_header,
                case_insensitive_embeddings=self.case_insensitive_embeddings,
            )
            self.embedding_size = pretrained.shape[-1]

            shape = None
            initializer = tf.constant(pretrained.astype(self.dtype.as_numpy_dtype()))
        else:
            shape = [self.vocabulary_size, self.embedding_size]
            initializer = None

        embeddings = tf.get_variable(
            "w_embs",
            shape=shape,
            dtype=self.dtype,
            initializer=initializer,
            trainable=self.trainable,
        )

    outputs = embedding_lookup(embeddings, inputs)

    outputs = tf.layers.dropout(
        outputs, rate=self.dropout, training=mode == tf.estimator.ModeKeys.TRAIN
    )

    return outputs


# Traceback (ValueError)
INFO:tensorflow:Saving checkpoints for 16221 into /home/lgy/deepModels/tf_models/wmt_ende_transformer_1gpu_lr2_ws8000_dur2_0.998_compositionalencoding_M64K16/model.ckpt.
INFO:tensorflow:Loss for final step: 3.9958026.
INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Starting evaluation at 2018-06-14-15:33:54
INFO:tensorflow:Graph was finalized.
2018-06-14 23:33:54.898526: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1435] Adding visible gpu devices: 0
2018-06-14 23:33:54.898579: I tensorflow/core/common_runtime/gpu/gpu_device.cc:923] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-06-14 23:33:54.898588: I tensorflow/core/common_runtime/gpu/gpu_device.cc:929]      0
2018-06-14 23:33:54.898594: I tensorflow/core/common_runtime/gpu/gpu_device.cc:942] 0:   N
2018-06-14 23:33:54.898894: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1053] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 15127 MB memory) -> physical GPU (device: 0, name: Tesla P100-SXM2-16GB, pci bus id: 0000:89:00.0, compute capability: 6.0)
INFO:tensorflow:Restoring parameters from /home/lgy/deepModels/tf_models/wmt_ende_transformer_1gpu_lr2_ws8000_dur2_0.998_compositionalencoding_M64K16/model.ckpt-16221
INFO:tensorflow:Running local_init_op.
2018-06-14 23:33:55.571416: I tensorflow/core/kernels/lookup_util.cc:373] Table trying to initialize from file /home/lgy/data/wmt/wmt14-de-en/amax/wmtende.vocab is already initialized.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Evaluation predictions saved to /home/lgy/deepModels/tf_models/wmt_ende_transformer_1gpu_lr2_ws8000_dur2_0.998_compositionalencoding_M64K16/eval/predictions.txt.16221
INFO:tensorflow:BLEU evaluation score: 15.020000
INFO:tensorflow:Finished evaluation at 2018-06-14-15:39:44
INFO:tensorflow:Saving dict for global step 16221: global_step = 16221, loss = 3.0185592
INFO:tensorflow:Calling model_fn.
Traceback (most recent call last):
File "tmp_train_transformer.py", line 74, in <module>
runner.train_and_evaluate()
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/OpenNMT_tf-1.5.0-py2.7.egg/opennmt/runner.py", line 148, in train_and_evaluate
tf.estimator.train_and_evaluate(self._estimator, train_spec, eval_spec)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/tensorflow/python/estimator/training.py", line 439, in train_and_evaluate
executor.run()
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/tensorflow/python/estimator/training.py", line 518, in run
self.run_local()
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/tensorflow/python/estimator/training.py", line 657, in run_local
eval_result = evaluator.evaluate_and_export()
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/tensorflow/python/estimator/training.py", line 858, in evaluate_and_export
self._export_eval_result(eval_result, is_the_final_export)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/tensorflow/python/estimator/training.py", line 889, in _export_eval_result
is_the_final_export=is_the_final_export)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/tensorflow/python/estimator/exporter.py", line 232, in export
is_the_final_export)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/tensorflow/python/estimator/exporter.py", line 123, in export
strip_default_attrs=self._strip_default_attrs)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.py", line 613, in export_savedmodel
config=self.config)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.py", line 831, in _call_model_fn
model_fn_results = self._model_fn(features=features, **kwargs)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/OpenNMT_tf-1.5.0-py2.7.egg/opennmt/models/model.py", line 128, in _model_fn
_, predictions = self._build(features, labels, params, mode, config=config)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/OpenNMT_tf-1.5.0-py2.7.egg/opennmt/models/sequence_to_sequence.py", line 185, in _build
return_alignment_history=True))
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/OpenNMT_tf-1.5.0-py2.7.egg/opennmt/decoders/self_attention_decoder.py", line 315, in dynamic_decode_and_search
eos_id=end_token)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/OpenNMT_tf-1.5.0-py2.7.egg/opennmt/utils/beam_search.py", line 557, in beam_search
back_prop=False)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 3224, in while_loop
result = loop_context.BuildLoop(cond, body, loop_vars, shape_invariants)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2956, in BuildLoop
pred, body, original_loop_vars, loop_vars, shape_invariants)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2893, in _BuildLoop
body_result = body(*packed_vars_for_body)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/OpenNMT_tf-1.5.0-py2.7.egg/opennmt/utils/beam_search.py", line 485, in inner_loop
i, alive_seq, alive_log_probs, states)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/OpenNMT_tf-1.5.0-py2.7.egg/opennmt/utils/beam_search.py", line 379, in grow_topk
flat_logits, flat_states = symbols_to_logits_fn(flat_ids, i, flat_states)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/OpenNMT_tf-1.5.0-py2.7.egg/opennmt/decoders/self_attention_decoder.py", line 101, in _impl
inputs = embedding_fn(ids[:, -1:])
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/OpenNMT_tf-1.5.0-py2.7.egg/opennmt/models/sequence_to_sequence.py", line 103, in _target_embedding_fn
return self.target_inputter.transform(ids, mode=mode)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/OpenNMT_tf-1.5.0-py2.7.egg/opennmt/inputters/text_inputter.py", line 390, in transform
trainable=self.trainable)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 1317, in get_variable
constraint=constraint)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 1079, in get_variable
constraint=constraint)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 425, in get_variable
constraint=constraint)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 394, in _true_getter
use_resource=use_resource, constraint=constraint)
File "/home/lgy/test/nmt_transformer_opennmt_tf-workspace/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 751, in _get_single_variable
"reuse=tf.AUTO_REUSE in VarScope?" % name)
ValueError: Variable transformer/decoder/w_embs does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=tf.AUTO_REUSE in VarScope?

# Fixed code
def transform(self, inputs, mode):
    try:
        embeddings = tf.get_variable(
            "w_embs", dtype=self.dtype, trainable=self.trainable
        )
    except ValueError:
        # Variable does not exist yet.
        if self.embedding_file:
            pretrained = load_pretrained_embeddings(
                self.embedding_file,
                self.vocabulary_file,
                num_oov_buckets=self.num_oov_buckets,
                with_header=self.embedding_file_with_header,
                case_insensitive_embeddings=self.case_insensitive_embeddings,
            )
            self.embedding_size = pretrained.shape[-1]

            initializer = tf.constant_initializer(
                pretrained.astype(self.dtype.as_numpy_dtype()), dtype=self.dtype
            )
        else:
            initializer = None

        shape = [self.vocabulary_size, self.embedding_size]
        embeddings = tf.get_variable(
            "w_embs",
            shape=shape,
            dtype=self.dtype,
            initializer=initializer,
            trainable=self.trainable,
        )

    outputs = embedding_lookup(embeddings, inputs)

    outputs = tf.layers.dropout(
        outputs, rate=self.dropout, training=mode == tf.estimator.ModeKeys.TRAIN
    )

    return outputs