Patch

Prompt:
Implement the Python class `AlphaQueues` described below.

Class description:
Client for queues service in the Cloud Tasks API.

Method signatures and docstrings:
- def Create(self, parent_ref, queue_ref, retry_config=None, rate_limits=None, pull_target=None, app_engine_http_target=None): Prepares and sends a Create request for creating a queue.
- def Patch(self, queue_ref, updated_fields, retry_config=None, rate_limits=None, app_engine_routing_override=None): Prepares and sends a Patch request for modifying a queue.

Implement the Python class `AlphaQueues` described below.

Class description:
Client for queues service in the Cloud Tasks API.

Method signatures and docstrings:
- def Create(self, parent_ref, queue_ref, retry_config=None, rate_limits=None, pull_target=None, app_engine_http_target=None): Prepares and sends a Create request for creating a queue.
- def Patch(self, queue_ref, updated_fields, retry_config=None, rate_limits=None, app_engine_routing_override=None): Prepares and sends a Patch request for modifying a queue.

<|skeleton|>
class AlphaQueues:
    """Client for queues service in the Cloud Tasks API."""

    def Create(self, parent_ref, queue_ref, retry_config=None, rate_limits=None, pull_target=None, app_engine_http_target=None):
        """Prepares and sends a Create request for creating a queue."""
        <|body_0|>

    def Patch(self, queue_ref, updated_fields, retry_config=None, rate_limits=None, app_engine_routing_override=None):
        """Prepares and sends a Patch request for modifying a queue."""
        <|body_1|>

<|end_skeleton|>

<|body_start_0|>
        if pull_target and app_engine_http_target:
            raise CreatingPullAndAppEngineQueueError('Attempting to send PullTarget and AppEngineHttpTarget simultaneously')
        queue = self.messages.Queue(name=queue_ref.RelativeName(), retryConfig=retry_config, rateLimits=rate_limits, pullTarget=pull_target, appEngineHttpTarget=app_engine_http_target)
        request = self.messages.CloudtasksProjectsLocationsQueuesCreateRequest(parent=parent_ref.RelativeName(), queue=queue)
        return self.queues_service.Create(request)
<|end_body_0|>

<|body_start_1|>
        if not any([retry_config, rate_limits, app_engine_routing_override]):
            raise NoFieldsSpecifiedError('Must specify at least one field to update.')
        queue = self.messages.Queue(name=queue_ref.RelativeName())
        if retry_config is not None:
            queue.retryConfig = retry_config
        if rate_limits is not None:
            queue.rateLimits = rate_limits
        if app_engine_routing_override is not None:
            if _IsEmptyConfig(app_engine_routing_override):
                queue.appEngineHttpTarget = self.messages.AppEngineHttpTarget()
            else:
                queue.appEngineHttpTarget = self.messages.AppEngineHttpTarget(appEngineRoutingOverride=app_engine_routing_override)
        update_mask = ','.join(updated_fields)
        request = self.messages.CloudtasksProjectsLocationsQueuesPatchRequest(name=queue_ref.RelativeName(), queue=queue, updateMask=update_mask)
        return self.queues_service.Patch(request)
<|end_body_1|>

Solution:
class AlphaQueues:
    """Client for queues service in the Cloud Tasks API."""

    def Create(self, parent_ref, queue_ref, retry_config=None, rate_limits=None, pull_target=None, app_engine_http_target=None):
        """Prepares and sends a Create request for creating a queue."""
        if pull_target and app_engine_http_target:
            raise CreatingPullAndAppEngineQueueError('Attempting to send PullTarget and AppEngineHttpTarget simultaneously')
        queue = self.messages.Queue(name=queue_ref.RelativeName(), retryConfig=retry_config, rateLimits=rate_limits, pullTarget=pull_target, appEngineHttpTarget=app_engine_http_target)
        request = self.messages.CloudtasksProjectsLocationsQueuesCreateRequest(parent=parent_ref.RelativeName(), queue=queue)
        return self.queues_service.Create(request)

    def Patch(self, queue_ref, updated_fields, retry_config=None, rate_limits=None, app_engine_routing_override=None):
        """Prepares and sends a Patch request for modifying a queue."""
        if not any([retry_config, rate_limits, app_engine_routing_override]):
            raise NoFieldsSpecifiedError('Must specify at least one field to update.')
        queue = self.messages.Queue(name=queue_ref.RelativeName())
        if retry_config is not None:
            queue.retryConfig = retry_config
        if rate_limits is not None:
            queue.rateLimits = rate_limits
        if app_engine_routing_override is not None:
            if _IsEmptyConfig(app_engine_routing_override):
                queue.appEngineHttpTarget = self.messages.AppEngineHttpTarget()
            else:
                queue.appEngineHttpTarget = self.messages.AppEngineHttpTarget(appEngineRoutingOverride=app_engine_routing_override)
        update_mask = ','.join(updated_fields)
        request = self.messages.CloudtasksProjectsLocationsQueuesPatchRequest(name=queue_ref.RelativeName(), queue=queue, updateMask=update_mask)
        return self.queues_service.Patch(request)


forward

Prompt:
Implement the Python class `MaskedMSELoss` described below.

Class description:
Implement the MaskedMSELoss class.

Method signatures and docstrings:
- def __init__(self, reduction='mean'): Masked MSE implementation :param reduction: the same, as in nn.MSELoss
- def forward(self, input, target, mask): calculates masked loss :param input: input image as array :param target: reconstructed image as array :param mask: mask of image as array :return: masked loss

Implement the Python class `MaskedMSELoss` described below.

Class description:
Implement the MaskedMSELoss class.

Method signatures and docstrings:
- def __init__(self, reduction='mean'): Masked MSE implementation :param reduction: the same, as in nn.MSELoss
- def forward(self, input, target, mask): calculates masked loss :param input: input image as array :param target: reconstructed image as array :param mask: mask of image as array :return: masked loss

<|skeleton|>
class MaskedMSELoss:

    def __init__(self, reduction='mean'):
        """Masked MSE implementation :param reduction: the same, as in nn.MSELoss"""
        <|body_0|>

    def forward(self, input, target, mask):
        """calculates masked loss :param input: input image as array :param target: reconstructed image as array :param mask: mask of image as array :return: masked loss"""
        <|body_1|>

<|end_skeleton|>

<|body_start_0|>
        super(MaskedMSELoss, self).__init__()
        self.reduction = reduction
        self.criterion = nn.MSELoss(reduction='none')
<|end_body_0|>

<|body_start_1|>
        loss = self.criterion(input * mask, target * mask)
        if self.reduction == 'mean':
            loss = torch.sum(loss) / torch.sum(mask)
        return loss
<|end_body_1|>

Solution:
class MaskedMSELoss:
    def __init__(self, reduction='mean'):
        """Masked MSE implementation :param reduction: the same, as in nn.MSELoss"""
        super(MaskedMSELoss, self).__init__()
        self.reduction = reduction
        self.criterion = nn.MSELoss(reduction='none')

    def forward(self, input, target, mask):
        """calculates masked loss :param input: input image as array :param target: reconstructed image as array :param mask: mask of image as array :return: masked loss"""
        loss = self.criterion(input * mask, target * mask)
        if self.reduction == 'mean':
            loss = torch.sum(loss) / torch.sum(mask)
        return loss


Prompt:
Implement the Python class `ExplainJobEncap` described below.

Class description:
Explain job list encapsulator.

Method signatures and docstrings:
- def query_explain_jobs(self, offset, limit): Query explain job list. Args: offset (int): Page offset. limit (int): Maximum number of items to be returned. Returns: tuple[int, list[Dict]], total number of jobs and job list.
- def query_meta(self, train_id): Query explain job meta-data. Args: train_id (str): Job ID. Returns: dict, the metadata.
- def _dir_2_info(cls, dir_info): Convert ExplainJob object to jsonable info object.
- def _job_2_info(cls, job): Convert ExplainJob object to jsonable info object.
- def _job_2_meta(cls, job): Convert ExplainJob's meta-data to jsonable info object.

Implement the Python class `ExplainJobEncap` described below.

Class description:
Explain job list encapsulator.

Method signatures and docstrings:
- def query_explain_jobs(self, offset, limit): Query explain job list. Args: offset (int): Page offset. limit (int): Maximum number of items to be returned. Returns: tuple[int, list[Dict]], total number of jobs and job list.
- def query_meta(self, train_id): Query explain job meta-data. Args: train_id (str): Job ID. Returns: dict, the metadata.
- def _dir_2_info(cls, dir_info): Convert ExplainJob object to jsonable info object.
- def _job_2_info(cls, job): Convert ExplainJob object to jsonable info object.
- def _job_2_meta(cls, job): Convert ExplainJob's meta-data to jsonable info object.

<|skeleton|>
class ExplainJobEncap:
    """Explain job list encapsulator."""

    def query_explain_jobs(self, offset, limit):
        """Query explain job list. Args: offset (int): Page offset. limit (int): Maximum number of items to be returned. Returns: tuple[int, list[Dict]], total number of jobs and job list."""
        <|body_0|>

    def query_meta(self, train_id):
        """Query explain job meta-data. Args: train_id (str): Job ID. Returns: dict, the metadata."""
        <|body_1|>

    def _dir_2_info(cls, dir_info):
        """Convert ExplainJob object to jsonable info object."""
        <|body_2|>

    def _job_2_info(cls, job):
        """Convert ExplainJob object to jsonable info object."""
        <|body_3|>

    def _job_2_meta(cls, job):
        """Convert ExplainJob's meta-data to jsonable info object."""
        <|body_4|>

<|end_skeleton|>

<|body_start_0|>
        total, dir_infos = self.job_manager.get_job_list(offset=offset, limit=limit)
        job_infos = [self._dir_2_info(dir_info) for dir_info in dir_infos]
        return (total, job_infos)
<|end_body_0|>

<|body_start_1|>
        job = self.job_manager.get_job(train_id)
        if job is None:
            raise TrainJobNotExistError(train_id)
        return self._job_2_meta(job)
<|end_body_1|>

<|body_start_2|>
        info = dict()
        info['train_id'] = dir_info['relative_path']
        info['create_time'] = dir_info['create_time'].strftime(cls.DATETIME_FORMAT)
        info['update_time'] = dir_info['update_time'].strftime(cls.DATETIME_FORMAT)
        info['saliency_map'] = dir_info['saliency_map']
        info['hierarchical_occlusion'] = dir_info['hierarchical_occlusion']
        return info
<|end_body_2|>

<|body_start_3|>
        info = dict()
        info['train_id'] = job.train_id
        info['create_time'] = datetime.fromtimestamp(job.create_time).strftime(cls.DATETIME_FORMAT)
        info['update_time'] = datetime.fromtimestamp(job.update_time).strftime(cls.DATETIME_FORMAT)
        return info
<|end_body_3|>

<|body_start_4|>
        info = cls._job_2_info(job)
        info['sample_count'] = job.sample_count
        info['classes'] = job.all_classes
        saliency_info = dict()
        if job.min_confidence is None:
            saliency_info['min_confidence'] = cls.DEFAULT_MIN_CONFIDENCE
        else:
            saliency_info['min_confidence'] = job.min_confidence
        saliency_info['explainers'] = list(job.explainers)
        saliency_info['metrics'] = list(job.metrics)
        info['saliency'] = saliency_info
        info['uncertainty'] = {'enabled': job.uncertainty_enabled}
        info['status'] = job.status
        return info
<|end_body_4|>

Solution:
class ExplainJobEncap:
    """Explain job list encapsulator."""

    def query_explain_jobs(self, offset, limit):
        """Query explain job list. Args: offset (int): Page offset. limit (int): Maximum number of items to be returned. Returns: tuple[int, list[Dict]], total number of jobs and job list."""
        total, dir_infos = self.job_manager.get_job_list(offset=offset, limit=limit)
        job_infos = [self._dir_2_info(dir_info) for dir_info in dir_infos]
        return (total, job_infos)

    def query_meta(self, train_id):
        """Query explain job meta-data. Args: train_id (str): Job ID. Returns: dict, the metadata."""
        job = self.job_manager.get_job(train_id)
        if job is None:
            raise TrainJobNotExistError(train_id)
        return self._job_2_meta(job)

    def _dir_2_info(cls, dir_info):
        """Convert ExplainJob object to jsonable info object."""
        info = dict()
        info['train_id'] = dir_info['relative_path']
        info['create_time'] = dir_info['create_time'].strftime(cls.DATETIME_FORMAT)
        info['update_time'] = dir_info['update_time'].strftime(cls.DATETIME_FORMAT)
        info['saliency_map'] = dir_info['saliency_map']
        info['hierarchical_occlusion'] = dir_info['hierarchical_occlusion']
        return info

    def _job_2_info(cls, job):
        """Convert ExplainJob object to jsonable info object."""
        info = dict()
        info['train_id'] = job.train_id
        info['create_time'] = datetime.fromtimestamp(job.create_time).strftime(cls.DATETIME_FORMAT)
        info['update_time'] = datetime.fromtimestamp(job.update_time).strftime(cls.DATETIME_FORMAT)
        return info

    def _job_2_meta(cls, job):
        """Convert ExplainJob's meta-data to jsonable info object."""
        info = cls._job_2_info(job)
        info['sample_count'] = job.sample_count
        info['classes'] = job.all_classes
        saliency_info = dict()
        if job.min_confidence is None:
            saliency_info['min_confidence'] = cls.DEFAULT_MIN_CONFIDENCE
        else:
            saliency_info['min_confidence'] = job.min_confidence
        saliency_info['explainers'] = list(job.explainers)
        saliency_info['metrics'] = list(job.metrics)
        info['saliency'] = saliency_info
        info['uncertainty'] = {'enabled': job.uncertainty_enabled}
        info['status'] = job.status
        return info


subarrayBitwiseORs

思路: 位运算剪枝来代替两个set轮流io

Prompt:
Implement the Python class `Solution` described below.

Class description:
[898. 子数组按位或操作](https://leetcode-cn.com/problems/bitwise-ors-of-subarrays/)

Method signatures and docstrings:
- def subarrayBitwiseORs(self, A: List[int]) -> int: 思路: 2个set存
- def subarrayBitwiseORs2(self, A: List[int]) -> int: 思路: 位运算剪枝来代替两个set轮流io

Implement the Python class `Solution` described below.

Class description:
[898. 子数组按位或操作](https://leetcode-cn.com/problems/bitwise-ors-of-subarrays/)

Method signatures and docstrings:
- def subarrayBitwiseORs(self, A: List[int]) -> int: 思路: 2个set存
- def subarrayBitwiseORs2(self, A: List[int]) -> int: 思路: 位运算剪枝来代替两个set轮流io

<|skeleton|>
class Solution:
    """[898. 子数组按位或操作](https://leetcode-cn.com/problems/bitwise-ors-of-subarrays/)"""

    def subarrayBitwiseORs(self, A: List[int]) -> int:
        """思路: 2个set存"""
        <|body_0|>

    def subarrayBitwiseORs2(self, A: List[int]) -> int:
        """思路: 位运算剪枝来代替两个set轮流io"""
        <|body_1|>

<|end_skeleton|>

<|body_start_0|>
        res = set()
        B = set()
        for a in A:
            B = {b | a for b in B} | {a}
            res |= B
        return len(res)
<|end_body_0|>

<|body_start_1|>
        max_a = max(A)
        mask = 1
        while mask <= max_a:
            mask <<= 1
        mask -= 1
        res = set()
        for i, a in enumerate(A):
            res.add(a)
            j = i - 1
            cur = a
            while j >= 0 and cur < mask:
                cur |= A[j]
                res.add(cur)
                j -= 1
        return len(res)
<|end_body_1|>

Solution:
class Solution:
    """[898. 子数组按位或操作](https://leetcode-cn.com/problems/bitwise-ors-of-subarrays/)"""

    def subarrayBitwiseORs(self, A: List[int]) -> int:
        """思路: 2个set存"""
        res = set()
        B = set()
        for a in A:
            B = {b | a for b in B} | {a}
            res |= B
        return len(res)

    def subarrayBitwiseORs2(self, A: List[int]) -> int:
        """思路: 位运算剪枝来代替两个set轮流io"""
        max_a = max(A)
        mask = 1
        while mask <= max_a:
            mask <<= 1
        mask -= 1
        res = set()
        for i, a in enumerate(A):
            res.add(a)
            j = i - 1
            cur = a
            while j >= 0 and cur < mask:
                cur |= A[j]
                res.add(cur)
                j -= 1
        return len(res)


Prompt:
Implement the Python class `EnameMainWindow` described below.

Class description:
This is the program's main window.

Method signatures and docstrings:
- def __init__(self, root=None): Initializes given frame instance.
- def _create_widgets(self): Blah, blah, blah, ...

Implement the Python class `EnameMainWindow` described below.

Class description:
This is the program's main window.

Method signatures and docstrings:
- def __init__(self, root=None): Initializes given frame instance.
- def _create_widgets(self): Blah, blah, blah, ...

<|skeleton|>
class EnameMainWindow:
    """This is the program's main window."""

    def __init__(self, root=None):
        """Initializes given frame instance."""
        <|body_0|>

    def _create_widgets(self):
        """Blah, blah, blah, ..."""
        <|body_1|>

<|end_skeleton|>

<|body_start_0|>
        super().__init__(master=root, borderwidth=3, relief=SUNKEN)
        root.title('Enamer v{major}.{minor}')
        root.title('Hello, world!')
        self.grid(row=0, column=0)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self._create_widgets()
<|end_body_0|>

<|body_start_1|>
        input_group = ttk.LabelFrame(self, text='Input filename:')
        input_fname = ttk.Entry(input_group)
        input_browse = ttk.Button(input_group, text='...')
        naming_group = ttk.LabelFrame(self, text='Naming options:')
        naming_entry = ttk.Entry(naming_group)
        naming_options = ttk.Frame(naming_group)
        naming_options.grid(row=0, column=1)
        naming_author = ttk.Frame(naming_options)
        ttk.Button(naming_author, text='>').grid(row=0, column=0)
        ttk.Entry(naming_author).grid(row=0, column=1)
        naming_author.grid(row=0, column=0)
        naming_title = ttk.Frame(naming_options)
        ttk.Button(naming_title, text='>').grid(row=0, column=0)
        ttk.Entry(naming_title).grid(row=0, column=1)
        naming_title.grid(row=1, column=0)
        output_group = ttk.LabelFrame(self)
        output_fname = ttk.Entry(self)
        output_browse = ttk.Button(self, text='...')
        rename = ttk.Button(self, text='Rename')
        cancel = ttk.Button(self, text='Cancel')
        input_group.grid(row=0, column=0, sticky=(W, E))
        naming_group.grid(row=1, column=0, sticky=(W, E))
        output_group.grid(row=2, column=0, sticky=(W, E))
        input_fname.grid(row=0, column=1)
        input_browse.grid(row=0, column=2)
        naming_entry.grid(row=0, column=0)
<|end_body_1|>

Solution:
class EnameMainWindow:
    """This is the program's main window."""

    def __init__(self, root=None):
        """Initializes given frame instance."""
        super().__init__(master=root, borderwidth=3, relief=SUNKEN)
        root.title('Enamer v{major}.{minor}')
        root.title('Hello, world!')
        self.grid(row=0, column=0)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self._create_widgets()

    def _create_widgets(self):
        """Blah, blah, blah, ..."""
        input_group = ttk.LabelFrame(self, text='Input filename:')
        input_fname = ttk.Entry(input_group)
        input_browse = ttk.Button(input_group, text='...')
        naming_group = ttk.LabelFrame(self, text='Naming options:')
        naming_entry = ttk.Entry(naming_group)
        naming_options = ttk.Frame(naming_group)
        naming_options.grid(row=0, column=1)
        naming_author = ttk.Frame(naming_options)
        ttk.Button(naming_author, text='>').grid(row=0, column=0)
        ttk.Entry(naming_author).grid(row=0, column=1)
        naming_author.grid(row=0, column=0)
        naming_title = ttk.Frame(naming_options)
        ttk.Button(naming_title, text='>').grid(row=0, column=0)
        ttk.Entry(naming_title).grid(row=0, column=1)
        naming_title.grid(row=1, column=0)
        output_group = ttk.LabelFrame(self)
        output_fname = ttk.Entry(self)
        output_browse = ttk.Button(self, text='...')
        rename = ttk.Button(self, text='Rename')
        cancel = ttk.Button(self, text='Cancel')
        input_group.grid(row=0, column=0, sticky=(W, E))
        naming_group.grid(row=1, column=0, sticky=(W, E))
        output_group.grid(row=2, column=0, sticky=(W, E))
        input_fname.grid(row=0, column=1)
        input_browse.grid(row=0, column=2)
        naming_entry.grid(row=0, column=0)


Prompt:
Implement the Python class `Agent_DQN` described below.

Class description:
Implement the Agent_DQN class.

Method signatures and docstrings:
- def __init__(self, env, args): Initialize every things you need here. For example: building your model
- def init_game_setting(self): Testing function will call this function at the begining of new game Put anything you want to initialize if necessary
- def train(self): Implement your training algorithm here
- def make_action(self, observation, test=True): Return predicted action of your agent Input: observation: np.array stack 4 last preprocessed frames, shape: (84, 84, 4) Return: action: int the predicted action from trained model

Implement the Python class `Agent_DQN` described below.

Class description:
Implement the Agent_DQN class.

Method signatures and docstrings:
- def __init__(self, env, args): Initialize every things you need here. For example: building your model
- def init_game_setting(self): Testing function will call this function at the begining of new game Put anything you want to initialize if necessary
- def train(self): Implement your training algorithm here
- def make_action(self, observation, test=True): Return predicted action of your agent Input: observation: np.array stack 4 last preprocessed frames, shape: (84, 84, 4) Return: action: int the predicted action from trained model

<|skeleton|>
class Agent_DQN:

    def __init__(self, env, args):
        """Initialize every things you need here. For example: building your model"""
        <|body_0|>

    def init_game_setting(self):
        """Testing function will call this function at the begining of new game Put anything you want to initialize if necessary"""
        <|body_1|>

    def train(self):
        """Implement your training algorithm here"""
        <|body_2|>

    def make_action(self, observation, test=True):
        """Return predicted action of your agent Input: observation: np.array stack 4 last preprocessed frames, shape: (84, 84, 4) Return: action: int the predicted action from trained model"""
        <|body_3|>

<|end_skeleton|>

<|body_start_0|>
        super(Agent_DQN, self).__init__(env)
        self.hparams = tf.contrib.training.HParams(n_actions=self.env.action_space.n, total_episode=100000, init_step=10000, exploration_step=1000000, epsilon_init=1.0, epsilon_min=0.025, n_obs=list(self.env.env.observation_space.shape), gamma=0.99, learning_rate=0.0001, use_dueling=False, use_ddqn=False, checkpoint_path='./checkpoints/agent_dqn', history_rewards_file='./history_rewards.npy', save_interval=100000, target_update_interval=1000, train_interval=4, replay_size=10000)
        self.epsilon = self.hparams.epsilon_init
        self.epsilon_decay = (self.hparams.epsilon_init - self.hparams.epsilon_min) / self.hparams.exploration_step
        self.dqn = DQN(self.hparams)
        self.session = tf.Session()
        if args.test_dqn:
            tf.logging.info('loading trained model')
        self.dqn.load(self.session, self.hparams.checkpoint_path)
<|end_body_0|>

<|body_start_1|>
        self.i_step = -1
        self.history_rewards = []
        self.replay_buf = deque(maxlen=self.hparams.replay_size)
<|end_body_1|>

<|body_start_2|>
        tf.logging.info('...... start training ......')
        self.init_game_setting()
        running_reward = None
        for i_episode in range(self.hparams.total_episode):
            state = self.env.reset()
            done = False
            cumulate_reward = 0
            episode_steps = 0
            while not done:
                self.i_step += 1
                episode_steps += 1
                action = self.make_action(state, test=False)
                next_state, reward, done, info = self.env.step(action)
                self.replay_buf.append([state, action, reward, next_state, int(done)])
                state = next_state
                cumulate_reward += reward
                if self.i_step >= self.hparams.init_step and self.i_step % self.hparams.train_interval == 0:
                    self.dqn.train(self.session, self.replay_buf)
                if self.i_step >= self.hparams.init_step and self.i_step % self.hparams.target_update_interval == 0:
                    self.dqn.cp2targetnet(self.session)
                if self.i_step % self.hparams.save_interval == 0:
                    self.dqn.save(self.session, self.hparams.checkpoint_path)
            running_reward = cumulate_reward if running_reward is None else running_reward * 0.99 + cumulate_reward * 0.01
            self.history_rewards.append(running_reward)
            tf.logging.info('I_EPISODE: {:06d} | EPISODE_STEPS: {:03d} | I_STEP: {:09d} | EPSILON: {:.5f} | CUR_REWARD: {:2.3f} | AVG_REWARD: {:2.3f}'.format(i_episode, episode_steps, self.i_step, self.epsilon, cumulate_reward, running_reward))
        np.save(self.hparams.history_rewards_file, self.history_rewards)
        self.dqn.save(self.session, self.hparams.checkpoint_path)
<|end_body_2|>

<|body_start_3|>
        if test:
            if random.random() < self.hparams.epsilon_min:
                return random.randrange(self.hparams.n_actions)
            output = self.dqn.predict(self.session, [observation])
            return np.argmax(output[0])
        else:
            if self.epsilon > self.hparams.epsilon_min and self.i_step > self.hparams.init_step:
                self.epsilon = self.epsilon - self.epsilon_decay
            if random.random() < self.epsilon:
                return random.randrange(self.hparams.n_actions)
            output = self.dqn.predict(self.session, [observation])
            return np.argmax(output[0])
<|end_body_3|>

Solution:
class Agent_DQN:
    def __init__(self, env, args):
        """Initialize every things you need here. For example: building your model"""
        super(Agent_DQN, self).__init__(env)
        self.hparams = tf.contrib.training.HParams(n_actions=self.env.action_space.n, total_episode=100000, init_step=10000, exploration_step=1000000, epsilon_init=1.0, epsilon_min=0.025, n_obs=list(self.env.env.observation_space.shape), gamma=0.99, learning_rate=0.0001, use_dueling=False, use_ddqn=False, checkpoint_path='./checkpoints/agent_dqn', history_rewards_file='./history_rewards.npy', save_interval=100000, target_update_interval=1000, train_interval=4, replay_size=10000)
        self.epsilon = self.hparams.epsilon_init
        self.epsilon_decay = (self.hparams.epsilon_init - self.hparams.epsilon_min) / self.hparams.exploration_step
        self.dqn = DQN(self.hparams)
        self.session = tf.Session()
        if args.test_dqn:
            tf.logging.info('loading trained model')
        self.dqn.load(self.session, self.hparams.checkpoint_path)

    def init_game_setting(self):
        """Testing function will call this function at the begining of new game Put anything you want to initialize if necessary"""
        self.i_step = -1
        self.history_rewards = []
        self.replay_buf = deque(maxlen=self.hparams.replay_size)

    def train(self):
        """Implement your training algorithm here"""
        tf.logging.info('...... start training ......')
        self.init_game_setting()
        running_reward = None
        for i_episode in range(self.hparams.total_episode):
            state = self.env.reset()
            done = False
            cumulate_reward = 0
            episode_steps = 0
            while not done:
                self.i_step += 1
                episode_steps += 1
                action = self.make_action(state, test=False)
                next_state, reward, done, info = self.env.step(action)
                self.replay_buf.append([state, action, reward, next_state, int(done)])
                state = next_state
                cumulate_reward += reward
                if self.i_step >= self.hparams.init_step and self.i_step % self.hparams.train_interval == 0:
                    self.dqn.train(self.session, self.replay_buf)
                if self.i_step >= self.hparams.init_step and self.i_step % self.hparams.target_update_interval == 0:
                    self.dqn.cp2targetnet(self.session)
                if self.i_step % self.hparams.save_interval == 0:
                    self.dqn.save(self.session, self.hparams.checkpoint_path)
            running_reward = cumulate_reward if running_reward is None else running_reward * 0.99 + cumulate_reward * 0.01
            self.history_rewards.append(running_reward)
            tf.logging.info('I_EPISODE: {:06d} | EPISODE_STEPS: {:03d} | I_STEP: {:09d} | EPSILON: {:.5f} | CUR_REWARD: {:2.3f} | AVG_REWARD: {:2.3f}'.format(i_episode, episode_steps, self.i_step, self.epsilon, cumulate_reward, running_reward))
        np.save(self.hparams.history_rewards_file, self.history_rewards)
        self.dqn.save(self.session, self.hparams.checkpoint_path)

    def make_action(self, observation, test=True):
        """Return predicted action of your agent Input: observation: np.array stack 4 last preprocessed frames, shape: (84, 84, 4) Return: action: int the predicted action from trained model"""
        if test:
            if random.random() < self.hparams.epsilon_min:
                return random.randrange(self.hparams.n_actions)
            output = self.dqn.predict(self.session, [observation])
            return np.argmax(output[0])
        else:
            if self.epsilon > self.hparams.epsilon_min and self.i_step > self.hparams.init_step:
                self.epsilon = self.epsilon - self.epsilon_decay
            if random.random() < self.epsilon:
                return random.randrange(self.hparams.n_actions)
            output = self.dqn.predict(self.session, [observation])
            return np.argmax(output[0])


serialize

Prompt:
Implement the Python class `DomainDnsSrvRecord` described below.

Class description:
Implement the DomainDnsSrvRecord class.

Method signatures and docstrings:
- def create_from_discriminator_value(parse_node: Optional[ParseNode]=None) -> DomainDnsSrvRecord: Creates a new instance of the appropriate class based on discriminator value Args: parse_node: The parse node to use to read the discriminator value and create the object Returns: DomainDnsSrvRecord
- def get_field_deserializers(self) -> Dict[str, Callable[[ParseNode], None]]: The deserialization information for the current model Returns: Dict[str, Callable[[ParseNode], None]]
- def serialize(self, writer: SerializationWriter) -> None: Serializes information the current object Args: writer: Serialization writer to use to serialize this model

Implement the Python class `DomainDnsSrvRecord` described below.

Class description:
Implement the DomainDnsSrvRecord class.

Method signatures and docstrings:
- def create_from_discriminator_value(parse_node: Optional[ParseNode]=None) -> DomainDnsSrvRecord: Creates a new instance of the appropriate class based on discriminator value Args: parse_node: The parse node to use to read the discriminator value and create the object Returns: DomainDnsSrvRecord
- def get_field_deserializers(self) -> Dict[str, Callable[[ParseNode], None]]: The deserialization information for the current model Returns: Dict[str, Callable[[ParseNode], None]]
- def serialize(self, writer: SerializationWriter) -> None: Serializes information the current object Args: writer: Serialization writer to use to serialize this model

<|skeleton|>
class DomainDnsSrvRecord:

    def create_from_discriminator_value(parse_node: Optional[ParseNode]=None) -> DomainDnsSrvRecord:
        """Creates a new instance of the appropriate class based on discriminator value Args: parse_node: The parse node to use to read the discriminator value and create the object Returns: DomainDnsSrvRecord"""
        <|body_0|>

    def get_field_deserializers(self) -> Dict[str, Callable[[ParseNode], None]]:
        """The deserialization information for the current model Returns: Dict[str, Callable[[ParseNode], None]]"""
        <|body_1|>

    def serialize(self, writer: SerializationWriter) -> None:
        """Serializes information the current object Args: writer: Serialization writer to use to serialize this model"""
        <|body_2|>

<|end_skeleton|>

<|body_start_0|>
        if not parse_node:
            raise TypeError('parse_node cannot be null.')
        return DomainDnsSrvRecord()
<|end_body_0|>

<|body_start_1|>
        from .domain_dns_record import DomainDnsRecord
        from .domain_dns_record import DomainDnsRecord
        fields: Dict[str, Callable[[Any], None]] = {'nameTarget': lambda n: setattr(self, 'name_target', n.get_str_value()), 'port': lambda n: setattr(self, 'port', n.get_int_value()), 'priority': lambda n: setattr(self, 'priority', n.get_int_value()), 'protocol': lambda n: setattr(self, 'protocol', n.get_str_value()), 'service': lambda n: setattr(self, 'service', n.get_str_value()), 'weight': lambda n: setattr(self, 'weight', n.get_int_value())}
        super_fields = super().get_field_deserializers()
        fields.update(super_fields)
        return fields
<|end_body_1|>

<|body_start_2|>
        if not writer:
            raise TypeError('writer cannot be null.')
        super().serialize(writer)
        writer.write_str_value('nameTarget', self.name_target)
        writer.write_int_value('port', self.port)
        writer.write_int_value('priority', self.priority)
        writer.write_str_value('protocol', self.protocol)
        writer.write_str_value('service', self.service)
        writer.write_int_value('weight', self.weight)
<|end_body_2|>

Solution:
class DomainDnsSrvRecord:
    def create_from_discriminator_value(parse_node: Optional[ParseNode]=None) -> DomainDnsSrvRecord:
        """Creates a new instance of the appropriate class based on discriminator value Args: parse_node: The parse node to use to read the discriminator value and create the object Returns: DomainDnsSrvRecord"""
        if not parse_node:
            raise TypeError('parse_node cannot be null.')
        return DomainDnsSrvRecord()

    def get_field_deserializers(self) -> Dict[str, Callable[[ParseNode], None]]:
        """The deserialization information for the current model Returns: Dict[str, Callable[[ParseNode], None]]"""
        from .domain_dns_record import DomainDnsRecord
        from .domain_dns_record import DomainDnsRecord
        fields: Dict[str, Callable[[Any], None]] = {'nameTarget': lambda n: setattr(self, 'name_target', n.get_str_value()), 'port': lambda n: setattr(self, 'port', n.get_int_value()), 'priority': lambda n: setattr(self, 'priority', n.get_int_value()), 'protocol': lambda n: setattr(self, 'protocol', n.get_str_value()), 'service': lambda n: setattr(self, 'service', n.get_str_value()), 'weight': lambda n: setattr(self, 'weight', n.get_int_value())}
        super_fields = super().get_field_deserializers()
        fields.update(super_fields)
        return fields

    def serialize(self, writer: SerializationWriter) -> None:
        """Serializes information the current object Args: writer: Serialization writer to use to serialize this model"""
        if not writer:
            raise TypeError('writer cannot be null.')
        super().serialize(writer)
        writer.write_str_value('nameTarget', self.name_target)
        writer.write_int_value('port', self.port)
        writer.write_int_value('priority', self.priority)
        writer.write_str_value('protocol', self.protocol)
        writer.write_str_value('service', self.service)
        writer.write_int_value('weight', self.weight)


permuteUnique

Prompt:
Implement the Python class `Solution` described below.

Class description:
Implement the Solution class.

Method signatures and docstrings:
- def permuteUnique(self, nums): :type nums: List[int] :rtype: List[List[int]]
- def permuteUnique(self, nums): :type nums: List[int] :rtype: List[List[int]]

Implement the Python class `Solution` described below.

Class description:
Implement the Solution class.

Method signatures and docstrings:
- def permuteUnique(self, nums): :type nums: List[int] :rtype: List[List[int]]
- def permuteUnique(self, nums): :type nums: List[int] :rtype: List[List[int]]

<|skeleton|>
class Solution:

    def permuteUnique(self, nums):
        """:type nums: List[int] :rtype: List[List[int]]"""
        <|body_0|>

    def permuteUnique(self, nums):
        """:type nums: List[int] :rtype: List[List[int]]"""
        <|body_1|>

<|end_skeleton|>

<|body_start_0|>
        ans = [[]]
        for n in nums:
            new_ans = []
            for l in ans:
                for i in range(len(l) + 1):
                    new_ans.append(l[:i] + [n] + l[i:])
                    print(i, l, new_ans)
                    if i < len(l) and l[i] == n:
                        print('skip')
                        break
            ans = new_ans
        return ans
<|end_body_0|>

<|body_start_1|>
        def dfs(partial, memo):
            print(partial, memo)
            if len(partial) == len(nums):
                res.append(partial)
                return
            cache = set()
            for i in range(len(nums)):
                if i in memo or nums[i] in cache:
                    continue
                memo.add(i)
                cache.add(nums[i])
                dfs(partial + [nums[i]], memo)
                memo.remove(i)
        res = []
        dfs([], set())
        return res
<|end_body_1|>

Solution:
class Solution:
    def permuteUnique(self, nums):
        """:type nums: List[int] :rtype: List[List[int]]"""
        ans = [[]]
        for n in nums:
            new_ans = []
            for l in ans:
                for i in range(len(l) + 1):
                    new_ans.append(l[:i] + [n] + l[i:])
                    print(i, l, new_ans)
                    if i < len(l) and l[i] == n:
                        print('skip')
                        break
            ans = new_ans
        return ans

    def permuteUnique(self, nums):
        """:type nums: List[int] :rtype: List[List[int]]"""
        def dfs(partial, memo):
            print(partial, memo)
            if len(partial) == len(nums):
                res.append(partial)
                return
            cache = set()
            for i in range(len(nums)):
                if i in memo or nums[i] in cache:
                    continue
                memo.add(i)
                cache.add(nums[i])
                dfs(partial + [nums[i]], memo)
                memo.remove(i)
        res = []
        dfs([], set())
        return res


Prompt:
Implement the Python class `NetworkPoolConfig` described below.

Class description:
Implementation of the 'NetworkPoolConfig' model. While caonfiguring the isilon protection source, this is the selected network pool config for the isilon access zone. Attributes: pool_name (string): Specifies the name of the Network pool. subnet (string): Specifies the name of the subnet the network pool belongs to. use_smart_connect (bool): Specifies whether to use SmartConnect if available. If true, DNS name for the SmartConnect zone will be used to balance the IPs. Otherwise, pool IPs will be balanced manually.

Method signatures and docstrings:
- def __init__(self, pool_name=None, subnet=None, use_smart_connect=None): Constructor for the NetworkPoolConfig class
- def from_dictionary(cls, dictionary): Creates an instance of this model from a dictionary Args: dictionary (dictionary): A dictionary representation of the object as obtained from the deserialization of the server's response. The keys MUST match property names in the API description. Returns: object: An instance of this structure class.

Implement the Python class `NetworkPoolConfig` described below.

Class description:
Implementation of the 'NetworkPoolConfig' model. While caonfiguring the isilon protection source, this is the selected network pool config for the isilon access zone. Attributes: pool_name (string): Specifies the name of the Network pool. subnet (string): Specifies the name of the subnet the network pool belongs to. use_smart_connect (bool): Specifies whether to use SmartConnect if available. If true, DNS name for the SmartConnect zone will be used to balance the IPs. Otherwise, pool IPs will be balanced manually.

Method signatures and docstrings:
- def __init__(self, pool_name=None, subnet=None, use_smart_connect=None): Constructor for the NetworkPoolConfig class
- def from_dictionary(cls, dictionary): Creates an instance of this model from a dictionary Args: dictionary (dictionary): A dictionary representation of the object as obtained from the deserialization of the server's response. The keys MUST match property names in the API description. Returns: object: An instance of this structure class.

<|skeleton|>
class NetworkPoolConfig:
    """Implementation of the 'NetworkPoolConfig' model. While caonfiguring the isilon protection source, this is the selected network pool config for the isilon access zone. Attributes: pool_name (string): Specifies the name of the Network pool. subnet (string): Specifies the name of the subnet the network pool belongs to. use_smart_connect (bool): Specifies whether to use SmartConnect if available. If true, DNS name for the SmartConnect zone will be used to balance the IPs. Otherwise, pool IPs will be balanced manually."""

    def __init__(self, pool_name=None, subnet=None, use_smart_connect=None):
        """Constructor for the NetworkPoolConfig class"""
        <|body_0|>

    def from_dictionary(cls, dictionary):
        """Creates an instance of this model from a dictionary Args: dictionary (dictionary): A dictionary representation of the object as obtained from the deserialization of the server's response. The keys MUST match property names in the API description. Returns: object: An instance of this structure class."""
        <|body_1|>

<|end_skeleton|>

<|body_start_0|>
        self.pool_name = pool_name
        self.subnet = subnet
        self.use_smart_connect = use_smart_connect
<|end_body_0|>

<|body_start_1|>
        if dictionary is None:
            return None
        pool_name = dictionary.get('poolName')
        subnet = dictionary.get('subnet')
        use_smart_connect = dictionary.get('useSmartConnect')
        return cls(pool_name, subnet, use_smart_connect)
<|end_body_1|>

Solution:
class NetworkPoolConfig:
    """Implementation of the 'NetworkPoolConfig' model. While caonfiguring the isilon protection source, this is the selected network pool config for the isilon access zone. Attributes: pool_name (string): Specifies the name of the Network pool. subnet (string): Specifies the name of the subnet the network pool belongs to. use_smart_connect (bool): Specifies whether to use SmartConnect if available. If true, DNS name for the SmartConnect zone will be used to balance the IPs. Otherwise, pool IPs will be balanced manually."""

    def __init__(self, pool_name=None, subnet=None, use_smart_connect=None):
        """Constructor for the NetworkPoolConfig class"""
        self.pool_name = pool_name
        self.subnet = subnet
        self.use_smart_connect = use_smart_connect

    def from_dictionary(cls, dictionary):
        """Creates an instance of this model from a dictionary Args: dictionary (dictionary): A dictionary representation of the object as obtained from the deserialization of the server's response. The keys MUST match property names in the API description. Returns: object: An instance of this structure class."""
        if dictionary is None:
            return None
        pool_name = dictionary.get('poolName')
        subnet = dictionary.get('subnet')
        use_smart_connect = dictionary.get('useSmartConnect')
        return cls(pool_name, subnet, use_smart_connect)


Prompt:
Implement the Python class `Solution` described below.

Class description:
Implement the Solution class.

Method signatures and docstrings:
- def maxProfit1(self, prices: List[int]) -> int: 思路：记忆化递归 @param prices: @return:
- def maxProfit2(self, prices: List[int]) -> int: 思路：动态规划法 @param prices: @return:
- def maxProfit3(self, prices: List[int]) -> int: 思路：两个状态 @param prices: @return:

Implement the Python class `Solution` described below.

Class description:
Implement the Solution class.

Method signatures and docstrings:
- def maxProfit1(self, prices: List[int]) -> int: 思路：记忆化递归 @param prices: @return:
- def maxProfit2(self, prices: List[int]) -> int: 思路：动态规划法 @param prices: @return:
- def maxProfit3(self, prices: List[int]) -> int: 思路：两个状态 @param prices: @return:

<|skeleton|>
class Solution:

    def maxProfit1(self, prices: List[int]) -> int:
        """思路：记忆化递归 @param prices: @return:"""
        <|body_0|>

    def maxProfit2(self, prices: List[int]) -> int:
        """思路：动态规划法 @param prices: @return:"""
        <|body_1|>

    def maxProfit3(self, prices: List[int]) -> int:
        """思路：两个状态 @param prices: @return:"""
        <|body_2|>

<|end_skeleton|>

<|body_start_0|>
        if not prices:
            return 0
        n = len(prices)

        @lru_cache(None)
        def dfs(index, status, k):
            if index == n or k == 0:
                return 0
            a, b, c = (0, 0, 0)
            a = dfs(index + 1, status, k)
            if status:
                b = dfs(index + 1, 0, k - 1) + prices[index]
            else:
                c = dfs(index + 1, 1, k) - prices[index]
            return max(a, b, c)
        return dfs(0, 0, 2)
<|end_body_0|>

<|body_start_1|>
        if not prices:
            return 0
        n = len(prices)
        dp = [[[0] * 2 for _ in range(3)] for _ in range(n)]
        for i in range(3):
            dp[0][i][0] = -prices[0]
        for i in range(1, n):
            for j in range(1, 3):
                dp[i][j - 1][0] = max(dp[i - 1][j - 1][0], dp[i - 1][j - 1][1] - prices[i])
                dp[i][j][1] = max(dp[i - 1][j][1], dp[i - 1][j - 1][0] + prices[i])
        return dp[-1][2][1]
<|end_body_1|>

<|body_start_2|>
        n = len(prices)
        buy1, buy2 = (-prices[0], -prices[0])
        sell1, sell2 = (0, 0)
        for i in range(1, n):
            buy1 = max(buy1, -prices[i])
            sell1 = max(sell1, buy1 + prices[i])
            buy2 = max(buy2, sell1 - prices[i])
            sell2 = max(sell2, buy2 + prices[i])
        return sell2
<|end_body_2|>

Solution:
class Solution:
    def maxProfit1(self, prices: List[int]) -> int:
        """思路：记忆化递归 @param prices: @return:"""
        if not prices:
            return 0
        n = len(prices)

        @lru_cache(None)
        def dfs(index, status, k):
            if index == n or k == 0:
                return 0
            a, b, c = (0, 0, 0)
            a = dfs(index + 1, status, k)
            if status:
                b = dfs(index + 1, 0, k - 1) + prices[index]
            else:
                c = dfs(index + 1, 1, k) - prices[index]
            return max(a, b, c)
        return dfs(0, 0, 2)

    def maxProfit2(self, prices: List[int]) -> int:
        """思路：动态规划法 @param prices: @return:"""
        if not prices:
            return 0
        n = len(prices)
        dp = [[[0] * 2 for _ in range(3)] for _ in range(n)]
        for i in range(3):
            dp[0][i][0] = -prices[0]
        for i in range(1, n):
            for j in range(1, 3):
                dp[i][j - 1][0] = max(dp[i - 1][j - 1][0], dp[i - 1][j - 1][1] - prices[i])
                dp[i][j][1] = max(dp[i - 1][j][1], dp[i - 1][j - 1][0] + prices[i])
        return dp[-1][2][1]

    def maxProfit3(self, prices: List[int]) -> int:
        """思路：两个状态 @param prices: @return:"""
        n = len(prices)
        buy1, buy2 = (-prices[0], -prices[0])
        sell1, sell2 = (0, 0)
        for i in range(1, n):
            buy1 = max(buy1, -prices[i])
            sell1 = max(sell1, buy1 + prices[i])
            buy2 = max(buy2, sell1 - prices[i])
            sell2 = max(sell2, buy2 + prices[i])
        return sell2


Prompt:
Implement the Python class `LAMBOptimizer_v1` described below.

Class description:
LAMBOptimizer optimizer. https://github.com/ymcui/LAMB_Optimizer_TF # IMPORTANT NOTE - This is NOT an official implementation. - LAMB optimizer is changed from arXiv v1 ~ v3. - We implement v3 version (which is the latest version on June, 2019.). - Our implementation is based on `AdamWeightDecayOptimizer` in BERT (provided by Google). # References - Large Batch Optimization for Deep Learning: Training BERT in 76 minutes. https://arxiv.org/abs/1904.00962v3 - BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding. https://arxiv.org/abs/1810.04805 # Parameters - There is nothing special, just the same as `AdamWeightDecayOptimizer`.

Method signatures and docstrings:
- def __init__(self, learning_rate, weight_decay_rate=0.01, beta_1=0.9, beta_2=0.999, epsilon=1e-06, exclude_from_weight_decay=None, include_in_weight_decay=['r_s_bias', 'r_r_bias', 'r_w_bias'], name='LAMBOptimizer'): Constructs a LAMBOptimizer.
- def apply_gradients(self, grads_and_vars, global_step=None, name=None, learning_rate=None): See base class.
- def _do_use_weight_decay(self, param_name): Whether to use L2 weight decay for `param_name`.
- def _get_variable_name(self, param_name): Get the variable name from the tensor name.

Implement the Python class `LAMBOptimizer_v1` described below.

Class description:
LAMBOptimizer optimizer. https://github.com/ymcui/LAMB_Optimizer_TF # IMPORTANT NOTE - This is NOT an official implementation. - LAMB optimizer is changed from arXiv v1 ~ v3. - We implement v3 version (which is the latest version on June, 2019.). - Our implementation is based on `AdamWeightDecayOptimizer` in BERT (provided by Google). # References - Large Batch Optimization for Deep Learning: Training BERT in 76 minutes. https://arxiv.org/abs/1904.00962v3 - BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding. https://arxiv.org/abs/1810.04805 # Parameters - There is nothing special, just the same as `AdamWeightDecayOptimizer`.

Method signatures and docstrings:
- def __init__(self, learning_rate, weight_decay_rate=0.01, beta_1=0.9, beta_2=0.999, epsilon=1e-06, exclude_from_weight_decay=None, include_in_weight_decay=['r_s_bias', 'r_r_bias', 'r_w_bias'], name='LAMBOptimizer'): Constructs a LAMBOptimizer.
- def apply_gradients(self, grads_and_vars, global_step=None, name=None, learning_rate=None): See base class.
- def _do_use_weight_decay(self, param_name): Whether to use L2 weight decay for `param_name`.
- def _get_variable_name(self, param_name): Get the variable name from the tensor name.

<|skeleton|>
class LAMBOptimizer_v1:
    """LAMBOptimizer optimizer. https://github.com/ymcui/LAMB_Optimizer_TF # IMPORTANT NOTE - This is NOT an official implementation. - LAMB optimizer is changed from arXiv v1 ~ v3. - We implement v3 version (which is the latest version on June, 2019.). - Our implementation is based on `AdamWeightDecayOptimizer` in BERT (provided by Google). # References - Large Batch Optimization for Deep Learning: Training BERT in 76 minutes. https://arxiv.org/abs/1904.00962v3 - BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding. https://arxiv.org/abs/1810.04805 # Parameters - There is nothing special, just the same as `AdamWeightDecayOptimizer`."""

    def __init__(self, learning_rate, weight_decay_rate=0.01, beta_1=0.9, beta_2=0.999, epsilon=1e-06, exclude_from_weight_decay=None, include_in_weight_decay=['r_s_bias', 'r_r_bias', 'r_w_bias'], name='LAMBOptimizer'):
        """Constructs a LAMBOptimizer."""
        <|body_0|>

    def apply_gradients(self, grads_and_vars, global_step=None, name=None, learning_rate=None):
        """See base class."""
        <|body_1|>

    def _do_use_weight_decay(self, param_name):
        """Whether to use L2 weight decay for `param_name`."""
        <|body_2|>

    def _get_variable_name(self, param_name):
        """Get the variable name from the tensor name."""
        <|body_3|>

<|end_skeleton|>

<|body_start_0|>
        super(LAMBOptimizer_v1, self).__init__(False, name)
        self.learning_rate = learning_rate
        self.weight_decay_rate = weight_decay_rate
        self.beta_1 = beta_1
        self.beta_2 = beta_2
        self.epsilon = epsilon
        self.exclude_from_weight_decay = exclude_from_weight_decay
        self.include_in_weight_decay = include_in_weight_decay
<|end_body_0|>

<|body_start_1|>
        assignments = []
        if learning_rate is None:
            learning_rate = self.learning_rate
            tf.logging.info('***** use default learning rate ***** ', learning_rate)
        else:
            tf.logging.info('***** use provided learning rate ***** ', learning_rate)
        for grad, param in grads_and_vars:
            if grad is None or param is None:
                continue
            param_name = self._get_variable_name(param.name)
            tf.logging.info('***** apply gradients parameter name ***** %s', param_name)
            tf.logging.info('***** param: %s learning rate: %s ***** ', param_name, str(learning_rate))
            m = tf.get_variable(name=param_name + '/lamb_m', shape=param.shape.as_list(), dtype=tf.float32, trainable=False, initializer=tf.zeros_initializer())
            v = tf.get_variable(name=param_name + '/lamb_v', shape=param.shape.as_list(), dtype=tf.float32, trainable=False, initializer=tf.zeros_initializer())
            next_m = tf.multiply(self.beta_1, m) + tf.multiply(1.0 - self.beta_1, grad)
            next_v = tf.multiply(self.beta_2, v) + tf.multiply(1.0 - self.beta_2, tf.square(grad))
            update = next_m / (tf.sqrt(next_v) + self.epsilon)
            if self._do_use_weight_decay(param_name):
                update += self.weight_decay_rate * param
            r1 = tf.sqrt(tf.reduce_sum(tf.square(param)))
            r2 = tf.sqrt(tf.reduce_sum(tf.square(update)))
            r = tf.where(tf.greater(r1, 0.0), tf.where(tf.greater(r2, 0.0), r1 / r2, 1.0), 1.0)
            eta = learning_rate * r
            update_with_lr = eta * update
            next_param = param - update_with_lr
            assignments.extend([param.assign(next_param), m.assign(next_m), v.assign(next_v)])
        return tf.group(*assignments, name=name)
<|end_body_1|>

<|body_start_2|>
        if not self.weight_decay_rate:
            return False
        if self.include_in_weight_decay:
            for r in self.include_in_weight_decay:
                if re.search(r, param_name) is not None:
                    tf.logging.info('Include %s in weight decay', param_name)
                    return True
        if self.exclude_from_weight_decay:
            for r in self.exclude_from_weight_decay:
                if re.search(r, param_name) is not None:
                    tf.logging.info('Adam WD excludes %s', param_name)
                    return False
        return True
<|end_body_2|>

<|body_start_3|>
        m = re.match('^(.*):\\d+$', param_name)
        if m is not None:
            param_name = m.group(1)
        return param_name
<|end_body_3|>

Solution:
class LAMBOptimizer_v1:
    """LAMBOptimizer optimizer. https://github.com/ymcui/LAMB_Optimizer_TF # IMPORTANT NOTE - This is NOT an official implementation. - LAMB optimizer is changed from arXiv v1 ~ v3. - We implement v3 version (which is the latest version on June, 2019.). - Our implementation is based on `AdamWeightDecayOptimizer` in BERT (provided by Google). # References - Large Batch Optimization for Deep Learning: Training BERT in 76 minutes. https://arxiv.org/abs/1904.00962v3 - BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding. https://arxiv.org/abs/1810.04805 # Parameters - There is nothing special, just the same as `AdamWeightDecayOptimizer`."""

    def __init__(self, learning_rate, weight_decay_rate=0.01, beta_1=0.9, beta_2=0.999, epsilon=1e-06, exclude_from_weight_decay=None, include_in_weight_decay=['r_s_bias', 'r_r_bias', 'r_w_bias'], name='LAMBOptimizer'):
        """Constructs a LAMBOptimizer."""
        super(LAMBOptimizer_v1, self).__init__(False, name)
        self.learning_rate = learning_rate
        self.weight_decay_rate = weight_decay_rate
        self.beta_1 = beta_1
        self.beta_2 = beta_2
        self.epsilon = epsilon
        self.exclude_from_weight_decay = exclude_from_weight_decay
        self.include_in_weight_decay = include_in_weight_decay

    def apply_gradients(self, grads_and_vars, global_step=None, name=None, learning_rate=None):
        """See base class."""
        assignments = []
        if learning_rate is None:
            learning_rate = self.learning_rate
            tf.logging.info('***** use default learning rate ***** ', learning_rate)
        else:
            tf.logging.info('***** use provided learning rate ***** ', learning_rate)
        for grad, param in grads_and_vars:
            if grad is None or param is None:
                continue
            param_name = self._get_variable_name(param.name)
            tf.logging.info('***** apply gradients parameter name ***** %s', param_name)
            tf.logging.info('***** param: %s learning rate: %s ***** ', param_name, str(learning_rate))
            m = tf.get_variable(name=param_name + '/lamb_m', shape=param.shape.as_list(), dtype=tf.float32, trainable=False, initializer=tf.zeros_initializer())
            v = tf.get_variable(name=param_name + '/lamb_v', shape=param.shape.as_list(), dtype=tf.float32, trainable=False, initializer=tf.zeros_initializer())
            next_m = tf.multiply(self.beta_1, m) + tf.multiply(1.0 - self.beta_1, grad)
            next_v = tf.multiply(self.beta_2, v) + tf.multiply(1.0 - self.beta_2, tf.square(grad))
            update = next_m / (tf.sqrt(next_v) + self.epsilon)
            if self._do_use_weight_decay(param_name):
                update += self.weight_decay_rate * param
            r1 = tf.sqrt(tf.reduce_sum(tf.square(param)))
            r2 = tf.sqrt(tf.reduce_sum(tf.square(update)))
            r = tf.where(tf.greater(r1, 0.0), tf.where(tf.greater(r2, 0.0), r1 / r2, 1.0), 1.0)
            eta = learning_rate * r
            update_with_lr = eta * update
            next_param = param - update_with_lr
            assignments.extend([param.assign(next_param), m.assign(next_m), v.assign(next_v)])
        return tf.group(*assignments, name=name)

    def _do_use_weight_decay(self, param_name):
        """Whether to use L2 weight decay for `param_name`."""
        if not self.weight_decay_rate:
            return False
        if self.include_in_weight_decay:
            for r in self.include_in_weight_decay:
                if re.search(r, param_name) is not None:
                    tf.logging.info('Include %s in weight decay', param_name)
                    return True
        if self.exclude_from_weight_decay:
            for r in self.exclude_from_weight_decay:
                if re.search(r, param_name) is not None:
                    tf.logging.info('Adam WD excludes %s', param_name)
                    return False
        return True

    def _get_variable_name(self, param_name):
        """Get the variable name from the tensor name."""
        m = re.match('^(.*):\\d+$', param_name)
        if m is not None:
            param_name = m.group(1)
        return param_name


Handle

Prompt:
Implement the Python class `GetZone` described below.

Class description:
Get a zone.

Method signatures and docstrings:
- def GetDetailRow(self, result): Returns an associative list of items for display in a detail table. Args: result: A dict returned by the server. Returns: A list.
- def Handle(self, zone_name): Get the specified zone. Args: zone_name: Path of the zone to get. Returns: The result of getting the zone.

Implement the Python class `GetZone` described below.

Class description:
Get a zone.

Method signatures and docstrings:
- def GetDetailRow(self, result): Returns an associative list of items for display in a detail table. Args: result: A dict returned by the server. Returns: A list.
- def Handle(self, zone_name): Get the specified zone. Args: zone_name: Path of the zone to get. Returns: The result of getting the zone.

<|skeleton|>
class GetZone:
    """Get a zone."""

    def GetDetailRow(self, result):
        """Returns an associative list of items for display in a detail table. Args: result: A dict returned by the server. Returns: A list."""
        <|body_0|>

    def Handle(self, zone_name):
        """Get the specified zone. Args: zone_name: Path of the zone to get. Returns: The result of getting the zone."""
        <|body_1|>

<|end_skeleton|>

<|body_start_0|>
        data = []
        for i, m in enumerate(result.get('availableMachineType', [])):
            key = ''
            if i == 0:
                key = 'machine types'
            data.append((key, self._presenter.PresentElement(m)))
        for window in result.get('maintenanceWindows', []):
            maintenance_info = []
            maintenance_info.append(('name', window['name']))
            maintenance_info.append(('description', window['description']))
            maintenance_info.append(('begin-time', window['beginTime']))
            maintenance_info.append(('end-time', window['endTime']))
            data.append(('maintenance-window', maintenance_info))
        return data
<|end_body_0|>

<|body_start_1|>
        zone_context = self._context_parser.ParseContextOrPrompt('zones', zone_name)
        request = self.api.zones.get(project=zone_context['project'], zone=zone_context['zone'])
        return request.execute()
<|end_body_1|>

Solution:
class GetZone:
    """Get a zone."""

    def GetDetailRow(self, result):
        """Returns an associative list of items for display in a detail table. Args: result: A dict returned by the server. Returns: A list."""
        data = []
        for i, m in enumerate(result.get('availableMachineType', [])):
            key = ''
            if i == 0:
                key = 'machine types'
            data.append((key, self._presenter.PresentElement(m)))
        for window in result.get('maintenanceWindows', []):
            maintenance_info = []
            maintenance_info.append(('name', window['name']))
            maintenance_info.append(('description', window['description']))
            maintenance_info.append(('begin-time', window['beginTime']))
            maintenance_info.append(('end-time', window['endTime']))
            data.append(('maintenance-window', maintenance_info))
        return data

    def Handle(self, zone_name):
        """Get the specified zone. Args: zone_name: Path of the zone to get. Returns: The result of getting the zone."""
        zone_context = self._context_parser.ParseContextOrPrompt('zones', zone_name)
        request = self.api.zones.get(project=zone_context['project'], zone=zone_context['zone'])
        return request.execute()


testReadConfigFileInvalid

Prompt:
Implement the Python class `ConfigurationTest` described below.

Class description:
Test WriteConfigFile() and ReadConfigFile().

Method signatures and docstrings:
- def testWriteReadConfigFile(self): Tests WriteConfigFile() then ReadConfigFile().
- def testWriteConfigFileInvalid(self): Tests writing an invalid configuration file.
- def testReadConfigFileInvalid(self): Tests reading an invalid configuration file.

Implement the Python class `ConfigurationTest` described below.

Class description:
Test WriteConfigFile() and ReadConfigFile().

Method signatures and docstrings:
- def testWriteReadConfigFile(self): Tests WriteConfigFile() then ReadConfigFile().
- def testWriteConfigFileInvalid(self): Tests writing an invalid configuration file.
- def testReadConfigFileInvalid(self): Tests reading an invalid configuration file.

<|skeleton|>
class ConfigurationTest:
    """Test WriteConfigFile() and ReadConfigFile()."""

    def testWriteReadConfigFile(self):
        """Tests WriteConfigFile() then ReadConfigFile()."""
        <|body_0|>

    def testWriteConfigFileInvalid(self):
        """Tests writing an invalid configuration file."""
        <|body_1|>

    def testReadConfigFileInvalid(self):
        """Tests reading an invalid configuration file."""
        <|body_2|>

<|end_skeleton|>

<|body_start_0|>
        path = os.path.join(self.tempdir, 'foo.json')
        config = {'foo': 1, 'bar': 2}
        workspace_lib.WriteConfigFile(path, config)
        self.assertDictEqual(config, workspace_lib.ReadConfigFile(path))
<|end_body_0|>

<|body_start_1|>
        path = os.path.join(self.tempdir, 'foo.json')
        config = Exception()
        with self.assertRaises(workspace_lib.ConfigFileError):
            workspace_lib.WriteConfigFile(path, config)
<|end_body_1|>

<|body_start_2|>
        path = os.path.join(self.tempdir, 'foo.json')
        osutils.WriteFile(path, 'invalid contents')
        with self.assertRaises(workspace_lib.ConfigFileError):
            workspace_lib.ReadConfigFile(path)
<|end_body_2|>

Solution:
class ConfigurationTest:
    """Test WriteConfigFile() and ReadConfigFile()."""

    def testWriteReadConfigFile(self):
        """Tests WriteConfigFile() then ReadConfigFile()."""
        path = os.path.join(self.tempdir, 'foo.json')
        config = {'foo': 1, 'bar': 2}
        workspace_lib.WriteConfigFile(path, config)
        self.assertDictEqual(config, workspace_lib.ReadConfigFile(path))

    def testWriteConfigFileInvalid(self):
        """Tests writing an invalid configuration file."""
        path = os.path.join(self.tempdir, 'foo.json')
        config = Exception()
        with self.assertRaises(workspace_lib.ConfigFileError):
            workspace_lib.WriteConfigFile(path, config)

    def testReadConfigFileInvalid(self):
        """Tests reading an invalid configuration file."""
        path = os.path.join(self.tempdir, 'foo.json')
        osutils.WriteFile(path, 'invalid contents')
        with self.assertRaises(workspace_lib.ConfigFileError):
            workspace_lib.ReadConfigFile(path)


Prompt:
Implement the Python class `Generator` described below.

Class description:
Model for Generator.

Method signatures and docstrings:
- def __init__(self, num_channels, z_dim, conv_dim, num_gpu): Init for Generator model.
- def forward(self, input): Forward step for Generator model.

Implement the Python class `Generator` described below.

Class description:
Model for Generator.

Method signatures and docstrings:
- def __init__(self, num_channels, z_dim, conv_dim, num_gpu): Init for Generator model.
- def forward(self, input): Forward step for Generator model.

<|skeleton|>
class Generator:
    """Model for Generator."""

    def __init__(self, num_channels, z_dim, conv_dim, num_gpu):
        """Init for Generator model."""
        <|body_0|>

    def forward(self, input):
        """Forward step for Generator model."""
        <|body_1|>

<|end_skeleton|>

<|body_start_0|>
        super(Generator, self).__init__()
        self.num_gpu = num_gpu
        self.layer = nn.Sequential(nn.ConvTranspose2d(z_dim, conv_dim * 8, 4, 1, 0, bias=False), nn.BatchNorm2d(conv_dim * 8), nn.ReLU(True), nn.ConvTranspose2d(conv_dim * 8, conv_dim * 4, 4, 2, 1, bias=False), nn.BatchNorm2d(conv_dim * 4), nn.ReLU(True), nn.ConvTranspose2d(conv_dim * 4, conv_dim * 2, 4, 2, 1, bias=False), nn.BatchNorm2d(conv_dim * 2), nn.ReLU(True), nn.ConvTranspose2d(conv_dim * 2, conv_dim, 4, 2, 1, bias=False), nn.BatchNorm2d(conv_dim), nn.ReLU(True), nn.ConvTranspose2d(conv_dim, num_channels, 4, 2, 1, bias=False), nn.Tanh())
<|end_body_0|>

<|body_start_1|>
        if isinstance(input.data, torch.cuda.FloatTensor) and self.num_gpu > 1:
            out = nn.parallel.data_parallel(self.layer, input, range(self.num_gpu))
        else:
            out = self.layer(input)
        return out
<|end_body_1|>

Solution:
class Generator:
    """Model for Generator."""

    def __init__(self, num_channels, z_dim, conv_dim, num_gpu):
        """Init for Generator model."""
        super(Generator, self).__init__()
        self.num_gpu = num_gpu
        self.layer = nn.Sequential(nn.ConvTranspose2d(z_dim, conv_dim * 8, 4, 1, 0, bias=False), nn.BatchNorm2d(conv_dim * 8), nn.ReLU(True), nn.ConvTranspose2d(conv_dim * 8, conv_dim * 4, 4, 2, 1, bias=False), nn.BatchNorm2d(conv_dim * 4), nn.ReLU(True), nn.ConvTranspose2d(conv_dim * 4, conv_dim * 2, 4, 2, 1, bias=False), nn.BatchNorm2d(conv_dim * 2), nn.ReLU(True), nn.ConvTranspose2d(conv_dim * 2, conv_dim, 4, 2, 1, bias=False), nn.BatchNorm2d(conv_dim), nn.ReLU(True), nn.ConvTranspose2d(conv_dim, num_channels, 4, 2, 1, bias=False), nn.Tanh())

    def forward(self, input):
        """Forward step for Generator model."""
        if isinstance(input.data, torch.cuda.FloatTensor) and self.num_gpu > 1:
            out = nn.parallel.data_parallel(self.layer, input, range(self.num_gpu))
        else:
            out = self.layer(input)
        return out


Prompt:
Implement the Python class `TestDataset` described below.

Class description:
test Dataset class

Method signatures and docstrings:
- def __init__(self, dataset): init
- def run(self, test_info): run
- def _check_unit(self, info): check unit case
- def _check_load(self, dir): check load case

Implement the Python class `TestDataset` described below.

Class description:
test Dataset class

Method signatures and docstrings:
- def __init__(self, dataset): init
- def run(self, test_info): run
- def _check_unit(self, info): check unit case
- def _check_load(self, dir): check load case

<|skeleton|>
class TestDataset:
    """test Dataset class"""

    def __init__(self, dataset):
        """init"""
        <|body_0|>

    def run(self, test_info):
        """run"""
        <|body_1|>

    def _check_unit(self, info):
        """check unit case"""
        <|body_2|>

    def _check_load(self, dir):
        """check load case"""
        <|body_3|>

<|end_skeleton|>

<|body_start_0|>
        self.dataset = dataset
        self.logger = logger
        self.length = len(self.dataset)
<|end_body_0|>

<|body_start_1|>
        if isinstance(test_info, str):
            self._check_load(test_info)
        else:
            self._check_unit(test_info)
<|end_body_1|>

<|body_start_2|>
        n, unit = info
        check_len = n == self.length
        if check_len:
            logger.get_log().info('dataset length check success !!!')
        else:
            logger.get_log().error('dataset length is {}, but the input length is {}'.format(self.dataset, n))
        for i in range(self.length):
            jud = np.array_equal(self.dataset[i][0], unit * i)
            if not jud:
                logger.get_log().error('Error in unit {}'.format(i))
                logger.get_log().error('dataset is {}, but calculation is {}'.format(self.dataset[i], unit * i))
                assert False
        logger.get_log().info('dataset check success !!!')
<|end_body_2|>

<|body_start_3|>
        data = DataGenerator(dir)()
        features, labels = data
        check_len = len(features) == len(self.dataset)
        if check_len:
            logger.get_log().info('dataset length check success !!!')
        else:
            logger.get_log().error('dataset length is {}, but the input length is {}'.format(self.dataset, len(features)))
            assert False
        step = len(self.dataset) // 100
        for i in range(0, len(self.dataset), step):
            jud_f = np.allclose(features[i], self.dataset[i][0])
            jud_l = np.allclose(labels[i], self.dataset[i][1])
            if not jud_f:
                logger.get_log().error('Feature Error in iter {}'.format(i))
                logger.get_log().error('dataset is {}, but calculation is {}'.format(self.dataset[i][0], features[i]))
                assert False
            if not jud_l:
                logger.get_log().error('Label Error in iter {}'.format(i))
                logger.get_log().error('dataset is {}, but calculation is {}'.format(self.dataset[i][1], labels[i]))
                assert False
        logger.get_log().info('dataset skip check success !!!')
<|end_body_3|>

Solution:
class TestDataset:
    """test Dataset class"""

    def __init__(self, dataset):
        """init"""
        self.dataset = dataset
        self.logger = logger
        self.length = len(self.dataset)

    def run(self, test_info):
        """run"""
        if isinstance(test_info, str):
            self._check_load(test_info)
        else:
            self._check_unit(test_info)

    def _check_unit(self, info):
        """check unit case"""
        n, unit = info
        check_len = n == self.length
        if check_len:
            logger.get_log().info('dataset length check success !!!')
        else:
            logger.get_log().error('dataset length is {}, but the input length is {}'.format(self.dataset, n))
        for i in range(self.length):
            jud = np.array_equal(self.dataset[i][0], unit * i)
            if not jud:
                logger.get_log().error('Error in unit {}'.format(i))
                logger.get_log().error('dataset is {}, but calculation is {}'.format(self.dataset[i], unit * i))
                assert False
        logger.get_log().info('dataset check success !!!')

    def _check_load(self, dir):
        """check load case"""
        data = DataGenerator(dir)()
        features, labels = data
        check_len = len(features) == len(self.dataset)
        if check_len:
            logger.get_log().info('dataset length check success !!!')
        else:
            logger.get_log().error('dataset length is {}, but the input length is {}'.format(self.dataset, len(features)))
            assert False
        step = len(self.dataset) // 100
        for i in range(0, len(self.dataset), step):
            jud_f = np.allclose(features[i], self.dataset[i][0])
            jud_l = np.allclose(labels[i], self.dataset[i][1])
            if not jud_f:
                logger.get_log().error('Feature Error in iter {}'.format(i))
                logger.get_log().error('dataset is {}, but calculation is {}'.format(self.dataset[i][0], features[i]))
                assert False
            if not jud_l:
                logger.get_log().error('Label Error in iter {}'.format(i))
                logger.get_log().error('dataset is {}, but calculation is {}'.format(self.dataset[i][1], labels[i]))
                assert False
        logger.get_log().info('dataset skip check success !!!')


delete

Prompt:
Implement the Python class `DeleteUserProfile` described below.

Class description:
Deletes the user profile :param LoginRequiredMixin, DeleteView: Mixin that will check if user is logged in, Django's Generic View :return: Render login form if successfully delete the current session and redirect.

Method signatures and docstrings:
- def delete(self, request, *args, **kwargs): Deletes the session
- def get_object(self, queryset=None): This will verify if the current user is deleting his profile or not

Implement the Python class `DeleteUserProfile` described below.

Class description:
Deletes the user profile :param LoginRequiredMixin, DeleteView: Mixin that will check if user is logged in, Django's Generic View :return: Render login form if successfully delete the current session and redirect.

Method signatures and docstrings:
- def delete(self, request, *args, **kwargs): Deletes the session
- def get_object(self, queryset=None): This will verify if the current user is deleting his profile or not

<|skeleton|>
class DeleteUserProfile:
    """Deletes the user profile :param LoginRequiredMixin, DeleteView: Mixin that will check if user is logged in, Django's Generic View :return: Render login form if successfully delete the current session and redirect."""

    def delete(self, request, *args, **kwargs):
        """Deletes the session"""
        <|body_0|>

    def get_object(self, queryset=None):
        """This will verify if the current user is deleting his profile or not"""
        <|body_1|>

<|end_skeleton|>

<|body_start_0|>
        self.object = self.get_object()
        success_url = self.get_success_url()
        self.object.delete()
        del request.session['username']
        request.session.modified = True
        return HttpResponseRedirect(success_url)
<|end_body_0|>

<|body_start_1|>
        current_user = super(DeleteUserProfile, self).get_object(queryset)
        if current_user.username != self.request.user.username:
            raise Http404("Please respect other's privacy!")
        return current_user
<|end_body_1|>

Solution:
class DeleteUserProfile:
    """Deletes the user profile :param LoginRequiredMixin, DeleteView: Mixin that will check if user is logged in, Django's Generic View :return: Render login form if successfully delete the current session and redirect."""

    def delete(self, request, *args, **kwargs):
        """Deletes the session"""
        self.object = self.get_object()
        success_url = self.get_success_url()
        self.object.delete()
        del request.session['username']
        request.session.modified = True
        return HttpResponseRedirect(success_url)

    def get_object(self, queryset=None):
        """This will verify if the current user is deleting his profile or not"""
        current_user = super(DeleteUserProfile, self).get_object(queryset)
        if current_user.username != self.request.user.username:
            raise Http404("Please respect other's privacy!")
        return current_user


price

Prompt:
Implement the Python class `ForwardRateAgreement` described below.

Class description:
Represents a batch of Forward Rate Agreements (FRA). An FRA is a contract for the period [T, T+tau] where the holder exchanges a fixed rate (agreed at the start of the contract) against a floating payment determined at time T based on the spot Libor rate for term `tau`. The cashflows are exchanged at the settlement time T_s, which is either equal to T or close to T. The FRA are structured so that the payments are made in T+tau dollars (ref [1]). The ForwardRateAgreement class can be used to create and price multiple FRAs simultaneously. However all FRAs within a FRA object must be priced using a common reference and discount curve. #### Example: The following example illustrates the construc

Method signatures and docstrings:
- def __init__(self, settlement_date, fixing_date, fixed_rate, notional=1.0, daycount_convention=None, rate_term=None, maturity_date=None, dtype=None, name=None): Initialize the batch of FRA contracts. Args: settlement_date: A rank 1 `DateTensor` specifying the dates on which cashflows are settled. The shape of the input correspond to the number of instruments being created. fixing_date: A rank 1 `DateTensor` specifying the dates on which forward rate will be fixed. The shape of the inout should be the same as that of `settlement_date`. fixed_rate: A rank 1 `Tensor` of real dtype specifying the fixed rate payment agreed at the initiation of the individual contracts. The shape should be the same as that of `settlement_date`. notional: A scalar or a rank 1 `Tensor` of real dtype specifying the notional amount for each contract. When the notional is spec
- def price(self, valuation_date, market, model=None): Returns the present value of the instrument on the valuation date. Args: valuation_date: A scalar `DateTensor` specifying the date on which valuation is being desired. market: A namedtuple of type `InterestRateMarket` which contains the necessary information for pricing the FRA instrument. model: Reserved for future use. Returns: A Rank 1 `Tensor` of real type containing the modeled price of each FRA contract based on the input market data.

Implement the Python class `ForwardRateAgreement` described below.

Class description:
Represents a batch of Forward Rate Agreements (FRA). An FRA is a contract for the period [T, T+tau] where the holder exchanges a fixed rate (agreed at the start of the contract) against a floating payment determined at time T based on the spot Libor rate for term `tau`. The cashflows are exchanged at the settlement time T_s, which is either equal to T or close to T. The FRA are structured so that the payments are made in T+tau dollars (ref [1]). The ForwardRateAgreement class can be used to create and price multiple FRAs simultaneously. However all FRAs within a FRA object must be priced using a common reference and discount curve. #### Example: The following example illustrates the construc

Method signatures and docstrings:
- def __init__(self, settlement_date, fixing_date, fixed_rate, notional=1.0, daycount_convention=None, rate_term=None, maturity_date=None, dtype=None, name=None): Initialize the batch of FRA contracts. Args: settlement_date: A rank 1 `DateTensor` specifying the dates on which cashflows are settled. The shape of the input correspond to the number of instruments being created. fixing_date: A rank 1 `DateTensor` specifying the dates on which forward rate will be fixed. The shape of the inout should be the same as that of `settlement_date`. fixed_rate: A rank 1 `Tensor` of real dtype specifying the fixed rate payment agreed at the initiation of the individual contracts. The shape should be the same as that of `settlement_date`. notional: A scalar or a rank 1 `Tensor` of real dtype specifying the notional amount for each contract. When the notional is spec
- def price(self, valuation_date, market, model=None): Returns the present value of the instrument on the valuation date. Args: valuation_date: A scalar `DateTensor` specifying the date on which valuation is being desired. market: A namedtuple of type `InterestRateMarket` which contains the necessary information for pricing the FRA instrument. model: Reserved for future use. Returns: A Rank 1 `Tensor` of real type containing the modeled price of each FRA contract based on the input market data.

<|skeleton|>
class ForwardRateAgreement:
    """Represents a batch of Forward Rate Agreements (FRA). An FRA is a contract for the period [T, T+tau] where the holder exchanges a fixed rate (agreed at the start of the contract) against a floating payment determined at time T based on the spot Libor rate for term `tau`. The cashflows are exchanged at the settlement time T_s, which is either equal to T or close to T. The FRA are structured so that the payments are made in T+tau dollars (ref [1]). The ForwardRateAgreement class can be used to create and price multiple FRAs simultaneously. However all FRAs within a FRA object must be priced using a common reference and discount curve. #### Example: The following example illustrates the construc"""

    def __init__(self, settlement_date, fixing_date, fixed_rate, notional=1.0, daycount_convention=None, rate_term=None, maturity_date=None, dtype=None, name=None):
        """Initialize the batch of FRA contracts. Args: settlement_date: A rank 1 `DateTensor` specifying the dates on which cashflows are settled. The shape of the input correspond to the number of instruments being created. fixing_date: A rank 1 `DateTensor` specifying the dates on which forward rate will be fixed. The shape of the inout should be the same as that of `settlement_date`. fixed_rate: A rank 1 `Tensor` of real dtype specifying the fixed rate payment agreed at the initiation of the individual contracts. The shape should be the same as that of `settlement_date`. notional: A scalar or a rank 1 `Tensor` of real dtype specifying the notional amount for each contract. When the notional is spec"""
        <|body_0|>

    def price(self, valuation_date, market, model=None):
        """Returns the present value of the instrument on the valuation date. Args: valuation_date: A scalar `DateTensor` specifying the date on which valuation is being desired. market: A namedtuple of type `InterestRateMarket` which contains the necessary information for pricing the FRA instrument. model: Reserved for future use. Returns: A Rank 1 `Tensor` of real type containing the modeled price of each FRA contract based on the input market data."""
        <|body_1|>

<|end_skeleton|>

<|body_start_0|>
        self._name = name or 'forward_rate_agreement'
        if rate_term is None and maturity_date is None:
            raise ValueError('Error creating FRA. Either rate_term or maturity_date is required.')
        with tf.name_scope(self._name):
            self._dtype = dtype
            self._notional = tf.convert_to_tensor(notional, dtype=self._dtype)
            self._fixing_date = dates.convert_to_date_tensor(fixing_date)
            self._settlement_date = dates.convert_to_date_tensor(settlement_date)
            self._accrual_start_date = dates.convert_to_date_tensor(settlement_date)
            if rate_term is None:
                self._accrual_end_date = dates.convert_to_date_tensor(maturity_date)
            else:
                self._accrual_end_date = self._accrual_start_date + rate_term
            if daycount_convention is None:
                daycount_convention = rc.DayCountConvention.ACTUAL_360
            self._fixed_rate = tf.convert_to_tensor(fixed_rate, dtype=self._dtype, name='fixed_rate')
            self._daycount_convention = daycount_convention
            self._daycount_fraction = rc.get_daycount_fraction(self._accrual_start_date, self._accrual_end_date, self._daycount_convention, self._dtype)
<|end_body_0|>

<|body_start_1|>
        del model, valuation_date
        reference_curve = market.reference_curve
        discount_curve = market.discount_curve
        fwd_rate = reference_curve.get_forward_rate(self._accrual_start_date, self._accrual_end_date, self._daycount_fraction)
        discount_at_settlement = discount_curve.get_discount_factor(self._settlement_date)
        return discount_at_settlement * self._notional * (fwd_rate - self._fixed_rate) * self._daycount_fraction / (1.0 + self._daycount_fraction * fwd_rate)
<|end_body_1|>

Solution:
class ForwardRateAgreement:
    """Represents a batch of Forward Rate Agreements (FRA). An FRA is a contract for the period [T, T+tau] where the holder exchanges a fixed rate (agreed at the start of the contract) against a floating payment determined at time T based on the spot Libor rate for term `tau`. The cashflows are exchanged at the settlement time T_s, which is either equal to T or close to T. The FRA are structured so that the payments are made in T+tau dollars (ref [1]). The ForwardRateAgreement class can be used to create and price multiple FRAs simultaneously. However all FRAs within a FRA object must be priced using a common reference and discount curve. #### Example: The following example illustrates the construc"""

    def __init__(self, settlement_date, fixing_date, fixed_rate, notional=1.0, daycount_convention=None, rate_term=None, maturity_date=None, dtype=None, name=None):
        """Initialize the batch of FRA contracts. Args: settlement_date: A rank 1 `DateTensor` specifying the dates on which cashflows are settled. The shape of the input correspond to the number of instruments being created. fixing_date: A rank 1 `DateTensor` specifying the dates on which forward rate will be fixed. The shape of the inout should be the same as that of `settlement_date`. fixed_rate: A rank 1 `Tensor` of real dtype specifying the fixed rate payment agreed at the initiation of the individual contracts. The shape should be the same as that of `settlement_date`. notional: A scalar or a rank 1 `Tensor` of real dtype specifying the notional amount for each contract. When the notional is spec"""
        self._name = name or 'forward_rate_agreement'
        if rate_term is None and maturity_date is None:
            raise ValueError('Error creating FRA. Either rate_term or maturity_date is required.')
        with tf.name_scope(self._name):
            self._dtype = dtype
            self._notional = tf.convert_to_tensor(notional, dtype=self._dtype)
            self._fixing_date = dates.convert_to_date_tensor(fixing_date)
            self._settlement_date = dates.convert_to_date_tensor(settlement_date)
            self._accrual_start_date = dates.convert_to_date_tensor(settlement_date)
            if rate_term is None:
                self._accrual_end_date = dates.convert_to_date_tensor(maturity_date)
            else:
                self._accrual_end_date = self._accrual_start_date + rate_term
            if daycount_convention is None:
                daycount_convention = rc.DayCountConvention.ACTUAL_360
            self._fixed_rate = tf.convert_to_tensor(fixed_rate, dtype=self._dtype, name='fixed_rate')
            self._daycount_convention = daycount_convention
            self._daycount_fraction = rc.get_daycount_fraction(self._accrual_start_date, self._accrual_end_date, self._daycount_convention, self._dtype)

    def price(self, valuation_date, market, model=None):
        """Returns the present value of the instrument on the valuation date. Args: valuation_date: A scalar `DateTensor` specifying the date on which valuation is being desired. market: A namedtuple of type `InterestRateMarket` which contains the necessary information for pricing the FRA instrument. model: Reserved for future use. Returns: A Rank 1 `Tensor` of real type containing the modeled price of each FRA contract based on the input market data."""
        del model, valuation_date
        reference_curve = market.reference_curve
        discount_curve = market.discount_curve
        fwd_rate = reference_curve.get_forward_rate(self._accrual_start_date, self._accrual_end_date, self._daycount_fraction)
        discount_at_settlement = discount_curve.get_discount_factor(self._settlement_date)
        return discount_at_settlement * self._notional * (fwd_rate - self._fixed_rate) * self._daycount_fraction / (1.0 + self._daycount_fraction * fwd_rate)


Prompt:
Implement the Python class `DynamicLossScaler` described below.

Class description:
Dynamically adjusts the loss scaling factor. Dynamic loss scalers are important in mixed-precision training. They help us avoid underflows and overflows in low-precision gradients. See here for information: <https://docs.nvidia.com/deeplearning/performance/mixed-precision-training/index.html#lossscaling> Shamelessly stolen and adapted from Fairseq. <https://github.com/pytorch/fairseq/blob/main/fairseq/optim/fp16_optimizer.py>

Method signatures and docstrings:
- def __init__(self, init_scale: float=2.0 ** 15, scale_factor: float=2.0, scale_window: int=2000, tolerance: float=0.0, threshold: float=None): :param init_scale: Initial loss scale. :param scale_factor: Factor by which to increase or decrease loss scale. :param scale_window: If we do not experience overflow in scale_window iterations, loss scale will increase by scale_factor. :param tolerance: Pct of iterations that have overflowed after which we must decrease the loss scale :param threshold: If not None, loss scale will decrease below this threshold
- def update_scale(self, overflow: bool): Update the loss scale. If overflow exceeds our tolerance, we decrease the loss scale. If the number of iterations since the last overflow exceeds the scale window, we increase the loss scale.
- def _decrease_loss_scale(self): Decrease the loss scale by self.scale_factor. NOTE: the loss_scale will not go below self.threshold.

Implement the Python class `DynamicLossScaler` described below.

Class description:
Dynamically adjusts the loss scaling factor. Dynamic loss scalers are important in mixed-precision training. They help us avoid underflows and overflows in low-precision gradients. See here for information: <https://docs.nvidia.com/deeplearning/performance/mixed-precision-training/index.html#lossscaling> Shamelessly stolen and adapted from Fairseq. <https://github.com/pytorch/fairseq/blob/main/fairseq/optim/fp16_optimizer.py>

Method signatures and docstrings:
- def __init__(self, init_scale: float=2.0 ** 15, scale_factor: float=2.0, scale_window: int=2000, tolerance: float=0.0, threshold: float=None): :param init_scale: Initial loss scale. :param scale_factor: Factor by which to increase or decrease loss scale. :param scale_window: If we do not experience overflow in scale_window iterations, loss scale will increase by scale_factor. :param tolerance: Pct of iterations that have overflowed after which we must decrease the loss scale :param threshold: If not None, loss scale will decrease below this threshold
- def update_scale(self, overflow: bool): Update the loss scale. If overflow exceeds our tolerance, we decrease the loss scale. If the number of iterations since the last overflow exceeds the scale window, we increase the loss scale.
- def _decrease_loss_scale(self): Decrease the loss scale by self.scale_factor. NOTE: the loss_scale will not go below self.threshold.

<|skeleton|>
class DynamicLossScaler:
    """Dynamically adjusts the loss scaling factor. Dynamic loss scalers are important in mixed-precision training. They help us avoid underflows and overflows in low-precision gradients. See here for information: <https://docs.nvidia.com/deeplearning/performance/mixed-precision-training/index.html#lossscaling> Shamelessly stolen and adapted from Fairseq. <https://github.com/pytorch/fairseq/blob/main/fairseq/optim/fp16_optimizer.py>"""

    def __init__(self, init_scale: float=2.0 ** 15, scale_factor: float=2.0, scale_window: int=2000, tolerance: float=0.0, threshold: float=None):
        """:param init_scale: Initial loss scale. :param scale_factor: Factor by which to increase or decrease loss scale. :param scale_window: If we do not experience overflow in scale_window iterations, loss scale will increase by scale_factor. :param tolerance: Pct of iterations that have overflowed after which we must decrease the loss scale :param threshold: If not None, loss scale will decrease below this threshold"""
        <|body_0|>

    def update_scale(self, overflow: bool):
        """Update the loss scale. If overflow exceeds our tolerance, we decrease the loss scale. If the number of iterations since the last overflow exceeds the scale window, we increase the loss scale."""
        <|body_1|>

    def _decrease_loss_scale(self):
        """Decrease the loss scale by self.scale_factor. NOTE: the loss_scale will not go below self.threshold."""
        <|body_2|>

<|end_skeleton|>

<|body_start_0|>
        self.loss_scale = init_scale
        self.scale_factor = scale_factor
        self.scale_window = scale_window
        self.tolerance = tolerance
        self.threshold = threshold
        self._iter = 0
        self._last_overflow_iter = -1
        self._last_rescale_iter = -1
        self._overflows_since_rescale = 0
<|end_body_0|>

<|body_start_1|>
        iter_since_rescale = self._iter - self._last_rescale_iter
        if overflow:
            self._last_overflow_iter = self._iter
            self._overflows_since_rescale += 1
            pct_overflow = self._overflows_since_rescale / float(iter_since_rescale)
            if pct_overflow >= self.tolerance:
                self._decrease_loss_scale()
                self._last_rescale_iter = self._iter
                self._overflows_since_rescale = 0
        elif (self._iter - self._last_overflow_iter) % self.scale_window == 0:
            self.loss_scale *= self.scale_factor
            self._last_rescale_iter = self._iter
        self._iter += 1
<|end_body_1|>

<|body_start_2|>
        self.loss_scale /= self.scale_factor
        if self.threshold is not None:
            self.loss_scale = max(self.loss_scale, self.threshold)
<|end_body_2|>

Solution:
class DynamicLossScaler:
    """Dynamically adjusts the loss scaling factor. Dynamic loss scalers are important in mixed-precision training. They help us avoid underflows and overflows in low-precision gradients. See here for information: <https://docs.nvidia.com/deeplearning/performance/mixed-precision-training/index.html#lossscaling> Shamelessly stolen and adapted from Fairseq. <https://github.com/pytorch/fairseq/blob/main/fairseq/optim/fp16_optimizer.py>"""

    def __init__(self, init_scale: float=2.0 ** 15, scale_factor: float=2.0, scale_window: int=2000, tolerance: float=0.0, threshold: float=None):
        """:param init_scale: Initial loss scale. :param scale_factor: Factor by which to increase or decrease loss scale. :param scale_window: If we do not experience overflow in scale_window iterations, loss scale will increase by scale_factor. :param tolerance: Pct of iterations that have overflowed after which we must decrease the loss scale :param threshold: If not None, loss scale will decrease below this threshold"""
        self.loss_scale = init_scale
        self.scale_factor = scale_factor
        self.scale_window = scale_window
        self.tolerance = tolerance
        self.threshold = threshold
        self._iter = 0
        self._last_overflow_iter = -1
        self._last_rescale_iter = -1
        self._overflows_since_rescale = 0

    def update_scale(self, overflow: bool):
        """Update the loss scale. If overflow exceeds our tolerance, we decrease the loss scale. If the number of iterations since the last overflow exceeds the scale window, we increase the loss scale."""
        iter_since_rescale = self._iter - self._last_rescale_iter
        if overflow:
            self._last_overflow_iter = self._iter
            self._overflows_since_rescale += 1
            pct_overflow = self._overflows_since_rescale / float(iter_since_rescale)
            if pct_overflow >= self.tolerance:
                self._decrease_loss_scale()
                self._last_rescale_iter = self._iter
                self._overflows_since_rescale = 0
        elif (self._iter - self._last_overflow_iter) % self.scale_window == 0:
            self.loss_scale *= self.scale_factor
            self._last_rescale_iter = self._iter
        self._iter += 1

    def _decrease_loss_scale(self):
        """Decrease the loss scale by self.scale_factor. NOTE: the loss_scale will not go below self.threshold."""
        self.loss_scale /= self.scale_factor
        if self.threshold is not None:
            self.loss_scale = max(self.loss_scale, self.threshold)