Set hyperparameter by loading the value from a file each time it get called. This is useful for manually tuning some parameters (e.g. learning_rate) without interrupting the training.
| 192 | |
| 193 | |
| 194 | class HumanHyperParamSetter(HyperParamSetter): |
| 195 | """ |
| 196 | Set hyperparameter by loading the value from a file each time it get called. |
| 197 | This is useful for manually tuning some parameters (e.g. learning_rate) |
| 198 | without interrupting the training. |
| 199 | """ |
| 200 | |
| 201 | def __init__(self, param, file_name='hyper.txt'): |
| 202 | """ |
| 203 | Args: |
| 204 | param: same as in :class:`HyperParamSetter`. |
| 205 | file_name(str): a file containing the new value of the parameter. |
| 206 | Each line in the file is a ``k:v`` pair, for example, ``learning_rate:1e-4``. |
| 207 | If the pair is not found, the param will not be changed. |
| 208 | """ |
| 209 | super(HumanHyperParamSetter, self).__init__(param) |
| 210 | self.file_name = os.path.join(logger.get_logger_dir(), file_name) |
| 211 | logger.info("Use {} to set hyperparam: '{}'.".format( |
| 212 | self.file_name, self.param.readable_name)) |
| 213 | |
| 214 | def _get_value_to_set(self): |
| 215 | # ignore if no such file exists |
| 216 | if not os.path.isfile(self.file_name): |
| 217 | return None |
| 218 | try: |
| 219 | with open(self.file_name) as f: |
| 220 | lines = f.readlines() |
| 221 | lines = [s.strip().split(':') for s in lines] |
| 222 | dic = {str(k): float(v) for k, v in lines} |
| 223 | ret = dic[self.param.readable_name] |
| 224 | return ret |
| 225 | except Exception: |
| 226 | logger.warn( |
| 227 | "Cannot find {} in {}".format( |
| 228 | self.param.readable_name, self.file_name)) |
| 229 | return None |
| 230 | |
| 231 | |
| 232 | class ScheduledHyperParamSetter(HyperParamSetter): |
no outgoing calls
no test coverage detected
searching dependent graphs…