How to use steps method in wpt

Best JavaScript code snippet using wpt

control.py

Source:control.py Github

copy

Full Screen

1## @package control2# Module caffe2.python.control3"""4Implement functions for controlling execution of nets and steps, including5 Do6 DoParallel7 For-loop8 While-loop9 Do-While-loop10 Switch11 If12"""13from __future__ import absolute_import14from __future__ import division15from __future__ import print_function16from __future__ import unicode_literals17from caffe2.python import core18from future.utils import viewitems19# Used to generate names of the steps created by the control functions.20# It is actually the internal index of these steps.21_current_idx = 122_used_step_names = set()23def _get_next_step_name(control_name, base_name):24 global _current_idx, _used_step_names25 concat_name = '%s/%s' % (base_name, control_name)26 next_name = concat_name27 while next_name in _used_step_names:28 next_name = '%s_%d' % (concat_name, _current_idx)29 _current_idx += 130 _used_step_names.add(next_name)31 return next_name32def _MakeList(input):33 """ input is a tuple.34 Example:35 (a, b, c) --> [a, b, c]36 (a) --> [a]37 ([a, b, c]) --> [a, b, c]38 """39 if len(input) == 0:40 raise ValueError(41 'input cannot be empty.')42 elif len(input) == 1:43 output = input[0]44 if not isinstance(output, list):45 output = [output]46 else:47 output = list(input)48 return output49def _IsNets(nets_or_steps):50 if isinstance(nets_or_steps, list):51 return all(isinstance(n, core.Net) for n in nets_or_steps)52 else:53 return isinstance(nets_or_steps, core.Net)54def _PrependNets(nets_or_steps, *nets):55 nets_or_steps = _MakeList((nets_or_steps,))56 nets = _MakeList(nets)57 if _IsNets(nets_or_steps):58 return nets + nets_or_steps59 else:60 return [Do('prepend', nets)] + nets_or_steps61def _AppendNets(nets_or_steps, *nets):62 nets_or_steps = _MakeList((nets_or_steps,))63 nets = _MakeList(nets)64 if _IsNets(nets_or_steps):65 return nets_or_steps + nets66 else:67 return nets_or_steps + [Do('append', nets)]68def GetConditionBlobFromNet(condition_net):69 """70 The condition blob is the last external_output that must71 be a single bool72 """73 assert len(condition_net.Proto().external_output) > 0, (74 "Condition net %s must has at least one external output" %75 condition_net.Proto.name)76 # we need to use a blob reference here instead of a string77 # otherwise, it will add another name_scope to the input later78 # when we create new ops (such as OR of two inputs)79 return core.BlobReference(condition_net.Proto().external_output[-1])80def BoolNet(*blobs_with_bool_value):81 """A net assigning constant bool values to blobs. It is mainly used for82 initializing condition blobs, for example, in multi-task learning, we83 need to access reader_done blobs before reader_net run. In that case,84 the reader_done blobs must be initialized.85 Args:86 blobs_with_bool_value: one or more (blob, bool_value) pairs. The net will87 assign each bool_value to the corresponding blob.88 returns89 bool_net: A net assigning constant bool values to blobs.90 Examples:91 - BoolNet((blob_1, bool_value_1), ..., (blob_n, bool_value_n))92 - BoolNet([(blob_1, net1), ..., (blob_n, bool_value_n)])93 - BoolNet((cond_1, bool_value_1))94 """95 blobs_with_bool_value = _MakeList(blobs_with_bool_value)96 bool_net = core.Net('bool_net')97 for blob, bool_value in blobs_with_bool_value:98 out_blob = bool_net.ConstantFill(99 [],100 [blob],101 shape=[],102 value=bool_value,103 dtype=core.DataType.BOOL)104 bool_net.AddExternalOutput(out_blob)105 return bool_net106def NotNet(condition_blob_or_net):107 """Not of a condition blob or net108 Args:109 condition_blob_or_net can be either blob or net. If condition_blob_or_net110 is Net, the condition is its last external_output111 that must be a single bool.112 returns113 not_net: the net NOT the input114 out_blob: the output blob of the not_net115 """116 if isinstance(condition_blob_or_net, core.Net):117 condition_blob = GetConditionBlobFromNet(condition_blob_or_net)118 else:119 condition_blob = condition_blob_or_net120 not_net = core.Net('not_net')121 out_blob = not_net.Not(condition_blob)122 not_net.AddExternalOutput(out_blob)123 return not_net, out_blob124def _CopyConditionBlobNet(condition_blob):125 """Make a condition net that copies the condition_blob126 Args:127 condition_blob is a single bool.128 returns129 not_net: the net NOT the input130 out_blob: the output blob of the not_net131 """132 condition_net = core.Net('copy_condition_blob_net')133 out_blob = condition_net.Copy(condition_blob)134 condition_net.AddExternalOutput(out_blob)135 return condition_net, out_blob136def MergeConditionNets(name, condition_nets, relation):137 """138 Merge multi condition nets into a single condition nets.139 Args:140 name: name of the new condition net.141 condition_nets: a list of condition nets. The last external_output142 of each condition net must be single bool value.143 relation: can be 'And' or 'Or'.144 Returns:145 - A new condition net. Its last external output is relation of all146 condition_nets.147 """148 if not isinstance(condition_nets, list):149 return condition_nets150 if len(condition_nets) <= 1:151 return condition_nets[0] if condition_nets else None152 merged_net = core.Net(name)153 for i in range(len(condition_nets)):154 net_proto = condition_nets[i].Proto()155 assert net_proto.device_option == merged_net.Proto().device_option156 assert net_proto.type == merged_net.Proto().type157 merged_net.Proto().op.extend(net_proto.op)158 merged_net.Proto().external_input.extend(net_proto.external_input)159 # discard external outputs as we're combining them together160 curr_cond = GetConditionBlobFromNet(condition_nets[i])161 if i == 0:162 last_cond = curr_cond163 else:164 last_cond = merged_net.__getattr__(relation)([last_cond, curr_cond])165 # merge attributes166 for k, v in viewitems(condition_nets[i]._attr_dict):167 merged_net._attr_dict[k] += v168 merged_net.AddExternalOutput(last_cond)169 return merged_net170def CombineConditions(name, condition_nets, relation):171 """172 Combine conditions of multi nets into a single condition nets. Unlike173 MergeConditionNets, the actual body of condition_nets is not copied into174 the combine condition net.175 One example is about multi readers. Each reader net has a reader_done176 condition. When we want to check whether all readers are done, we can177 use this function to build a new net.178 Args:179 name: name of the new condition net.180 condition_nets: a list of condition nets. The last external_output181 of each condition net must be single bool value.182 relation: can be 'And' or 'Or'.183 Returns:184 - A new condition net. Its last external output is relation of all185 condition_nets.186 """187 if not condition_nets:188 return None189 if not isinstance(condition_nets, list):190 raise ValueError('condition_nets must be a list of nets.')191 if len(condition_nets) == 1:192 condition_blob = GetConditionBlobFromNet(condition_nets[0])193 condition_net, _ = _CopyConditionBlobNet(condition_blob)194 return condition_net195 combined_net = core.Net(name)196 for i in range(len(condition_nets)):197 curr_cond = GetConditionBlobFromNet(condition_nets[i])198 if i == 0:199 last_cond = curr_cond200 else:201 last_cond = combined_net.__getattr__(relation)(202 [last_cond, curr_cond])203 combined_net.AddExternalOutput(last_cond)204 return combined_net205def Do(name, *nets_or_steps):206 """207 Execute the sequence of nets or steps once.208 Examples:209 - Do('myDo', net1, net2, ..., net_n)210 - Do('myDo', list_of_nets)211 - Do('myDo', step1, step2, ..., step_n)212 - Do('myDo', list_of_steps)213 """214 nets_or_steps = _MakeList(nets_or_steps)215 if (len(nets_or_steps) == 1 and isinstance(216 nets_or_steps[0], core.ExecutionStep)):217 return nets_or_steps[0]218 else:219 return core.scoped_execution_step(220 _get_next_step_name('Do', name), nets_or_steps)221def DoParallel(name, *nets_or_steps):222 """223 Execute the nets or steps in parallel, waiting for all of them to finish224 Examples:225 - DoParallel('pDo', net1, net2, ..., net_n)226 - DoParallel('pDo', list_of_nets)227 - DoParallel('pDo', step1, step2, ..., step_n)228 - DoParallel('pDo', list_of_steps)229 """230 nets_or_steps = _MakeList(nets_or_steps)231 if (len(nets_or_steps) == 1 and isinstance(232 nets_or_steps[0], core.ExecutionStep)):233 return nets_or_steps[0]234 else:235 return core.scoped_execution_step(236 _get_next_step_name('DoParallel', name),237 nets_or_steps,238 concurrent_substeps=True)239def _RunOnceIf(name, condition_blob_or_net, nets_or_steps):240 """241 Execute nets_or_steps once if condition_blob_or_net evaluates as true.242 If condition_blob_or_net is Net, the condition is its last external_output243 that must be a single bool. And this net will be executed before244 nets_or_steps so as to get the condition.245 """246 condition_not_net, stop_blob = NotNet(condition_blob_or_net)247 if isinstance(condition_blob_or_net, core.Net):248 nets_or_steps = _PrependNets(249 nets_or_steps, condition_blob_or_net, condition_not_net)250 else:251 nets_or_steps = _PrependNets(nets_or_steps, condition_not_net)252 def if_step(control_name):253 return core.scoped_execution_step(254 _get_next_step_name(control_name, name),255 nets_or_steps,256 should_stop_blob=stop_blob,257 only_once=True,258 )259 if _IsNets(nets_or_steps):260 bool_net = BoolNet((stop_blob, False))261 return Do(name + '/_RunOnceIf',262 bool_net, if_step('_RunOnceIf-inner'))263 else:264 return if_step('_RunOnceIf')265def _RunOnceIfNot(name, condition_blob_or_net, nets_or_steps):266 """267 Similar to _RunOnceIf() but Execute nets_or_steps once if268 condition_blob_or_net evaluates as false.269 """270 if isinstance(condition_blob_or_net, core.Net):271 condition_blob = GetConditionBlobFromNet(condition_blob_or_net)272 nets_or_steps = _PrependNets(nets_or_steps, condition_blob_or_net)273 else:274 copy_net, condition_blob = _CopyConditionBlobNet(condition_blob_or_net)275 nets_or_steps = _PrependNets(nets_or_steps, copy_net)276 return core.scoped_execution_step(277 _get_next_step_name('_RunOnceIfNot', name),278 nets_or_steps,279 should_stop_blob=condition_blob,280 only_once=True,281 )282def For(name, nets_or_steps, iter_num):283 """284 Execute nets_or_steps iter_num times.285 Args:286 nets_or_steps: a ExecutionStep or a Net or a list of ExecutionSteps or287 a list nets.288 iter_num: the number times to execute the nets_or_steps.289 Returns:290 A ExecutionStep instance.291 """292 init_net = core.Net('init-net')293 iter_cnt = init_net.CreateCounter([], init_count=iter_num)294 iter_net = core.Net('For-iter')295 iter_done = iter_net.CountDown([iter_cnt])296 for_step = core.scoped_execution_step(297 _get_next_step_name('For-inner', name),298 _PrependNets(nets_or_steps, iter_net),299 should_stop_blob=iter_done)300 return Do(name + '/For',301 Do(name + '/For-init-net', init_net),302 for_step)303def While(name, condition_blob_or_net, nets_or_steps):304 """305 Execute nets_or_steps when condition_blob_or_net returns true.306 Args:307 condition_blob_or_net: If it is an instance of Net, its last308 external_output must be a single bool.309 nets_or_steps: a ExecutionStep or a Net or a list of ExecutionSteps or310 a list nets.311 Returns:312 A ExecutionStep instance.313 """314 condition_not_net, stop_blob = NotNet(condition_blob_or_net)315 if isinstance(condition_blob_or_net, core.Net):316 nets_or_steps = _PrependNets(317 nets_or_steps, condition_blob_or_net, condition_not_net)318 else:319 nets_or_steps = _PrependNets(nets_or_steps, condition_not_net)320 def while_step(control_name):321 return core.scoped_execution_step(322 _get_next_step_name(control_name, name),323 nets_or_steps,324 should_stop_blob=stop_blob,325 )326 if _IsNets(nets_or_steps):327 # In this case, while_step has sub-nets:328 # [condition_blob_or_net, condition_not_net, nets_or_steps]329 # If stop_blob is pre-set to True (this may happen when While() is330 # called twice), the loop will exit after executing331 # condition_blob_or_net. So we use BootNet to set stop_blob to332 # False.333 bool_net = BoolNet((stop_blob, False))334 return Do(name + '/While', bool_net, while_step('While-inner'))335 else:336 return while_step('While')337def Until(name, condition_blob_or_net, nets_or_steps):338 """339 Similar to While() but execute nets_or_steps when340 condition_blob_or_net returns false341 """342 if isinstance(condition_blob_or_net, core.Net):343 stop_blob = GetConditionBlobFromNet(condition_blob_or_net)344 nets_or_steps = _PrependNets(nets_or_steps, condition_blob_or_net)345 else:346 stop_blob = core.BlobReference(str(condition_blob_or_net))347 return core.scoped_execution_step(348 _get_next_step_name('Until', name),349 nets_or_steps,350 should_stop_blob=stop_blob)351def DoWhile(name, condition_blob_or_net, nets_or_steps):352 """353 Execute nets_or_steps when condition_blob_or_net returns true. It will354 execute nets_or_steps before evaluating condition_blob_or_net.355 Args:356 condition_blob_or_net: if it is an instance of Net, tts last external_output357 must be a single bool.358 nets_or_steps: a ExecutionStep or a Net or a list of ExecutionSteps or359 a list nets.360 Returns:361 A ExecutionStep instance.362 """363 condition_not_net, stop_blob = NotNet(condition_blob_or_net)364 if isinstance(condition_blob_or_net, core.Net):365 nets_or_steps = _AppendNets(366 nets_or_steps, condition_blob_or_net, condition_not_net)367 else:368 nets_or_steps = _AppendNets(nets_or_steps, condition_not_net)369 # If stop_blob is pre-set to True (this may happen when DoWhile() is370 # called twice), the loop will exit after executing the first net/step371 # in nets_or_steps. This is not what we want. So we use BootNet to372 # set stop_blob to False.373 bool_net = BoolNet((stop_blob, False))374 return Do(name + '/DoWhile', bool_net, core.scoped_execution_step(375 _get_next_step_name('DoWhile-inner', name),376 nets_or_steps,377 should_stop_blob=stop_blob,378 ))379def DoUntil(name, condition_blob_or_net, nets_or_steps):380 """381 Similar to DoWhile() but execute nets_or_steps when382 condition_blob_or_net returns false. It will execute383 nets_or_steps before evaluating condition_blob_or_net.384 Special case: if condition_blob_or_net is a blob and is pre-set to385 true, then only the first net/step of nets_or_steps will be executed and386 loop is exited. So you need to be careful about the initial value the387 condition blob when using DoUntil(), esp when DoUntil() is called twice.388 """389 if not isinstance(condition_blob_or_net, core.Net):390 stop_blob = core.BlobReference(condition_blob_or_net)391 return core.scoped_execution_step(392 _get_next_step_name('DoUntil', name),393 nets_or_steps,394 should_stop_blob=stop_blob)395 nets_or_steps = _AppendNets(nets_or_steps, condition_blob_or_net)396 stop_blob = GetConditionBlobFromNet(condition_blob_or_net)397 # If stop_blob is pre-set to True (this may happen when DoWhile() is398 # called twice), the loop will exit after executing the first net/step399 # in nets_or_steps. This is not what we want. So we use BootNet to400 # set stop_blob to False.401 bool_net = BoolNet((stop_blob, False))402 return Do(name + '/DoUntil', bool_net, core.scoped_execution_step(403 _get_next_step_name('DoUntil-inner', name),404 nets_or_steps,405 should_stop_blob=stop_blob,406 ))407def Switch(name, *conditions):408 """409 Execute the steps for which the condition is true.410 Each condition is a tuple (condition_blob_or_net, nets_or_steps).411 Note:412 1. Multi steps can be executed if their conditions are true.413 2. The conditions_blob_or_net (if it is Net) of all steps will be414 executed once.415 Examples:416 - Switch('name', (cond_1, net_1), (cond_2, net_2), ..., (cond_n, net_n))417 - Switch('name', [(cond_1, net1), (cond_2, net_2), ..., (cond_n, net_n)])418 - Switch('name', (cond_1, net_1))419 """420 conditions = _MakeList(conditions)421 return core.scoped_execution_step(422 _get_next_step_name('Switch', name),423 [_RunOnceIf(name + '/Switch', cond, step) for cond, step in conditions])424def SwitchNot(name, *conditions):425 """426 Similar to Switch() but execute the steps for which the condition is False.427 """428 conditions = _MakeList(conditions)429 return core.scoped_execution_step(430 _get_next_step_name('SwitchNot', name),431 [_RunOnceIfNot(name + '/SwitchNot', cond, step)432 for cond, step in conditions])433def If(name, condition_blob_or_net,434 true_nets_or_steps, false_nets_or_steps=None):435 """436 condition_blob_or_net is first evaluated or executed. If the condition is437 true, true_nets_or_steps is then executed, otherwise, false_nets_or_steps438 is executed.439 If condition_blob_or_net is Net, the condition is its last external_output440 that must be a single bool. And this Net will be executred before both441 true/false_nets_or_steps so as to get the condition.442 """443 if not false_nets_or_steps:444 return _RunOnceIf(name + '/If',445 condition_blob_or_net, true_nets_or_steps)446 if isinstance(condition_blob_or_net, core.Net):447 condition_blob = GetConditionBlobFromNet(condition_blob_or_net)448 else:449 condition_blob = condition_blob_or_net450 return Do(451 name + '/If',452 _RunOnceIf(name + '/If-true',453 condition_blob_or_net, true_nets_or_steps),454 _RunOnceIfNot(name + '/If-false', condition_blob, false_nets_or_steps)455 )456def IfNot(name, condition_blob_or_net,457 true_nets_or_steps, false_nets_or_steps=None):458 """459 If condition_blob_or_net returns false, executes true_nets_or_steps,460 otherwise executes false_nets_or_steps461 """462 if not false_nets_or_steps:463 return _RunOnceIfNot(name + '/IfNot',464 condition_blob_or_net, true_nets_or_steps)465 if isinstance(condition_blob_or_net, core.Net):466 condition_blob = GetConditionBlobFromNet(condition_blob_or_net)467 else:468 condition_blob = condition_blob_or_net469 return Do(470 name + '/IfNot',471 _RunOnceIfNot(name + '/IfNot-true',472 condition_blob_or_net, true_nets_or_steps),473 _RunOnceIf(name + '/IfNot-false', condition_blob, false_nets_or_steps)...

Full Screen

Full Screen

steps.py

Source:steps.py Github

copy

Full Screen

...347 # -- REPORT API:348 def report(self):349 self.report_used_step_definitions()350 self.report_unused_step_definitions()351 self.report_undefined_steps()352 self.stream.write("\n")353 # -- REPORT SPECIFIC-API:354 def report_used_step_definitions(self):355 # -- STEP: Used step definitions.356 # ORDERING: Sort step definitions by file location.357 get_location = lambda x: x[0].location358 step_definition_items = self.step_usage_database.items()359 step_definition_items = sorted(step_definition_items, key=get_location)360 for step_definition, steps in step_definition_items:361 stepdef_text = self.describe_step_definition(step_definition)362 steps_text = [u" %s %s" % (step.keyword, step.name)363 for step in steps]364 steps_text.append(stepdef_text)365 max_size = compute_words_maxsize(steps_text)366 if max_size < self.min_location_column:367 max_size = self.min_location_column368 schema = u"%-" + _text(max_size) + "s # %s\n"369 self.stream.write(schema % (stepdef_text, step_definition.location))370 schema = u"%-" + _text(max_size) + "s # %s\n"371 for step, step_text in zip(steps, steps_text):372 self.stream.write(schema % (step_text, step.location))373 self.stream.write("\n")374 def report_unused_step_definitions(self):375 unused_step_definitions = self.select_unused_step_definitions()376 if not unused_step_definitions:377 return378 # -- STEP: Prepare report for unused step definitions.379 # ORDERING: Sort step definitions by file location.380 get_location = lambda x: x.location381 step_definitions = sorted(unused_step_definitions, key=get_location)382 step_texts = [self.describe_step_definition(step_definition)383 for step_definition in step_definitions]384 max_size = compute_words_maxsize(step_texts)385 if max_size < self.min_location_column-2:386 max_size = self.min_location_column-2387 # -- STEP: Write report.388 schema = u" %-" + _text(max_size) + "s # %s\n"389 self.stream.write("UNUSED STEP DEFINITIONS[%d]:\n" % len(step_texts))390 for step_definition, step_text in zip(step_definitions, step_texts):391 self.stream.write(schema % (step_text, step_definition.location))392 def report_undefined_steps(self):393 if not self.undefined_steps:394 return395 # -- STEP: Undefined steps.396 undefined_steps = sorted(self.undefined_steps,397 key=attrgetter("location"))398 steps_text = [u" %s %s" % (step.keyword, step.name)399 for step in undefined_steps]400 max_size = compute_words_maxsize(steps_text)401 if max_size < self.min_location_column:402 max_size = self.min_location_column403 self.stream.write("\nUNDEFINED STEPS[%d]:\n" % len(steps_text))404 schema = u"%-" + _text(max_size) + "s # %s\n"405 for step, step_text in zip(undefined_steps, steps_text):406 self.stream.write(schema % (step_text, step.location))...

Full Screen

Full Screen

rl_experiment.py

Source:rl_experiment.py Github

copy

Full Screen

...72 @property73 def agent(self):74 return self._agent75 @property76 def train_steps(self):77 return self._train_steps78 @property79 def eval_steps(self):80 return self._eval_steps81 def _call_train(self, steps=None, first_update=None, update_frequency=None, episodes=None,82 hooks=None, max_steps=None, max_episodes=None):83 return self._agent.train(env=self._env, first_update=first_update,84 update_frequency=update_frequency, episodes=episodes, steps=steps,85 max_steps=max_steps, max_episodes=max_episodes, hooks=hooks)86 def train(self, delay_secs=None):87 """Fit the agent.88 Train the agent for `self._train_steps` steps, after waiting for `delay_secs` seconds.89 If `self._train_steps` is `None`, train forever.90 Args:91 delay_secs: Start training after this many seconds.92 Returns:93 The trained estimator....

Full Screen

Full Screen

set_initial_cumulative_probability.py

Source:set_initial_cumulative_probability.py Github

copy

Full Screen

1import numpy as np2import gdal, os, sys, glob, random3import pylab as pl4def set_initial_cumulative_probability(self):5 6 """7 Setting the cumulative probability array and initializing all cohorts in8 all elements to 0.0.9 10 """11 print ' Initializing Cumulative Probability of Initiation'12 # ---------------------------------------------------------------------13 # Array format is row = element, col = Probability of initiation value.14 #----------------------------------------------------------------------15# self.Wet_NPG_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])16# self.Wet_LCP_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])17# self.Wet_CLC_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])18# self.Wet_FCP_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])19# self.Wet_HCP_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])20# self.Gra_NPG_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])21# self.Gra_LCP_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])22# self.Gra_FCP_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])23# self.Gra_HCP_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])24# self.Shr_NPG_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])25# self.Shr_LCP_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])26# self.Shr_FCP_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])27# self.Shr_HCP_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])28# self.Lakes_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])29# self.Ponds_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])30 self.ATTM_CLC_WT_Y_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])31 self.ATTM_CLC_WT_M_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])32 self.ATTM_CLC_WT_O_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])33 self.ATTM_CoastalWaters_WT_O_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])34 self.ATTM_DrainedSlope_WT_Y_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])35 self.ATTM_DrainedSlope_WT_M_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])36 self.ATTM_DrainedSlope_WT_O_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])37 self.ATTM_FCP_WT_Y_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])38 self.ATTM_FCP_WT_M_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])39 self.ATTM_FCP_WT_O_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])40 self.ATTM_HCP_WT_Y_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])41 self.ATTM_HCP_WT_M_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])42 self.ATTM_HCP_WT_O_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])43 self.ATTM_LCP_WT_Y_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])44 self.ATTM_LCP_WT_M_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])45 self.ATTM_LCP_WT_O_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])46 self.ATTM_Meadow_WT_Y_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])47 self.ATTM_Meadow_WT_M_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])48 self.ATTM_Meadow_WT_O_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])49 self.ATTM_NoData_WT_O_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])50 self.ATTM_SandDunes_WT_Y_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])51 self.ATTM_SandDunes_WT_M_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])52 self.ATTM_SandDunes_WT_O_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])53 self.ATTM_SaturatedBarrens_WT_Y_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])54 self.ATTM_SaturatedBarrens_WT_M_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])55 self.ATTM_SaturatedBarrens_WT_O_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])56 self.ATTM_Shurbs_WT_O_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])57 self.ATTM_Urban_WT_POI = np.zeros([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])58 self.ATTM_LargeLakes_WT_Y_POI = ([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])59 self.ATTM_LargeLakes_WT_M_POI = ([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])60 self.ATTM_LargeLakes_WT_O_POI = ([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])61 self.ATTM_MediumLakes_WT_Y_POI = ([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])62 self.ATTM_MediumLakes_WT_M_POI = ([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])63 self.ATTM_MediumLakes_WT_O_POI = ([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])64 self.ATTM_SmallLakes_WT_Y_POI = ([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])65 self.ATTM_SmallLakes_WT_M_POI = ([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])66 self.ATTM_SmallLakes_WT_O_POI = ([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])67 self.ATTM_Ponds_WT_Y_POI = ([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])68 self.ATTM_Ponds_WT_M_POI = ([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])69 self.ATTM_Ponds_WT_O_POI = ([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])70 self.ATTM_Rivers_WT_Y_POI = ([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])71 self.ATTM_Rivers_WT_M_POI = ([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])72 self.ATTM_Rivers_WT_O_POI = ([self.ATTM_nrows * self.ATTM_ncols, self.ATTM_time_steps])...

Full Screen

Full Screen

tri_stage_lr_scheduler.py

Source:tri_stage_lr_scheduler.py Github

copy

Full Screen

1# Copyright (c) Facebook, Inc. and its affiliates.2#3# This source code is licensed under the MIT license found in the4# LICENSE file in the root directory of this source tree.5import math6from dataclasses import dataclass, field7from typing import Optional, List, Tuple8from omegaconf import II9from fairseq.dataclass import FairseqDataclass10from fairseq.optim.lr_scheduler import FairseqLRScheduler, register_lr_scheduler11@dataclass12class TriStageLRScheduleConfig(FairseqDataclass):13 warmup_steps: int = field(14 default=0,15 metadata={"help": "warmup the learning rate linearly for the first N updates"},16 )17 hold_steps: int = field(18 default=0,19 metadata={"help": "steps in hold stage"},20 )21 decay_steps: int = field(22 default=0,23 metadata={"help": "steps in decay stages"},24 )25 phase_ratio: Optional[Tuple[float, float, float]] = field(26 default=None,27 metadata={28 "help": (29 "if set, automatically sets warmup/hold/decay steps to the ratio "30 "specified here from max_updates. the ratios must add up to 1.0"31 )32 },33 )34 init_lr_scale: float = field(35 default=0.01,36 metadata={"help": "initial learning rate scale during warmup phase"},37 )38 final_lr_scale: float = field(39 default=0.01,40 metadata={"help": "final learning rate scale"},41 )42 max_update: float = II("optimization.max_update")43 lr: List[float] = II("optimization.lr")44@register_lr_scheduler("tri_stage", dataclass=TriStageLRScheduleConfig)45class TriStageLRSchedule(FairseqLRScheduler):46 """Tristage learning rate schedulr47 Implement the learning rate scheduler in https://arxiv.org/pdf/1904.08779.pdf48 Similar to inverse_squre_root scheduler, but tri_stage learning rate employs49 three stages LR scheduling:50 - warmup stage, starting from `lr` * `init_lr_scale`, linearly51 increased to `lr` in `warmup_steps` iterations52 - hold stage, after `warmup_steps`, keep the LR as `lr` for `hold_steps`53 iterations54 - decay stage, after hold stage, decay LR exponetially to55 `lr` * `final_lr_scale` in `decay_steps`;56 after that LR is keep as `final_lr_scale` * `lr`57 During warmup::58 init_lr = cfg.init_lr_scale * cfg.lr59 lrs = torch.linspace(init_lr, cfg.lr, cfg.warmup_steps)60 lr = lrs[update_num]61 During hold::62 lr = cfg.lr63 During decay::64 decay_factor = - math.log(cfg.final_lr_scale) / cfg.decay_steps65 lr = cfg.lr * exp(- (update_num - warmup_steps - decay_steps) * decay_factor)66 After that::67 lr = cfg.lr * cfg.final_lr_scale68 """69 def __init__(self, cfg: TriStageLRScheduleConfig, optimizer):70 super().__init__(cfg, optimizer)71 if len(cfg.lr) > 1:72 raise ValueError(73 "Cannot use a fixed learning rate schedule with tri-stage lr."74 " Consider --lr-scheduler=fixed instead."75 )76 # calculate LR at each point77 self.peak_lr = cfg.lr[0]78 self.init_lr = cfg.init_lr_scale * cfg.lr[0]79 self.final_lr = cfg.final_lr_scale * cfg.lr[0]80 if cfg.phase_ratio is not None:81 assert cfg.max_update > 082 assert sum(cfg.phase_ratio) == 1, "phase ratios must add up to 1"83 self.warmup_steps = int(cfg.max_update * cfg.phase_ratio[0])84 self.hold_steps = int(cfg.max_update * cfg.phase_ratio[1])85 self.decay_steps = int(cfg.max_update * cfg.phase_ratio[2])86 else:87 self.warmup_steps = cfg.warmup_steps88 self.hold_steps = cfg.hold_steps89 self.decay_steps = cfg.decay_steps90 assert (91 self.warmup_steps + self.hold_steps + self.decay_steps > 092 ), "please specify steps or phase_ratio"93 self.warmup_rate = (94 (self.peak_lr - self.init_lr) / self.warmup_steps95 if self.warmup_steps != 096 else 097 )98 self.decay_factor = -math.log(cfg.final_lr_scale) / self.decay_steps99 # initial learning rate100 self.lr = self.init_lr101 self.optimizer.set_lr(self.lr)102 def _decide_stage(self, update_step):103 """104 return stage, and the corresponding steps within the current stage105 """106 if update_step < self.warmup_steps:107 # warmup state108 return 0, update_step109 offset = self.warmup_steps110 if update_step < offset + self.hold_steps:111 # hold stage112 return 1, update_step - offset113 offset += self.hold_steps114 if update_step <= offset + self.decay_steps:115 # decay stage116 return 2, update_step - offset117 offset += self.decay_steps118 # still here ? constant lr stage119 return 3, update_step - offset120 def step(self, epoch, val_loss=None):121 """Update the learning rate at the end of the given epoch."""122 super().step(epoch, val_loss)123 # we don't change the learning rate at epoch boundaries124 return self.optimizer.get_lr()125 def step_update(self, num_updates):126 """Update the learning rate after each update."""127 stage, steps_in_stage = self._decide_stage(num_updates)128 if stage == 0:129 self.lr = self.init_lr + self.warmup_rate * steps_in_stage130 elif stage == 1:131 self.lr = self.peak_lr132 elif stage == 2:133 self.lr = self.peak_lr * math.exp(-self.decay_factor * steps_in_stage)134 elif stage == 3:135 self.lr = self.final_lr136 else:137 raise ValueError("Undefined stage")138 self.optimizer.set_lr(self.lr)...

Full Screen

Full Screen

lr_generator.py

Source:lr_generator.py Github

copy

Full Screen

1# Copyright 2020 Huawei Technologies Co., Ltd2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14# ============================================================================15"""learning rate generator"""16import numpy as np171819def get_lr(global_step, lr_init, lr_end, lr_max, warmup_epochs, total_epochs, steps_per_epoch, lr_decay_mode):20 """21 generate learning rate array2223 Args:24 global_step(int): total steps of the training25 lr_init(float): init learning rate26 lr_end(float): end learning rate27 lr_max(float): max learning rate28 warmup_epochs(int): number of warmup epochs29 total_epochs(int): total epoch of training30 steps_per_epoch(int): steps of one epoch31 lr_decay_mode(string): learning rate decay mode, including steps, poly or default3233 Returns:34 np.array, learning rate array35 """36 lr_each_step = []37 total_steps = steps_per_epoch * total_epochs38 warmup_steps = steps_per_epoch * warmup_epochs39 if lr_decay_mode == 'steps':40 # decay_epoch_index = [0.3 * total_steps, 0.6 * total_steps, 0.8 * total_steps]41 # for i in range(total_steps):42 # if i < decay_epoch_index[0]:43 # lr = lr_max44 # elif i < decay_epoch_index[1]:45 # lr = lr_max * 0.146 # elif i < decay_epoch_index[2]:47 # lr = lr_max * 0.0148 # else:49 # lr = lr_max * 0.00150 # lr_each_step.append(lr)51 lr_each_step = []52 total_steps = steps_per_epoch * total_epochs5354 for i in range(total_steps):55 lr_temp = lr_init56 for j in range(int(i/steps_per_epoch)):57 lr_temp = lr_temp * 0.958 lr_each_step.append(lr_temp)59 #print("step={0}, lr={1}".format(i, lr_temp))60 elif lr_decay_mode == 'poly':61 if warmup_steps != 0:62 inc_each_step = (float(lr_max) - float(lr_init)) / float(warmup_steps)63 else:64 inc_each_step = 065 for i in range(total_steps):66 if i < warmup_steps:67 lr = float(lr_init) + inc_each_step * float(i)68 else:69 base = (1.0 - (float(i) - float(warmup_steps)) / (float(total_steps) - float(warmup_steps)))70 lr = float(lr_max) * base * base71 if lr < 0.0:72 lr = 0.073 lr_each_step.append(lr)74 else:75 for i in range(total_steps):76 if i < warmup_steps:77 lr = lr_init + (lr_max - lr_init) * i / warmup_steps78 else:79 lr = lr_max - (lr_max - lr_end) * (i - warmup_steps) / (total_steps - warmup_steps)80 lr_each_step.append(lr)8182 current_step = global_step83 lr_each_step = np.array(lr_each_step).astype(np.float32)84 learning_rate = lr_each_step[current_step:]85 ...

Full Screen

Full Screen

day_7.py

Source:day_7.py Github

copy

Full Screen

1#!/usr/bin/env python32""" Advent of code """3import re4from collections import namedtuple5def part_1(fh):6 graph = {}7 all_steps = set()8 for line in fh:9 m = re.match(10 r"Step (.) must be finished before step (.) can begin.", line)11 all_steps.add(m.group(1))12 all_steps.add(m.group(2))13 try:14 graph[m.group(2)].add(m.group(1))15 except KeyError:16 graph[m.group(2)] = {m.group(1)}17 return ''.join(find_path(all_steps, graph))18def find_path(all_steps, graph, path=[]):19 print(':', all_steps, graph, path)20 # Find the step with no prereqs21 next_step = find_next_step(all_steps, graph)22 # If none left, we're done23 if next_step == None:24 return path25 # Otherwise, recurse26 return find_path(all_steps - {next_step}, graph, path + [next_step])27def find_next_step(all_steps, graph, ignore=set()):28 possible = set()29 for s in sorted(all_steps):30 try:31 if len(graph[s] & all_steps) == 0:32 possible.add(s)33 except KeyError:34 possible.add(s)35 next_steps = (possible - ignore)36 if len(next_steps) == 0:37 return None38 return sorted(next_steps)[0]39Worker = namedtuple('Worker', ['step', 'time_spent'])40step_duration = 6041num_workers = 542def part_2(fh):43 graph = {}44 all_steps = set()45 for line in fh:46 m = re.match(47 r"Step (.) must be finished before step (.) can begin.", line)48 all_steps.add(m.group(1))49 all_steps.add(m.group(2))50 try:51 graph[m.group(2)].add(m.group(1))52 except KeyError:53 graph[m.group(2)] = {m.group(1)}54 workers = {}55 for i in range(num_workers):56 workers[i] = Worker('', 0)57 done = set()58 print("Second", "Workers", "Done")59 second = 060 while True:61 done |= per_second(graph, all_steps - done, workers)62 print(second, workers, done)63 if len(done) == len(all_steps):64 break65 second += 166 return second67def per_second(graph, all_steps, workers):68 finished_tasks = set()69 for i in workers:70 # Find finished tasks71 if workers[i].step != '':72 task_time = step_duration + ord(workers[i].step)-6473 if workers[i].time_spent == task_time:74 finished_tasks.add(workers[i].step)75 all_steps = all_steps - {workers[i].step}76 workers[i] = Worker('', 0)77 # Assign Work78 if workers[i].step == '':79 # Find next task (and our exit condition)80 next_step = find_next_step(81 all_steps, graph, steps_in_progress(workers))82 #print('find_next-step', all_steps, next_step)83 if next_step != None:84 workers[i] = Worker(next_step, 0)85 # Assign Hours86 if workers[i].step != '':87 workers[i] = Worker(workers[i].step, workers[i].time_spent+1)88 return finished_tasks89def steps_in_progress(workers):90 steps = set()91 for i in workers:92 if workers[i].step != '':93 steps.add(workers[i].step)94 return steps95def main():96 "Main Entrypoint"97 print('Part 1 is: ', part_1(open('input_7', 'r')))98 print('Part 2 is: ', part_2(open('input_7', 'r'))-1)99if __name__ == "__main__":...

Full Screen

Full Screen

Walking.py

Source:Walking.py Github

copy

Full Screen

1steps = 02while steps < 10000:3 command = input()4 if command == 'Going home':5 steps_to_home = int(input())6 steps += steps_to_home7 break8 else:9 walked_steps = int(command)10 steps += walked_steps11difference = abs(steps - 10000)12if steps >= 10000:13 print(f'Goal reached! Good job!')14 print(f"{difference} steps over the goal!")15else:16 print(f'{difference} more steps to reach goal.')17# steps = int(input())18# steps_text = str(steps)19#20# sum = 021#22# while steps_text != "":23# if steps_text == "Going home":24# steps_text = input()25# continue26# sum += int(steps_text)27# steps_text = input()28#29# difference = abs(sum - 10000)30#31# if sum >= 10000:32# print("Goal reached! Good job!")33# print(f"{difference} steps over the goal!")34# else:...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var test = new wpt('www.webpagetest.org');3 if (err) {4 console.log('Error: ' + err);5 } else {6 console.log('Test status: ' + data.statusCode);7 console.log('Test ID: ' + data.data.testId);8 test.getTestResults(data.data.testId, function(err, data) {9 if (err) {10 console.log('Error: ' + err);11 } else {12 console.log('Test status: ' + data.statusCode);13 console.log('Test ID: ' + data.data.testId);14 console.log('Test status: ' + data.data.statusText);15 console.log('Test results: ' + JSON.stringify(data.data.runs[1].firstView.steps));16 }17 });18 }19});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var webPageTest = new wpt('API_KEY');3var url = 'URL';4var options = {5};6webPageTest.runTest(url, options, function(err, data) {7 if (err) return console.error(err);8 console.log('Test initiated for %s. Polling results...', url);9 webPageTest.getTestResults(data.data.testId, function(err, data) {10 if (err) return console.error(err);11 console.log(data.data.runs[1].firstView.steps);12 });13});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var test = new wpt('WPT_API_KEY');3test.runTest(url, function(err, data) {4 if (err) return console.log(err);5 test.getTestResults(data.data.testId, function(err, data) {6 if (err) return console.log(err);7 console.log(data.data.testId);8 });9});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful