"src/vscode:/vscode.git/clone" did not exist on "eaaed92161808c5f27e5378dd128c0488f0cf8e2"
simple_strategy.py 1.4 KB
Newer Older
1
2
3
4
5
6
import json
import logging
import random
import os

from nni.retiarii import Model, submit_models, wait_models
QuanluZhang's avatar
QuanluZhang committed
7
from nni.retiarii.strategy import BaseStrategy
8
9
from nni.retiarii import Sampler

QuanluZhang's avatar
QuanluZhang committed
10

11
12
13
14
15
16
_logger = logging.getLogger(__name__)

class RandomSampler(Sampler):
    def choice(self, candidates, mutator, model, index):
        return random.choice(candidates)

QuanluZhang's avatar
QuanluZhang committed
17
18
19
class SimpleStrategy(BaseStrategy):
    def __init__(self):
        self.name = ''
20

QuanluZhang's avatar
QuanluZhang committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    def run(self, base_model, applied_mutators, trainer):
        try:
            _logger.info('stargety start...')
            while True:
                model = base_model
                _logger.info('apply mutators...')
                _logger.info('mutators: {}'.format(applied_mutators))
                random_sampler = RandomSampler()
                for mutator in applied_mutators:
                    _logger.info('mutate model...')
                    mutator.bind_sampler(random_sampler)
                    model = mutator.apply(model)
                # get and apply training approach
                _logger.info('apply training approach...')
                model.apply_trainer(trainer['modulename'], trainer['args'])
                # run models
                submit_models(model)
                wait_models(model)
                _logger.info('Strategy says:', model.metric)
        except Exception as e:
            _logger.error(logging.exception('message'))