Creates parameter with a given name and initializer. If param_name is instance of BlobRefernce - then this blob will be used to store parameter (no any logic will affect it's location). If param_name is instance of a string type, then the final blob will be
(self, param_name, shape, initializer, tags=None)
| 159 | return set(tags) if isinstance(tags, list) else set([tags]) |
| 160 | |
| 161 | def create_param(self, param_name, shape, initializer, tags=None): |
| 162 | """ |
| 163 | Creates parameter with a given name and initializer. |
| 164 | |
| 165 | If param_name is instance of BlobRefernce - then this blob will be used |
| 166 | to store parameter (no any logic will affect it's location). |
| 167 | |
| 168 | If param_name is instance of a string type, then the final blob will |
| 169 | be created in the CurrentNameScope with the respect of all parameter |
| 170 | sharing logic, i.e. 'resolved_name_scope/param_name'. |
| 171 | |
| 172 | Parameter sharing logic is going to override CurrentNameScope according |
| 173 | to the rules that are specified through ParameterSharing contexts, |
| 174 | all ParameterSharing contexts are applied recursively until there are no |
| 175 | extra overrides present, where on each step the best match will be |
| 176 | applied first. |
| 177 | |
| 178 | The following examples should clarify the way ParameterSharing logic |
| 179 | works: |
| 180 | |
| 181 | As an example if this function is called with parameter 'w': |
| 182 | a. Call from some scope 'global_scope' with no Parameter sharing: |
| 183 | 'global_scope/w' |
| 184 | b. Call from scope 'scope_b', with override {'scope_b': 'scope_a'}: |
| 185 | 'scope_a/w' |
| 186 | c. Call from scope 'scope_a', with override {'scope_a': ''}: |
| 187 | 'scope_a/w' |
| 188 | d. Call from scope 'scope_b/shared', with overrides |
| 189 | {'scope_b/shared': 'scope_b', 'scope_b': 'scope_a'}: |
| 190 | 'scope_a/w' |
| 191 | d. Call from scope 'scope_b/unshared', with overrides |
| 192 | {'scope_b/shared': 'scope_b', 'scope_b': 'scope_a'}: |
| 193 | 'scope_a/unshared/w' |
| 194 | """ |
| 195 | # ParameterSharing works only for case when param_name is instance of |
| 196 | # a string type. If param_name is a BlobReference - no attempt for |
| 197 | # ParameterSharing will be applied. |
| 198 | if isinstance(param_name, core.BlobReference): |
| 199 | param_name = str(param_name) |
| 200 | elif isinstance(param_name, str): |
| 201 | # Parameter name will be equal to current Namescope that got |
| 202 | # resolved with the respect of parameter sharing of the scopes. |
| 203 | param_name = parameter_sharing_context.get_parameter_name( |
| 204 | param_name) |
| 205 | else: |
| 206 | raise TypeError("Unsupported type for param_name") |
| 207 | |
| 208 | if param_name in self._parameters_info: |
| 209 | assert self._parameters_info[param_name].shape == shape |
| 210 | return self._parameters_info[param_name].blob |
| 211 | |
| 212 | param_info = initializer.create_param( |
| 213 | param_name=core.BlobReference(param_name), |
| 214 | init_net=self.param_init_net, |
| 215 | shape=shape, |
| 216 | ) |
| 217 | optim_context = OptimizerContext.current() |
| 218 | for tag in self._normalize_tags(tags): |