Instruction:
How would you implement a function in Python 3 that
uses this method to delete a message from a chat. Returns True on success.

Response:
def delete_message(self, chat_id, message_id):
        """
        Use this method to delete message. Returns True on success.
        :param chat_id: in which chat to delete
        :param message_id: which message to delete
        :return: API reply.
        """
        return apihelper.delete_message(self.token, chat_id, message_id)


Instruction:
Implement a Python 3 function for
using this method to send photos.

Response:
def send_photo(self, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None,
                   parse_mode=None, disable_notification=None):
        """
        Use this method to send photos.
        :param disable_notification:
        :param chat_id:
        :param photo:
        :param caption:
        :param parse_mode
        :param reply_to_message_id:
        :param reply_markup:
        :return: API reply.
        """
        return types.Message.de_json(
            apihelper.send_photo(self.token, chat_id, photo, caption, reply_to_message_id, reply_markup,
                                 parse_mode, disable_notification))


Instruction:
Create a Python 3 function for
using this method to send audio files. You must be an administrator in the chat for this to work and must have the appropriate admin rights.

Response:
def send_audio(self, chat_id, audio, caption=None, duration=None, performer=None, title=None,
                   reply_to_message_id=None, reply_markup=None, parse_mode=None, disable_notification=None,
                   timeout=None):
        """
        Use this method to send audio files, if you want Telegram clients to display them in the music player. Your audio must be in the .mp3 format.
        :param chat_id:Unique identifier for the message recipient
        :param audio:Audio file to send.
        :param duration:Duration of the audio in seconds
        :param performer:Performer
        :param title:Track name
        :param parse_mode
        :param reply_to_message_id:If the message is a reply, ID of the original message
        :param reply_markup:
        :return: Message
        """
        return types.Message.de_json(
            apihelper.send_audio(self.token, chat_id, audio, caption, duration, performer, title, reply_to_message_id,
                                 reply_markup, parse_mode, disable_notification, timeout))


Instruction:
Can you create a Python 3 function that
uses this method to send a document.

Response:
def send_document(self, chat_id, data, reply_to_message_id=None, caption=None, reply_markup=None,
                      parse_mode=None, disable_notification=None, timeout=None):
        """
        Use this method to send general files.
        :param chat_id:
        :param data:
        :param reply_to_message_id:
        :param reply_markup:
        :param parse_mode:
        :param disable_notification:
        :return: API reply.
        """
        return types.Message.de_json(
            apihelper.send_data(self.token, chat_id, data, 'document', reply_to_message_id, reply_markup,
                                parse_mode, disable_notification, timeout, caption=caption))


Instruction:
Can you create a Python 3 function that
uses this method to send a sticker.

Response:
def send_sticker(self, chat_id, data, reply_to_message_id=None, reply_markup=None, disable_notification=None,
                     timeout=None):
        """
        Use this method to send .webp stickers.
        :param chat_id:
        :param data:
        :param reply_to_message_id:
        :param reply_markup:
        :return: API reply.
        """
        return types.Message.de_json(
            apihelper.send_data(self.token, chat_id, data, 'sticker', reply_to_message_id, reply_markup,
                                disable_notification, timeout))


Instruction:
How would you implement a function in Python 3 that
uses this method to send a video note. You can use this method to send video files. You can use the video files Telegram clients can use the mp4 videos API.

Response:
def send_video_note(self, chat_id, data, duration=None, length=None, reply_to_message_id=None, reply_markup=None,
                        disable_notification=None, timeout=None):
        """
        Use this method to send video files, Telegram clients support mp4 videos.
        :param chat_id: Integer : Unique identifier for the message recipient — User or GroupChat id
        :param data: InputFile or String : Video note to send. You can either pass a file_id as String to resend a video that is already on the Telegram server
        :param duration: Integer : Duration of sent video in seconds
        :param length: Integer : Video width and height, Can't be None and should be in range of (0, 640)
        :param reply_to_message_id:
        :param reply_markup:
        :return:
        """
        return types.Message.de_json(
            apihelper.send_video_note(self.token, chat_id, data, duration, length, reply_to_message_id, reply_markup,
                                      disable_notification, timeout))


Instruction:
Can you write a function in Python 3 where it
uses this method to send a group of photos or videos as an album. You must be an administrator in the chat for this to work and must have the appropriate admin rights.

Response:
def send_media_group(self, chat_id, media, disable_notification=None, reply_to_message_id=None):
        """
        send a group of photos or videos as an album. On success, an array of the sent Messages is returned.
        :param chat_id:
        :param media:
        :param disable_notification:
        :param reply_to_message_id:
        :return:
        """
        result = apihelper.send_media_group(self.token, chat_id, media, disable_notification, reply_to_message_id)
        ret = []
        for msg in result:
            ret.append(types.Message.de_json(msg))
        return ret


Instruction:
Can you tell what is the following Python 3 function doing
def send_location(self, chat_id, latitude, longitude, live_period=None, reply_to_message_id=None, reply_markup=None,
                      disable_notification=None):
        """
        Use this method to send point on the map.
        :param chat_id:
        :param latitude:
        :param longitude:
        :param live_period
        :param reply_to_message_id:
        :param reply_markup:
        :return: API reply.
        """
        return types.Message.de_json(
            apihelper.send_location(self.token, chat_id, latitude, longitude, live_period, reply_to_message_id,
                                    reply_markup,
                                    disable_notification))

Response: Use this method to send location on the map.


Instruction:
Can you tell what is the following Python 3 function doing
def edit_message_live_location(self, latitude, longitude, chat_id=None, message_id=None,
                                   inline_message_id=None, reply_markup=None):
        """
        Use this method to edit live location
        :param latitude:
        :param longitude:
        :param chat_id:
        :param message_id:
        :param inline_message_id:
        :param reply_markup:
        :return:
        """
        return types.Message.de_json(
            apihelper.edit_message_live_location(self.token, latitude, longitude, chat_id, message_id,
                                                 inline_message_id, reply_markup))

Response: Use this method to edit live location of a message.


Instruction:
Can you create a Python 3 function that
uses this method to stop updating a live location message.

Response:
def stop_message_live_location(self, chat_id=None, message_id=None, inline_message_id=None, reply_markup=None):
        """
        Use this method to stop updating a live location message sent by the bot
        or via the bot (for inline bots) before live_period expires
        :param chat_id:
        :param message_id:
        :param inline_message_id:
        :param reply_markup:
        :return:
        """
        return types.Message.de_json(
            apihelper.stop_message_live_location(self.token, chat_id, message_id, inline_message_id, reply_markup))


Instruction:
Implement a function in Python 3 to
use this method to send information about a venue.

Response:
def send_venue(self, chat_id, latitude, longitude, title, address, foursquare_id=None, disable_notification=None,
                   reply_to_message_id=None, reply_markup=None):
        """
        Use this method to send information about a venue.
        :param chat_id: Integer or String : Unique identifier for the target chat or username of the target channel
        :param latitude: Float : Latitude of the venue
        :param longitude: Float : Longitude of the venue
        :param title: String : Name of the venue
        :param address: String : Address of the venue
        :param foursquare_id: String : Foursquare identifier of the venue
        :param disable_notification:
        :param reply_to_message_id:
        :param reply_markup:
        :return:
        """
        return types.Message.de_json(
            apihelper.send_venue(self.token, chat_id, latitude, longitude, title, address, foursquare_id,
                                 disable_notification, reply_to_message_id, reply_markup)
        )


Instruction:
Explain what the following Python 3 code does
def send_chat_action(self, chat_id, action):
        return apihelper.send_chat_action(self.token, chat_id, action)

Response: Use this method when you want to send a message to a chat.


Instruction:
How would you explain what the following Python 3 function does
def kick_chat_member(self, chat_id, user_id, until_date=None):
        return apihelper.kick_chat_member(self.token, chat_id, user_id, until_date)

Response: Use this method to kick a user from a chat.


Instruction:
How would you implement a function in Python 3 that
uses this method to restrict a user in a supergroup.

Response:
def restrict_chat_member(self, chat_id, user_id, until_date=None, can_send_messages=None,
                             can_send_media_messages=None, can_send_other_messages=None,
                             can_add_web_page_previews=None):
        """
        Use this method to restrict a user in a supergroup.
        The bot must be an administrator in the supergroup for this to work and must have
        the appropriate admin rights. Pass True for all boolean parameters to lift restrictions from a user.
        Returns True on success.
        :param chat_id: Int or String : 	Unique identifier for the target group or username of the target supergroup
            or channel (in the format @channelusername)
        :param user_id: Int : Unique identifier of the target user
        :param until_date: Date when restrictions will be lifted for the user, unix time.
            If user is restricted for more than 366 days or less than 30 seconds from the current time,
            they are considered to be restricted forever
        :param can_send_messages: Pass True, if the user can send text messages, contacts, locations and venues
        :param can_send_media_messages Pass True, if the user can send audios, documents, photos, videos, video notes
            and voice notes, implies can_send_messages
        :param can_send_other_messages: Pass True, if the user can send animations, games, stickers and
            use inline bots, implies can_send_media_messages
        :param can_add_web_page_previews: Pass True, if the user may add web page previews to their messages,
            implies can_send_media_messages
        :return: types.Message
        """
        return apihelper.restrict_chat_member(self.token, chat_id, user_id, until_date, can_send_messages,
                                              can_send_media_messages, can_send_other_messages,
                                              can_add_web_page_previews)


Instruction:
Can you create a Python 3 function that
promotes a user in a supergroup or a channel.

Response:
def promote_chat_member(self, chat_id, user_id, can_change_info=None, can_post_messages=None,
                            can_edit_messages=None, can_delete_messages=None, can_invite_users=None,
                            can_restrict_members=None, can_pin_messages=None, can_promote_members=None):
        """
        Use this method to promote or demote a user in a supergroup or a channel. The bot must be an administrator
        in the chat for this to work and must have the appropriate admin rights.
        Pass False for all boolean parameters to demote a user. Returns True on success.
        :param chat_id: Unique identifier for the target chat or username of the target channel (
            in the format @channelusername)
        :param user_id: Int : Unique identifier of the target user
        :param can_change_info: Bool: Pass True, if the administrator can change chat title, photo and other settings
        :param can_post_messages: Bool : Pass True, if the administrator can create channel posts, channels only
        :param can_edit_messages: Bool : Pass True, if the administrator can edit messages of other users, channels only
        :param can_delete_messages: Bool : Pass True, if the administrator can delete messages of other users
        :param can_invite_users: Bool : Pass True, if the administrator can invite new users to the chat
        :param can_restrict_members: Bool: Pass True, if the administrator can restrict, ban or unban chat members
        :param can_pin_messages: Bool: Pass True, if the administrator can pin messages, supergroups only
        :param can_promote_members: Bool: Pass True, if the administrator can add new administrators with a subset
            of his own privileges or demote administrators that he has promoted, directly or indirectly
            (promoted by administrators that were appointed by him)
        :return:
        """
        return apihelper.promote_chat_member(self.token, chat_id, user_id, can_change_info, can_post_messages,
                                             can_edit_messages, can_delete_messages, can_invite_users,
                                             can_restrict_members, can_pin_messages, can_promote_members)


Instruction:
Given the following Python 3 function, write the documentation
def set_chat_photo(self, chat_id, photo):
        return apihelper.set_chat_photo(self.token, chat_id, photo)

Response: Use this method to set a new profile photo for a chat. Photos can t be changed for private chats.


Instruction:
How would you explain what the following Python 3 function does
def set_chat_title(self, chat_id, title):
        return apihelper.set_chat_title(self.token, chat_id, title)

Response: Use this method to change the title of a chat.


Instruction:
Make a summary of the following Python 3 code
def set_chat_description(self, chat_id, description):
        return apihelper.set_chat_description(self.token, chat_id, description)

Response: Use this method to change the chat description


Instruction:
Can you tell what is the following Python 3 function doing
def pin_chat_message(self, chat_id, message_id, disable_notification=False):
        return apihelper.pin_chat_message(self.token, chat_id, message_id, disable_notification)

Response: Use this method to pin a message in a supergroup.


Instruction:
How would you explain what the following Python 3 function does
def reply_to(self, message, text, **kwargs):
        return self.send_message(message.chat.id, text, reply_to_message_id=message.message_id, **kwargs)

Response: Reply to a message.


Instruction:
Write a Python 3 script to
send an inline query to the server.

Response:
def answer_inline_query(self, inline_query_id, results, cache_time=None, is_personal=None, next_offset=None,
                            switch_pm_text=None, switch_pm_parameter=None):
        """
        Use this method to send answers to an inline query. On success, True is returned.
        No more than 50 results per query are allowed.
        :param inline_query_id: Unique identifier for the answered query
        :param results: Array of results for the inline query
        :param cache_time: The maximum amount of time in seconds that the result of the inline query may be cached on the server.
        :param is_personal: Pass True, if results may be cached on the server side only for the user that sent the query.
        :param next_offset: Pass the offset that a client should send in the next query with the same text to receive more results.
        :param switch_pm_parameter: If passed, clients will display a button with specified text that switches the user
         to a private chat with the bot and sends the bot a start message with the parameter switch_pm_parameter
        :param switch_pm_text: 	Parameter for the start message sent to the bot when user presses the switch button
        :return: True means success.
        """
        return apihelper.answer_inline_query(self.token, inline_query_id, results, cache_time, is_personal, next_offset,
                                             switch_pm_text, switch_pm_parameter)


Instruction:
Make a summary of the following Python 3 code
def answer_callback_query(self, callback_query_id, text=None, show_alert=None, url=None, cache_time=None):
        return apihelper.answer_callback_query(self.token, callback_query_id, text, show_alert, url, cache_time)

Response: Send an answer to a callback query.


Instruction:
Write a Python 3 script for
getting a specific sticker set by its name.

Response:
def get_sticker_set(self, name):
        """
        Use this method to get a sticker set. On success, a StickerSet object is returned.
        :param name:
        :return:
        """
        result = apihelper.get_sticker_set(self.token, name)
        return types.StickerSet.de_json(result)


Instruction:
Write a Python 3 function for
uploading a. png file with a sticker to a new StickerSet.

Response:
def upload_sticker_file(self, user_id, png_sticker):
        """
        Use this method to upload a .png file with a sticker for later use in createNewStickerSet and addStickerToSet
        methods (can be used multiple times). Returns the uploaded File on success.
        :param user_id:
        :param png_sticker:
        :return:
        """
        result = apihelper.upload_sticker_file(self.token, user_id, png_sticker)
        return types.File.de_json(result)


Instruction:
Make a summary of the following Python 3 code
def create_new_sticker_set(self, user_id, name, title, png_sticker, emojis, contains_masks=None,
                               mask_position=None):
        """
        Use this method to create new sticker set owned by a user. The bot will be able to edit the created sticker set.
        Returns True on success.
        :param user_id:
        :param name:
        :param title:
        :param png_sticker:
        :param emojis:
        :param contains_masks:
        :param mask_position:
        :return:
        """
        return apihelper.create_new_sticker_set(self.token, user_id, name, title, png_sticker, emojis, contains_masks,
                                                mask_position)

Response: This method creates a new sticker set owned by a user.


Instruction:
Can you tell what is the following Python 3 function doing
def add_sticker_to_set(self, user_id, name, png_sticker, emojis, mask_position=None):
        return apihelper.add_sticker_to_set(self.token, user_id, name, png_sticker, emojis, mask_position)

Response: Add a new sticker to a set.


Instruction:
Explain what the following Python 3 code does
def set_sticker_position_in_set(self, sticker, position):
        return apihelper.set_sticker_position_in_set(self.token, sticker, position)

Response: Move a sticker in a specific set.


Instruction:
Can you tell what is the following Python 3 function doing
def register_for_reply(self, message, callback, *args, **kwargs):
        message_id = message.message_id
        self.register_for_reply_by_message_id(message_id, callback, *args, **kwargs)

Response: Registers a callback function to be notified when a reply to a message arrives.


Instruction:
How would you explain what the following Python 3 function does
def register_for_reply_by_message_id(self, message_id, callback, *args, **kwargs):
        if message_id in self.reply_handlers.keys():
            self.reply_handlers[message_id].append(Handler(callback, *args, **kwargs))
        else:
            self.reply_handlers[message_id] = [Handler(callback, *args, **kwargs)]
        if self.reply_saver is not None:
            self.reply_saver.start_save_timer()

Response: Registers a callback function to be notified when a reply to a message.


Instruction:
Can you write a function in Python 3 where it
registers a callback function to be notified when new message arrives after message.

Response:
def register_next_step_handler(self, message, callback, *args, **kwargs):
        """
        Registers a callback function to be notified when new message arrives after `message`.

        Warning: In case `callback` as lambda function, saving next step handlers will not work.

        :param message:     The message for which we want to handle new message in the same chat.
        :param callback:    The callback function which next new message arrives.
        :param args:        Args to pass in callback func
        :param kwargs:      Args to pass in callback func
        """
        chat_id = message.chat.id
        self.register_next_step_handler_by_chat_id(chat_id, callback, *args, **kwargs)


Instruction:
Can you tell what is the following Python 3 function doing
def register_next_step_handler_by_chat_id(self, chat_id, callback, *args, **kwargs):
        if chat_id in self.next_step_handlers.keys():
            self.next_step_handlers[chat_id].append(Handler(callback, *args, **kwargs))
        else:
            self.next_step_handlers[chat_id] = [Handler(callback, *args, **kwargs)]

        if self.next_step_saver is not None:
            self.next_step_saver.start_save_timer()

Response: Registers a callback function to be notified when new message arrives after a chat.


Instruction:
Explain what the following Python 3 code does
def clear_step_handler(self, message):
        chat_id = message.chat.id
        self.clear_step_handler_by_chat_id(chat_id)

Response: Clears all callback functions registered by register_next_step_handler.


Instruction:
How would you explain what the following Python 3 function does
def clear_step_handler_by_chat_id(self, chat_id):
        self.next_step_handlers[chat_id] = []

        if self.next_step_saver is not None:
            self.next_step_saver.start_save_timer()

Response: Clears all callback functions registered by register_next_step_handler.


Instruction:
How would you implement a function in Python 3 that
clears all callback functions registered by register_for_reply and register_for_reply_by_message_id.

Response:
def clear_reply_handlers_by_message_id(self, message_id):
        """
        Clears all callback functions registered by register_for_reply() and register_for_reply_by_message_id().

        :param message_id: The message id for which we want to clear reply handlers
        """
        self.reply_handlers[message_id] = []

        if self.reply_saver is not None:
            self.reply_saver.start_save_timer()


Instruction:
Can you create a Python 3 function that
messages handler decorator. This decorator can be used to decorate functions that must handle certain types of messages. All message handlers are tested in the order they were added. Example: bot = TeleBot('TOKEN') # Handles all messages which text matches regexp. @bot.message_handler(regexp='someregexp') def command_help(message): bot.send_message(message.chat.id, 'Did someone call for help?') # Handle all sent documents of type 'text/plain'. @bot.message_handler(func=lambda message: message.document.mime_type == 'text/plain', content_types=['document']) def command_handle_document(message): bot.send_message(message.chat.id, 'Document received, sir!') # Handle all other commands. @bot.message_handler(func=lambda message: True, content_types=['audio', 'video', 'document', 'text', 'location', 'contact', 'sticker']) def default_command(message): bot.send_message(message.chat.id, "This is the default command handler.") :param regexp: Optional regular expression. :param func: Optional lambda function. The lambda receives the message to test as the first parameter. It must return True if the command should handle the message. :param content_types: This commands' supported content types. Must be a list. Defaults to ['text'].

Response:
def message_handler(self, commands=None, regexp=None, func=None, content_types=['text'], **kwargs):
        """
        Message handler decorator.
        This decorator can be used to decorate functions that must handle certain types of messages.
        All message handlers are tested in the order they were added.

        Example:

        bot = TeleBot('TOKEN')

        # Handles all messages which text matches regexp.
        @bot.message_handler(regexp='someregexp')
        def command_help(message):
            bot.send_message(message.chat.id, 'Did someone call for help?')

        # Handle all sent documents of type 'text/plain'.
        @bot.message_handler(func=lambda message: message.document.mime_type == 'text/plain', content_types=['document'])
        def command_handle_document(message):
            bot.send_message(message.chat.id, 'Document received, sir!')

        # Handle all other commands.
        @bot.message_handler(func=lambda message: True, content_types=['audio', 'video', 'document', 'text', 'location', 'contact', 'sticker'])
        def default_command(message):
            bot.send_message(message.chat.id, "This is the default command handler.")

        :param regexp: Optional regular expression.
        :param func: Optional lambda function. The lambda receives the message to test as the first parameter. It must return True if the command should handle the message.
        :param content_types: This commands' supported content types. Must be a list. Defaults to ['text'].
        """

        def decorator(handler):
            handler_dict = self._build_handler_dict(handler,
                                                    commands=commands,
                                                    regexp=regexp,
                                                    func=func,
                                                    content_types=content_types,
                                                    **kwargs)

            self.add_message_handler(handler_dict)

            return handler

        return decorator


Instruction:
Can you generate the documentation for the following Python 3 function
def generate_code(max_length, max_nest, ops):
  stack = []
  def fetch_one():
    # Always use an existing nested value for one of the operands.
    if stack:
      return stack.pop()
    else:
      # Produce a numeral of max_length-digits.
      value = random.randint(10 ** (max_length - 1), 10 ** max_length - 1)
      code = str(value)
      return value, code

  def fetch(num_operands):
    values, codes = zip(*[fetch_one() for _ in six.moves.range(num_operands)])
    return values, codes

  for _ in six.moves.range(max_nest):
    op = random.choice(ops)
    values, codes = fetch(op.num_operands)
    new_value = op.eval(values)
    new_code = op.get_code(codes)
    stack.append((new_value, "(" + new_code + ")"))
  final_value, final_code = stack.pop()
  final_code = final_code[1:-1]
  final_code.strip("()")
  if not op.is_memory:
    final_value = int(final_value) % 10 ** (max_length+1)
  return str(final_value), final_code

Response:
Generates code samples.

  Args:
    max_length: int.  max literal length.
    max_nest: int. max nesting level.
    ops: CodeOp. set of allowable operations.

  Returns:
    1. (str) output value.
    2. (str) Code operation.


Instruction:
Here you have a function in Python 3, explain what it does
def get_tokens(max_value):
  vocab = [str(i) for i in range(max_value)]
  vocab = set(vocab)
  vocab.update(CodeOp.LITERALS)
  vocab.update(CodeOp.KEYWORDS)
  vocab |= set("".join(vocab))
  return sorted(vocab)

Response:
Defines tokens.
 Returns a list of string tokens in vocabulary.


Instruction:
Can you generate the documentation for the following Python 3 function
def LearnToExecute(   # pylint: disable=invalid-name
    batch_size, max_length=1, max_nesting=1, token_by_char=True,
    mode=Mode.TRAIN_COMBINE, loss_threshold=0.1,
    min_tries=DEFAULT_MIN_CURRICULUM_EVAL_TRIES, task_type=TaskType.ALG_CTRL):
  """Factory method for LearnToExecute Dataset module.

  Args:
    batch_size: (int). The number of elements in a mini-batch.
    max_length: (int). Maximum character length.
    max_nesting: (int). Maximum level of statement nesting.
    token_by_char: (bool). Tokenize by character or words?
    mode: (string). Either 'train', 'test'.
    loss_threshold: (int) curriculum threshold for error below which increase
        the task difficulty.
    min_tries: (int) minimum update tries for curriculum difficulty level.
    task_type: (string) defines the task by allowable ops (see TASK_TYPE_OPS).

  Returns:
    tf.Data.Dataset for LearnToExecute sample generator with the
    LearnToExecuteState monkey patched into the `state` attribute.

  Raises:
    ValueError: in case of bad `mode`.
  """
  # defaults mode to "train-combine"
  if mode == Mode.TRAIN_COMBINE:
    curriculum = CombineCurriculum(
        max_length, max_nesting, loss_threshold, min_tries=min_tries)
  elif mode == Mode.TRAIN_MIX:
    curriculum = MixCurriculum(
        max_length, max_nesting, loss_threshold, min_tries=min_tries)
  elif mode == Mode.TRAIN_NAIVE:
    curriculum = NaiveCurriculum(
        max_length, max_nesting, loss_threshold, min_tries=min_tries)
  elif mode == Mode.TEST:
    curriculum = BaselineCurriculum(
        max_length, max_nesting, loss_threshold, min_tries=0)
  else:
    raise ValueError("Invalid mode.")
  lte = LearnToExecuteState(batch_size, max_length, max_nesting,
                            curriculum, token_by_char, task_type=task_type)
  types_ = (tf.float32, tf.float32, tf.float32, tf.int64, tf.int64)
  shapes_ = (tf.TensorShape([lte.num_steps, batch_size, lte.vocab_size]),
             tf.TensorShape([lte.num_steps_out, batch_size, lte.vocab_size]),
             tf.TensorShape([lte.num_steps_out, batch_size, lte.vocab_size]),
             tf.TensorShape([batch_size,]),
             tf.TensorShape([batch_size,]))
  dataset = tf.data.Dataset.from_generator(lte.make_batch, types_, shapes_)
  dataset.state = lte
  return dataset

Response:
Factory method for LearnToExecute Dataset module.

  Args:
    batch_size: (int). The number of elements in a mini-batch.
    max_length: (int). Maximum character length.
    max_nesting: (int). Maximum level of statement nesting.
    token_by_char: (bool). Tokenize by character or words?
    mode: (string). Either 'train', 'test'.
    loss_threshold: (int) curriculum threshold for error below which increase
        the task difficulty.
    min_tries: (int) minimum update tries for curriculum difficulty level.
    task_type: (string) defines the task by allowable ops (see TASK_TYPE_OPS).

  Returns:
    tf.Data.Dataset for LearnToExecute sample generator with the
    LearnToExecuteState monkey patched into the `state` attribute.

  Raises:
    ValueError: in case of bad `mode`.


Instruction:
Can you generate the documentation for the following Python 3 function
def fetch(self):
    length = np.random.randint(1, self._max_length + 1)
    nesting = np.random.randint(1, self._max_length + 1)
    return length, nesting

Response: Samples up to maximum difficulty.


Instruction:
How would you implement a function in Python 3 that
increments level difficulty by 1 until maximum.

Response:
def update(self, loss, force=False):
    """Increments level difficulty (length and nesting) by 1 until maximum."""
    do_update = super(CombineCurriculum, self).update(loss, force)
    if not do_update:
      return False

    if self._curr_length < self._max_length:
      self._curr_length += 1
    elif self._curr_nesting < self._max_nesting:
      self._curr_nesting += 1
    else:
      self._set_new_task = False

    if self._set_new_task:
      tf.logging.info("New level: (length=%d, nesting=%d)",
                      self._curr_length, self._curr_nesting)
    return self._set_new_task


Instruction:
Make a summary of the following Python 3 code
def fetch(self):
    length = np.random.randint(1, self._curr_length + 1)
    nesting = np.random.randint(1, self._curr_nesting + 1)
    return length, nesting

Response: Samples up to current difficulty.


Instruction:
How would you explain what the following Python 3 function does
def generate_flat_data(self):
    # Construct the string statements.
    all_statements = []
    all_targets = []
    self.sequence_sizes_in = []
    self.sequence_sizes_out = []
    for _ in six.moves.range(self._batch_size):
      length, nest = self.curriculum_obj.fetch()
      seq_size_in = self._max_seq_length
      # Generate batch within max length.
      is_valid_sample = False
      tries_remaining = 10
      while not is_valid_sample:
        value, code = generate_code(length, nest, self._ops)
        tokens_in, seq_size_in = self.tokenize(
            code, self._max_seq_length, self._token_by_char)
        tokens_out, seq_size_out = self.tokenize(
            value, self._max_seq_length, self._token_by_char)
        is_valid_sample = self._max_seq_length >= seq_size_in
        if is_valid_sample:
          self.sequence_sizes_in.append(seq_size_in)
          self.sequence_sizes_out.append(seq_size_out)
        if tries_remaining == 0:
          raise ValueError("Could not generate a sample below the allowable "
                           "maximum, consider reducing either max_length or "
                           "max_nest.")
        else:
          tries_remaining -= 1
      all_statements += tokens_in
      all_targets += tokens_out
    # Store the flattened data.
    self.flat_data = np.array(all_statements, dtype=np.int64)
    self.num_tokens = self.flat_data.shape[0]
    self.flat_targets = np.array(all_targets, dtype=np.int64)
    self.num_tokens_target = self.flat_targets.shape[0]
    self.start_token = np.array(self.tokenize(
        [get_start_token()], 1)[0], dtype=np.int64)
    self.end_token = np.array(self.tokenize(
        [get_end_token()], 1)[0], dtype=np.int64)

Response: Generates batched data in flat numpy arrays.


Instruction:
Make a summary of the following Python 3 code
def tokenize(self, char_input, max_len, by_char=False):
    if by_char:
      tokenized_list = [self._vocab_dict[token] for token in char_input]
    else:
      tokenized_list = []
      compound_token = ""
      for token in char_input:
        # Compose alphanumeric inputs into compound tokens.
        add_number = compound_token.isdigit() and not token.isdigit()
        add_word = compound_token.isalpha() and not token.isalpha()
        if add_number or add_word:
          tokenized_list.append(self._vocab_dict[compound_token])
          compound_token = ""
        # Add token or build compound token.
        if token.isdigit():
          compound_token += token
        elif token.isalpha():
          compound_token += token
        else:
          tokenized_list.append(self._vocab_dict[token])
      if compound_token:
        tokenized_list.append(self._vocab_dict[compound_token])

    # To ensure uniform batch sequence length pad the sequence.
    seq_size = len(tokenized_list)
    if seq_size < max_len:
      padding = [self._vocab_dict[get_padding()]] * (max_len - seq_size)
      tokenized_list.extend(padding)
    elif seq_size > max_len:
      raise ValueError("Token sequence is too large: {}".format(
          len(tokenized_list)))

    return tokenized_list, seq_size

Response: Takes a character string and returns a list of integer indices corresponding to a token list.


Instruction:
How would you code a function in Python 3 to
return an operations list based on the specified task type.

Response:
def get_task_ops(task_type=TaskType.ALG_CTRL):
    """Returns an operations list based on the specified task index.

    Args:
      task_type: indicates the task type used.

    Returns:
      List of the eligible ops.
    """
    try:
      return LearnToExecuteState.TASK_TYPE_OPS[task_type]
    except KeyError:
      raise KeyError("Bad task_type '%s', check config." % task_type)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def make_batch(self):
    while True:
      self.reset_data_source()
      obs = np.reshape(self._data_source.flat_data,
                       [self.batch_size, -1])[:, :self._num_steps].T
      target = np.reshape(
          self._data_source.flat_targets,
          [self.batch_size, -1])[:, :self._num_steps_out].T
      start_tokens = np.ndarray([1, self.batch_size], dtype=np.int32)
      start_tokens.fill(self._data_source.start_token[0])
      target_in = np.concatenate((start_tokens, target[:-1, :]), axis=0)
      yield (self._np_one_hot(obs, self._num_steps),
             self._np_one_hot(target, self._num_steps_out),
             self._np_one_hot(target_in, self._num_steps_out),
             self.seq_sizes_in,
             self.seq_sizes_out)

Response: Generates a batch of data for learning.


Instruction:
Write a Python 3 script for
returning a human - readable version of a one - hot encoding of words.

Response:
def to_human_readable(self, data, label_batch_entries=True, indices=None,
                        sep="\n"):
    """Returns a human-readable version of a one-hot encoding of words.

    Args:
      data: (numpy.ndarray S x B x OH). One-hot encoding of words. S is
          sequence length, B is batch size, OH is one hot dimensionality.
      label_batch_entries: (bool). Whether to add numerical label before each
          batch element in the output string.
      indices: (list(int) or None). Used to select a subset of minibatch indices
          to print. None will print the whole minibatch.
      sep: (str) separator which separates the output for each batch. Defaults
          to the newline character.

    Returns:
      String composed from the data.
    """
    batch_size = data.shape[1]
    result = []
    indices = indices or six.moves.range(batch_size)
    for b in indices:
      index_seq = np.argmax(data[:, b], axis=1)
      prefix = "b_{}: ".format(b) if label_batch_entries else ""
      result.append(prefix + self._data_source.decode_to_string(index_seq))
    return sep.join(result)


Instruction:
Here you have a function in Python 3, explain what it does
def _infer_fused_data_format(self, input_batch):
    input_shape = input_batch.get_shape().as_list()
    input_shape_len = len(input_shape)
    if input_shape_len != 4:
      raise NotImplementedError("fused batch norm supports only input with "
                                "4 dimensions, it received input of "
                                "dimensionality {:d}".format(input_shape_len))
    axis = range(input_shape_len)[:-1] if self._axis is None else self._axis
    axis = tuple(axis)
    if axis == (0, 1, 2):
      # Reduce over the last dimension.
      return "NHWC"
    elif axis == (0, 2, 3):
      # Reduce over the second dimension.
      return "NCHW"
    else:
      raise ValueError("Invalid axis option {}. This does not correspond to"
                       " either the NHWC format (0, 1, 2) or the NCHW "
                       "(0, 2, 3).".format(axis))

Response: Infer the data format for the fused batch norm.


Instruction:
Here you have a function in Python 3, explain what it does
def _fused_batch_norm_op(self, input_batch, mean, variance, use_batch_stats):
    # Store the original shape of the mean and variance.
    mean_shape = mean.get_shape()
    variance_shape = variance.get_shape()
    # The fused batch norm expects the mean, variance, gamma and beta
    # tensors to have dimension 1, so we flatten them to remove the
    # extra dimensions.
    gamma_flatten = tf.reshape(self._gamma, shape=(-1,))
    beta_flatten = tf.reshape(self._beta, shape=(-1,))
    flatten_mean = tf.reshape(mean, shape=(-1,))
    flatten_variance = tf.reshape(variance, shape=(-1,))
    use_batch_stats = tf.convert_to_tensor(use_batch_stats)

    common_args = {
        "scale": gamma_flatten,
        "offset": beta_flatten,
        "epsilon": self._eps,
        "data_format": self._infer_fused_data_format(input_batch),
        "name": "batch_norm"
    }

    def use_batch_stats_fused_batch_norm():
      return tf.nn.fused_batch_norm(input_batch, mean=None, variance=None,
                                    is_training=True, **common_args)

    def moving_average_fused_batch_norm():
      return tf.nn.fused_batch_norm(input_batch, mean=flatten_mean,
                                    variance=flatten_variance,
                                    is_training=False, **common_args)

    batch_norm_op, mean, variance = utils.smart_cond(
        use_batch_stats, use_batch_stats_fused_batch_norm,
        moving_average_fused_batch_norm)

    mean = tf.reshape(mean, mean_shape)
    variance = tf.reshape(variance, variance_shape)
    return batch_norm_op, mean, variance

Response: Creates a fused batch normalization op.


Instruction:
Given the following Python 3 function, write the documentation
def _batch_norm_op(self, input_batch, mean, variance, use_batch_stats,
                     stat_dtype):
    """Creates a batch normalization op.

    It uses the tf.nn.batch_normalization op by default and the
    tf.nn.fused_batch_norm op to support fused batch normalization.

    Args:
      input_batch: A input Tensor of arbitrary dimension.
      mean: A mean tensor, of the same dtype as `input_batch`.
      variance: A variance tensor, of the same dtype as `input_batch`.
      use_batch_stats: A bool value that indicates whether the operation should
         use the batch statistics.
      stat_dtype: TensorFlow datatype used for the moving mean and variance.

    Returns:
      A batch normalization operation.
      The current mean tensor, of datatype `stat_dtype`.
      The current variance tensor, of datatype `stat_dtype`.
    """
    if self._fused:
      # For the non-training case where not using batch stats,
      # pass in the moving statistic variables directly.
      # These will already be in the correct dtype, even for float16 input.
      batch_norm_op, mean, variance = self._fused_batch_norm_op(
          input_batch,
          self._moving_mean, self._moving_variance, use_batch_stats)
    else:
      batch_norm_op = tf.nn.batch_normalization(
          input_batch,
          mean,
          variance,
          self._beta,
          self._gamma,
          self._eps,
          name="batch_norm")
      # We'll echo the supplied mean and variance so that they can also be used
      # to update the moving statistics. Cast to matching type if necessary.
      if input_batch.dtype.base_dtype != stat_dtype:
        mean = tf.cast(mean, stat_dtype)
        variance = tf.cast(variance, stat_dtype)

    return batch_norm_op, mean, variance

Response: Creates a batch normalization op.


Instruction:
Create a Python 3 function to
set up optional scale and offset factors.

Response:
def _build_scale_offset(self, dtype):
    """Sets up optional scale and offset factors."""

    # tf.nn.fused_batch_norm accepts float16 batch data, but not scale/offset.
    if self._fused and dtype == tf.float16:
      dtype = tf.float32

    # The fused batch norm operation needs the beta, gamma variables,
    # so in this case we build them and set the trainable option according
    # to the values of _offset and _scale.
    self._beta = None
    if self._offset or self._fused:
      if self.BETA not in self._initializers:
        self._initializers[self.BETA] = create_beta_initializer()
      self._beta = tf.get_variable(
          self.BETA,
          dtype=dtype,
          shape=self._mean_shape,
          initializer=self._initializers[self.BETA],
          partitioner=self._partitioners.get(self.BETA, None),
          regularizer=self._regularizers.get(self.BETA, None),
          trainable=self._offset)

    self._gamma = None
    if self._scale or self._fused:
      if self.GAMMA not in self._initializers:
        self._initializers[self.GAMMA] = create_gamma_initializer()
      self._gamma = tf.get_variable(
          self.GAMMA,
          dtype=dtype,
          shape=self._mean_shape,
          initializer=self._initializers[self.GAMMA],
          partitioner=self._partitioners.get(self.GAMMA, None),
          regularizer=self._regularizers.get(self.GAMMA, None),
          trainable=self._scale)


Instruction:
Can you implement a function in Python 3 that
connects the BatchNorm module into the graph.

Response:
def _build(self, input_batch, is_training, test_local_stats=True):
    """Connects the BatchNorm module into the graph.

    Args:
      input_batch: A Tensor of arbitrary dimension. By default, the final
        dimension is not reduced over when computing the minibatch statistics.
      is_training: A boolean to indicate if the module should be connected in
        training mode, meaning the moving averages are updated. Can be a Tensor.
      test_local_stats: A boolean to indicate if local batch statistics should
        be used when `is_training=False`. If not, moving averages are used.
        By default `True`. Can be a Tensor.

    Returns:
      A tensor with the same shape as `input_batch`.

    Raises:
      base.IncompatibleShapeError: If `axis` is not valid for the
        input shape or has negative entries.
      base.NotSupportedError: If `input_batch` has data type of `tf.bfloat16`.
    """
    input_shape = input_batch.get_shape()

    if self._axis is not None:
      if len(self._axis) > len(input_shape):
        raise base.IncompatibleShapeError(
            "Too many indices specified in axis: len({}) > len({}).".format(
                self._axis, input_shape))

      if max(self._axis) >= len(input_shape):
        raise base.IncompatibleShapeError(
            "One or more index in axis is too large for "
            "input shape: {} >= {:d}.".format(self._axis, len(input_shape)))

      if min(self._axis) < 0:
        raise base.IncompatibleShapeError(
            "Indices in axis must be non-negative: {} < 0.".format(
                self._axis))

      axis = self._axis
    else:
      # Reduce over all dimensions except the last.
      axis = tuple(range(len(input_shape))[:-1])

    dtype = input_batch.dtype.base_dtype
    if self._fused and dtype == tf.bfloat16:
      raise base.NotSupportedError(
          "Fused batch norm does not support tf.bfloat16.")
    # Maintain moving averages at a minimum precision of tf.float32.
    stat_dtype = tf.float32 if dtype in [tf.float16, tf.bfloat16] else dtype

    self._mean_shape = input_batch.get_shape().as_list()
    for index in axis:
      self._mean_shape[index] = 1

    use_batch_stats = is_training | test_local_stats

    mean, variance = self._build_statistics(input_batch, axis,
                                            use_batch_stats, stat_dtype)

    # Sets up optional gamma and beta parameters
    self._build_scale_offset(dtype)
    # Sets up the batch normalization op.
    out, mean, variance = self._batch_norm_op(input_batch, mean, variance,
                                              use_batch_stats, stat_dtype)
    # Sets up the update op.
    update_ops = self._build_update_ops(mean, variance, is_training)

    # Put update ops in the update ops collection if given, otherwise add as
    # control dependencies of the output.
    if update_ops:
      if self._update_ops_collection:
        for update_op in update_ops:
          tf.add_to_collection(self._update_ops_collection, update_op)
      else:
        with tf.control_dependencies(update_ops):
          out = tf.identity(out)

    return out


Instruction:
Write a Python 3 script for
producing the list of integer indices corresponding to a token list.

Response:
def tokenize(self, token_list):
    """Produces the list of integer indices corresponding to a token list."""
    return [
        self._vocab_dict.get(token, self._vocab_dict[self.UNK])
        for token in token_list
    ]


Instruction:
Here you have a function in Python 3, explain what it does
def _get_batch(self):
    batch_indices = np.mod(
        np.array([
            np.arange(head_index, head_index + self._num_steps + 1) for
            head_index in self._head_indices]),
        self._n_flat_elements)

    obs = np.array([
        self._flat_data[indices[:self._num_steps]]
        for indices in batch_indices]).T
    target = np.array([
        self._flat_data[indices[1:self._num_steps + 1]]
        for indices in batch_indices]).T

    if self._random_sampling:
      self._reset_head_indices()
    else:
      self._head_indices = np.mod(
          self._head_indices + self._num_steps, self._n_flat_elements)
    return obs, target

Response: Returns a batch of sequences.


Instruction:
Write a Python 3 script to
build the sequence data ops.

Response:
def _build(self):
    """Returns a tuple containing observation and target one-hot tensors."""
    q = tf.FIFOQueue(
        self._queue_capacity, [self._dtype, self._dtype],
        shapes=[[self._num_steps, self._batch_size, self._vocab_size]]*2)
    obs, target = tf.py_func(self._get_batch, [], [tf.int32, tf.int32])
    obs = self._one_hot(obs)
    target = self._one_hot(target)
    enqueue_op = q.enqueue([obs, target])
    obs, target = q.dequeue()
    tf.train.add_queue_runner(tf.train.QueueRunner(q, [enqueue_op]))
    return SequenceDataOpsNoMask(obs, target)


Instruction:
Write a Python 3 script to
return the cost for a sequence of logits and target.

Response:
def cost(self, logits, target):
    """Returns cost.

    Args:
      logits: model output.
      target: target.

    Returns:
      Cross-entropy loss for a sequence of logits. The loss will be averaged
      across time steps if time_average_cost was enabled at construction time.
    """
    logits = tf.reshape(logits, [self._num_steps * self._batch_size, -1])
    target = tf.reshape(target, [self._num_steps * self._batch_size, -1])
    xent = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=target)
    loss = tf.reduce_sum(xent)

    return loss / self._batch_size


Instruction:
Implement a Python 3 function for
returning a human - readable version of a one - hot encoding of words.

Response:
def to_human_readable(self,
                        data,
                        label_batch_entries=True,
                        indices=None,
                        sep="\n"):
    """Returns a human-readable version of a one-hot encoding of words.

    Args:
      data: A tuple with (obs, target). `obs` is a numpy array with one-hot
          encoding of words.
      label_batch_entries: bool. Whether to add numerical label before each
          batch element in the output string.
      indices: List of int or None. Used to select a subset of minibatch indices
          to print. None will print the whole minibatch.
      sep: A char separator which separates the output for each batch. Defaults
          to the newline character.

    Returns:
      String with the words from `data[0]`.
    """
    obs = data[0]
    batch_size = obs.shape[1]
    result = []
    indices = xrange(batch_size) if not indices else indices
    for b in indices:
      index_seq = np.argmax(obs[:, b], axis=1)
      prefix = "b_{}: ".format(b) if label_batch_entries else ""
      result.append(prefix + self._data_source.decode(index_seq))
    return sep.join(result)


Instruction:
Write a Python 3 function for
returning training and testing dataset iterators.

Response:
def get_data(name, train_batch_size, test_batch_size):
  """Gets training and testing dataset iterators.

  Args:
    name: String. Name of dataset, either 'mnist' or 'cifar10'.
    train_batch_size: Integer. Batch size for training.
    test_batch_size: Integer. Batch size for testing.

  Returns:
    Dict containing:
      train_iterator: A tf.data.Iterator, over training data.
      test_iterator: A tf.data.Iterator, over test data.
      num_classes: Integer. Number of class labels.
  """
  if name not in ['mnist', 'cifar10']:
    raise ValueError(
        'Expected dataset \'mnist\' or \'cifar10\', but got %s' % name)
  dataset = getattr(tf.keras.datasets, name)
  num_classes = 10

  # Extract the raw data.
  raw_data = dataset.load_data()
  (images_train, labels_train), (images_test, labels_test) = raw_data

  # Normalize inputs and fix types.
  images_train = images_train.astype(np.float32) / 255.
  images_test = images_test.astype(np.float32) / 255.
  labels_train = labels_train.astype(np.int32).squeeze()
  labels_test = labels_test.astype(np.int32).squeeze()

  # Add a dummy 'color channel' dimension if it is not present.
  if images_train.ndim == 3:
    images_train = np.expand_dims(images_train, -1)
    images_test = np.expand_dims(images_test, -1)

  # Put the data onto the graph as constants.
  train_data = tf.data.Dataset.from_tensor_slices((images_train, labels_train))
  test_data = tf.data.Dataset.from_tensor_slices((images_test, labels_test))

  # Create iterators for each dataset.
  train_iterator = (
      train_data
      # Note: For larger datasets e.g. ImageNet, it will not be feasible to have
      # a shuffle buffer this large.
      .shuffle(buffer_size=len(images_train))
      .batch(train_batch_size)
      .repeat()
      .make_one_shot_iterator()
  )
  test_iterator = test_data.batch(test_batch_size).make_initializable_iterator()
  return dict(
      train_iterator=train_iterator,
      test_iterator=test_iterator,
      num_classes=num_classes)


Instruction:
Explain what the following Python 3 code does
def get_variable_scope_name(value):
  # If the object has a "variable_scope" property, use it.
  value = getattr(value, "variable_scope", value)
  if isinstance(value, tf.VariableScope):
    return value.name
  elif isinstance(value, six.string_types):
    return value
  else:
    raise ValueError("Not a variable scope: {}".format(value))

Response: Returns the name of the variable scope indicated by the given value.


Instruction:
Write a Python 3 script for
returning a tuple of tf. Variable s in a given scope for a given collection.

Response:
def get_variables_in_scope(scope, collection=tf.GraphKeys.TRAINABLE_VARIABLES):
  """Returns a tuple `tf.Variable`s in a scope for a given collection.

  Args:
    scope: `tf.VariableScope` or string to retrieve variables from.
    collection: Collection to restrict query to. By default this is
        `tf.Graphkeys.TRAINABLE_VARIABLES`, which doesn't include non-trainable
        variables such as moving averages.

  Returns:
    A tuple of `tf.Variable` objects.
  """
  scope_name = get_variable_scope_name(scope)

  if scope_name:
    # Escape the name in case it contains any "." characters. Add a closing
    # slash so we will not search any scopes that have this scope name as a
    # prefix.
    scope_name = re.escape(scope_name) + "/"

  return tuple(tf.get_collection(collection, scope_name))


Instruction:
Given the following Python 3 function, write the documentation
def get_variables_in_module(module,
                            collection=tf.GraphKeys.TRAINABLE_VARIABLES):
  """Returns tuple of `tf.Variable`s declared inside an `snt.Module`.

  Note that this operates by searching the variable scope a module contains,
  and so does not know about any modules which were constructed elsewhere but
  used inside this module.

  Args:
    module: `snt.Module` instance to query the scope of.
    collection: Collection to restrict query to. By default this is
      `tf.Graphkeys.TRAINABLE_VARIABLES`, which doesn't include non-trainable
      variables such as moving averages.

  Returns:
    A tuple of `tf.Variable` objects.

  Raises:
    NotConnectedError: If the module is not connected to the Graph.
  """
  return module.get_variables(collection=collection)

Response: Returns a tuple of tf. Variable s declared inside a module.


Instruction:
Can you implement a function in Python 3 that
checks if all items in the dictionary and all subdictionaries with callables are callables.

Response:
def _check_nested_callables(dictionary, object_name):
  """Checks if all items in the dictionary and in subdictionaries are callables.

  Args:
    dictionary: Dictionary of callables or other dictionaries with callables.
    object_name: The name of the object that is expected in the dictionary.
      E.g. 'Initializer', 'Partitioner' or 'Regularizer'. The first letter
      should be capitalised as this will be the first word in the error message.

  Raises:
    TypeError: If the dictionary contains something that is not either a
      dictionary or a callable.
  """
  for key, entry in six.iteritems(dictionary):
    if hasattr(entry, "items"):
      _check_nested_callables(entry, object_name)
    elif not callable(entry):
      raise TypeError(
          "{} for '{}' is not a callable function or dictionary"
          .format(object_name, key))


Instruction:
Make a summary of the following Python 3 code
def _assert_is_dictlike(maybe_dictlike, valid_keys):
  # This covers a common mistake when people use incorrect dictionary nesting
  # for initializers / partitioners etc. The previous error message was quite
  # opaque, this should be much clearer.
  if not hasattr(maybe_dictlike, "__getitem__"):
    raise TypeError(
        "Expected a dict-like object with possible keys %s, received %s" %
        (str(valid_keys), str(maybe_dictlike)))

Response: Raises a TypeError iff maybe_dictlike is not a dictlike object.


Instruction:
Can you create a Python 3 function that
checks that the given initializers are valid and that all of the keys in are in the given keys.

Response:
def check_initializers(initializers, keys):
  """Checks the given initializers.

  This checks that `initializers` is a dictionary that only contains keys in
  `keys`, and furthermore the entries in `initializers` are functions or
  further dictionaries (the latter used, for example, in passing initializers
  to modules inside modules) that must satisfy the same constraints.

  Args:
    initializers: Dictionary of initializers (allowing nested dictionaries) or
      None.
    keys: Iterable of valid keys for `initializers`.

  Returns:
    Copy of checked dictionary of initializers. If `initializers=None`, an empty
    dictionary will be returned.

  Raises:
    KeyError: If an initializer is provided for a key not in `keys`.
    TypeError: If a provided initializer is not a callable function, or
      `initializers` is not a Mapping.
  """
  if initializers is None:
    return {}
  _assert_is_dictlike(initializers, valid_keys=keys)

  keys = set(keys)

  if not set(initializers) <= keys:
    extra_keys = set(initializers) - keys
    raise KeyError(
        "Invalid initializer keys {}, initializers can only "
        "be provided for {}".format(
            ", ".join("'{}'".format(key) for key in extra_keys),
            ", ".join("'{}'".format(key) for key in keys)))

  _check_nested_callables(initializers, "Initializer")

  return dict(initializers)


Instruction:
Create a Python 3 function for
checking that the given partitioners are valid and that they are nested dictionaries.

Response:
def check_partitioners(partitioners, keys):
  """Checks the given partitioners.

  This checks that `partitioners` is a dictionary that only contains keys in
  `keys`, and furthermore the entries in `partitioners` are functions or
  further dictionaries (the latter used, for example, in passing partitioners
  to modules inside modules) that must satisfy the same constraints.

  Args:
    partitioners: Dictionary of partitioners (allowing nested dictionaries) or
        None.
    keys: Iterable of valid keys for `partitioners`.

  Returns:
    Checked dictionary of partitioners. If `partitioners=None`, an empty
    dictionary will be returned.

  Raises:
    KeyError: If an partitioner is provided for a key not in `keys`.
    TypeError: If a provided partitioner is not a callable function, or
      `partitioners` is not a Mapping.
  """
  if partitioners is None:
    return {}
  _assert_is_dictlike(partitioners, valid_keys=keys)

  keys = set(keys)

  if not set(partitioners) <= keys:
    extra_keys = set(partitioners) - keys
    raise KeyError(
        "Invalid partitioner keys {}, partitioners can only "
        "be provided for {}".format(
            ", ".join("'{}'".format(key) for key in extra_keys),
            ", ".join("'{}'".format(key) for key in keys)))

  _check_nested_callables(partitioners, "Partitioner")

  return partitioners


Instruction:
Can you generate a brief explanation for the following Python 3 code
def check_regularizers(regularizers, keys):
  if regularizers is None:
    return {}
  _assert_is_dictlike(regularizers, valid_keys=keys)

  keys = set(keys)

  if not set(regularizers) <= keys:
    extra_keys = set(regularizers) - keys
    raise KeyError(
        "Invalid regularizer keys {}, regularizers can only "
        "be provided for {}".format(
            ", ".join("'{}'".format(key) for key in extra_keys),
            ", ".join("'{}'".format(key) for key in keys)))

  _check_nested_callables(regularizers, "Regularizer")

  return dict(regularizers)

Response: Checks that regularizers are valid and that all of the keys in keys are valid.


Instruction:
Can you generate the documentation for the following Python 3 function
def _is_scope_prefix(scope_name, prefix_name):

  if not prefix_name:
    return True

  if not scope_name.endswith("/"):
    scope_name += "/"

  if not prefix_name.endswith("/"):
    prefix_name += "/"

  return scope_name.startswith(prefix_name)

Response: Checks that prefix_name is a proper scope prefix of scope_name.


Instruction:
Explain what the following Python 3 code does
def _get_sliced_variables(var_list):
  unsliced_variables = []
  sliced_variables = collections.defaultdict(lambda: [])
  for var in var_list:
    if var._save_slice_info:
      sliced_variables[var._save_slice_info.full_name].append(var)
    else:
      unsliced_variables.append(var)
  return unsliced_variables, sliced_variables

Response: Separates the sliced ( partitioned ) and unsliced variables in var_list.


Instruction:
Create a Python 3 function for
creating a custom getter that routes to a specific pattern matching the requested variable name.

Response:
def custom_getter_router(custom_getter_map, name_fn):
  """Creates a custom getter than matches requests to dict of custom getters.

  Custom getters are callables which implement the
  [custom getter API]
  (https://www.tensorflow.org/versions/r1.0/api_docs/python/tf/get_variable).

  The returned custom getter dispatches calls based on pattern matching the
  name of the requested variable to the keys of custom_getter_map. For example,

      {
        ".*/w": snt.custom_getters.stop_gradient,
      }

  will match all variables named with the suffix "/w". The `name_fn` is
  provided to allow processing of the name, such as stripping off a scope prefix
  before matching.

  Args:
    custom_getter_map: Mapping of regular expressions to custom getter
      functions.
    name_fn: Callable to map variable name through before matching to regular
      expressions. This might, for example, strip off a scope prefix.

  Returns:
    A custom getter.

  Raises:
    TypeError: If an entry in `custom_getter_map` is not a callable function.
  """

  for custom_getter in custom_getter_map.values():
    if not callable(custom_getter):
      raise TypeError("Given custom_getter is not callable.")

  def _custom_getter(getter, name, *args, **kwargs):
    """A custom getter that routes based on pattern matching the variable name.

    Args:
      getter: The true getter to call.
      name: The fully qualified variable name, i.e. including all scopes.
      *args: Arguments, in the same format as tf.get_variable.
      **kwargs: Keyword arguments, in the same format as tf.get_variable.

    Returns:
      The return value of the appropriate custom getter. If there are no
      matches, it returns the return value of `getter`.

    Raises:
      KeyError: If more than one pattern matches the variable name.
    """
    bare_name = name_fn(name)
    matches = [
        (custom_getter, pattern)
        for pattern, custom_getter in custom_getter_map.items()
        if re.match(pattern, bare_name) is not None]

    num_matches = len(matches)

    if num_matches == 0:
      return getter(name, *args, **kwargs)
    elif num_matches == 1:
      custom_getter, pattern = matches[0]
      return custom_getter(getter, name, *args, **kwargs)
    else:
      raise KeyError("More than one custom_getter matched {} ({}): {}".format(
          name, bare_name, [pattern for _, pattern in matches]))

  return _custom_getter


Instruction:
Given the following Python 3 function, write the documentation
def get_normalized_variable_map(scope_or_module,
                                collection=tf.GraphKeys.GLOBAL_VARIABLES,
                                context=None,
                                group_sliced_variables=True):
  """Builds map of `tf.Variable`s in scope or module with normalized names.

  The names of the variables are normalized to remove the scope prefix.

  Args:
    scope_or_module: Scope or module to build map from.
    collection: Collection to restrict query to. By default this is
        `tf.Graphkeys.GLOBAL_VARIABLES`, which includes non-trainable variables
        such as moving averages.
    context: Scope or module, identical to or parent of `scope`. If given, this
        will be used as the stripped prefix. By default `None`, which means
        `context=scope`.
    group_sliced_variables: Boolean, if set to True, sliced variables are
       grouped together in the returned map; if set to False, each partition of
       a sliced variable is a separate (key, value) pair.

  Returns:
    Dictionary mapping normalized variable name to `tf.Variable`, or a list
        of `tf.Variables` if the variable is a sliced (partitioned) variable.

  Raises:
    ValueError: If `context` is given but is not a proper prefix of `scope`.
  """
  scope_name = get_variable_scope_name(scope_or_module)

  if context is None:
    context = scope_or_module

  prefix = get_variable_scope_name(context)
  prefix_length = len(prefix) + 1 if prefix else 0

  if not _is_scope_prefix(scope_name, prefix):
    raise ValueError("Scope '{}' is not prefixed by '{}'.".format(
        scope_name, prefix))

  variables = get_variables_in_scope(scope_name, collection)

  if not group_sliced_variables:
    single_vars = variables
    grouped_vars = dict()
  else:
    single_vars, grouped_vars = _get_sliced_variables(variables)

  var_map = {var.op.name[prefix_length:]: var for var in single_vars}
  for full_name, var_group in grouped_vars.items():
    name = full_name[prefix_length:]
    if name in var_map:
      raise ValueError("Mixing slices and non-slices with the same name: " +
                       str(name))
    var_map[name] = var_group
  return var_map

Response: Returns a dictionary mapping normalized variable names to tf. Variables.


Instruction:
Can you tell what is the following Python 3 function doing
def get_saver(scope, collections=(tf.GraphKeys.GLOBAL_VARIABLES,),  # pylint: disable=redefined-outer-name
              context=None, **kwargs):
  """Builds a `tf.train.Saver` for the scope or module, with normalized names.

  The names of the variables are normalized to remove the scope prefix.
  This allows the same variables to be restored into another similar scope or
  module using a complementary `tf.train.Saver` object.

  Args:
    scope: Scope or module. Variables within will be saved or restored.
    collections: Sequence of collections of variables to restrict
        `tf.train.Saver` to. By default this is `tf.GraphKeys.GLOBAL_VARIABLES`
        which includes moving averages variables as well as trainable variables.
    context: Scope or module, identical to or parent of `scope`. If given, this
        will be used as the stripped prefix.
    **kwargs: Extra keyword arguments to pass to tf.train.Saver.

  Returns:
    A `tf.train.Saver` object for Variables in the scope or module.
  """

  variable_map = {}
  for collection in collections:
    variable_map.update(get_normalized_variable_map(scope, collection, context))

  return tf.train.Saver(var_list=variable_map, **kwargs)

Response: Builds a tf. train. Saver object for the given scope or module.


Instruction:
Write a Python 3 function that can
yield an iterator over ( string tf. Variable pairs in variable map.

Response:
def variable_map_items(variable_map):
  """Yields an iterator over (string, variable) pairs in the variable map.

  In general, variable maps map variable names to either a `tf.Variable`, or
  list of `tf.Variable`s (in case of sliced variables).

  Args:
    variable_map: dict, variable map over which to iterate.

  Yields:
    (string, tf.Variable) pairs.
  """
  for key, var_or_vars in six.iteritems(variable_map):
    if isinstance(var_or_vars, (list, tuple)):
      for variable in var_or_vars:
        yield key, variable
    else:
      yield key, var_or_vars


Instruction:
Make a summary of the following Python 3 code
def _get_vars_to_collections(variables):
  var_to_collections = collections.defaultdict(lambda: [])
  if isinstance(variables, dict):
    variables = list(v for _, v in variable_map_items(variables))
  for graph in set(v.graph for v in variables):
    for collection_name in list(graph.collections):
      entries = set(entry for entry in graph.get_collection(collection_name)
                    if isinstance(entry, tf.Variable))
      # For legacy reasons, tf.GraphKeys.GLOBAL_VARIABLES == "variables".
      # Correcting for this here, to avoid confusion.
      if collection_name == tf.GraphKeys.GLOBAL_VARIABLES:
        collection_name = "global_variables"
      for var in entries.intersection(variables):
        var_to_collections[var].append(collection_name)
  return var_to_collections

Response: Returns a dict mapping variables to the collections they appear in.


Instruction:
Here you have a function in Python 3, explain what it does
def _format_device(var):
  if var.dtype.name.endswith("_ref"):
    resource_var_annotation = "(legacy)"
  else:
    resource_var_annotation = "(resource)"

  if var.device:
    return "{} {}".format(var.device, resource_var_annotation)
  else:
    return resource_var_annotation

Response: Formats the device with an annotation specifying ResourceVariable.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def format_variables(variables, join_lines=True):
  rows = []
  rows.append(("Variable", "Shape", "Type", "Collections", "Device"))
  var_to_collections = _get_vars_to_collections(variables)
  for var in sorted(variables, key=lambda var: var.op.name):
    if var.get_shape().is_fully_defined():
      shape = "x".join(str(dim) for dim in var.get_shape().as_list())
    else:
      shape = "undefined"
    dtype = repr(var.dtype.base_dtype).replace("tf.", "")
    coll = ", ".join(sorted(var_to_collections[var]))
    rows.append((var.op.name, shape, dtype, coll, _format_device(var)))
  return _format_table(rows, join_lines)

Response: Takes a collection of variables and formats it as a table.


Instruction:
Write a Python 3 script for
taking a key - to - variable map and formats it as a table.

Response:
def format_variable_map(variable_map, join_lines=True):
  """Takes a key-to-variable map and formats it as a table."""
  rows = []
  rows.append(("Key", "Variable", "Shape", "Type", "Collections", "Device"))
  var_to_collections = _get_vars_to_collections(variable_map)

  sort_key = lambda item: (item[0], item[1].name)
  for key, var in sorted(variable_map_items(variable_map), key=sort_key):
    shape = "x".join(str(dim) for dim in var.get_shape().as_list())
    dtype = repr(var.dtype.base_dtype).replace("tf.", "")
    coll = ", ".join(sorted(var_to_collections[var]))
    rows.append((key, var.op.name, shape, dtype, coll, _format_device(var)))
  return _format_table(rows, join_lines)


Instruction:
Create a Python 3 function for
logging variables for either or all variables.

Response:
def log_variables(variables=None):
  """Logs variable information.

  This function logs the name, shape, type, collections, and device for either
  all variables or a given iterable of variables. In the "Device" columns,
  the nature of the variable (legacy or resource (for ResourceVariables)) is
  also specified in parenthesis.

  Args:
    variables: iterable of variables; if not provided, then all variables
        (in the default graph) are logged.
  """
  if variables is None:
    variables = tf.global_variables() + tf.local_variables()
  for row in format_variables(variables, join_lines=False):
    tf.logging.info(row)


Instruction:
Write a Python 3 script for
returning human readable string of how much memory num_bytes fills.

Response:
def _num_bytes_to_human_readable(num_bytes):
  """Returns human readable string of how much memory `num_bytes` fills."""
  if num_bytes < (2 ** 10):
    return "%d B" % num_bytes
  elif num_bytes < (2 ** 20):
    return "%.3f KB" % (float(num_bytes) / (2 ** 10))
  elif num_bytes < (2 ** 30):
    return "%.3f MB" % (float(num_bytes) / (2 ** 20))
  else:
    return "%.3f GB" % (float(num_bytes) / (2 ** 30))


Instruction:
Can you generate the documentation for the following Python 3 function
def summarize_variables(variables=None):

  variable_counts = count_variables_by_type(variables=variables)
  total_num_scalars = 0
  total_num_bytes = 0

  # Sort by string representation of type name, so output is deterministic.
  for dtype in sorted(variable_counts,
                      key=lambda dtype: "%r" % dtype):
    var_info_for_type = variable_counts[dtype]
    num_bytes = var_info_for_type["num_scalars"] * dtype.size
    total_num_scalars += var_info_for_type["num_scalars"]
    total_num_bytes += num_bytes
    tf.logging.info("%r: %d variables comprising %d scalars, %s",
                    dtype, var_info_for_type["num_variables"],
                    var_info_for_type["num_scalars"],
                    _num_bytes_to_human_readable(num_bytes))

Response: Logs a summary of the number of variables comprising scalars and total memory consumed.


Instruction:
Write a Python 3 function that can
return a dict mapping dtypes to number of variables and scalars.

Response:
def count_variables_by_type(variables=None):
  """Returns a dict mapping dtypes to number of variables and scalars.

  Args:
    variables: iterable of `tf.Variable`s, or None. If None is passed, then all
      global and local variables in the current graph are used.

  Returns:
    A dict mapping tf.dtype keys to a dict containing the keys 'num_scalars' and
      'num_variables'.
  """
  if variables is None:
    variables = tf.global_variables() + tf.local_variables()
  unique_types = set(v.dtype.base_dtype for v in variables)
  results_dict = {}
  for dtype in unique_types:
    if dtype == tf.string:
      tf.logging.warning(
          "NB: string Variables present. The memory usage for these  Variables "
          "will not be accurately computed as it depends on the exact strings "
          "stored in a particular session.")
    vars_of_type = [v for v in variables if v.dtype.base_dtype == dtype]
    num_scalars = sum(v.shape.num_elements() for v in vars_of_type)
    results_dict[dtype] = {
        "num_variables": len(vars_of_type),
        "num_scalars": num_scalars
    }
  return results_dict


Instruction:
How would you code a function in Python 3 to
wrap an arbitrary method so it does variable sharing. This decorator creates variables the first time it calls `method`, and reuses them for subsequent calls. The object that calls `method` provides a `tf.VariableScope`, either as a `variable_scope` attribute or as the return value of an `_enter_variable_scope()` method. The first time the wrapped method is invoked, it enters the caller's `tf.VariableScope` with `reuse=False`. On all subsequent calls it enters the same variable scope with `reuse=True`. Variables are created in the context of the `tf.VariableScope` provided by the caller object. Ops are created with an additional `tf.name_scope()`, which adds a scope for the wrapped method name. For example: ```python class MyClass(object): def __init__(self, name): with tf.variable_scope(None, default_name=name) as variable_scope: self.variable_scope = variable_scope @snt.reuse_variables def add_x(self, tensor): x = tf.get_variable("x", shape=tensor.get_shape()) return tensor + x module = MyClass("my_module_name") input_tensor = tf.zeros(shape=(5,)) # This creates the variable "my_module_name/x" # and op "my_module_name/add_x/add" output = module.add_x(input_tensor) ``` For performance when executing eagerly it may be desirable to additionally annotate these methods using `defun`, such that they are encapsulated as graph functions. This is not recommended if your method returns a variable since the output of `defun` would be an op that returned the variable's value when evaluated (rather than the variable instance). ```python class FooModule(snt.AbstractModule): def _build(self, inputs): return complex_math(inputs) @tfe.defun @snt.reuse_variables def more_complex_stuff(self, inputs): return more_complex_math(inputs) ``` Args: method: The method to wrap. Returns: The wrapped method.

Response:
def reuse_variables(method):
  """Wraps an arbitrary method so it does variable sharing.

  This decorator creates variables the first time it calls `method`, and reuses
  them for subsequent calls. The object that calls `method` provides a
  `tf.VariableScope`, either as a `variable_scope` attribute or as the return
  value of an `_enter_variable_scope()` method.

  The first time the wrapped method is invoked, it enters the caller's
  `tf.VariableScope` with `reuse=False`. On all subsequent calls it enters the
  same variable scope with `reuse=True`.

  Variables are created in the context of the `tf.VariableScope` provided by the
  caller object. Ops are created with an additional `tf.name_scope()`, which
  adds a scope for the wrapped method name. For example:

  ```python
  class MyClass(object):

    def __init__(self, name):
      with tf.variable_scope(None, default_name=name) as variable_scope:
        self.variable_scope = variable_scope

    @snt.reuse_variables
    def add_x(self, tensor):
      x = tf.get_variable("x", shape=tensor.get_shape())
      return tensor + x

  module = MyClass("my_module_name")
  input_tensor = tf.zeros(shape=(5,))

  # This creates the variable "my_module_name/x"
  # and op "my_module_name/add_x/add"
  output = module.add_x(input_tensor)
  ```

  For performance when executing eagerly it may be desirable to additionally
  annotate these methods using `defun`, such that they are encapsulated as
  graph functions. This is not recommended if your method returns a variable
  since the output of `defun` would be an op that returned the variable's value
  when evaluated (rather than the variable instance).

  ```python
  class FooModule(snt.AbstractModule):
    def _build(self, inputs):
      return complex_math(inputs)

    @tfe.defun
    @snt.reuse_variables
    def more_complex_stuff(self, inputs):
      return more_complex_math(inputs)
  ```

  Args:
    method: The method to wrap.

  Returns:
    The wrapped method.
  """
  initialized_variable_scopes_eager = set()
  initialized_variable_scopes_graph = weakref.WeakKeyDictionary()

  # Ensure that the argument passed in is really a method by checking that the
  # first positional argument to it is "self".
  arg_spec = inspect.getargspec(method)
  is_method = arg_spec.args and arg_spec.args[0] == "self"

  if not is_method:
    raise TypeError("reuse_variables can only be used with methods.")

  @wrapt.decorator
  def eager_test(method, obj, args, kwargs):
    """Validates runtime state in eager mode."""
    # If @reuse_variables is combined with @property, obj is passed in args
    # and method is still unbound at this stage.
    if obj is None:
      obj = args[0]

    if tf.executing_eagerly() and not hasattr(obj, "_template"):
      raise ValueError(
          "reuse_variables is not supported in eager mode except in Sonnet "
          "modules.")

    return method(*args, **kwargs)

  @wrapt.decorator
  def call_method(method, obj, args, kwargs):
    """Calls `method` with a variable scope whose reuse flag is set correctly.

    The first time the wrapper is called it creates a
    `(tf.Graph, tf.VariableScope)` key and checks it for membership in
    `initialized_variable_scopes`. The check is `False` if and only if this is
    the first time the wrapper has been called with the key, otherwise the
    check is `True`. The result of this check is used as the `reuse` flag for
    entering the provided variable scope before calling `method`.

    Here are two examples of how to use the reuse_variables decorator.

    1. Decorate an arbitrary instance method with a `variable_scope` attribute:

      ```python
      class Reusable(object):

        def __init__(self, name):
          with tf.variable_scope(None, default_name=name) as vs:
            self.variable_scope = vs

        @snt.reuse_variables
        def add_a(self, input_tensor):
          a = tf.get_variable("a", shape=input_tensor.get_shape())
          return a + input_tensor

      obj = Reusable("reusable")
      x = tf.constant(5.0)
      out1 = obj.add_a(x)
      out2 = obj.add_a(x)
      # out1 == out2
      ```

    2. Decorating a snt.AbstractModule instance method:

      ```python
      class ReusableModule(snt.AbstractModule):

        @snt.reuse_variables
        def add_a(self, input_tensor):
          a = tf.get_variable("a", shape=input_tensor.get_shape())
          return a + input_tensor

        # We don't need @snt.reuse_variables here because build is
        wrapped by # `tf.make_template` inside `snt.AbstractModule`.
        def _build(self, input_tensor):
          b = tf.get_variable("b", shape=input_tensor.get_shape())
          return b + self.add_a(input_tensor)

      obj = Reusable("reusable")
      x = tf.constant(5.0)
      out1 = obj(x)
      out2 = obj(x)
      # out1 == out2
      ```

    Args:
      method: The method to wrap.
      obj: The object instance passed to the wrapped method.
      args: The positional arguments (Tensors) passed to the wrapped method.
      kwargs: The keyword arguments passed to the wrapped method.

    Returns:
      Output of the wrapped method.

    Raises:
      ValueError: If no variable scope is provided or if `method` is a method
                  and a variable_scope keyword argument is also provided.
    """

    # If @reuse_variables is combined with @property, obj is passed in args
    # and method is still unbound at this stage.
    if obj is None:
      obj = args[0]

    def default_context_manager(reuse=None):
      variable_scope = obj.variable_scope
      return tf.variable_scope(variable_scope, reuse=reuse)

    variable_scope_context_manager = getattr(obj, "_enter_variable_scope",
                                             default_context_manager)

    with tf.init_scope():
      # We need `init_scope` incase we're running inside a defun. In that case
      # what we want is information about where the function will be called not
      # where the function is being built.
      graph = tf.get_default_graph()
      will_call_in_eager_context = tf.executing_eagerly()

    if will_call_in_eager_context:
      initialized_variable_scopes = initialized_variable_scopes_eager
    else:
      if graph not in initialized_variable_scopes_graph:
        initialized_variable_scopes_graph[graph] = set()
      initialized_variable_scopes = initialized_variable_scopes_graph[graph]

    # Temporarily enter the variable scope to capture it
    with variable_scope_context_manager() as tmp_variable_scope:
      variable_scope = tmp_variable_scope

    reuse = variable_scope.name in initialized_variable_scopes

    # Enter the pure variable scope with reuse correctly set
    with variable_scope_ops._pure_variable_scope(  # pylint:disable=protected-access
        variable_scope, reuse=reuse) as pure_variable_scope:
      current_name_scope = tf.get_default_graph().get_name_scope()
      # Force tf.name_scope to treat current_name_scope as an "absolute" scope
      # so we can re-enter it.
      if current_name_scope and current_name_scope[-1] != "/":
        current_name_scope += "/"
      with tf.name_scope(current_name_scope):
        module_name = pure_variable_scope.name
        method_name = to_snake_case(method.__name__)
        method_name_scope = "{}/{}".format(module_name, method_name)
        with tf.name_scope(method_name_scope) as scope:
          if hasattr(obj, "_capture_variables"):
            with obj._capture_variables():  # pylint: disable=protected-access
              out_ops = method(*args, **kwargs)
          else:
            out_ops = method(*args, **kwargs)
      initialized_variable_scopes.add(pure_variable_scope.name)
      try:
        # If `obj` is a Sonnet module, let it know it's been connected
        # to the TF graph.
        obj._is_connected = True  # pylint: disable=protected-access
        if not tf.executing_eagerly():
          obj._add_connected_subgraph(  # pylint: disable=protected-access
              method, out_ops, scope, args, kwargs)
      except AttributeError:
        pass
    return out_ops

  return eager_test(call_method(method))


Instruction:
Make a summary of the following Python 3 code
def name_for_callable(func):
  if isinstance(func, functools.partial):
    return name_for_callable(func.func)

  try:
    name = func.__name__
  except AttributeError:
    return None

  if name == "<lambda>":
    return None
  else:
    return to_snake_case(name)

Response: Returns a module name for a callable or None if no name can be found.


Instruction:
Create a Python 3 function for
converting a string to a snake_case string.

Response:
def to_snake_case(camel_case):
  """Returns a CamelCase string as a snake_case string."""
  if not re.match(r"^[A-Za-z_]\w*$", camel_case):
    raise ValueError(
        "Input string %s is not a valid Python identifier." % camel_case)

  # Add underscore at word start and ends.
  underscored = re.sub(r"([A-Z][a-z])", r"_\1", camel_case)
  underscored = re.sub(r"([a-z])([A-Z])", r"\1_\2", underscored)
  # Add underscore before alphanumeric chunks.
  underscored = re.sub(r"([a-z])([0-9][^_]*)", r"\1_\2", underscored)
  # Remove any underscores at start or end of name and convert to lowercase.
  return underscored.strip("_").lower()


Instruction:
Write a Python 3 function that can
call callback for all newly created variables.

Response:
def notify_about_new_variables(callback):
  """Calls `callback(var)` for all newly created variables.

  Callback should not modify the variable passed in. Use cases that require
  variables to be modified should use `variable_creator_scope` directly and sit
  within the variable creator stack.

  >>> variables = []
  >>> with notify_about_variables(variables.append):
  ...   v = tf.Variable(1.0, name='v')
  ...   w = tf.get_variable('w', [])
  >>> assert variables == [v, w]

  Args:
    callback: a callable taking a single argument which is a tf.Variable.

  Yields:
    `None` - used for contextmanager API.
  """
  def _tracking_creator(getter, **kwargs):
    v = getter(**kwargs)
    callback(v)
    return v

  with tf.variable_creator_scope(_tracking_creator):
    yield


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _recursive_getattr(module, path):
  if "." not in path:
    return getattr(module, path)
  else:
    first, rest = path.split(".", 1)
    return _recursive_getattr(getattr(module, first), rest)

Response: Recursively gets attributes inside module as specified by path.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def parse_string_to_constructor(ctor_string):
  orig_ctor_string = ctor_string
  if "." not in ctor_string:
    # No module specified - assume part of Sonnet
    ctor_string = "sonnet." + ctor_string

  if ctor_string.startswith("snt."):
    # Replace common short name with full name
    ctor_string = "sonnet." + ctor_string[len("snt."):]

  # Cannot just use importlib directly because of the way we alias subpackages,
  # i.e. 'sonnet.nets.ConvNet2D' does not work because 'sonnet.nets' is actually
  # stored as 'sonnet.python.modules.nets'. To support these aliases we use
  # importlib only for the top level package, and then recursive getattr.
  package_name, rest = ctor_string.split(".", 1)
  package = importlib.import_module(package_name)
  try:
    return _recursive_getattr(package, rest)
  except AttributeError:
    raise ValueError("could not find `{}`, after normalizing to `{}`".format(
        orig_ctor_string, ctor_string))

Response: Converts a string representation of a module in Sonnet to a callable which corresponds to the string representation of a module.


Instruction:
Given the following Python 3 function, write the documentation
def supports_kwargs(module_or_fn, kwargs_list):
  if isinstance(kwargs_list, six.string_types):
    kwargs_list = [kwargs_list]

  # If it's not a function or method, then assume it's a module, so introspect
  # the __call__ method. wrapt ensures that for Sonnet modules the _build
  # signature is available here.
  if not (inspect.isfunction(module_or_fn) or inspect.ismethod(module_or_fn)):
    module_or_fn = module_or_fn.__call__

  arg_spec = inspect.getargspec(module_or_fn)

  # If there is a keywords element, then an arbitrary kwargs will work, as far
  # as we can tell from here.
  takes_arbitrary_kwargs = (arg_spec.keywords is not None)

  for kwarg in kwargs_list:
    if not isinstance(kwarg, six.string_types):
      raise ValueError("kwargs should be strings, instead got {}".format(
          kwarg))
    if kwarg not in arg_spec.args:
      if not takes_arbitrary_kwargs:
        # The function doesn't take **kwargs, and this name is not in the
        # regular args, so it would definitely cause an error to call this.
        return NOT_SUPPORTED
      else:
        # The function may accept the kwarg, but we can't say for sure. Even
        # though this is only one kwarg, we can't be certain about the whole
        # lot, so the combined answer is now "maybe".
        return MAYBE_SUPPORTED
  # All the kwargs must actually be present in the specific args list
  return SUPPORTED

Response: Determines whether the provided callable supports all the kwargs.


Instruction:
Given the following Python 3 function, write the documentation
def remove_unsupported_kwargs(module_or_fn, all_kwargs_dict):
  if all_kwargs_dict is None:
    all_kwargs_dict = {}
  if not isinstance(all_kwargs_dict, dict):
    raise ValueError("all_kwargs_dict must be a dict with string keys.")
  return {
      kwarg: value for kwarg, value in all_kwargs_dict.items()
      if supports_kwargs(module_or_fn, kwarg) != NOT_SUPPORTED
  }

Response: Removes any kwargs not supported by module_or_fn from all_kwargs_dict.


Instruction:
Can you create a Python 3 function that
merges the first dimensions of a tensor.

Response:
def merge_leading_dims(array_or_tensor, n_dims=2):
  """Merge the first dimensions of a tensor.

  Args:
    array_or_tensor: Tensor to have its first dimensions merged. Can also
        be an array or numerical value, which will be converted to a tensor
        for batch application, if needed.
    n_dims: Number of dimensions to merge.

  Returns:
    Either the input value converted to a Tensor, with the requested dimensions
    merged, or the unmodified input value if the input has less than `n_dims`
    dimensions.

  Raises:
    ValueError: If the rank of `array_or_tensor` is not well-defined.
  """
  tensor = tf.convert_to_tensor(array_or_tensor)
  tensor_shape_static = tensor.get_shape()

  # Check if the rank of the input tensor is well-defined.
  if tensor_shape_static.dims is None:
    raise ValueError("Can't merge leading dimensions of tensor of unknown "
                     "rank.")
  tensor_shape_list = tensor_shape_static.as_list()

  # We can only merge the n_dims leading dimensions if the rank of the given
  # tensor is sufficiently large.
  if n_dims > len(tensor_shape_list):
    return array_or_tensor

  if tensor_shape_static.is_fully_defined():
    new_shape = (
        [np.prod(tensor_shape_list[:n_dims])] + tensor_shape_list[n_dims:])

    return tf.reshape(tensor, new_shape)

  # Shape can't be inferred statically.
  tensor_shape = tf.shape(tensor)
  new_first_dim = tf.reduce_prod(tensor_shape[:n_dims], keepdims=True)
  other_dims = tensor_shape[n_dims:]
  new_size = tf.concat([new_first_dim, other_dims], 0)
  result = tf.reshape(tensor, new_size)

  if all(value is not None for value in tensor_shape_list[:n_dims]):
    merged_leading_size = np.prod(tensor_shape_list[:n_dims])
  else:
    merged_leading_size = None
  # We need to set the result size of this, as otherwise we won't be able to
  # pass to e.g. a Linear. Here we need to know at least the rank of the tensor.
  result.set_shape([merged_leading_size] + tensor_shape_list[n_dims:])
  return result


Instruction:
Explain what the following Python 3 code does
def split_leading_dim(tensor, inputs, n_dims=2):
  input_shape_static = inputs.get_shape()
  input_shape_list = input_shape_static.as_list()
  tensor_shape_static = tensor.get_shape()
  tensor_shape_list = tensor_shape_static.as_list()
  if (input_shape_static.is_fully_defined()
      and tensor_shape_static.is_fully_defined()):
    new_shape = input_shape_list[:n_dims] + tensor_shape_list[1:]
    return tf.reshape(tensor, new_shape)

  # Shape can't be inferred statically.
  dims_after_first = tf.shape(tensor)[1:]
  split_sizes = tf.shape(inputs)[:n_dims]
  known_split_sizes = input_shape_list[:n_dims]
  known_dims_after_first = tensor_shape_list[1:]
  output_size = tf.concat([split_sizes, dims_after_first], 0)
  result = tf.reshape(tensor, output_size)
  result.set_shape(known_split_sizes + known_dims_after_first)
  return result

Response: Splits the first dimension of a tensor.


Instruction:
How would you implement a function in Python 3 that
returns a default initializer for weights of a linear module.

Response:
def create_linear_initializer(input_size, dtype=tf.float32):
  """Returns a default initializer for weights of a linear module."""
  stddev = 1 / math.sqrt(input_size)
  return tf.truncated_normal_initializer(stddev=stddev, dtype=dtype)


Instruction:
Create a Python 3 function to
calculate the bias_shape based on the input_shape and bias_dims.

Response:
def calculate_bias_shape(input_shape, bias_dims):
  """Calculate `bias_shape` based on the `input_shape` and `bias_dims`.

  Args:
    input_shape: Shape of the input being passed into the module. The leading
        dimension is the minibatch size.
    bias_dims: The dimensions that bias should be applied over. The remaining
        dimensions will get broadcasted over.

  Returns:
    bias_shape: Tuple corresponding to the shape of bias Variable to create.

  Raises:
    ValueError: If the user attempts to add bias over the minibatch dimension,
        e.g. `bias_dims=[0]`.
  """
  input_rank = len(input_shape)
  # If None, default is to use all dimensions.
  if bias_dims is None:
    return input_shape[1:]
  # If empty list, use a scalar bias.
  elif not bias_dims:
    return ()
  # Otherwise, calculate bias_shape from bias_dims.
  else:
    bias_shape = [1] * input_rank
    # Populate bias dimensions.
    for dim in bias_dims:
      dim %= input_rank
      if dim == 0:
        raise ValueError("Cannot apply bias across the minibatch dimension.")
      bias_shape[dim] = input_shape[dim]
    # Strip leading unit dimensions.
    start = input_rank
    for dim in xrange(1, input_rank):
      if bias_shape[dim] != 1:
        start = dim
        break
    return tuple(bias_shape[start:])


Instruction:
Explain what the following Python 3 code does
def _build(self, inputs):
    input_shape = tuple(inputs.get_shape().as_list())

    if len(input_shape) != 2:
      raise base.IncompatibleShapeError(
          "{}: rank of shape must be 2 not: {}".format(
              self.scope_name, len(input_shape)))

    if input_shape[1] is None:
      raise base.IncompatibleShapeError(
          "{}: Input size must be specified at module build time".format(
              self.scope_name))

    if self._input_shape is not None and input_shape[1] != self._input_shape[1]:
      raise base.IncompatibleShapeError(
          "{}: Input shape must be [batch_size, {}] not: [batch_size, {}]"
          .format(self.scope_name, self._input_shape[1], input_shape[1]))

    self._input_shape = input_shape
    dtype = inputs.dtype

    if "w" not in self._initializers:
      self._initializers["w"] = create_linear_initializer(self._input_shape[1],
                                                          dtype)

    if "b" not in self._initializers and self._use_bias:
      self._initializers["b"] = create_bias_initializer(self._input_shape[1],
                                                        dtype)

    weight_shape = (self._input_shape[1], self.output_size)
    self._w = tf.get_variable("w",
                              shape=weight_shape,
                              dtype=dtype,
                              initializer=self._initializers["w"],
                              partitioner=self._partitioners.get("w", None),
                              regularizer=self._regularizers.get("w", None))
    outputs = tf.matmul(inputs, self._w)

    if self._use_bias:
      bias_shape = (self.output_size,)
      self._b = tf.get_variable("b",
                                shape=bias_shape,
                                dtype=dtype,
                                initializer=self._initializers["b"],
                                partitioner=self._partitioners.get("b", None),
                                regularizer=self._regularizers.get("b", None))
      outputs += self._b

    return outputs

Response: Connects the Linear module into the graph with input Tensor inputs.


Instruction:
Can you tell what is the following Python 3 function doing
def output_size(self):
    if callable(self._output_size):
      self._output_size = self._output_size()
    return self._output_size

Response: Returns the module output size.