# Buggy code
def minimize(
    self,
    model,
    session=None,
    var_list=None,
    feed_dict=None,
    maxiter=1000,
    initialize=True,
    anchor=True,
    **kwargs,
):
    """
    Minimizes objective function of the model.

    :param model: GPflow model with objective tensor.
    :param session: Session where optimization will be run.
    :param var_list: List of extra variables which should be trained during optimization.
    :param feed_dict: Feed dictionary of tensors passed to session run method.
    :param maxiter: Number of run interation.
    :param initialize: If `True` model parameters will be re-initialized even if they were
        initialized before for gotten session.
    :param anchor: If `True` trained variable values computed during optimization at
        particular session will be synchronized with internal parameter values.
    :param kwargs: This is a dictionary of extra parameters for session run method.
    """

    if model is None or not isinstance(model, Model):
        raise ValueError("Unknown type passed for optimization.")

    session = model.enquire_session(session)

    self._model = model
    objective = model.objective

    with session.graph.as_default():
        full_var_list = self._gen_var_list(model, var_list)

        # Create optimizer variables before initialization.
        self._minimize_operation = self.optimizer.minimize(
            objective, var_list=full_var_list, **kwargs
        )

        model.initialize(session=session, force=initialize)
        self._initialize_optimizer(session, full_var_list)

        feed_dict = self._gen_feed_dict(model, feed_dict)
        for _i in range(maxiter):
            session.run(self.minimize_operation, feed_dict=feed_dict)

    if anchor:
        model.anchor(session)


# Traceback (AttributeError)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-e3c07a8fceb7> in <module>()
10                                 np.random.randn(M, D),
11                                 minibatch_size=2)
---> 12 model.compute_log_likelihood()

/[...]/GPflow/gpflow/decors.py in runnable(obj, *args, **kwargs)
152                     _setup_storage(store, *af_args, **af_kwargs)
153                     _build_method(method, obj, store)
--> 154             return _session_run(session, obj, store, *args, **kwargs)
155         return runnable
156     return autoflow_wrapper

/[...]/GPflow/gpflow/decors.py in _session_run(session, obj, store, *args, **kwargs)
189         feed_dict.update(obj.feeds)
190     initialize = kwargs.pop('initialize', False)
--> 191     obj.initialize(session=session, force=initialize)
192     return session.run(store['result'], **kwargs)
193

/[...]/GPflow/gpflow/core/node.py in initialize(self, session, force)
82                 session=session,
83                 force=force,
---> 84                 feed_dict=self.initializable_feeds)
85
86     def clear(self):

/[...]/GPflow/gpflow/misc.py in initialize_variables(variables, session, force, **run_kwargs)
82             initializer = tf.variables_initializer(variables)
83         else:
---> 84             uninitialized = tf.report_uninitialized_variables(var_list=variables)
85             def uninitialized_names():
86                 for uv in session.run(uninitialized):

/[...]/anaconda3/envs/py35cpu/lib/python3.5/site-packages/tensorflow/python/util/tf_should_use.py in wrapped(*args, **kwargs)
105   """
106   def wrapped(*args, **kwargs):
--> 107     return _add_should_use_warning(fn(*args, **kwargs))
108   return tf_decorator.make_decorator(
109       fn, wrapped, 'should_use_result',

/[...]/anaconda3/envs/py35cpu/lib/python3.5/site-packages/tensorflow/python/ops/variables.py in report_uninitialized_variables(var_list, name)
1519         variables_mask = math_ops.logical_not(
1520             array_ops.stack(
-> 1521                 [state_ops.is_variable_initialized(v) for v in var_list]))
1522         # Get a 1-D string tensor containing all the variable names.
1523         variable_names_tensor = array_ops.constant(

/[...]/anaconda3/envs/py35cpu/lib/python3.5/site-packages/tensorflow/python/ops/variables.py in <listcomp>(.0)
1519         variables_mask = math_ops.logical_not(
1520             array_ops.stack(
-> 1521                 [state_ops.is_variable_initialized(v) for v in var_list]))
1522         # Get a 1-D string tensor containing all the variable names.
1523         variable_names_tensor = array_ops.constant(

/[...]/anaconda3/envs/py35cpu/lib/python3.5/site-packages/tensorflow/python/ops/state_ops.py in is_variable_initialized(ref, name)
182     A `Tensor` of type `bool`.
183   """
--> 184   if ref.dtype._is_ref_dtype:
185     return gen_state_ops.is_variable_initialized(ref=ref, name=name)
186   # Handle resource variables.

AttributeError: 'Iterator' object has no attribute 'dtype'

# Fixed code
def minimize(
    self,
    model,
    session=None,
    var_list=None,
    feed_dict=None,
    maxiter=1000,
    initialize=False,
    anchor=True,
    **kwargs,
):
    """
    Minimizes objective function of the model.

    :param model: GPflow model with objective tensor.
    :param session: Session where optimization will be run.
    :param var_list: List of extra variables which should be trained during optimization.
    :param feed_dict: Feed dictionary of tensors passed to session run method.
    :param maxiter: Number of run interation.
    :param initialize: If `True` model parameters will be re-initialized even if they were
        initialized before for gotten session.
    :param anchor: If `True` trained variable values computed during optimization at
        particular session will be synchronized with internal parameter values.
    :param kwargs: This is a dictionary of extra parameters for session run method.
    """

    if model is None or not isinstance(model, Model):
        raise ValueError("Unknown type passed for optimization.")

    session = model.enquire_session(session)

    self._model = model
    objective = model.objective

    with session.graph.as_default():
        full_var_list = self._gen_var_list(model, var_list)

        # Create optimizer variables before initialization.
        self._minimize_operation = self.optimizer.minimize(
            objective, var_list=full_var_list, **kwargs
        )

        model.initialize(session=session, force=initialize)
        self._initialize_optimizer(session, full_var_list)

        feed_dict = self._gen_feed_dict(model, feed_dict)
        for _i in range(maxiter):
            session.run(self.minimize_operation, feed_dict=feed_dict)

    if anchor:
        model.anchor(session)