Instruction:
Implement a function in Python 3 to
get a list of ad leads.

Response:
def get_ad_leads(self, start_date=None, end_date=None, filtering=(), page=1, page_size=100, version='v1.0'):
        """
         获取朋友圈销售线索数据接口

        :param start_date: 开始日期 默认今天
        :param end_date: 结束日期 默认今天
        :param filtering: 过滤条件 [{field: 过滤字段, operator: 操作符, values: 字段取值}]
        :param page: 页码，获取指定页数据
        :param page_size: 一页获取的数据条数(1-100)
        :param version: 版本号 v1.0
        """
        today = datetime.date.today()
        if start_date is None:
            start_date = today
        if end_date is None:
            end_date = today
        if isinstance(start_date, datetime.date):
            start_date = start_date.strftime("%Y-%m-%d")
        if isinstance(end_date, datetime.date):
            end_date = end_date.strftime("%Y-%m-%d")

        return self._get(
            'wechat_ad_leads/get',
            params=optionaldict(
                date_range=json.dumps({'start_date': start_date, 'end_date': end_date}),
                filtering=json.dumps(filtering) if filtering else None,
                page=page,
                page_size=page_size,
                version=version
            ),
            result_processor=lambda x: x['data']
        )


Instruction:
Can you tell what is the following Python 3 function doing
def create(self, trade_type, body, total_fee, notify_url, client_ip=None,
               user_id=None, out_trade_no=None, detail=None, attach=None,
               fee_type='CNY', time_start=None, time_expire=None, goods_tag=None,
               product_id=None, device_info=None, limit_pay=None, scene_info=None, sub_user_id=None):
        """
        统一下单接口

        :param trade_type: 交易类型，取值如下：JSAPI，NATIVE，APP，WAP, MWEB
        :param body: 商品描述
        :param total_fee: 总金额，单位分
        :param notify_url: 接收微信支付异步通知回调地址
        :param client_ip: 可选，APP和网页支付提交用户端ip，Native支付填调用微信支付API的机器IP
        :param user_id: 可选，用户在商户appid下的唯一标识。trade_type=JSAPI和appid已设定，此参数必传
        :param sub_user_id: 可选，小程序appid下的唯一标识。trade_type=JSAPI和sub_appid已设定，此参数必传
        :param out_trade_no: 可选，商户订单号，默认自动生成
        :param detail: 可选，商品详情
        :param attach: 可选，附加数据，在查询API和支付通知中原样返回，该字段主要用于商户携带订单的自定义数据
        :param fee_type: 可选，符合ISO 4217标准的三位字母代码，默认人民币：CNY
        :param time_start: 可选，订单生成时间，默认为当前时间
        :param time_expire: 可选，订单失效时间，默认为订单生成时间后两小时
        :param goods_tag: 可选，商品标记，代金券或立减优惠功能的参数
        :param product_id: 可选，trade_type=NATIVE，此参数必传。此id为二维码中包含的商品ID，商户自行定义
        :param device_info: 可选，终端设备号(门店号或收银设备ID)，注意：PC网页或公众号内支付请传"WEB"
        :param limit_pay: 可选，指定支付方式，no_credit--指定不能使用信用卡支付
        :param scene_info: 可选，上报支付的场景信息
        :type scene_info: dict
        :return: 返回的结果数据
        """
        now = datetime.fromtimestamp(time.time(), tz=timezone('Asia/Shanghai'))
        hours_later = now + timedelta(hours=2)
        if time_start is None:
            time_start = now
        if time_expire is None:
            time_expire = hours_later
        if not out_trade_no:
            out_trade_no = '{0}{1}{2}'.format(
                self.mch_id,
                now.strftime('%Y%m%d%H%M%S'),
                random.randint(1000, 10000)
            )
        if scene_info is not None:
            scene_info = json.dumps(scene_info, ensure_ascii=False)
        data = {
            'appid': self.appid,
            'sub_appid': self.sub_appid,
            'device_info': device_info,
            'body': body,
            'detail': detail,
            'attach': attach,
            'out_trade_no': out_trade_no,
            'fee_type': fee_type,
            'total_fee': total_fee,
            'spbill_create_ip': client_ip or get_external_ip(),
            'time_start': time_start.strftime('%Y%m%d%H%M%S'),
            'time_expire': time_expire.strftime('%Y%m%d%H%M%S'),
            'goods_tag': goods_tag,
            'notify_url': notify_url,
            'trade_type': trade_type,
            'limit_pay': limit_pay,
            'product_id': product_id,
            'openid': user_id,
            'sub_openid': sub_user_id,
            'scene_info': scene_info,
        }
        return self._post('pay/unifiedorder', data=data)

Response: Create a new node.


Instruction:
Explain what the following Python 3 code does
def reverse(self, transaction_id=None, out_trade_no=None):
        data = {
            'appid': self.appid,
            'transaction_id': transaction_id,
            'out_trade_no': out_trade_no,
        }
        return self._post('secapi/pay/reverse', data=data)

Response:
撤销订单

        :param transaction_id: 可选，微信的订单号，优先使用
        :param out_trade_no: 可选，商户系统内部的订单号,
                            transaction_id、out_trade_no二选一，
                            如果同时存在优先级：transaction_id> out_trade_no
        :return: 返回的结果数据


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get(self, user_id, lang='zh_CN'):
        assert lang in ('zh_CN', 'zh_TW', 'en'), 'lang can only be one of \
            zh_CN, zh_TW, en language codes'
        return self._get(
            'user/info',
            params={
                'openid': user_id,
                'lang': lang
            }
        )

Response:
获取用户基本信息（包括UnionID机制）
        详情请参考
        https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140839

        :param user_id: 普通用户的标识，对当前公众号唯一
        :param lang: 返回国家地区语言版本，zh_CN 简体，zh_TW 繁体，en 英语
        :return: 返回的 JSON 数据包

        使用示例::

            from wechatpy import WeChatClient

            client = WeChatClient('appid', 'secret')
            user = client.user.get('openid')


Instruction:
Make a summary of the following Python 3 code
def get_followers(self, first_user_id=None):
        params = {}
        if first_user_id:
            params['next_openid'] = first_user_id
        return self._get(
            'user/get',
            params=params
        )

Response: Get list of followers.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_batch(self, user_list):
        if all((isinstance(x, six.string_types) for x in user_list)):
            user_list = [{'openid': oid} for oid in user_list]
        res = self._post(
            'user/info/batchget',
            data={'user_list': user_list},
            result_processor=lambda x: x['user_info_list']
        )
        return res

Response: Get a list of user_list


Instruction:
Given the following Python 3 function, write the documentation
def change_openid(self, from_appid, openid_list):
        '''微信公众号主体变更迁移用户 openid

        详情请参考
        http://kf.qq.com/faq/170221aUnmmU170221eUZJNf.html

        :param from_appid: 原公众号的 appid
        :param openid_list: 需要转换的openid，这些必须是旧账号目前关注的才行，否则会出错；一次最多100个
        :return: 转换后的 openid 信息列表
        '''
        return self._post(
            'changeopenid',
            data={'from_appid': from_appid, 'openid_list': openid_list},
            result_processor=lambda x: x['result_list']
        )

Response:
微信公众号主体变更迁移用户 openid

        详情请参考
        http://kf.qq.com/faq/170221aUnmmU170221eUZJNf.html

        :param from_appid: 原公众号的 appid
        :param openid_list: 需要转换的openid，这些必须是旧账号目前关注的才行，否则会出错；一次最多100个
        :return: 转换后的 openid 信息列表


Instruction:
Explain what the following Python 3 code does
def send(self, agent_id, user_ids, party_ids='',
             tag_ids='', msg=None):
        """
        通用的消息发送接口。msg 内需要指定 msgtype 和对应类型消息必须的字段。
        如果部分接收人无权限或不存在，发送仍然执行，但会返回无效的部分（即invaliduser或invalidparty或invalidtag），常见的原因是接收人不在应用的可见范围内。
        user_ids、party_ids、tag_ids 不能同时为空，后面不再强调。

        :param agent_id: 必填，企业应用的id，整型。可在应用的设置页面查看。
        :param user_ids: 成员ID列表。
        :param party_ids: 部门ID列表。
        :param tag_ids: 标签ID列表。
        :param msg: 发送消息的 dict 对象
        :type msg: dict | None
        :return: 接口调用结果
        """
        msg = msg or {}
        if isinstance(user_ids, (tuple, list)):
            user_ids = '|'.join(user_ids)
        if isinstance(party_ids, (tuple, list)):
            party_ids = '|'.join(party_ids)
        if isinstance(tag_ids, (tuple, list)):
            tag_ids = '|'.join(tag_ids)

        data = {
            'touser': user_ids,
            'toparty': party_ids,
            'totag': tag_ids,
            'agentid': agent_id
        }
        data.update(msg)
        return self._post('message/send', data=data)

Response: Send a message to the specified agent.


Instruction:
Write a Python 3 function that can
send a textcard to the given user_ids.

Response:
def send_text_card(self, agent_id, user_ids, title, description, url, btntxt='详情',
                       party_ids='', tag_ids=''):
        """ 文本卡片消息

        https://work.weixin.qq.com/api/doc#90000/90135/90236/文本卡片消息

        请求示例：
        {
           "touser" : "UserID1|UserID2|UserID3",
           "toparty" : "PartyID1 | PartyID2",
           "totag" : "TagID1 | TagID2",
           "msgtype" : "textcard",
           "agentid" : 1,
           "textcard" : {
                "title" : "领奖通知",
                "description" : "<div class=\"gray\">2016年9月26日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台，
                    领奖码：xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>",
                "url" : "URL",
                "btntxt":"更多"
           }
        }

        特殊说明：
        卡片消息的展现形式非常灵活，支持使用br标签或者空格来进行换行处理，也支持使用div标签来使用不同的字体颜色，
        目前内置了3种文字颜色：灰色(gray)、高亮(highlight)、默认黑色(normal)，将其作为div标签的class属性即可，
        具体用法请参考上面的示例。

        :param agent_id: 必填，企业应用的id，整型。可在应用的设置页面查看。
        :param user_ids: 成员ID列表（消息接收者，多个接收者用‘|’分隔，最多支持1000个）。
        :param title: 标题，不超过128个字节，超过会自动截断。
        :param description: 必填，描述，不超过512个字节，超过会自动截断
        :param url: 必填，点击后跳转的链接。
        :param btntxt: 按钮文字。 默认为“详情”， 不超过4个文字，超过自动截断。
        :param party_ids: 部门ID列表。
        :param tag_ids: 标签ID列表。
        """
        return self.send(
            agent_id,
            user_ids,
            party_ids,
            tag_ids,
            msg={
                'msgtype': 'textcard',
                'textcard': {
                    'title': title,
                    'description': description,
                    'url': url,
                    'btntxt': btntxt,
                },
            }
        )


Instruction:
Can you generate a brief explanation for the following Python 3 code
def send_markdown(self, agent_id, user_ids, content, party_ids='', tag_ids=''):
        msg = {
            "msgtype": "markdown",
            "markdown": {"content": content}
        }
        return self._send_message(
            agent_id=agent_id,
            user_ids=user_ids,
            party_ids=party_ids,
            tag_ids=tag_ids,
            msg=msg
        )

Response: Send markdown to the specified user.


Instruction:
How would you explain what the following Python 3 function does
def multivariate_normal(x, mu, L):
    mu_ndims = mu.shape.ndims
    x_ndims = x.shape.ndims
    if x_ndims is not None and x_ndims != 2:
        raise ValueError('Shape of x must be 2D.')
    if mu_ndims is not None and mu_ndims != 2:
        raise ValueError('Shape of mu must be 2D.')

    d = x - mu
    alpha = tf.matrix_triangular_solve(L, d, lower=True)
    num_dims = tf.cast(tf.shape(d)[0], L.dtype)
    p = - 0.5 * tf.reduce_sum(tf.square(alpha), 0)
    p -= 0.5 * num_dims * np.log(2 * np.pi)
    p -= tf.reduce_sum(tf.log(tf.matrix_diag_part(L)))
    return p

Response: Computes the log - density of a multivariate normal distribution.


Instruction:
Write a Python 3 script for
making Tensorflow optimization tensor. This method builds optimization tensor and initializes all necessary variables created by optimizer. :param model: GPflow model. :param session: Tensorflow session. :param var_list: List of variables for training. :param kwargs: Dictionary of extra parameters passed to Tensorflow optimizer's minimize method. :return: Tensorflow optimization tensor or operation.

Response:
def make_optimize_tensor(self, model, session=None, var_list=None, **kwargs):
        """
        Make Tensorflow optimization tensor.
        This method builds optimization tensor and initializes all necessary variables
        created by optimizer.

            :param model: GPflow model.
            :param session: Tensorflow session.
            :param var_list: List of variables for training.
            :param kwargs: Dictionary of extra parameters passed to Tensorflow
                optimizer's minimize method.
            :return: Tensorflow optimization tensor or operation.
        """
        session = model.enquire_session(session)
        objective = model.objective
        full_var_list = self._gen_var_list(model, var_list)
        # Create optimizer variables before initialization.
        with session.as_default():
            minimize = self.optimizer.minimize(objective, var_list=full_var_list, **kwargs)
            model.initialize(session=session)
            self._initialize_optimizer(session)
            return minimize


Instruction:
Given the following Python 3 function, write the documentation
def make_optimize_action(self, model, session=None, var_list=None, **kwargs):
        if model is None or not isinstance(model, Model):
            raise ValueError('Unknown type passed for optimization.')
        session = model.enquire_session(session)
        feed_dict = kwargs.pop('feed_dict', None)
        feed_dict_update = self._gen_feed_dict(model, feed_dict)
        run_kwargs = {} if feed_dict_update is None else {'feed_dict': feed_dict_update}
        optimizer_tensor = self.make_optimize_tensor(model, session, var_list=var_list, **kwargs)
        opt = Optimization()
        opt.with_optimizer(self)
        opt.with_model(model)
        opt.with_optimizer_tensor(optimizer_tensor)
        opt.with_run_kwargs(**run_kwargs)
        return opt

Response: Build Optimization action task with Tensorflow optimizer.


Instruction:
Given the following Python 3 function, write the documentation
def minimize(self, model, session=None, var_list=None, feed_dict=None,
                 maxiter=1000, initialize=False, anchor=True, step_callback=None, **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 step_callback: A callback function to execute at each optimization step.
            The callback should accept variable argument list, where first argument is
            optimization step number.
        :type step_callback: Callable[[], None]
        :param kwargs: This is a dictionary of extra parameters for session run method.
        """

        if model is None or not isinstance(model, Model):
            raise ValueError('The `model` argument must be a GPflow model.')

        opt = self.make_optimize_action(model,
            session=session,
            var_list=var_list,
            feed_dict=feed_dict, **kwargs)

        self._model = opt.model
        self._minimize_operation = opt.optimizer_tensor

        session = model.enquire_session(session)
        with session.as_default():
            for step in range(maxiter):
                opt()
                if step_callback is not None:
                    step_callback(step)

        if anchor:
            opt.model.anchor(session)

Response: Minimizes objective function of the model.


Instruction:
Write a Python 3 function for
returning a tf. Session with the given arguments and kwargs.

Response:
def get_session(*args, **kwargs):
    """
    Pass session configuration options
    """
    if 'config' not in kwargs:
        kwargs['config'] = tf.ConfigProto(**settings.session)
    if settings.profiling.dump_timeline:
        def fill_kwargs(key, value):
            """
            Internal function for filling default None values with meaningful
            values from gpflow settings.
            """
            if kwargs.get(key) is None:
                kwargs[key] = value
        fill_kwargs('output_file_name', settings.profiling.output_file_name)
        fill_kwargs('output_directory', settings.profiling.output_directory)
        fill_kwargs('each_time', settings.profiling.each_time)
        return TracerSession(*args, **kwargs)
    kwargs.pop("output_file_name", None)
    kwargs.pop("output_directory", None)
    kwargs.pop("each_time", None)
    return tf.Session(*args, **kwargs)


Instruction:
How would you implement a function in Python 3 that
makes SciPy optimization tensor. The `make_optimize_tensor` method builds optimization tensor and initializes all necessary variables created by optimizer. :param model: GPflow model. :param session: Tensorflow session. :param var_list: List of variables for training. :param kwargs: Scipy optional optimization parameters, - `maxiter`, maximal number of iterations to perform. - `disp`, if True, prints convergence messages. :return: Tensorflow operation.

Response:
def make_optimize_tensor(self, model, session=None, var_list=None, **kwargs):
        """
        Make SciPy optimization tensor.
        The `make_optimize_tensor` method builds optimization tensor and initializes
        all necessary variables created by optimizer.

            :param model: GPflow model.
            :param session: Tensorflow session.
            :param var_list: List of variables for training.
            :param kwargs: Scipy optional optimization parameters,
                - `maxiter`, maximal number of iterations to perform.
                - `disp`, if True, prints convergence messages.
            :return: Tensorflow operation.
        """
        session = model.enquire_session(session)
        with session.as_default():
            var_list = self._gen_var_list(model, var_list)
            optimizer_kwargs = self._optimizer_kwargs.copy()
            options = optimizer_kwargs.get('options', {})
            options.update(kwargs)
            optimizer_kwargs.update(dict(options=options))
            objective = model.objective
            optimizer = external_optimizer.ScipyOptimizerInterface(
                objective, var_list=var_list, **optimizer_kwargs)
            model.initialize(session=session)
            return optimizer


Instruction:
Write a Python 3 script for
minimizing objective function of the model.

Response:
def minimize(self, model, session=None, var_list=None, feed_dict=None, maxiter=1000,
                 disp=False, initialize=False, anchor=True, step_callback=None, **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. Note: scipy optimizer can do early stopping
            if model converged.
        :param disp: ScipyOptimizer option. Set to True to print convergence messages.
        :param initialize: If `True` model parameters will be re-initialized even if they were
            initialized before for gotten session.
        :param anchor: If `True` trained parameters computed during optimization at
            particular session will be synchronized with internal parameter values.
        :param step_callback: A function to be called at each optimization step;
            arguments are the current values of all optimization variables
            flattened into a single vector.
        :type step_callback: Callable[[np.ndarray], None]
        :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.')

        if model.is_built_coherence() is Build.NO:
            raise GPflowError('Model is not built.')

        session = model.enquire_session(session)
        self._model = model
        optimizer = self.make_optimize_tensor(model, session,
            var_list=var_list, maxiter=maxiter, disp=disp)
        self._optimizer = optimizer
        feed_dict = self._gen_feed_dict(model, feed_dict)
        optimizer.minimize(session=session, feed_dict=feed_dict, step_callback=step_callback,
                           **kwargs)
        if anchor:
            model.anchor(session)


Instruction:
How would you code a function in Python 3 to
compile the GPMC into a dictionary of parameters.

Response:
def compile(self, session=None):
        """
        Before calling the standard compile function, check to see if the size
        of the data has changed and add parameters appropriately.

        This is necessary because the shape of the parameters depends on the
        shape of the data.
        """
        if not self.num_data == self.X.shape[0]:
            self.num_data = self.X.shape[0]
            self.V = Parameter(np.zeros((self.num_data, self.num_latent)))
            self.V.prior = Gaussian(0., 1.)

        return super(GPMC, self).compile(session=session)


Instruction:
Can you generate the documentation for the following Python 3 function
def _build_likelihood(self):
        K = self.kern.K(self.X)
        L = tf.cholesky(
            K + tf.eye(tf.shape(self.X)[0], dtype=settings.float_type) * settings.numerics.jitter_level)
        F = tf.matmul(L, self.V) + self.mean_function(self.X)

        return tf.reduce_sum(self.likelihood.logp(F, self.Y))

Response:
r Construct a tf function to compute the likelihood of a general GP
            model.


Instruction:
Create a Python 3 function to
build the predict function for the new set of entries in the cluster.

Response:
def _build_predict(self, Xnew, full_cov=False):
        """
        Xnew is a data matrix, point at which we want to predict

        This method computes

            p(F* | (F=LV) )

        where F* are points on the GP at Xnew, F=LV are points on the GP at X.

        """
        mu, var = conditional(Xnew, self.X, self.kern, self.V,
                              full_cov=full_cov,
                              q_sqrt=None, white=True)
        return mu + self.mean_function(Xnew), var


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _init_variational_parameters(self, num_inducing, q_mu, q_sqrt, q_diag):
        q_mu = np.zeros((num_inducing, self.num_latent)) if q_mu is None else q_mu
        self.q_mu = Parameter(q_mu, dtype=settings.float_type)  # M x P

        if q_sqrt is None:
            if self.q_diag:
                self.q_sqrt = Parameter(np.ones((num_inducing, self.num_latent), dtype=settings.float_type),
                                        transform=transforms.positive)  # M x P
            else:
                q_sqrt = np.array([np.eye(num_inducing, dtype=settings.float_type) for _ in range(self.num_latent)])
                self.q_sqrt = Parameter(q_sqrt, transform=transforms.LowerTriangular(num_inducing, self.num_latent))  # P x M x M
        else:
            if q_diag:
                assert q_sqrt.ndim == 2
                self.num_latent = q_sqrt.shape[1]
                self.q_sqrt = Parameter(q_sqrt, transform=transforms.positive)  # M x L/P
            else:
                assert q_sqrt.ndim == 3
                self.num_latent = q_sqrt.shape[0]
                num_inducing = q_sqrt.shape[1]
                self.q_sqrt = Parameter(q_sqrt, transform=transforms.LowerTriangular(num_inducing, self.num_latent))

Response: Initializes the variational parameters for the current class.


Instruction:
Explain what the following Python 3 code does
def _build_likelihood(self):

        # Get prior KL.
        KL = self.build_prior_KL()

        # Get conditionals
        fmean, fvar = self._build_predict(self.X, full_cov=False, full_output_cov=False)

        # Get variational expectations.
        var_exp = self.likelihood.variational_expectations(fmean, fvar, self.Y)

        # re-scale for minibatch size
        scale = tf.cast(self.num_data, settings.float_type) / tf.cast(tf.shape(self.X)[0], settings.float_type)

        return tf.reduce_sum(var_exp) * scale - KL

Response: Build the likelihood for the current model.


Instruction:
Can you generate the documentation for the following Python 3 function
def compile(self, session=None):
        session = self.enquire_session(session)
        if self.is_built_coherence(session.graph) is Build.NO:
            with session.graph.as_default():
                self.build()
        self.initialize(session, force=True)

Response: This method compiles the node for the passed session.


Instruction:
Write a Python 3 function for
initializing TensorFlow variables which are returned by initializables property and uses feed dictionary returned by initializable_feeds property.

Response:
def initialize(self, session=None, force=False):
        """
        Initializes TensorFlow variables, which are returned by `initializables` property and
        uses feed dictionary returned by `initializable_feeds` property defined at ICompilable
        interface and implemented by descendants.

        :param session: TensorFlow session used for initializing. In case when session is None,
            default TensorFlow session will be checked first, if session is still None, then
            default GPflowFlow session will used, but there is *no garuantee* that GPflow
            session's graph is compliant with node's tensors graph.
        :param force: inidicates either the initialized TensorFlow variables must be
            re-initialized or not.

        :raises: GPflowError exception if session's graph is different from the graph
            used by node tensors.
        """
        session = self.enquire_session(session)
        initializables = self.initializables
        if initializables:
            misc.initialize_variables(
                variables=initializables,
                session=session,
                force=force,
                feed_dict=self.initializable_feeds)


Instruction:
Here you have a function in Python 3, explain what it does
def clear(self):
        parent = self.parent
        if parent is not self and parent.is_built_coherence(self.graph) is Build.YES:
            raise GPflowError('Clear method cannot be started. Upper nodes are built.')
        self._clear()

Response: Calls _clear abstract method which must be implemented by descendants.


Instruction:
Write a Python 3 function that can
verify and returns relevant TensorFlow graph.

Response:
def enquire_graph(self, graph=None):
        """
        Verifies and returns relevant TensorFlow graph. If non-None graph were passed,
        the same graph is returned. Otherwise, nodes's graph is exposed and it is
        undefined the default TensorFlow graph is used.

        :param graph: TensorFlow graph or None. Default is None.
        :return: TensorFlow graph.
        """
        if graph is None:
            graph = self.root.graph if self.graph is None else self.graph
            if graph is None:
                graph = tf.get_default_graph()
        return graph


Instruction:
How would you implement a function in Python 3 that
ensures and returns relevant TensorFlow session.

Response:
def enquire_session(self, session=None):
        """
        Verifies and returns relevant TensorFlow session. If non-None session
        were passed, session is checked for graph compliance and returned back.
        Otherwise, default TensorFlow session is returned. When TensorFlow default
        session is not set up, GPflow session's manager creates or uses existing
        one for returning.

        :param session: TensorFlow session or None. Default value is None.
        :return: TensorFlow session.
        :raises GPflowError: Session's graph is not compilable with node's graph.
        """
        if session is None:
            session = tf.get_default_session()
            if session is None:
                session = session_manager.get_default_session()
        self.is_built_coherence(session.graph)
        return session


Instruction:
How would you explain what the following Python 3 function does
def is_built_coherence(self, graph=None):
        graph = self.enquire_graph(graph=graph)
        is_built = self.is_built(graph)
        if is_built is Build.NOT_COMPATIBLE_GRAPH:
            raise GPflowError('Tensor "{}" uses different graph.'.format(self.pathname))
        return is_built

Response: Checks that node was built using input graph.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def build(self):
        if self.is_built_coherence() is Build.NO:
            with tf.name_scope(self.tf_name_scope):
                self._build()

Response: Implementation for ICompilable interface build method. Builds tensors within the current node s name scope using parentable node s name.


Instruction:
Can you tell what is the following Python 3 function doing
def tf_name_scope(self):
        if self.parent is self:
            leader_name = self.name
            leader_index = self.index
            if leader_index is None:
                return leader_name
            return "{name}-{index}".format(name=leader_name, index=leader_index)
        return self.pathname.rsplit('/')[-1]

Response: Auxilary method for composing the tree name scopes.


Instruction:
Can you generate the documentation for the following Python 3 function
def tf_pathname(self):
        if self.parent is self:
            return self.tf_name_scope
        tail = self.pathname.split('/', 1)[-1]
        leader = self.root.tf_name_scope
        return "{leader_name}/{tail_name}".format(leader_name=leader, tail_name=tail)

Response: Returns the full path name for this tensor at build time.


Instruction:
Make a summary of the following Python 3 code
def autobuild_decorate(func, caller):
    evaldict = dict(_call_=caller, _func_=func)
    fun = AutobuildFunctionMaker.create(
        func, "return _call_(_func_, %(shortsignature)s)",
        evaldict, __wrapped__=func)
    if hasattr(func, '__qualname__'):
        fun.__qualname__ = func.__qualname__
    return fun

Response: Decorates a function using a caller.


Instruction:
How would you explain what the following Python 3 function does
def _build_prior(self, prior_tensors):
        # TODO(@awav): What prior must represent empty list of parameters?
        if not prior_tensors:
            return tf.constant(0, dtype=settings.float_type)
        return tf.add_n(prior_tensors, name='prior')

Response: Builds a tf expression for the prior by summing all child - parameter priors.


Instruction:
Can you generate the documentation for the following Python 3 function
def make_deprecated_class(oldname, NewClass):
    msg = ("{module}.{} has been renamed to {module}.{}"
           .format(oldname, NewClass.__name__, module=NewClass.__module__))

    class OldClass(NewClass):
        def __new__(cls, *args, **kwargs):
            raise NotImplementedError(msg)
    OldClass.__doc__ = msg
    OldClass.__qualname__ = OldClass.__name__ = oldname
    return OldClass

Response: Returns a class that raises NotImplementedError on instantiation.


Instruction:
Given the following Python 3 function, write the documentation
def _validate_ard_shape(self, name, value, ARD=None):
        if ARD is None:
            ARD = np.asarray(value).squeeze().shape != ()

        if ARD:
            # accept float or array:
            value = value * np.ones(self.input_dim, dtype=settings.float_type)

        if self.input_dim == 1 or not ARD:
            correct_shape = ()
        else:
            correct_shape = (self.input_dim,)

        if np.asarray(value).squeeze().shape != correct_shape:
            raise ValueError("shape of {} does not match input_dim".format(name))

        return value, ARD

Response: Validates the shape of an ARD hyperparameter.


Instruction:
Explain what the following Python 3 code does
def on_separate_dims(self, other_kernel):
        if isinstance(self.active_dims, slice) or isinstance(other_kernel.active_dims, slice):
            # Be very conservative for kernels defined over slices of dimensions
            return False

        if np.any(self.active_dims.reshape(-1, 1) == other_kernel.active_dims.reshape(1, -1)):
            return False

        return True

Response: Checks if the kernels defined on different dimensions overlap.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _slice(self, X, X2):
        if isinstance(self.active_dims, slice):
            X = X[..., self.active_dims]
            if X2 is not None:
                X2 = X2[..., self.active_dims]
        else:
            X = tf.gather(X, self.active_dims, axis=-1)
            if X2 is not None:
                X2 = tf.gather(X2, self.active_dims, axis=-1)

        input_dim_shape = tf.shape(X)[-1]
        input_dim = tf.convert_to_tensor(self.input_dim, dtype=settings.int_type)
        with tf.control_dependencies([tf.assert_equal(input_dim_shape, input_dim)]):
            X = tf.identity(X)

        return X, X2

Response: Slice the correct dimensions for use in the kernel.


Instruction:
Can you tell what is the following Python 3 function doing
def _slice_cov(self, cov):
        cov = tf.cond(tf.equal(tf.rank(cov), 2), lambda: tf.matrix_diag(cov), lambda: cov)

        if isinstance(self.active_dims, slice):
            cov = cov[..., self.active_dims, self.active_dims]
        else:
            cov_shape = tf.shape(cov)
            covr = tf.reshape(cov, [-1, cov_shape[-1], cov_shape[-1]])
            gather1 = tf.gather(tf.transpose(covr, [2, 1, 0]), self.active_dims)
            gather2 = tf.gather(tf.transpose(gather1, [1, 0, 2]), self.active_dims)
            cov = tf.reshape(tf.transpose(gather2, [2, 0, 1]),
                             tf.concat([cov_shape[:-2], [len(self.active_dims), len(self.active_dims)]], 0))
        return cov

Response: Slice the correct dimensions for use in the kernel for use in the kernel.


Instruction:
Make a summary of the following Python 3 code
def _scaled_square_dist(self, X, X2):

        X = X / self.lengthscales

        if X2 is None:
            Xs = tf.reduce_sum(tf.square(X), axis=-1, keepdims=True)
            dist = -2 * tf.matmul(X, X, transpose_b=True)
            dist += Xs + tf.matrix_transpose(Xs)
            return dist

        Xs = tf.reduce_sum(tf.square(X), axis=-1)
        X2 = X2 / self.lengthscales
        X2s = tf.reduce_sum(tf.square(X2), axis=-1)
        dist = -2 * tf.tensordot(X, X2, [[-1], [-1]])
        dist += _broadcasting_elementwise_op(tf.add, Xs, X2s)
        return dist

Response: Returns the scaled square distance between two sets of entries.


Instruction:
Given the following Python 3 function, write the documentation
def scaled_euclid_dist(self, X, X2):  # pragma: no cover
        warnings.warn('scaled_euclid_dist is deprecated and will be removed '
                      'in GPflow version 1.4.0. For stationary kernels, '
                      'define K_r(r) instead.',
                      DeprecationWarning)
        r2 = self.scaled_square_dist(X, X2)
        return self._clipped_sqrt(r2)

Response: Returns the Euclidean distance between X and X2.


Instruction:
Here you have a function in Python 3, explain what it does
def K(self, X, X2=None, presliced=False):
        if not presliced:
            X, X2 = self._slice(X, X2)
        return self.K_r2(self.scaled_square_dist(X, X2))

Response: Calculates the kernel matrix for a set of stationary kernels.


Instruction:
Can you implement a function in Python 3 that
returns the kernel evaluated on r2 which is the scaled squared distance.

Response:
def K_r2(self, r2):
        """
        Returns the kernel evaluated on `r2`, which is the scaled squared distance.
        Will call self.K_r(r=sqrt(r2)), or can be overwritten directly (and should operate element-wise on r2).
        """
        r = self._clipped_sqrt(r2)
        return self.K_r(r)


Instruction:
Implement a function in Python 3 to
return the Jacobian of the object in the order dependent family of functions defined in equations 4 to 7 in the reference paper.

Response:
def _J(self, theta):
        """
        Implements the order dependent family of functions defined in equations
        4 to 7 in the reference paper.
        """
        if self.order == 0:
            return np.pi - theta
        elif self.order == 1:
            return tf.sin(theta) + (np.pi - theta) * tf.cos(theta)
        elif self.order == 2:
            return 3. * tf.sin(theta) * tf.cos(theta) + \
                   (np.pi - theta) * (1. + 2. * tf.cos(theta) ** 2)


Instruction:
Create a Python 3 function for
checking whether the kernels in the combination act on disjoint subsets of dimensions.

Response:
def on_separate_dimensions(self):
        """
        Checks whether the kernels in the combination act on disjoint subsets
        of dimensions. Currently, it is hard to asses whether two slice objects
        will overlap, so this will always return False.
        :return: Boolean indicator.
        """
        if np.any([isinstance(k.active_dims, slice) for k in self.kernels]):
            # Be conservative in the case of a slice object
            return False
        else:
            dimlist = [k.active_dims for k in self.kernels]
            overlapping = False
            for i, dims_i in enumerate(dimlist):
                for dims_j in dimlist[i + 1:]:
                    if np.any(dims_i.reshape(-1, 1) == dims_j.reshape(1, -1)):
                        overlapping = True
            return not overlapping


Instruction:
Create a Python 3 function for
transforming from the packed to unpacked representations ( numpy ) N

Response:
def forward(self, x):
        """
        Transforms from the packed to unpacked representations (numpy)

        :param x: packed numpy array. Must have shape `self.num_matrices x triangular_number
        :return: Reconstructed numpy array y of shape self.num_matrices x N x N
        """
        fwd = np.zeros((self.num_matrices, self.N, self.N), settings.float_type)
        indices = np.tril_indices(self.N, 0)
        z = np.zeros(len(indices[0])).astype(int)
        for i in range(self.num_matrices):
            fwd[(z + i,) + indices] = x[i, :]
        return fwd.squeeze(axis=0) if self.squeeze else fwd


Instruction:
How would you explain what the following Python 3 function does
def backward(self, y):
        if self.squeeze:
            y = y[None, :, :]
        ind = np.tril_indices(self.N)
        return np.vstack([y_i[ind] for y_i in y])

Response: Transforms a series of triangular matrices y to the packed representation x


Instruction:
Can you generate the documentation for the following Python 3 function
def forward_tensor(self, x):
        fwd = vec_to_tri(x, self.N)
        return tf.squeeze(fwd, axis=0) if self.squeeze else fwd

Response: Transforms from the packed representation of N x N to unpacked representations of N x N


Instruction:
Can you tell what is the following Python 3 function doing
def backward_tensor(self, y):
        if self.squeeze:
            y = tf.expand_dims(y, axis=0)
        indices = np.vstack(np.tril_indices(self.N)).T
        indices = itertools.product(np.arange(self.num_matrices), indices)
        indices = np.array([np.hstack(x) for x in indices])
        triangular = tf.gather_nd(y, indices)
        return tf.reshape(triangular, [self.num_matrices, (self.N**2 + self.N) // 2])

Response: Transforms a series of triangular matrices y to the packed representation x.


Instruction:
Create a Python 3 function for
extracting a dictionary with tensors specified by name from an object existing dictionary with tensors specified by name.

Response:
def get_autoflow(cls, obj, name):
        """
        Extracts from an object existing dictionary with tensors specified by name.
        If there is no such object then new one will be created. Intenally, it appends
        autoflow prefix to the name and saves it as an attribute.

        :param obj: target GPflow object.
        :param name: unique part of autoflow attribute's name.

        :raises: ValueError exception if `name` is not a string.
        """
        if not isinstance(name, str):
            raise ValueError('Name must be string.')
        prefix = cls.__autoflow_prefix__
        autoflow_name = prefix + name
        store = misc.get_attribute(obj, autoflow_name, allow_fail=True, default={})
        if not store:
            setattr(obj, autoflow_name, store)
        return store


Instruction:
Can you generate a brief explanation for the following Python 3 code
def clear_autoflow(cls, obj, name=None):
        if name is not None and not isinstance(name, str):
            raise ValueError('Name must be a string.')
        prefix = cls.__autoflow_prefix__
        if name:
            prefix = "" if name.startswith(prefix) else prefix
            delattr(obj, prefix + name)
        else:
            keys = [attr for attr in obj.__dict__ if attr.startswith(prefix)]
            for key in keys:
                delattr(obj, key)

Response: Clears autoflow s tensor storage.


Instruction:
Create a Python 3 function for
compiling the VGP into a single parameter vector.

Response:
def compile(self, session=None):
        """
        Before calling the standard compile function, check to see if the size
        of the data has changed and add variational parameters appropriately.

        This is necessary because the shape of the parameters depends on the
        shape of the data.
        """
        if not self.num_data == self.X.shape[0]:
            self.num_data = self.X.shape[0]
            self.q_mu = Parameter(np.zeros((self.num_data, self.num_latent)))
            self.q_sqrt = Parameter(np.eye(self.num_data)[:, :, None] *
                                    np.ones((1, 1, self.num_latent)))

        return super(VGP, self).compile(session=session)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def compile(self, session=None):
        if not self.num_data == self.X.shape[0]:
            self.num_data = self.X.shape[0]
            self.q_alpha = Parameter(np.zeros((self.num_data, self.num_latent)))
            self.q_lambda = Parameter(np.ones((self.num_data, self.num_latent)),
                                      transforms.positive)
        return super(VGP_opper_archambeau, self).compile(session=session)

Response: Compile the VGP_opper_archambeau object.


Instruction:
Write a Python 3 function that can
build predict and variance of the new object.

Response:
def _build_predict(self, Xnew, full_cov=False):
        """
        The posterior variance of F is given by
            q(f) = N(f | K alpha + mean, [K^-1 + diag(lambda**2)]^-1)
        Here we project this to F*, the values of the GP at Xnew which is given
        by
           q(F*) = N ( F* | K_{*F} alpha + mean, K_{**} - K_{*f}[K_{ff} +
                                           diag(lambda**-2)]^-1 K_{f*} )
        """

        # compute kernel things
        Kx = self.kern.K(self.X, Xnew)
        K = self.kern.K(self.X)

        # predictive mean
        f_mean = tf.matmul(Kx, self.q_alpha, transpose_a=True) + self.mean_function(Xnew)

        # predictive var
        A = K + tf.matrix_diag(tf.transpose(1. / tf.square(self.q_lambda)))
        L = tf.cholesky(A)
        Kx_tiled = tf.tile(tf.expand_dims(Kx, 0), [self.num_latent, 1, 1])
        LiKx = tf.matrix_triangular_solve(L, Kx_tiled)
        if full_cov:
            f_var = self.kern.K(Xnew) - tf.matmul(LiKx, LiKx, transpose_a=True)
        else:
            f_var = self.kern.Kdiag(Xnew) - tf.reduce_sum(tf.square(LiKx), 1)
        return f_mean, tf.transpose(f_var)


Instruction:
Can you generate the documentation for the following Python 3 function
def _build_likelihood(self):
        # get the (marginals of) q(f): exactly predicting!
        fmean, fvar = self._build_predict(self.X, full_cov=False)
        return tf.reduce_sum(self.likelihood.variational_expectations(fmean, fvar, self.Y))

Response: This function computes the optimal density for v up to a constant


Instruction:
How would you explain what the following Python 3 function does
def _parse(string):
    if not isinstance(string, str):
        raise ValueError('Config value "{0}" expected to be string.'
                         .format(string))
    if string in ['true', 'True']:
        return True
    elif string in ['false', 'False']:
        return False
    elif string in ['float64', 'float32', 'float16',
                    'int64', 'int32', 'int16']:
        return getattr(np, string)
    else:
        try:
            return int(string)
        except ValueError:
            pass
        try:
            return float(string)
        except ValueError:
            return string

Response: Parses a string into a base64 - encoded version of the attribute.


Instruction:
Implement a function in Python 3 to
take a dictionary and returns a nested tuple of named tuples.

Response:
def _namedtuplify(mapping):
    """
    Make the dictionary into a nested series of named tuples.
    This is what allows accessing by attribute: settings.numerics.jitter
    Thank you https://gist.github.com/hangtwenty/5960435
    """
    if isinstance(mapping, collections.Mapping):
        for key, value in list(mapping.items()):
            mapping[key] = _namedtuplify(value)
        try:
            mapping.pop('__name__')
        except KeyError:
            pass
        # return collections.namedtuple('settingsa', dict(**mapping))(**mapping)
        return _MutableNamedTuple(mapping)
    return _parse(mapping)


Instruction:
Can you tell what is the following Python 3 function doing
def _read_config_file(path=None):
    cfg = configparser.ConfigParser()

    if path is None:  # pragma: no cover
        dirs = [os.curdir, os.path.expanduser('~'),
                os.path.dirname(os.path.realpath(__file__))]
        locations = map(os.path.abspath, dirs)
        for loc in locations:
            if cfg.read(os.path.join(loc, 'gpflowrc')):
                break
            if cfg.read(os.path.join(loc, '.gpflowrc')):
                break
    else:
        if not cfg.read(path):
            raise RuntimeError("Config at '{0}' cannot be read".format(path))
    return cfg

Response:
Reads config file.
    First look for config file in the current directory, then in the
    user's home directory, then in the same directory as this file.
    Tries to find config file both with and without preceeding 'dot'
    for hidden files (prefer non-hidden).


Instruction:
Write a Python 3 script for
returning the evaluation locations x and weights wn for a multivariate Gauss - Hermite quadrature.

Response:
def mvhermgauss(H: int, D: int):
    """
    Return the evaluation locations 'xn', and weights 'wn' for a multivariate
    Gauss-Hermite quadrature.

    The outputs can be used to approximate the following type of integral:
    int exp(-x)*f(x) dx ~ sum_i w[i,:]*f(x[i,:])

    :param H: Number of Gauss-Hermite evaluation points.
    :param D: Number of input dimensions. Needs to be known at call-time.
    :return: eval_locations 'x' (H**DxD), weights 'w' (H**D)
    """
    gh_x, gh_w = hermgauss(H)
    x = np.array(list(itertools.product(*(gh_x,) * D)))  # H**DxD
    w = np.prod(np.array(list(itertools.product(*(gh_w,) * D))), 1)  # H**D
    return x, w


Instruction:
Can you implement a function in Python 3 that
computes N Gaussian expectation integrals of a single function f.

Response:
def mvnquad(func, means, covs, H: int, Din: int=None, Dout=None):
    """
    Computes N Gaussian expectation integrals of a single function 'f'
    using Gauss-Hermite quadrature.
    :param f: integrand function. Takes one input of shape ?xD.
    :param means: NxD
    :param covs: NxDxD
    :param H: Number of Gauss-Hermite evaluation points.
    :param Din: Number of input dimensions. Needs to be known at call-time.
    :param Dout: Number of output dimensions. Defaults to (). Dout is assumed
    to leave out the item index, i.e. f actually maps (?xD)->(?x*Dout).
    :return: quadratures (N,*Dout)
    """
    # Figure out input shape information
    if Din is None:
        Din = means.shape[1] if type(means.shape) is tuple else means.shape[1].value

    if Din is None:
        raise GPflowError("If `Din` is passed as `None`, `means` must have a known shape. "
                          "Running mvnquad in `autoflow` without specifying `Din` and `Dout` "
                          "is problematic. Consider using your own session.")  # pragma: no cover

    xn, wn = mvhermgauss(H, Din)
    N = tf.shape(means)[0]

    # transform points based on Gaussian parameters
    cholXcov = tf.cholesky(covs)  # NxDxD
    Xt = tf.matmul(cholXcov, tf.tile(xn[None, :, :], (N, 1, 1)), transpose_b=True)  # NxDxH**D
    X = 2.0 ** 0.5 * Xt + tf.expand_dims(means, 2)  # NxDxH**D
    Xr = tf.reshape(tf.transpose(X, [2, 0, 1]), (-1, Din))  # (H**D*N)xD

    # perform quadrature
    fevals = func(Xr)
    if Dout is None:
        Dout = tuple((d if type(d) is int else d.value) for d in fevals.shape[1:])

    if any([d is None for d in Dout]):
        raise GPflowError("If `Dout` is passed as `None`, the output of `func` must have known "
                          "shape. Running mvnquad in `autoflow` without specifying `Din` and `Dout` "
                          "is problematic. Consider using your own session.")  # pragma: no cover
    fX = tf.reshape(fevals, (H ** Din, N,) + Dout)
    wr = np.reshape(wn * np.pi ** (-Din * 0.5),
                    (-1,) + (1,) * (1 + len(Dout)))
    return tf.reduce_sum(fX * wr, 0)


Instruction:
Make a summary of the following Python 3 code
def ndiagquad(funcs, H: int, Fmu, Fvar, logspace: bool=False, **Ys):
    def unify(f_list):
        """
        Stack a list of means/vars into a full block
        """
        return tf.reshape(
                tf.concat([tf.reshape(f, (-1, 1)) for f in f_list], axis=1),
                (-1, 1, Din))

    if isinstance(Fmu, (tuple, list)):
        Din = len(Fmu)
        shape = tf.shape(Fmu[0])
        Fmu, Fvar = map(unify, [Fmu, Fvar])    # both N x 1 x Din
    else:
        Din = 1
        shape = tf.shape(Fmu)
        Fmu, Fvar = [tf.reshape(f, (-1, 1, 1)) for f in [Fmu, Fvar]]

    xn, wn = mvhermgauss(H, Din)
    # xn: H**Din x Din, wn: H**Din

    gh_x = xn.reshape(1, -1, Din)             # 1 x H**Din x Din
    Xall = gh_x * tf.sqrt(2.0 * Fvar) + Fmu   # N x H**Din x Din
    Xs = [Xall[:, :, i] for i in range(Din)]  # N x H**Din  each

    gh_w = wn * np.pi ** (-0.5 * Din)  # H**Din x 1

    for name, Y in Ys.items():
        Y = tf.reshape(Y, (-1, 1))
        Y = tf.tile(Y, [1, H**Din])  # broadcast Y to match X
        # without the tiling, some calls such as tf.where() (in bernoulli) fail
        Ys[name] = Y  # now N x H**Din

    def eval_func(f):
        feval = f(*Xs, **Ys)  # f should be elementwise: return shape N x H**Din
        if logspace:
            log_gh_w = np.log(gh_w.reshape(1, -1))
            result = tf.reduce_logsumexp(feval + log_gh_w, axis=1)
        else:
            result = tf.matmul(feval, gh_w.reshape(-1, 1))
        return tf.reshape(result, shape)

    if isinstance(funcs, Iterable):
        return [eval_func(f) for f in funcs]
    else:
        return eval_func(funcs)

Response: This function calculates the N Gaussian expectation integrals of one or more functions and returns the shape of the resulting array.


Instruction:
Explain what the following Python 3 code does
def ndiag_mc(funcs, S: int, Fmu, Fvar, logspace: bool=False, epsilon=None, **Ys):
    N, D = tf.shape(Fmu)[0], tf.shape(Fvar)[1]

    if epsilon is None:
        epsilon = tf.random_normal((S, N, D), dtype=settings.float_type)

    mc_x = Fmu[None, :, :] + tf.sqrt(Fvar[None, :, :]) * epsilon
    mc_Xr = tf.reshape(mc_x, (S * N, D))

    for name, Y in Ys.items():
        D_out = tf.shape(Y)[1]
        # we can't rely on broadcasting and need tiling
        mc_Yr = tf.tile(Y[None, ...], [S, 1, 1])  # S x N x D_out
        Ys[name] = tf.reshape(mc_Yr, (S * N, D_out))  # S * N x D_out

    def eval_func(func):
        feval = func(mc_Xr, **Ys)
        feval = tf.reshape(feval, (S, N, -1))
        if logspace:
            log_S = tf.log(tf.cast(S, settings.float_type))
            return tf.reduce_logsumexp(feval, axis=0) - log_S  # N x D
        else:
            return tf.reduce_mean(feval, axis=0)

    if isinstance(funcs, Iterable):
        return [eval_func(f) for f in funcs]
    else:
        return eval_func(funcs)

Response: Computes N Gaussian expectation integrals of one or more functions.


Instruction:
Given the following Python 3 function, write the documentation
def params_as_tensors(method):
    @functools.wraps(method)
    def tensor_mode_wrapper(obj, *args, **kwargs):
        if not isinstance(obj, Parameterized):
            raise GPflowError(
                'Tensor mode works only for Parameterized object.')
        prev_value = _params_as_tensors_enter(obj, True)
        try:
            result = method(obj, *args, **kwargs)
        finally:
            _params_as_tensors_exit(obj, prev_value)
        return result
    return tensor_mode_wrapper

Response:
A function decorator that converts representation for parameters into
    their unconstrained tensors and data holders to their data holders.


Instruction:
Implement a function in Python 3 to
compute the KL divergence for a single set of independent distributions.

Response:
def gauss_kl(q_mu, q_sqrt, K=None, *, K_cholesky=None):
    """
    Compute the KL divergence KL[q || p] between

          q(x) = N(q_mu, q_sqrt^2)
    and
          p(x) = N(0, K)

    We assume N multiple independent distributions, given by the columns of
    q_mu and the last dimension of q_sqrt. Returns the sum of the divergences.

    q_mu is a matrix [M, L], each column contains a mean.

    q_sqrt can be a 3D tensor [L, M, M], each matrix within is a lower
        triangular square-root matrix of the covariance of q.
    q_sqrt can be a matrix [M, L], each column represents the diagonal of a
        square-root matrix of the covariance of q.

    K is the covariance of p, [M, M] or [L, M, M]
    K_cholesky is the cholesky of the covariance of p, [M, M] or [L, M, M]

    Note: if no K matrix is given (both `K` and `K_cholesky` are None),
    `gauss_kl` computes the KL divergence from p(x) = N(0, I) instead.
    The K matrix can be passed either directly as `K`, or as its Cholesky factor, `K_cholesky`.
    In either case, it can be a single matrix [M, M], in which case the sum of the L KL divergences
    is computed by broadcasting, or L different covariances [L, M, M].
    """

    if (K is not None) and (K_cholesky is not None):
        raise ValueError("Ambiguous arguments: gauss_kl() must only "
                         "be passed one of `K` or `K_cholesky`.")

    white = (K is None) and (K_cholesky is None)
    diag = q_sqrt.get_shape().ndims == 2

    M, B = tf.shape(q_mu)[0], tf.shape(q_mu)[1]

    if white:
        alpha = q_mu  # [M, B]
    else:
        if K is not None:
            Lp = tf.cholesky(K)  # [B, M, M] or [M, M]
        elif K_cholesky is not None:
            Lp = K_cholesky  # [B, M, M] or [M, M]

        batched = Lp.get_shape().ndims == 3
        q_mu = tf.transpose(q_mu)[:, :, None] if batched else q_mu  # [B, M, 1] or [M, B]
        alpha = tf.matrix_triangular_solve(Lp, q_mu, lower=True)  # [B, M, 1] or [M, B]

    if diag:
        Lq = Lq_diag = q_sqrt
        Lq_full = tf.matrix_diag(tf.transpose(q_sqrt))  # [B, M, M]
    else:
        Lq = Lq_full = tf.matrix_band_part(q_sqrt, -1, 0)  # force lower triangle # [B, M, M]
        Lq_diag = tf.matrix_diag_part(Lq)  # [M, B]

    # Mahalanobis term: μqᵀ Σp⁻¹ μq
    mahalanobis = tf.reduce_sum(tf.square(alpha))

    # Constant term: - B * M
    constant = - tf.cast(tf.size(q_mu, out_type=tf.int64), dtype=settings.float_type)

    # Log-determinant of the covariance of q(x):
    logdet_qcov = tf.reduce_sum(tf.log(tf.square(Lq_diag)))

    # Trace term: tr(Σp⁻¹ Σq)
    if white:
        trace = tf.reduce_sum(tf.square(Lq))
    else:
        if diag and not batched:
            # K is [M, M] and q_sqrt is [M, B]: fast specialisation
            LpT = tf.transpose(Lp)  # [M, M]
            Lp_inv = tf.matrix_triangular_solve(Lp, tf.eye(M, dtype=settings.float_type),lower=True)  # [M, M]
            K_inv = tf.matrix_diag_part(tf.matrix_triangular_solve(LpT, Lp_inv, lower=False))[:, None]  # [M, M] -> [M, 1]
            trace = tf.reduce_sum(K_inv * tf.square(q_sqrt))
        else:
            # TODO: broadcast instead of tile when tf allows (not implemented in tf <= 1.12)
            Lp_full = Lp if batched else tf.tile(tf.expand_dims(Lp, 0), [B, 1, 1])
            LpiLq = tf.matrix_triangular_solve(Lp_full, Lq_full, lower=True)
            trace = tf.reduce_sum(tf.square(LpiLq))

    twoKL = mahalanobis + constant - logdet_qcov + trace

    # Log-determinant of the covariance of p(x):
    if not white:
        log_sqdiag_Lp = tf.log(tf.square(tf.matrix_diag_part(Lp)))
        sum_log_sqdiag_Lp = tf.reduce_sum(log_sqdiag_Lp)
        # If K is B x M x M, num_latent is no longer implicit, no need to multiply the single kernel logdet
        scale = 1.0 if batched else tf.cast(B, settings.float_type)
        twoKL += scale * sum_log_sqdiag_Lp

    return 0.5 * twoKL


Instruction:
Write a Python 3 script to
build a tensorflow function to compute the bound on the marginallikelihood.

Response:
def _build_likelihood(self):
        """
        Construct a tensorflow function to compute the bound on the marginal
        likelihood.
        """
        pX = DiagonalGaussian(self.X_mean, self.X_var)

        num_inducing = len(self.feature)
        psi0 = tf.reduce_sum(expectation(pX, self.kern))
        psi1 = expectation(pX, (self.kern, self.feature))
        psi2 = tf.reduce_sum(expectation(pX, (self.kern, self.feature), (self.kern, self.feature)), axis=0)
        Kuu = features.Kuu(self.feature, self.kern, jitter=settings.jitter)
        L = tf.cholesky(Kuu)
        sigma2 = self.likelihood.variance
        sigma = tf.sqrt(sigma2)

        # Compute intermediate matrices
        A = tf.matrix_triangular_solve(L, tf.transpose(psi1), lower=True) / sigma
        tmp = tf.matrix_triangular_solve(L, psi2, lower=True)
        AAT = tf.matrix_triangular_solve(L, tf.transpose(tmp), lower=True) / sigma2
        B = AAT + tf.eye(num_inducing, dtype=settings.float_type)
        LB = tf.cholesky(B)
        log_det_B = 2. * tf.reduce_sum(tf.log(tf.matrix_diag_part(LB)))
        c = tf.matrix_triangular_solve(LB, tf.matmul(A, self.Y), lower=True) / sigma

        # KL[q(x) || p(x)]
        dX_var = self.X_var if len(self.X_var.get_shape()) == 2 else tf.matrix_diag_part(self.X_var)
        NQ = tf.cast(tf.size(self.X_mean), settings.float_type)
        D = tf.cast(tf.shape(self.Y)[1], settings.float_type)
        KL = -0.5 * tf.reduce_sum(tf.log(dX_var)) \
             + 0.5 * tf.reduce_sum(tf.log(self.X_prior_var)) \
             - 0.5 * NQ \
             + 0.5 * tf.reduce_sum((tf.square(self.X_mean - self.X_prior_mean) + dX_var) / self.X_prior_var)

        # compute log marginal bound
        ND = tf.cast(tf.size(self.Y), settings.float_type)
        bound = -0.5 * ND * tf.log(2 * np.pi * sigma2)
        bound += -0.5 * D * log_det_B
        bound += -0.5 * tf.reduce_sum(tf.square(self.Y)) / sigma2
        bound += 0.5 * tf.reduce_sum(tf.square(c))
        bound += -0.5 * D * (tf.reduce_sum(psi0) / sigma2 -
                             tf.reduce_sum(tf.matrix_diag_part(AAT)))
        bound -= KL
        return bound


Instruction:
Here you have a function in Python 3, explain what it does
def _build_predict(self, Xnew, full_cov=False):
        pX = DiagonalGaussian(self.X_mean, self.X_var)

        num_inducing = len(self.feature)
        psi1 = expectation(pX, (self.kern, self.feature))
        psi2 = tf.reduce_sum(expectation(pX, (self.kern, self.feature), (self.kern, self.feature)), axis=0)
        Kuu = features.Kuu(self.feature, self.kern, jitter=settings.numerics.jitter_level)
        Kus = features.Kuf(self.feature, self.kern, Xnew)
        sigma2 = self.likelihood.variance
        sigma = tf.sqrt(sigma2)
        L = tf.cholesky(Kuu)

        A = tf.matrix_triangular_solve(L, tf.transpose(psi1), lower=True) / sigma
        tmp = tf.matrix_triangular_solve(L, psi2, lower=True)
        AAT = tf.matrix_triangular_solve(L, tf.transpose(tmp), lower=True) / sigma2
        B = AAT + tf.eye(num_inducing, dtype=settings.float_type)
        LB = tf.cholesky(B)
        c = tf.matrix_triangular_solve(LB, tf.matmul(A, self.Y), lower=True) / sigma
        tmp1 = tf.matrix_triangular_solve(L, Kus, lower=True)
        tmp2 = tf.matrix_triangular_solve(LB, tmp1, lower=True)
        mean = tf.matmul(tmp2, c, transpose_a=True)
        if full_cov:
            var = self.kern.K(Xnew) + tf.matmul(tmp2, tmp2, transpose_a=True) \
                  - tf.matmul(tmp1, tmp1, transpose_a=True)
            shape = tf.stack([1, 1, tf.shape(self.Y)[1]])
            var = tf.tile(tf.expand_dims(var, 2), shape)
        else:
            var = self.kern.Kdiag(Xnew) + tf.reduce_sum(tf.square(tmp2), 0) \
                  - tf.reduce_sum(tf.square(tmp1), 0)
            shape = tf.stack([1, tf.shape(self.Y)[1]])
            var = tf.tile(tf.expand_dims(var, 1), shape)
        return mean + self.mean_function(Xnew), var

Response: Compute the mean and variance of the latent function at some new points.


Instruction:
Create a Python 3 function for
returning a single - output conditional for a single - output case.

Response:
def _conditional(Xnew, feat, kern, f, *, full_cov=False, full_output_cov=False, q_sqrt=None, white=False):
    """
    Multi-output GP with independent GP priors.
    Number of latent processes equals the number of outputs (L = P).

    The covariance matrices used to calculate the conditional have the following shape:
    - Kuu: P x M x M
    - Kuf: P x M x N
    - Kff: P x N or P x N x N

    Further reference
    -----------------
    - See `gpflow.conditionals._conditional` for a detailed explanation of
      conditional in the single-output case.
    - See the multiouput notebook for more information about the multiouput framework.
    - See above for the parameters and the return value.
    """

    logger.debug("conditional: object, SharedIndependentMof, SeparateIndependentMok, object")
    # Following are: P x M x M  -  P x M x N  -  P x N(x N)
    Kmms = Kuu(feat, kern, jitter=settings.numerics.jitter_level)  # P x M x M
    Kmns = Kuf(feat, kern, Xnew)  # P x M x N
    kern_list = kern.kernels if isinstance(kern, Combination) else [kern.kern] * len(feat.feat_list)
    Knns = tf.stack([k.K(Xnew) if full_cov else k.Kdiag(Xnew) for k in kern_list], axis=0)
    fs = tf.transpose(f)[:, :, None]  # P x M x 1
    # P x 1 x M x M  or  P x M x 1
    q_sqrts = tf.transpose(q_sqrt)[:, :, None] if q_sqrt.shape.ndims == 2 else q_sqrt[:, None, :, :]

    def single_gp_conditional(t):
        Kmm, Kmn, Knn, f, q_sqrt = t
        return base_conditional(Kmn, Kmm, Knn, f, full_cov=full_cov, q_sqrt=q_sqrt, white=white)

    rmu, rvar = tf.map_fn(single_gp_conditional,
                          (Kmms, Kmns, Knns, fs, q_sqrts),
                          (settings.float_type, settings.float_type))  # P x N x 1, P x 1 x N x N or P x N x 1

    fmu = _rollaxis_left(rmu[..., 0], 1)  # N x P

    if full_cov:
        fvar = rvar[..., 0, :, :]  # P x N x N
    else:
        fvar = _rollaxis_left(rvar[..., 0], 1)  # N x P

    return fmu, _expand_independent_outputs(fvar, full_cov, full_output_cov)


Instruction:
Can you write a function in Python 3 where it
samples from a conditional distribution.

Response:
def _sample_conditional(Xnew, feat, kern, f, *, full_cov=False, full_output_cov=False, q_sqrt=None, white=False, num_samples=None):
    """
    `sample_conditional` will return a sample from the conditinoal distribution.
    In most cases this means calculating the conditional mean m and variance v and then
    returning m + sqrt(v) * eps, with eps ~ N(0, 1).
    However, for some combinations of Mok and Mof more efficient sampling routines exists.
    The dispatcher will make sure that we use the most efficent one.

    :return: N x P (full_output_cov = False) or N x P x P (full_output_cov = True)
    """
    logger.debug("sample conditional: (MixedKernelSharedMof, MixedKernelSeparateMof), SeparateMixedMok")
    if full_cov:
        raise NotImplementedError("full_cov not yet implemented")
    if full_output_cov:
        raise NotImplementedError("full_output_cov not yet implemented")
    independent_cond = conditional.dispatch(object, SeparateIndependentMof, SeparateIndependentMok, object)
    g_mu, g_var = independent_cond(Xnew, feat, kern, f, white=white, q_sqrt=q_sqrt,
                                   full_output_cov=False, full_cov=False)  # [..., N, L], [..., N, L]
    g_sample = _sample_mvn(g_mu, g_var, "diag", num_samples=num_samples)  # [..., (S), N, L]
    with params_as_tensors_for(kern):
        f_mu, f_var = _mix_latent_gp(kern.W, g_mu, g_var, full_cov, full_output_cov)
        f_sample = tf.tensordot(g_sample, kern.W, [[-1], [-1]])  # [..., N, P]
    return f_sample, f_mu, f_var


Instruction:
Given the following Python 3 function, write the documentation
def _mix_latent_gp(W, g_mu, g_var, full_cov, full_output_cov):
    f_mu = tf.tensordot(g_mu, W, [[-1], [-1]])  # [..., N, P]

    rk = tf.rank(g_var)
    leading_dims = (rk - 3) if full_cov else (rk - 2)

    if full_cov and full_output_cov:  # g_var is [L, ..., N, N]
        # this branch is practically never taken
        g_var = _rollaxis_left(g_var, 1)  # [..., N, N, L]
        g_var = tf.expand_dims(g_var, axis=-2)  # [..., N, N, 1, L]
        g_var_W = g_var * W  # [..., N, P, L]
        f_var = tf.tensordot(g_var_W, W, [[-1], [-1]])  # [..., N, N, P, P]
        f_var = misc.leading_transpose(f_var, [..., -4, -2, -3, -1])  # [..., N, P, N, P]

    elif full_cov and not full_output_cov:  # g_var is [L, ..., N, N]
        # this branch is practically never taken
        f_var = tf.tensordot(g_var, W**2, [[0], [-1]])  # [..., N, N, P]
        f_var = misc.leading_transpose(f_var, [..., -1, -3, -2])  # [..., P, N, N]

    elif not full_cov and full_output_cov:  # g_var is [..., N, L]
        g_var = tf.expand_dims(g_var, axis=-2)  # [..., N, 1, L]
        g_var_W = g_var * W  # [..., N, P, L]
        f_var = tf.tensordot(g_var_W, W, [[-1], [-1]])  # [..., N, P, P]

    elif not full_cov and not full_output_cov:  # g_var is [..., N, L]
        W_squared = W**2  # [P, L]
        f_var = tf.tensordot(g_var, W_squared, [[-1], [-1]])  # [..., N, P]

    return f_mu, f_var

Response:
r Makes the mean and variance of an uncorrelated L - dimensional latent GP
    and returns the mean and variance of the mixed GP.


Instruction:
Make a summary of the following Python 3 code
def size(self):
        return np.multiply.reduce(self.shape, dtype=np.int32)

Response: The size of this parameter equivalent to self. value. size


Instruction:
Make a summary of the following Python 3 code
def tf_compilation_index(self):
        if self.parameter_tensor is None:
            return None
        name = self.parameter_tensor.name
        return name.split('-', 1)[-1].split('/')[0]

Response:
Takes out index from the parameter s tensor name. E. g. parameter tensor name is
        GPR - 0 - 0 - lengthcales


Instruction:
Given the following Python 3 function, write the documentation
def _build_prior(self, unconstrained_tensor, constrained_tensor):
        if not misc.is_tensor(unconstrained_tensor):
            raise GPflowError("Unconstrained input must be a tensor.")

        if not misc.is_tensor(constrained_tensor):
            raise GPflowError("Constrained input must be a tensor.")

        prior_name = 'prior'

        if self.prior is None:
            return tf.constant(0.0, settings.float_type, name=prior_name)

        log_jacobian = self.transform.log_jacobian_tensor(unconstrained_tensor)
        logp_var = self.prior.logp(constrained_tensor)
        return tf.squeeze(tf.add(logp_var, log_jacobian, name=prior_name))

Response: Builds a tensorflow representation of the prior density.


Instruction:
Implement a function in Python 3 to
minimize a scalar Tensor.

Response:
def minimize(self,
               session=None,
               feed_dict=None,
               fetches=None,
               step_callback=None,
               loss_callback=None,
               **run_kwargs):
    """Minimize a scalar `Tensor`.

    Variables subject to optimization are updated in-place at the end of
    optimization.

    Note that this method does *not* just return a minimization `Op`, unlike
    `Optimizer.minimize()`; instead it actually performs minimization by
    executing commands to control a `Session`.

    Args:
      session: A `Session` instance.
      feed_dict: A feed dict to be passed to calls to `session.run`.
      fetches: A list of `Tensor`s to fetch and supply to `loss_callback`
        as positional arguments.
      step_callback: A function to be called at each optimization step;
        arguments are the current values of all optimization variables
        flattened into a single vector.
      loss_callback: A function to be called every time the loss and gradients
        are computed, with evaluated fetches supplied as positional arguments.
      **run_kwargs: kwargs to pass to `session.run`.
    """
    session = session or ops.get_default_session()
    feed_dict = feed_dict or {}
    fetches = fetches or []

    loss_callback = loss_callback or (lambda *fetches: None)
    step_callback = step_callback or (lambda xk: None)

    self._initialize_updated_shapes(session)

    # Construct loss function and associated gradient.
    loss_grad_func = self._make_eval_func([self._loss,
                                           self._packed_loss_grad], session,
                                          feed_dict, fetches, loss_callback)

    # Construct equality constraint functions and associated gradients.
    equality_funcs = self._make_eval_funcs(self._equalities, session, feed_dict,
                                           fetches)
    equality_grad_funcs = self._make_eval_funcs(self._packed_equality_grads,
                                                session, feed_dict, fetches)

    # Construct inequality constraint functions and associated gradients.
    inequality_funcs = self._make_eval_funcs(self._inequalities, session,
                                             feed_dict, fetches)
    inequality_grad_funcs = self._make_eval_funcs(self._packed_inequality_grads,
                                                  session, feed_dict, fetches)

    # Get initial value from TF session.
    initial_packed_var_val = session.run(self._packed_var)

    # Perform minimization.
    packed_var_val = self._minimize(
        initial_val=initial_packed_var_val,
        loss_grad_func=loss_grad_func,
        equality_funcs=equality_funcs,
        equality_grad_funcs=equality_grad_funcs,
        inequality_funcs=inequality_funcs,
        inequality_grad_funcs=inequality_grad_funcs,
        packed_bounds=self._packed_bounds,
        step_callback=step_callback,
        optimizer_kwargs=self.optimizer_kwargs)
    var_vals = [
        packed_var_val[packing_slice] for packing_slice in self._packing_slices
    ]

    # Set optimization variables to their new values.
    session.run(
        self._var_updates,
        feed_dict=dict(zip(self._update_placeholders, var_vals)),
        **run_kwargs)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _minimize(self, initial_val, loss_grad_func, equality_funcs,
                equality_grad_funcs, inequality_funcs, inequality_grad_funcs,
                packed_bounds, step_callback, optimizer_kwargs):
    """Wrapper for a particular optimization algorithm implementation.

    It would be appropriate for a subclass implementation of this method to
    raise `NotImplementedError` if unsupported arguments are passed: e.g. if an
    algorithm does not support constraints but `len(equality_funcs) > 0`.

    Args:
      initial_val: A NumPy vector of initial values.
      loss_grad_func: A function accepting a NumPy packed variable vector and
        returning two outputs, a loss value and the gradient of that loss with
        respect to the packed variable vector.
      equality_funcs: A list of functions each of which specifies a scalar
        quantity that an optimizer should hold exactly zero.
      equality_grad_funcs: A list of gradients of equality_funcs.
      inequality_funcs: A list of functions each of which specifies a scalar
        quantity that an optimizer should hold >= 0.
      inequality_grad_funcs: A list of gradients of inequality_funcs.
      packed_bounds: A list of bounds for each index, or `None`.
      step_callback: A callback function to execute at each optimization step,
        supplied with the current value of the packed variable vector.
      optimizer_kwargs: Other key-value arguments available to the optimizer.

    Returns:
      The optimal variable vector as a NumPy vector.
    """
    raise NotImplementedError(
        'To use ExternalOptimizerInterface, subclass from it and implement '
        'the _minimize() method.')

Response: Wrapper for the external optimizer interface to minimize an internal key - value vector.


Instruction:
Create a Python 3 function to
pack a list of Tensor s into a single flattened rank - 1 Tensor.

Response:
def _pack(cls, tensors):
    """Pack a list of `Tensor`s into a single, flattened, rank-1 `Tensor`."""
    if not tensors:
      return None
    elif len(tensors) == 1:
      return array_ops.reshape(tensors[0], [-1])
    else:
      flattened = [array_ops.reshape(tensor, [-1]) for tensor in tensors]
      return array_ops.concat(flattened, 0)


Instruction:
Can you write a function in Python 3 where it
constructs a function that evaluates a Tensor or list of Tensors.

Response:
def _make_eval_func(self, tensors, session, feed_dict, fetches,
                      callback=None):
    """Construct a function that evaluates a `Tensor` or list of `Tensor`s."""
    if not isinstance(tensors, list):
      tensors = [tensors]
    num_tensors = len(tensors)

    def eval_func(x):
      """Function to evaluate a `Tensor`."""
      shapes = dict(zip(self._vars, self._var_shapes))
      augmented_feed_dict = {
          var: x[packing_slice].reshape(shapes[var])
          for var, packing_slice in zip(self._vars, self._packing_slices)
      }
      augmented_feed_dict.update(feed_dict)
      augmented_fetches = tensors + fetches

      augmented_fetch_vals = session.run(
          augmented_fetches, feed_dict=augmented_feed_dict)

      if callable(callback):
        callback(*augmented_fetch_vals[num_tensors:])

      return augmented_fetch_vals[:num_tensors]

    return eval_func


Instruction:
Can you tell what is the following Python 3 function doing
def create_global_step(session: tf.Session) -> tf.Variable:
    global_step_tensor = tf.Variable(0, trainable=False, name="global_step")
    session.run(global_step_tensor.initializer)
    return global_step_tensor

Response: Creates the Tensorflow global_step variable.


Instruction:
Given the following Python 3 function, write the documentation
def restore_session(session: tf.Session, checkpoint_dir: str,
                    saver: Optional[tf.train.Saver] = None) -> None:
    """
    Restores Tensorflow session from the latest checkpoint.
    :param session: The TF session
    :param checkpoint_dir: checkpoint files directory.
    :param saver: The saver object, if not provided a default saver object will be created.
    """
    checkpoint_path = tf.train.latest_checkpoint(checkpoint_dir)
    logger = settings.logger()
    if logger.isEnabledFor(logging.INFO):
        logger.info("Restoring session from `%s`.", checkpoint_path)

    saver = saver or get_default_saver()
    saver.restore(session, checkpoint_path)

Response: Restores Tensorflow session from the latest checkpoint.


Instruction:
How would you explain what the following Python 3 function does
def get_default_saver(max_to_keep: int=3) -> tf.train.Saver:
    return tf.train.Saver(max_to_keep=max_to_keep)

Response: Returns a Tensorflow Saver object with default values.


Instruction:
Can you create a Python 3 function that
updates the state of the current state of the current optimiser.

Response:
def update_optimiser(context, *args, **kwargs) -> None:
    """
    Writes optimiser state into corresponding TensorFlow variables. This may need to be done
    for optimisers like ScipyOptimiser that work with their own copies of the variables.
    Normally the source variables would be updated only when the optimiser has finished the
    minimisation. This function may be called from the callback in order to get the TensorFlow
    variables up-to-date so that they can be written into TensorBoard for example.

    The expected state parameters and the procedure of writing them into variables is specific
    to each optimiser. Currently it is implemented only for ScipyOptimiser.

    After the state is updated a flag is set to the context to prevent multiple updates in
    the same callback.

    :param context: Monitor context
    :param args: Optimiser's state passed to the callback
    :param kwargs: Optimiser's state passed to the callback
    """

    if context.optimiser is None or context.optimiser_updated:
        return

    if isinstance(context.optimiser, ScipyOptimizer) and len(args) > 0:

        optimizer = context.optimiser.optimizer  # get access to ExternalOptimizerInterface
        var_vals = [args[0][packing_slice] for packing_slice in optimizer._packing_slices]
        context.session.run(optimizer._var_updates,
                            feed_dict=dict(zip(optimizer._update_placeholders, var_vals)))
        context.optimiser_updated = True


Instruction:
Can you implement a function in Python 3 that
evaluates the value of the global step variable.

Response:
def global_step(self) -> int:
        """
        Evaluates the value of the global step variable if it is set, otherwise returns the
        current iteration number.
        """
        if self.session is None or self.global_step_tensor is None:
            return self.iteration_no + self.init_global_step
        else:
            return self.session.run(self.global_step_tensor)


Instruction:
Write a Python 3 function for
setting the condition that will be evaluated during the optimisation cycle.

Response:
def with_condition(self, condition: Callable[[MonitorContext], bool]) -> 'MonitorTask':
        """
        Sets the task running condition that will be evaluated during the optimisation cycle.
        """
        self._condition = condition
        return self


Instruction:
Can you create a Python 3 function that
sets the flag indicating that the task should also run after the optimisation is ended.

Response:
def with_exit_condition(self, exit_condition: Optional[bool]=True) -> 'MonitorTask':
        """
        Sets the flag indicating that the task should also run after the optimisation is ended.
        """
        self._exit_condition = exit_condition
        return self


Instruction:
Here you have a function in Python 3, explain what it does
def start_monitoring(self) -> None:
        self._context.init_global_step = self._context.global_step
        self._start_timestamp = get_hr_time()
        self._last_timestamp = self._start_timestamp

Response: Start monitoring for the current context.


Instruction:
Can you implement a function in Python 3 that
stops monitoring the current context.

Response:
def stop_monitoring(self) -> None:
        """
        The recommended way of using Monitor is opening it with the `with` statement. In this case
        the user doesn't need to call this function explicitly. Otherwise the function should be
        called when the optimisation is done.

        The function sets the optimisation completed flag in the monitoring context and runs the
        tasks once more. If the monitor was created with the `print_summary` option it prints the
        tasks' timing summary.
        """

        self._context.optimisation_finished = True
        self._on_iteration()

        if self._print_summary:
            self.print_summary()


Instruction:
Can you tell what is the following Python 3 function doing
def print_summary(self) -> None:
        print("Tasks execution time summary:")
        for mon_task in self._monitor_tasks:
            print("%s:\t%.4f (sec)" % (mon_task.task_name, mon_task.total_time))

Response: Prints the tasks execution time summary.


Instruction:
Write a Python 3 script for
calling at each iteration.

Response:
def _on_iteration(self, *args, **kwargs) -> None:
        """
        Called at each iteration.

        This function does time measurements, updates timing in the monitoring context and calls
        all monitoring tasks.
        """

        # Update timing and the iteration number in the monitoring context.
        current_timestamp = get_hr_time()
        self._context.optimisation_time += current_timestamp - self._last_timestamp
        self._context.total_time = current_timestamp - self._start_timestamp
        self._context.optimiser_updated = False
        if not self._context.optimisation_finished:
            self._context.iteration_no += 1

        # Call all monitoring functions
        for func in self._monitor_tasks:
            func(self._context, *args, **kwargs)

        # Remember the time when the control is returned back to the optimiser, so that the next
        # optimisation step can be accurately timed.
        self._last_timestamp = get_hr_time()


Instruction:
Can you tell what is the following Python 3 function doing
def _growing_step_sequence(interval_growth, max_interval, init_interval, start_level=None):
        interval = init_interval
        next_level = start_level or init_interval
        while True:
            yield next_level
            interval = min(interval * interval_growth, max_interval)
            next_level += interval

Response: Generates an iterator that yields a sequence of trigger levels with growing intervals.


Instruction:
How would you code a function in Python 3 to
lock the location used by this writer.

Response:
def __lock_location(self) -> None:
        """
        Attempts to lock the location used by this writer. Will raise an error if the location is
        already locked by another writer. Will do nothing if the location is already locked by
        this writer.
        """
        if not self._is_active:
            if self._location in LogdirWriter._locked_locations:
                raise RuntimeError('TensorBoard event file in directory %s with suffix %s '
                                   'is already in use. At present multiple TensoBoard file writers '
                                   'cannot write data into the same file.' % self._location)
            LogdirWriter._locked_locations.add(self._location)
            self._is_active = True


Instruction:
How would you explain what the following Python 3 function does
def __release_location(self) -> None:
        if self._is_active:
            LogdirWriter._locked_locations.remove(self._location)
            self._is_active = False

Response: Release the lock on the location used by this writer.


Instruction:
Can you write a function in Python 3 where it
sets the flag indicating that the event file should be flushed at each call.

Response:
def with_flush_immediately(self, flush_immediately: Optional[bool]=True)\
            -> 'BaseTensorBoardTask':
        """
        Sets the flag indicating that the event file should be flushed at each call.
        """
        self._flush_immediately = flush_immediately
        return self


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _eval_summary(self, context: MonitorContext, feed_dict: Optional[Dict]=None) -> None:

        if self._summary is None:
            raise RuntimeError('TensorBoard monitor task should set the Tensorflow.Summary object')

        if context.session is None:
            raise RuntimeError('To run a TensorBoard monitor task the TF session object'
                               ' must be provided when creating an instance of the Monitor')

        summary = context.session.run(self._summary, feed_dict=feed_dict)
        self._file_writer.add_summary(summary, context.global_step)
        if self._flush_immediately:
            self.flush()

Response: Evaluates the summary tensor and writes the result to the event file.


Instruction:
How would you code a function in Python 3 to
return two n - by - n matrices. The first one contains all the x values and the second all the y values . The second one contains all the x values and the second all the y values .

Response:
def make_grid(xx, yy):
    """
    Returns two n-by-n matrices. The first one contains all the x values
    and the second all the y values of a cartesian product between `xx` and `yy`.
    """
    n = len(xx)
    xx, yy = np.meshgrid(xx, yy)
    grid = np.array([xx.ravel(), yy.ravel()]).T
    x = grid[:, 0].reshape(n, n)
    y = grid[:, 1].reshape(n, n)
    return x, y


Instruction:
Make a summary of the following Python 3 code
def elapsed(self):
        if self._stop is None:
            return timer() - self._start
        return self._stop - self._start

Response: Returns the elapsed time between start and stop timestamps.


Instruction:
Can you generate the documentation for the following Python 3 function
def watcher(self) -> Watcher:
        if not hasattr(self, "_watcher"):
            self._watcher = Watcher()
        return self._watcher

Response: Returns an instance of Watcher that can be used to access the action s watcher.


Instruction:
Given the following Python 3 function, write the documentation
def with_settings(self,
            stop: Optional[int] = None,
            start: int = 0,
            step: int = 1) -> 'Loop':
        """
        Set start, stop and step loop configuration.

            :param stop: Looop stop iteration integer. If None then loop
                becomes infinite.
            :param start: Loop iteration start integer.
            :param step: Loop iteration interval integer.
            :return: Loop itself.
        """
        self.start = start
        self.stop = stop
        self.step = step
        return self

Response: Sets start stop and step loop configuration.


Instruction:
Write a Python 3 script to
run performs loop iterations. :param context: Action context.

Response:
def run(self, context: ActionContext):
        """
        Run performs loop iterations.

            :param context: Action context.
        """
        iterator = itertools.count(start=self.start, step=self.step)
        for i in iterator:
            self.with_iteration(i)
            if self.stop is not None and i >= self.stop:
                break
            try:
                self._action(context)
            except Loop.Continue:
                continue
            except Loop.Break:
                break


Instruction:
Can you write a function in Python 3 where it
replaces optimizer tensor with new model.

Response:
def with_optimizer_tensor(self, tensor: Union[tf.Tensor, tf.Operation]) -> 'Optimization':
        """
        Replace optimizer tensor.

            :param model: Tensorflow tensor.
            :return: Optimization instance self reference.
        """
        self._optimizer_tensor = tensor
        return self


Instruction:
Implement a Python 3 function for
replacing Tensorflow session run kwargs with kwargs.

Response:
def with_run_kwargs(self, **kwargs: Dict[str, Any]) -> 'Optimization':
        """
        Replace Tensorflow session run kwargs.
        Check Tensorflow session run [documentation](https://www.tensorflow.org/api_docs/python/tf/Session).

            :param kwargs: Dictionary of tensors as keys and numpy arrays or
                primitive python types as values.
            :return: Optimization instance self reference.
        """
        self._run_kwargs = kwargs
        return self


Instruction:
How would you explain what the following Python 3 function does
def _conditional(Xnew, feat, kern, f, *, full_cov=False, full_output_cov=False, q_sqrt=None, white=False):
    logger.debug("Conditional: Inducing Feature - Kernel")
    Kmm = Kuu(feat, kern, jitter=settings.numerics.jitter_level)  # M x M
    Kmn = Kuf(feat, kern, Xnew)  # M x N
    Knn = kern.K(Xnew) if full_cov else kern.Kdiag(Xnew)

    fmean, fvar = base_conditional(Kmn, Kmm, Knn, f, full_cov=full_cov,
                                   q_sqrt=q_sqrt, white=white)  # N x R,  R x N x N or N x R
    return fmean, _expand_independent_outputs(fvar, full_cov, full_output_cov)

Response: Function to calculate the covariance matrix of a single - output GP conditional.


Instruction:
Create a Python 3 function for
giving f, representing the GP at the points X, produce the mean and (co-)variance of the GP at the points Xnew. Additionally, there may be Gaussian uncertainty about f as represented by q_sqrt. In this case `f` represents the mean of the distribution and q_sqrt the square-root of the covariance. Additionally, the GP may have been centered (whitened) so that p(v) = N(0, I) f = L v thus p(f) = N(0, LL^T) = N(0, K). In this case `f` represents the values taken by v. The method can either return the diagonals of the covariance matrix for each output (default) or the full covariance matrix (full_cov=True). We assume R independent GPs, represented by the columns of f (and the first dimension of q_sqrt). :param Xnew: data matrix, size N x D. Evaluate the GP at these new points :param X: data points, size M x D. :param kern: GPflow kernel. :param f: data matrix, M x R, representing the function values at X, for R functions. :param q_sqrt: matrix of standard-deviations or Cholesky matrices, size M x R or R x M x M. :param white: boolean of whether to use the whitened representation as described above. :return: - mean: N x R - variance: N x R (full_cov = False), R x N x N (full_cov = True)

Response:
def _conditional(Xnew, X, kern, f, *, full_cov=False, q_sqrt=None, white=False, full_output_cov=None):
    """
    Given f, representing the GP at the points X, produce the mean and
    (co-)variance of the GP at the points Xnew.

    Additionally, there may be Gaussian uncertainty about f as represented by
    q_sqrt. In this case `f` represents the mean of the distribution and
    q_sqrt the square-root of the covariance.

    Additionally, the GP may have been centered (whitened) so that
        p(v) = N(0, I)
        f = L v
    thus
        p(f) = N(0, LL^T) = N(0, K).
    In this case `f` represents the values taken by v.

    The method can either return the diagonals of the covariance matrix for
    each output (default) or the full covariance matrix (full_cov=True).

    We assume R independent GPs, represented by the columns of f (and the
    first dimension of q_sqrt).

    :param Xnew: data matrix, size N x D. Evaluate the GP at these new points
    :param X: data points, size M x D.
    :param kern: GPflow kernel.
    :param f: data matrix, M x R, representing the function values at X,
        for R functions.
    :param q_sqrt: matrix of standard-deviations or Cholesky matrices,
        size M x R or R x M x M.
    :param white: boolean of whether to use the whitened representation as
        described above.
    :return:
        - mean:     N x R
        - variance: N x R (full_cov = False), R x N x N (full_cov = True)
    """
    logger.debug("Conditional: Kernel")
    num_data = tf.shape(X)[-2]  # M
    Kmm = kern.K(X) + tf.eye(num_data, dtype=settings.float_type) * settings.numerics.jitter_level  #  [..., M, M]
    Kmn = kern.K(X, Xnew)  # [M, ..., N]

    if full_cov:
        Knn = kern.K(Xnew)  # [...,N,N]
    else:
        Knn = kern.Kdiag(Xnew)  # [...,N]

    mean, var = base_conditional(Kmn, Kmm, Knn, f, full_cov=full_cov, q_sqrt=q_sqrt, white=white)

    return mean, var


Instruction:
Write a Python 3 script to
sample from a conditional distribution.

Response:
def _sample_conditional(Xnew, feat, kern, f, *, full_cov=False, full_output_cov=False, q_sqrt=None, white=False, num_samples=None):
    """
    `sample_conditional` will return a sample from the conditional distribution.
    In most cases this means calculating the conditional mean m and variance v and then
    returning m + sqrt(v) * eps, with eps ~ N(0, 1).
    However, for some combinations of Mok and Mof more efficient sampling routines exists.
    The dispatcher will make sure that we use the most efficient one.

    :return: samples, mean, cov
        samples has shape [num_samples, N, P] or [N, P] if num_samples is None
        mean and cov as for conditional()
    """
    if full_cov and full_output_cov:
        raise NotImplementedError("The combination of both full_cov and full_output_cov is not "
                                  "implemented for sample_conditional.")

    logger.debug("sample conditional: InducingFeature Kernel")
    mean, cov = conditional(Xnew, feat, kern, f, q_sqrt=q_sqrt, white=white,
                            full_cov=full_cov, full_output_cov=full_output_cov)
    if full_cov:
        # mean: [..., N, P]
        # cov: [..., P, N, N]
        mean_PN = tf.matrix_transpose(mean)  # [..., P, N]
        samples = _sample_mvn(mean_PN, cov, 'full', num_samples=num_samples)  # [..., (S), P, N]
        samples = tf.matrix_transpose(samples)  # [..., (S), P, N]

    else:
        # mean: [..., N, P]
        # cov: [..., N, P] or [..., N, P, P]
        cov_structure = "full" if full_output_cov else "diag"
        samples = _sample_mvn(mean, cov, cov_structure, num_samples=num_samples)  # [..., (S), P, N]

    return samples, mean, cov


Instruction:
Can you tell what is the following Python 3 function doing
def base_conditional(Kmn, Kmm, Knn, f, *, full_cov=False, q_sqrt=None, white=False):
    logger.debug("base conditional")
    # compute kernel stuff
    num_func = tf.shape(f)[-1]  # R
    N = tf.shape(Kmn)[-1]
    M = tf.shape(f)[-2]

    # get the leadings dims in Kmn to the front of the tensor
    # if Kmn has rank two, i.e. [M, N], this is the identity op.
    K = tf.rank(Kmn)
    perm = tf.concat([tf.reshape(tf.range(1, K-1), [K-2]), # leading dims (...)
                      tf.reshape(0, [1]),  # [M]
                      tf.reshape(K-1, [1])], 0)  # [N]
    Kmn = tf.transpose(Kmn, perm)  # ... x M x N

    leading_dims = tf.shape(Kmn)[:-2]
    Lm = tf.cholesky(Kmm)  # [M,M]

    # Compute the projection matrix A
    Lm = tf.broadcast_to(Lm, tf.concat([leading_dims, tf.shape(Lm)], 0))  # [...,M,M]
    A = tf.matrix_triangular_solve(Lm, Kmn, lower=True)  # [...,M,N]
    # compute the covariance due to the conditioning
    if full_cov:
        fvar = Knn - tf.matmul(A, A, transpose_a=True)  # [...,N,N]
        cov_shape = tf.concat([leading_dims, [num_func, N, N]], 0)
        fvar = tf.broadcast_to(tf.expand_dims(fvar, -3), cov_shape)  # [...,R,N,N]
    else:
        fvar = Knn - tf.reduce_sum(tf.square(A), -2)  # [...,N]
        cov_shape = tf.concat([leading_dims, [num_func, N]], 0) # [...,R,N]
        fvar = tf.broadcast_to(tf.expand_dims(fvar, -2), cov_shape)  # [...,R,N]

    # another backsubstitution in the unwhitened case
    if not white:
        A = tf.matrix_triangular_solve(tf.matrix_transpose(Lm), A, lower=False)

    # construct the conditional mean
    f_shape = tf.concat([leading_dims, [M, num_func]], 0)  # [...,M,R]
    f = tf.broadcast_to(f, f_shape)  # [...,M,R]
    fmean = tf.matmul(A, f, transpose_a=True)  # [...,N,R]

    if q_sqrt is not None:
        if q_sqrt.get_shape().ndims == 2:
            LTA = A * tf.expand_dims(tf.transpose(q_sqrt), 2)  # R x M x N
        elif q_sqrt.get_shape().ndims == 3:
            L = q_sqrt
            L = tf.broadcast_to(L, tf.concat([leading_dims, tf.shape(L)], 0))

            shape = tf.concat([leading_dims, [num_func, M, N]], 0)
            A_tiled = tf.broadcast_to(tf.expand_dims(A, -3), shape)
            LTA = tf.matmul(L, A_tiled, transpose_a=True)  # R x M x N
        else:  # pragma: no cover
            raise ValueError("Bad dimension for q_sqrt: %s" %
                             str(q_sqrt.get_shape().ndims))
        if full_cov:
            fvar = fvar + tf.matmul(LTA, LTA, transpose_a=True)  # R x N x N
        else:
            fvar = fvar + tf.reduce_sum(tf.square(LTA), -2)  # R x N

    if not full_cov:
        fvar = tf.matrix_transpose(fvar)  # N x R

    return fmean, fvar

Response: r Base conditional for the KNN and Kmm.