| 10 | |
| 11 | |
| 12 | class Seq2SeqModelHelper(ModelHelper): |
| 13 | |
| 14 | def __init__(self, init_params=True, **kwargs): |
| 15 | arg_scope = { |
| 16 | 'use_cudnn': kwargs.pop('use_cudnn', True), |
| 17 | 'cudnn_exhaustive_search': kwargs.pop('cudnn_exhaustive_search', False), |
| 18 | 'order': 'NHWC', |
| 19 | } |
| 20 | if kwargs.get('ws_nbytes_limit', None): |
| 21 | arg_scope['ws_nbytes_limit'] = kwargs.pop('ws_nbytes_limit') |
| 22 | |
| 23 | super().__init__(init_params=init_params, arg_scope=arg_scope, **kwargs) |
| 24 | self.non_trainable_params = [] |
| 25 | |
| 26 | def AddParam(self, name, init=None, init_value=None, trainable=True): |
| 27 | """Adds a parameter to the model's net and it's initializer if needed |
| 28 | |
| 29 | Args: |
| 30 | init: a tuple (<initialization_op_name>, <initialization_op_kwargs>) |
| 31 | init_value: int, float or str. Can be used instead of `init` as a |
| 32 | simple constant initializer |
| 33 | trainable: bool, whether to compute gradient for this param or not |
| 34 | """ |
| 35 | if init_value is not None: |
| 36 | assert init is None |
| 37 | assert type(init_value) in [int, float, str] |
| 38 | init = ('ConstantFill', dict( |
| 39 | shape=[1], |
| 40 | value=init_value, |
| 41 | )) |
| 42 | |
| 43 | if self.init_params: |
| 44 | param = self.param_init_net.__getattr__(init[0])( |
| 45 | [], |
| 46 | name, |
| 47 | **init[1] |
| 48 | ) |
| 49 | else: |
| 50 | param = self.net.AddExternalInput(name) |
| 51 | |
| 52 | if trainable: |
| 53 | self.params.append(param) |
| 54 | else: |
| 55 | self.non_trainable_params.append(param) |
| 56 | |
| 57 | return param |
| 58 | |
| 59 | def GetNonTrainableParams(self, namescope=None): |
| 60 | ''' |
| 61 | Returns the params in current namescope |
| 62 | ''' |
| 63 | if namescope is None: |
| 64 | namescope = scope.CurrentNameScope() |
| 65 | else: |
| 66 | if not namescope.endswith(scope._NAMESCOPE_SEPARATOR): |
| 67 | namescope += scope._NAMESCOPE_SEPARATOR |
| 68 | |
| 69 | if namescope == '': |
no outgoing calls
no test coverage detected
searching dependent graphs…