How to use resolve_param method in localstack

Best Python code snippet using localstack_python

recurrent.py

Source:recurrent.py Github

copy

Full Screen

...58 W_b_init)59 self.W_o, self.b_o = self.resolve_param_pair(W_o, W_o_shape,60 b_o, b_o_shape,61 W_b_init)62 self.W_i = self.resolve_param(W_i, W_i_shape, W_init)63 else:64 self.W_h = self.resolve_param(W_h, W_h_shape, W_init)65 self.W_i = self.resolve_param(W_i, W_i_shape, W_init)66 self.W_o = self.resolve_param(W_o, W_o_shape, W_init)67 self.b_h = self.resolve_param(b_h, b_h_shape, b_init)68 self.b_o = self.resolve_param(b_o, b_o_shape, b_init)69 def calc_output_shape(self):70 return self.incoming_shape[:2] + (self.num_nodes,)71 def check_compatible(self, incoming):72 if not len(incoming.get_output_shape()) == 3:73 raise Exception(('Incoming layer\'s output shape %s '74 'incompatible, try passing it through '75 'a ReshapeLayer or SequenceFlattenLayer first')76 % str(incoming.get_output_shape()))77 def get_base_name(self):78 return 'brnn'79 def recurrence_fn(self, h_prev, x_t):80 return self.nonlin_h(81 tf.matmul(h_prev, self.W_h) +82 tf.matmul(x_t, self.W_i) + self.b_h)83 def output_fn(self, state):84 return self.nonlin_o(85 tf.matmul(state, self.W_o) + self.b_o)86 def get_initial_hidden(self, incoming_var):87 initial_hidden = tf.matmul(88 incoming_var[:, 0, :],89 tf.zeros([self.incoming_shape[2], self.num_nodes]))90 return initial_hidden91 # def get_single_hidden_shape(self):92 # return (1, self.num_nodes)93 def get_input_hidden_var(self, timestep=False):94 if not timestep:95 return self.input_hidden96 else:97 return self.input_timestep_hidden98 def get_output_hidden_var(self):99 return self.output_timestep_hidden100 def get_init_hidden(self):101 return self.init_hidden102 def get_assign_hidden_op(self, zero=False, **kwargs):103 if zero:104 return self.zero_hidden_op105 else:106 return self.assign_hidden_op107 def get_output(self, incoming_var,108 timestep=False,109 input_hidden=False,110 **kwargs):111 if not timestep:112 if not input_hidden:113 initial_hidden = self.get_initial_hidden(incoming_var)114 else:115 self.input_hidden = tf.placeholder(116 tf.float32,117 self.get_initial_hidden(incoming_var).get_shape())118 initial_hidden = self.input_hidden119 incoming_var = tf.transpose(incoming_var, (1, 0, 2))120 states = tf.scan(self.recurrence_fn,121 incoming_var,122 initializer=initial_hidden)123 outputs = tf.map_fn(self.output_fn,124 states)125 outputs = tf.transpose(outputs, (1, 0, 2), name='end_transpose')126 return outputs127 if input_hidden:128 self.input_timestep_hidden = tf.placeholder(129 tf.float32,130 shape=self.get_initial_hidden(incoming_var).get_shape(),131 name='timestep_hidden')132 self.init_hidden = self.get_initial_hidden(incoming_var)133 hidden = self.input_timestep_hidden134 else:135 if self.timestep_hidden is None:136 self.timestep_hidden = self.get_initial_hidden(incoming_var)137 hidden = self.timestep_hidden138 state = self.recurrence_fn(hidden, incoming_var[:, 0, :])139 if input_hidden:140 self.output_timestep_hidden = state141 else:142 self.timestep_hidden = state143 output = self.output_fn(state)144 output = tf.expand_dims(output, 1)145 return output146class LSTMLayer(BasicRNNLayer):147 def __init__(self,148 incoming,149 num_nodes,150 nonlin_i=nonlin.sigmoid,151 nonlin_c=nonlin.tanh,152 nonlin_o=nonlin.relu,153 W_init=init.truncated_normal(),154 b_init=init.constant(),155 U_init=init.truncated_normal(),156 W_b_init=None,157 W_i=None,158 U_i=None,159 W_f=None,160 U_f=None,161 W_g=None,162 U_g=None,163 W_c=None,164 U_c=None,165 W_o=None,166 b_i=None,167 b_f=None,168 b_g=None,169 b_c=None,170 b_o=None,171 **kwargs):172 Layer.__init__(self, [incoming], **kwargs)173 self.incoming_shape = incoming.get_output_shape()174 self.num_nodes = num_nodes175 self.nonlin_i = nonlin_i176 self.nonlin_c = nonlin_c177 self.nonlin_o = nonlin_o178 self.check_compatible(incoming)179 self.output_shape = self.calc_output_shape()180 self.initialize_params(W_i, U_i, W_f, U_f, W_g, U_g,181 W_c, U_c, W_o,182 b_i, b_f, b_g, b_c, b_o,183 W_init, b_init, W_b_init, U_init)184 self.params = {'W_i': self.W_i,185 'U_i': self.U_i,186 'W_f': self.W_f,187 'U_f': self.U_f,188 'W_g': self.W_g,189 'U_g': self.U_g,190 'W_c': self.W_c,191 'U_c': self.U_c,192 'W_o': self.W_o,193 'b_i': self.b_i,194 'b_f': self.b_f,195 'b_g': self.b_g,196 'b_c': self.b_c,197 'b_o': self.b_o}198 self.config.update({199 'num_nodes': num_nodes,200 'nonlin_i': nonlin_i,201 'nonlin_c': nonlin_c,202 'nonlin_o': nonlin_o,203 'W_init': W_init,204 'b_init': b_init,205 'W_b_init': W_b_init206 })207 self.input_hidden = None208 self.input_timestep_hidden = None209 self.timestep_hidden = None210 def initialize_params(self,211 W_i, U_i, W_f, U_f, W_g, U_g,212 W_c, U_c, W_o,213 b_i, b_f, b_g, b_c, b_o,214 W_init, b_init, W_b_init, U_init):215 input_size = self.incoming_shape[2]216 W_i_shape = (input_size, self.num_nodes)217 U_i_shape = (self.num_nodes, self.num_nodes)218 W_f_shape = (input_size, self.num_nodes)219 U_f_shape = (self.num_nodes, self.num_nodes)220 W_g_shape = (input_size, self.num_nodes)221 U_g_shape = (self.num_nodes, self.num_nodes)222 W_c_shape = (input_size, self.num_nodes)223 U_c_shape = (self.num_nodes, self.num_nodes)224 W_o_shape = (self.num_nodes, self.num_nodes)225 b_i_shape = (self.num_nodes,)226 b_f_shape = (self.num_nodes,)227 b_g_shape = (self.num_nodes,)228 b_c_shape = (self.num_nodes,)229 b_o_shape = (self.num_nodes,)230 if W_b_init is not None:231 self.W_i, self.b_i = self.resolve_param_pair(W_i, W_i_shape,232 b_i, b_i_shape,233 W_b_init)234 self.W_f, self.b_f = self.resolve_param_pair(W_f, W_f_shape,235 b_f, b_f_shape,236 W_b_init)237 self.W_g, self.b_g = self.resolve_param_pair(W_g, W_g_shape,238 b_g, b_g_shape,239 W_b_init)240 self.W_c, self.b_c = self.resolve_param_pair(W_c, W_c_shape,241 b_c, b_c_shape,242 W_b_init)243 self.W_o, self.b_o = self.resolve_param_pair(W_o, W_o_shape,244 b_o, b_o_shape,245 W_b_init)246 else:247 self.W_i = self.resolve_param(W_i, W_i_shape, W_init)248 self.b_i = self.resolve_param(b_i, b_i_shape, b_init)249 self.W_f = self.resolve_param(W_f, W_f_shape, W_init)250 self.b_f = self.resolve_param(b_f, b_f_shape, b_init)251 self.W_g = self.resolve_param(W_g, W_g_shape, W_init)252 self.b_g = self.resolve_param(b_g, b_g_shape, b_init)253 self.W_c = self.resolve_param(W_c, W_c_shape, W_init)254 self.b_c = self.resolve_param(b_c, b_c_shape, b_init)255 self.W_o = self.resolve_param(W_o, W_o_shape, W_init)256 self.b_o = self.resolve_param(b_o, b_o_shape, b_init)257 self.U_i = self.resolve_param(U_i, U_i_shape, U_init)258 self.U_f = self.resolve_param(U_f, U_f_shape, U_init)259 self.U_g = self.resolve_param(U_g, U_g_shape, U_init)260 self.U_c = self.resolve_param(U_c, U_c_shape, U_init)261 def get_base_name(self):262 return 'lstm'263 # def get_single_hidden_shape(self):264 # return (2, 1, self.num_nodes)265 def get_initial_hidden(self, incoming_var):266 initial_hidden = tf.matmul(267 incoming_var[:, 0, :],268 tf.zeros([self.incoming_shape[2], self.num_nodes]))269 return tf.pack([initial_hidden, initial_hidden])270 def recurrence_fn(self, h_prev_tuple, x_t):271 h_prev, c_prev = tf.unpack(h_prev_tuple)272 i = self.nonlin_i(273 tf.matmul(x_t, self.W_i,274 name='i_w') +...

Full Screen

Full Screen

intcode.py

Source:intcode.py Github

copy

Full Screen

...49 self.instruction_s = '0' + self.instruction_s50 code = int(self.instruction_s[3:])51 assert code in self.instruction_table.keys(), f"unknown instruction {code}"52 self.instruction = self.instruction_table[code]53 def resolve_param(self, number):54 value = self.check_data(self.pointer + number)55 if number == 3 and self.instruction_s[0] == '2' or number == 2 and self.instruction_s[1] == '2' or number == 1 and self.instruction_s[2] == '2':56 value = self.check_data(self.base + value)57 elif number == 3 and self.instruction_s[0] == '0' or number == 2 and self.instruction_s[1] == '0' or number == 1 and self.instruction_s[2] == '0':58 value = self.check_data(value)59 return value60 def is_running(self):61 return self.data[self.pointer] != 9962 def run(self):63 while self.run_instruction():64 True65 def check_data(self, n):66 if n not in self.data:67 self.data[n] = 068 return self.data[n]69 def run_instruction(self):70 self.check_data(self.pointer)71 self.instruction_s = str(self.data[self.pointer])72 self.parse_instruction()73 # 1 == add74 if self.instruction == 'add': # 0175 if self.instruction_s[0] == '2':76 value = self.check_data(self.pointer + 3)77 self.data[self.base + value] = self.resolve_param(1) + self.resolve_param(2)78 else:79 self.data[self.data[self.pointer + 3]] = self.resolve_param(1) + self.resolve_param(2)80 if self.verbose:81 print(f"{self.name} {self.pointer:3} {self.instruction:13} {self.instruction_s:5} {self.resolve_param(1)} {self.resolve_param(2)}")82 self.pointer += 483 return True84 # 2 == mult85 elif self.instruction == 'mult': # 0286 if self.instruction_s[0] == '2':87 value = self.check_data(self.pointer + 3)88 self.data[self.base + value] = self.resolve_param(1) * self.resolve_param(2)89 else:90 self.data[self.data[self.pointer + 3]] = self.resolve_param(1) * self.resolve_param(2)91 if self.verbose:92 print(f"{self.name} {self.pointer:3} {self.instruction:13} {self.instruction_s:5} {self.resolve_param(1)} {self.resolve_param(2)}")93 self.pointer += 494 return True95 # 3 = input96 elif self.instruction == 'input': # 0397 if self.manual_input:98 self.inputs = [int(input("enter input: ").strip())]99 if len(self.inputs) == 0:100 return False101 if self.instruction_s[2] == '2':102 value = self.check_data(self.pointer + 1)103 self.data[self.base + value] = self.inputs[0]104 else:105 self.data[self.data[self.pointer + 1]] = self.inputs[0]106 self.inputs = self.inputs[1:]107 if self.verbose:108 print(f"{self.name} {self.pointer:3} {self.instruction:13} {self.instruction_s:5} ({self.data[self.pointer + 1]}){self.data[self.data[self.pointer + 1]]}")109 self.pointer += 2110 return True111 # 4 = output112 elif self.instruction == 'output': # 04113 self.outputs += [self.resolve_param(1)]114 if self.verbose:115 print(f"{self.name} {self.pointer:3} {self.instruction:13} {self.instruction_s:5} {self.resolve_param(1):3}")116 self.pointer += 2117 return True118 # 5 = jump-if-true119 elif self.instruction == 'jump-if-true': # 05120 if self.verbose:121 print(f"{self.name} {self.pointer:3} {self.instruction:13} {self.instruction_s:5} {self.resolve_param(1)} {self.resolve_param(2)}")122 if self.resolve_param(1) != 0:123 self.pointer = self.resolve_param(2)124 else:125 self.pointer += 3126 # 6 = jump-if-false127 return True128 elif self.instruction == 'jump-if-false': # 06129 if self.verbose:130 print(f"{self.name} {self.pointer:3} {self.instruction:13} {self.instruction_s:5} {self.resolve_param(1)} {self.resolve_param(2)}")131 if self.resolve_param(1) == 0:132 self.pointer = self.resolve_param(2)133 else:134 self.pointer += 3135 return True136 # 7 = less than137 elif self.instruction == 'less-than': # 07138 if self.resolve_param(1) < self.resolve_param(2):139 if self.instruction_s[0] == '2':140 value = self.check_data(self.pointer + 3)141 self.data[self.base + value] = 1142 else:143 self.data[self.data[self.pointer + 3]] = 1144 else:145 if self.instruction_s[0] == '2':146 value = self.check_data(self.pointer + 3)147 self.data[self.base + value] = 0148 else:149 self.data[self.data[self.pointer + 3]] = 0150 if self.verbose:151 print(f"{self.name} {self.pointer:3} {self.instruction:13} {self.instruction_s:5} {self.data[self.pointer+1]}[{self.resolve_param(1)}] {self.data[self.pointer+2]}[{self.resolve_param(2)}] {self.data[self.pointer + 3]}[{self.data[self.data[self.pointer + 3]]}]")152 self.pointer += 4153 return True154 # 8 = equals155 elif self.instruction == 'equals': # 08156 if self.verbose:157 print(f"{self.name} {self.pointer:3} {self.instruction:13} {self.instruction_s:5} {self.resolve_param(1)} {self.resolve_param(2)} {self.data[self.pointer + 3]}")158 if self.resolve_param(1) == self.resolve_param(2):159 if self.instruction_s[0] == '2':160 value = self.check_data(self.pointer + 3)161 self.data[self.base + value] = 1162 else:163 self.data[self.data[self.pointer + 3]] = 1164 else:165 if self.instruction_s[0] == '2':166 value = self.check_data(self.pointer + 3)167 self.data[self.base + value] = 0168 else:169 self.data[self.data[self.pointer + 3]] = 0170 self.pointer += 4171 return True172 # 9 = adjust-base173 elif self.instruction == 'adjust-base': # 09174 self.base += self.resolve_param(1)175 if self.verbose:176 print(f"{self.name} {self.pointer:3} {self.instruction:13} {self.instruction_s:5} {self.data[self.pointer+1]}[{self.resolve_param(1)}] base={self.base}")177 self.pointer += 2178 return True179 # 99 = halt180 if self.instruction == 'halt': # 99181 if self.verbose:182 print(f"{self.name} {self.pointer:3} {self.instruction:13} {self.instruction_s:5}")183 return False184def test_intcode():185 intcode = Intcode([1002, 5, 2, 5, 99, 33], [], verbose=True) # mult186 intcode.run()187 assert(intcode.data[5] == 66)188 intcode = Intcode([1001, 5, 2, 5, 99, 33], [], verbose=True) # add189 intcode.run()190 assert(intcode.data[5] == 35)...

Full Screen

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 localstack 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