Conditions | 72 |
Total Lines | 412 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like get_dummy_args() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | #!/usr/bin/env python |
||
38 | def get_dummy_args(sequences=None, |
||
39 | outputs_info=None, |
||
40 | non_sequences=None, |
||
41 | n_steps=None, |
||
42 | truncate_gradient=-1, |
||
43 | go_backwards=False, |
||
44 | mode=None, |
||
45 | name=None, |
||
46 | profile=False, |
||
47 | allow_gc=None, |
||
48 | strict=False): |
||
49 | ################################################################## P1> |
||
50 | # check if inputs are just single variables instead of lists |
||
51 | def wrap_into_list(x): |
||
52 | """ |
||
53 | Wrap the input into a list if it is not already a list. |
||
54 | |||
55 | """ |
||
56 | if x is None: |
||
57 | return [] |
||
58 | elif not isinstance(x, (list, tuple)): |
||
59 | return [x] |
||
60 | else: |
||
61 | return list(x) |
||
62 | |||
63 | seqs = wrap_into_list(sequences) |
||
64 | outs_info = wrap_into_list(outputs_info) |
||
65 | |||
66 | # Make sure we get rid of numpy arrays or ints or anything like that |
||
67 | # passed as inputs to scan |
||
68 | non_seqs = [] |
||
69 | for elem in wrap_into_list(non_sequences): |
||
70 | if not isinstance(elem, gof.Variable): |
||
71 | non_seqs.append(tensor.as_tensor_variable(elem)) |
||
72 | else: |
||
73 | non_seqs.append(elem) |
||
74 | |||
75 | # If we provided a known number of steps ( before compilation) |
||
76 | # and if that number is 1 or -1, then we can skip the Scan Op, |
||
77 | # and just apply the inner function once |
||
78 | # To do that we check here to see the nature of n_steps |
||
79 | n_fixed_steps = None |
||
80 | |||
81 | if isinstance(n_steps, (float, integer_types)): |
||
82 | n_fixed_steps = int(n_steps) |
||
83 | else: |
||
84 | try: |
||
85 | n_fixed_steps = opt.get_scalar_constant_value(n_steps) |
||
86 | except tensor.basic.NotScalarConstantError: |
||
87 | n_fixed_steps = None |
||
88 | |||
89 | # Check n_steps is an int |
||
90 | if (hasattr(n_steps, 'dtype') and |
||
91 | str(n_steps.dtype)[:3] not in ('uin', 'int')): |
||
92 | raise ValueError(' n_steps must be an int. dtype provided ' |
||
93 | 'is %s' % n_steps.dtype) |
||
94 | |||
95 | # compute number of sequences and number of outputs |
||
96 | n_seqs = len(seqs) |
||
97 | n_outs = len(outs_info) |
||
98 | |||
99 | return_steps = OrderedDict() |
||
100 | # wrap sequences in a dictionary if they are not already dictionaries |
||
101 | for i in xrange(n_seqs): |
||
102 | if not isinstance(seqs[i], dict): |
||
103 | seqs[i] = OrderedDict([('input', seqs[i]), ('taps', [0])]) |
||
104 | elif seqs[i].get('taps', None) is not None: |
||
105 | seqs[i]['taps'] = wrap_into_list(seqs[i]['taps']) |
||
106 | elif seqs[i].get('taps', None) is None: |
||
107 | # seqs dictionary does not have the ``taps`` key |
||
108 | seqs[i]['taps'] = [0] |
||
109 | |||
110 | # wrap outputs info in a dictionary if they are not already in one |
||
111 | for i in xrange(n_outs): |
||
112 | if outs_info[i] is not None: |
||
113 | if isinstance(outs_info[i], dict): |
||
114 | # DEPRECATED : |
||
115 | if outs_info[i].get('return_steps', None) is not None: |
||
116 | raise ValueError( |
||
117 | "Using `return_steps` has been deprecated. " |
||
118 | "Simply select the entries you need using a " |
||
119 | "subtensor. Scan will optimize memory " |
||
120 | "consumption, so do not worry about that.") |
||
121 | # END |
||
122 | |||
123 | if not isinstance(outs_info[i], dict): |
||
124 | # by default any output has a tap value of -1 |
||
125 | outs_info[i] = OrderedDict([('initial', outs_info[i]), ('taps', [-1])]) |
||
126 | elif (outs_info[i].get('initial', None) is None and |
||
127 | outs_info[i].get('taps', None) is not None): |
||
128 | # ^ no initial state but taps provided |
||
129 | raise ValueError(('If you are using slices of an output ' |
||
130 | 'you need to provide a initial state ' |
||
131 | 'for it'), outs_info[i]) |
||
132 | elif (outs_info[i].get('initial', None) is not None and |
||
133 | outs_info[i].get('taps', None) is None): |
||
134 | # ^ initial state but taps not provided |
||
135 | if 'taps' in outs_info[i]: |
||
136 | # ^ explicitly provided a None for taps |
||
137 | _logger.warning('Output %s ( index %d) has a initial ' |
||
138 | 'state but taps is explicitly set to None ', |
||
139 | getattr(outs_info[i]['initial'], 'name', 'None'), |
||
140 | i) |
||
141 | outs_info[i]['taps'] = [-1] |
||
142 | else: |
||
143 | # if a None is provided as the output info we replace it |
||
144 | # with an empty OrdereDict() to simplify handling |
||
145 | outs_info[i] = OrderedDict() |
||
146 | |||
147 | ## |
||
148 | # Step 2. Generate inputs and outputs of the inner functions |
||
149 | # for compiling a dummy function (Iteration #1) |
||
150 | ## |
||
151 | |||
152 | # create theano inputs for the recursive function |
||
153 | # note : this is a first batch of possible inputs that will |
||
154 | # be compiled in a dummy function; we used this dummy |
||
155 | # function to detect shared variables and their updates |
||
156 | # and to construct a new and complete list of inputs and |
||
157 | # outputs |
||
158 | |||
159 | n_seqs = 0 |
||
160 | scan_seqs = [] # Variables passed as inputs to the scan op |
||
161 | inner_seqs = [] # Variables passed as inputs to the inner function |
||
162 | inner_slices = [] # Actual slices if scan is removed from the picture |
||
163 | # go through sequences picking up time slices as needed |
||
164 | for i, seq in enumerate(seqs): |
||
165 | # Note that you can have something like no taps for |
||
166 | # a sequence, though is highly unlikely in practice |
||
167 | if 'taps' in seq: |
||
168 | # go through the indicated slice |
||
169 | mintap = numpy.min(seq['taps']) |
||
170 | maxtap = numpy.max(seq['taps']) |
||
171 | for k in seq['taps']: |
||
172 | # create one slice of the input |
||
173 | # Later on, if we decide not to use scan because we are |
||
174 | # going for just one step, it makes things easier if we |
||
175 | # compute the correct outputs here. This way we can use |
||
176 | # the output of the lambda expression directly to replace |
||
177 | # the output of scan. |
||
178 | |||
179 | # If not we need to use copies, that will be replaced at |
||
180 | # each frame by the corresponding slice |
||
181 | actual_slice = seq['input'][k - mintap] |
||
182 | _seq_val = tensor.as_tensor_variable(seq['input']) |
||
183 | _seq_val_slice = _seq_val[k - mintap] |
||
184 | nw_slice = _seq_val_slice.type() |
||
185 | |||
186 | # Try to transfer test_value to the new variable |
||
187 | if config.compute_test_value != 'off': |
||
188 | try: |
||
189 | nw_slice.tag.test_value = gof.Op._get_test_value( |
||
190 | _seq_val_slice) |
||
191 | except AttributeError as e: |
||
192 | if config.compute_test_value != 'ignore': |
||
193 | # No need to print a warning or raise an error now, |
||
194 | # it will be done when fn will be called. |
||
195 | _logger.info(('Cannot compute test value for ' |
||
196 | 'the inner function of scan, input value ' |
||
197 | 'missing %s'), e) |
||
198 | |||
199 | # Add names to slices for debugging and pretty printing .. |
||
200 | # that is if the input already has a name |
||
201 | if getattr(seq['input'], 'name', None) is not None: |
||
202 | if k > 0: |
||
203 | nw_name = seq['input'].name + '[t+%d]' % k |
||
204 | elif k == 0: |
||
205 | nw_name = seq['input'].name + '[t]' |
||
206 | else: |
||
207 | nw_name = seq['input'].name + '[t%d]' % k |
||
208 | nw_slice.name = nw_name |
||
209 | |||
210 | # We cut the sequence such that seq[i] to correspond to |
||
211 | # seq[i-k]. For the purposes of cutting the sequences, we |
||
212 | # need to pretend tap 0 is used to avoid cutting the sequences |
||
213 | # too long if the taps are all lower or all higher than 0. |
||
214 | maxtap_proxy = max(maxtap, 0) |
||
215 | mintap_proxy = min(mintap, 0) |
||
216 | start = (k - mintap_proxy) |
||
217 | if k == maxtap_proxy: |
||
218 | nw_seq = seq['input'][start:] |
||
219 | else: |
||
220 | end = -(maxtap_proxy - k) |
||
221 | nw_seq = seq['input'][start:end] |
||
222 | |||
223 | if go_backwards: |
||
224 | nw_seq = nw_seq[::-1] |
||
225 | |||
226 | scan_seqs.append(nw_seq) |
||
227 | inner_seqs.append(nw_slice) |
||
228 | inner_slices.append(actual_slice) |
||
229 | n_seqs += 1 |
||
230 | |||
231 | # Since we've added all sequences now we need to level them up based on |
||
232 | # n_steps or their different shapes |
||
233 | lengths_vec = [] |
||
234 | for seq in scan_seqs: |
||
235 | lengths_vec.append(seq.shape[0]) |
||
236 | |||
237 | if not scan_utils.isNaN_or_Inf_or_None(n_steps): |
||
238 | # ^ N_steps should also be considered |
||
239 | lengths_vec.append(tensor.as_tensor(n_steps)) |
||
240 | |||
241 | if len(lengths_vec) == 0: |
||
242 | # ^ No information about the number of steps |
||
243 | raise ValueError('No information about the number of steps ' |
||
244 | 'provided. Either provide a value for ' |
||
245 | 'n_steps argument of scan or provide an input ' |
||
246 | 'sequence') |
||
247 | |||
248 | # If the user has provided the number of steps, do that regardless ( and |
||
249 | # raise an error if the sequences are not long enough ) |
||
250 | if scan_utils.isNaN_or_Inf_or_None(n_steps): |
||
251 | actual_n_steps = lengths_vec[0] |
||
252 | for contestant in lengths_vec[1:]: |
||
253 | actual_n_steps = tensor.minimum(actual_n_steps, contestant) |
||
254 | else: |
||
255 | actual_n_steps = tensor.as_tensor(n_steps) |
||
256 | |||
257 | # Add names -- it helps a lot when debugging |
||
258 | |||
259 | for (nw_seq, seq) in zip(scan_seqs, seqs): |
||
260 | if getattr(seq['input'], 'name', None) is not None: |
||
261 | nw_seq.name = seq['input'].name + '[%d:]' % k |
||
262 | |||
263 | scan_seqs = [seq[:actual_n_steps] for seq in scan_seqs] |
||
264 | # Conventions : |
||
265 | # mit_mot = multiple input taps, multiple output taps ( only provided |
||
266 | # by the gradient function ) |
||
267 | # mit_sot = multiple input taps, single output tap (t + 0) |
||
268 | # sit_sot = single input tap, single output tap (t + 0) |
||
269 | # nit_sot = no input tap, single output tap (t + 0) |
||
270 | |||
271 | # MIT_MOT -- not provided by the user only by the grad function |
||
272 | n_mit_mot = 0 |
||
273 | n_mit_mot_outs = 0 |
||
274 | mit_mot_scan_inputs = [] |
||
275 | mit_mot_inner_inputs = [] |
||
276 | mit_mot_inner_outputs = [] |
||
277 | mit_mot_out_slices = [] |
||
278 | mit_mot_rightOrder = [] |
||
279 | |||
280 | # SIT_SOT -- provided by the user |
||
281 | n_mit_sot = 0 |
||
282 | mit_sot_scan_inputs = [] |
||
283 | mit_sot_inner_inputs = [] |
||
284 | mit_sot_inner_slices = [] |
||
285 | mit_sot_inner_outputs = [] |
||
286 | mit_sot_return_steps = OrderedDict() |
||
287 | mit_sot_tap_array = [] |
||
288 | mit_sot_rightOrder = [] |
||
289 | |||
290 | n_sit_sot = 0 |
||
291 | sit_sot_scan_inputs = [] |
||
292 | sit_sot_inner_inputs = [] |
||
293 | sit_sot_inner_slices = [] |
||
294 | sit_sot_inner_outputs = [] |
||
295 | sit_sot_return_steps = OrderedDict() |
||
296 | sit_sot_rightOrder = [] |
||
297 | |||
298 | # go through outputs picking up time slices as needed |
||
299 | for i, init_out in enumerate(outs_info): |
||
300 | # Note that our convention dictates that if an output uses |
||
301 | # just the previous time step, as a initial state we will only |
||
302 | # provide a tensor of the same dimension as one time step; This |
||
303 | # makes code much cleaner for those who do not use taps. Otherwise |
||
304 | # they would always had to shape_padleft the initial state .. |
||
305 | # which is ugly |
||
306 | if init_out.get('taps', None) == [-1]: |
||
307 | |||
308 | actual_arg = init_out['initial'] |
||
309 | if not isinstance(actual_arg, tensor.Variable): |
||
310 | actual_arg = tensor.as_tensor_variable(actual_arg) |
||
311 | arg = safe_new(actual_arg) |
||
312 | if isinstance(arg, tensor.Constant): |
||
313 | # safe new returns a clone of the constants, but that is not |
||
314 | # what we need for initial states |
||
315 | arg = arg.type() |
||
316 | |||
317 | # Try to transfer test_value to the new variable |
||
318 | if config.compute_test_value != 'off': |
||
319 | try: |
||
320 | arg.tag.test_value = gof.Op._get_test_value(actual_arg) |
||
321 | except AttributeError as e: |
||
322 | if config.compute_test_value != 'ignore': |
||
323 | # No need to print a warning or raise an error now, |
||
324 | # it will be done when fn will be called. |
||
325 | _logger.info(('Cannot compute test value for the ' |
||
326 | 'inner function of scan, input value missing %s'), |
||
327 | e) |
||
328 | |||
329 | if getattr(init_out['initial'], 'name', None) is not None: |
||
330 | arg.name = init_out['initial'].name + '[t-1]' |
||
331 | |||
332 | # We need now to allocate space for storing the output and copy |
||
333 | # the initial state over. We do this using the expand function |
||
334 | # defined in scan utils |
||
335 | sit_sot_scan_inputs.append( |
||
336 | scan_utils.expand_empty( |
||
337 | tensor.unbroadcast( |
||
338 | tensor.shape_padleft(actual_arg), 0), |
||
339 | actual_n_steps |
||
340 | )) |
||
341 | |||
342 | sit_sot_inner_slices.append(actual_arg) |
||
343 | if i in return_steps: |
||
344 | sit_sot_return_steps[n_sit_sot] = return_steps[i] |
||
345 | sit_sot_inner_inputs.append(arg) |
||
346 | sit_sot_rightOrder.append(i) |
||
347 | n_sit_sot += 1 |
||
348 | |||
349 | elif init_out.get('taps', None): |
||
350 | |||
351 | if numpy.any(numpy.array(init_out.get('taps', [])) > 0): |
||
352 | # Make sure we do not have requests for future values of a |
||
353 | # sequence we can not provide such values |
||
354 | raise ValueError('Can not use future taps of outputs', |
||
355 | init_out) |
||
356 | # go through the taps |
||
357 | mintap = abs(numpy.min(init_out['taps'])) |
||
358 | mit_sot_tap_array.append(init_out['taps']) |
||
359 | idx_offset = abs(numpy.min(init_out['taps'])) |
||
360 | # Sequence |
||
361 | mit_sot_scan_inputs.append( |
||
362 | scan_utils.expand_empty(init_out['initial'][:mintap], |
||
363 | actual_n_steps)) |
||
364 | |||
365 | if i in return_steps: |
||
366 | mit_sot_return_steps[n_mit_sot] = return_steps[i] |
||
367 | mit_sot_rightOrder.append(i) |
||
368 | n_mit_sot += 1 |
||
369 | for k in init_out['taps']: |
||
370 | # create a new slice |
||
371 | actual_nw_slice = init_out['initial'][k + mintap] |
||
372 | _init_out_var = tensor.as_tensor_variable(init_out['initial']) |
||
373 | _init_out_var_slice = _init_out_var[k + mintap] |
||
374 | nw_slice = _init_out_var_slice.type() |
||
375 | |||
376 | # Try to transfer test_value to the new variable |
||
377 | if config.compute_test_value != 'off': |
||
378 | try: |
||
379 | nw_slice.tag.test_value = gof.Op._get_test_value( |
||
380 | _init_out_var_slice) |
||
381 | except AttributeError as e: |
||
382 | if config.compute_test_value != 'ignore': |
||
383 | # No need to print a warning or raise an error now, |
||
384 | # it will be done when fn will be called. |
||
385 | _logger.info(('Cannot compute test value for ' |
||
386 | 'the inner function of scan, input value ' |
||
387 | 'missing. %s'), e) |
||
388 | |||
389 | # give it a name or debugging and pretty printing |
||
390 | if getattr(init_out['initial'], 'name', None) is not None: |
||
391 | if k > 0: |
||
392 | nw_slice.name = (init_out['initial'].name + |
||
393 | '[t+%d]' % k) |
||
394 | elif k == 0: |
||
395 | nw_slice.name = init_out['initial'].name + '[t]' |
||
396 | else: |
||
397 | nw_slice.name = (init_out['initial'].name + |
||
398 | '[t%d]' % k) |
||
399 | mit_sot_inner_inputs.append(nw_slice) |
||
400 | mit_sot_inner_slices.append(actual_nw_slice) |
||
401 | # NOTE: there is another case, in which we do not want to provide |
||
402 | # any previous value of the output to the inner function (i.e. |
||
403 | # a map); in that case we do not have to do anything .. |
||
404 | |||
405 | # Re-order args |
||
406 | max_mit_sot = numpy.max([-1] + mit_sot_rightOrder) + 1 |
||
407 | max_sit_sot = numpy.max([-1] + sit_sot_rightOrder) + 1 |
||
408 | n_elems = numpy.max([max_mit_sot, max_sit_sot]) |
||
409 | _ordered_args = [[] for x in xrange(n_elems)] |
||
410 | offset = 0 |
||
411 | for idx in xrange(n_mit_sot): |
||
412 | n_inputs = len(mit_sot_tap_array[idx]) |
||
413 | if n_fixed_steps in [1, -1]: |
||
414 | _ordered_args[mit_sot_rightOrder[idx]] = \ |
||
415 | mit_sot_inner_slices[offset:offset + n_inputs] |
||
416 | else: |
||
417 | _ordered_args[mit_sot_rightOrder[idx]] = \ |
||
418 | mit_sot_inner_inputs[offset:offset + n_inputs] |
||
419 | offset += n_inputs |
||
420 | |||
421 | for idx in xrange(n_sit_sot): |
||
422 | if n_fixed_steps in [1, -1]: |
||
423 | _ordered_args[sit_sot_rightOrder[idx]] = \ |
||
424 | [sit_sot_inner_slices[idx]] |
||
425 | else: |
||
426 | _ordered_args[sit_sot_rightOrder[idx]] = \ |
||
427 | [sit_sot_inner_inputs[idx]] |
||
428 | |||
429 | ordered_args = [] |
||
430 | for ls in _ordered_args: |
||
431 | ordered_args += ls |
||
432 | if n_fixed_steps in [1, -1]: |
||
433 | args = (inner_slices + |
||
434 | ordered_args + |
||
435 | non_seqs) |
||
436 | |||
437 | else: |
||
438 | args = (inner_seqs + |
||
439 | ordered_args + |
||
440 | non_seqs) |
||
441 | |||
442 | # add only the non-shared variables and non-constants to the arguments of |
||
443 | # the dummy function [ a function should not get shared variables or |
||
444 | # constants as input ] |
||
445 | dummy_args = [arg for arg in args |
||
446 | if (not isinstance(arg, SharedVariable) and |
||
447 | not isinstance(arg, tensor.Constant))] |
||
448 | ################################################################## P1< |
||
449 | return dummy_args, locals() |
||
450 | |||
860 | return (scan_out_list, update_map) |