Пример #1
0
    def __init__(self, *args, **kwargs):
        '''
        inputs:
            a_dim: action spaces
            base_dir: the directory that store data, like model, logs, and other data
        '''
        super().__init__()
        self.no_save = bool(kwargs.get('no_save', False))
        self.base_dir = base_dir = kwargs.get('base_dir')
        tf_dtype = str(kwargs.get('tf_dtype'))
        self._tf_data_type = tf.float32 if tf_dtype == 'float32' else tf.float64
        tf.keras.backend.set_floatx(tf_dtype)

        self.device = get_device()

        self.cp_dir, self.log_dir, self.excel_dir = [os.path.join(base_dir, i) for i in ['model', 'log', 'excel']]

        if not self.no_save:
            check_or_create(self.cp_dir, 'checkpoints(models)')

        if 1 == 0:  # Not used
            import pandas as pd
            check_or_create(self.excel_dir, 'excel')
            self.excel_writer = pd.ExcelWriter(self.excel_dir + '/data.xlsx')

        self.global_step = tf.Variable(0, name="global_step", trainable=False, dtype=tf.int64)  # in TF 2.x must be tf.int64, because function set_step need args to be tf.int64.
        self._worker_params_dict = {}
        self._all_params_dict = dict(global_step=self.global_step)
        self.writer = self._create_writer(self.log_dir)  # TODO: Annotation

        if bool(kwargs.get('logger2file', False)):
            set_log_file(log_file=os.path.join(self.log_dir, 'log.txt'))
Пример #2
0
    def __init__(self, log_dir, ids=list(), *args, **kwargs):

        self._ids = ids
        if len(ids) == 0:
            self._is_multi_logger = False
            check_or_create(log_dir, 'logs(summaries)')
            self._writer = SummaryWriter(log_dir)
        else:
            self._is_multi_logger = True
            self._writer = {}
            for id in ids:
                _log_dir = log_dir + f'_{id}'
                check_or_create(_log_dir, 'logs(summaries)')
                self._writer['id'] = SummaryWriter(_log_dir)
Пример #3
0
    def __init__(self, *args, **kwargs):
        '''
        inputs:
            a_dim: action spaces
            is_continuous: action type, refer to whether this control problem is continuous(True) or discrete(False)
            base_dir: the directory that store data, like model, logs, and other data
        '''
        super().__init__()
        base_dir = kwargs.get('base_dir')
        tf_dtype = str(kwargs.get('tf_dtype'))
        self._tf_data_type = tf.float32 if tf_dtype == 'float32' else tf.float64
        tf.keras.backend.set_floatx(tf_dtype)

        tf.random.set_seed(int(kwargs.get('seed', 0)))
        self.device = get_device()

        self.cp_dir, self.log_dir, self.excel_dir = [
            os.path.join(base_dir, i) for i in ['model', 'log', 'excel']
        ]

        self.logger = create_logger(name='rls.algos.base',
                                    logger2file=bool(
                                        kwargs.get('logger2file', False)),
                                    file_name=self.log_dir + 'log.txt')

        check_or_create(self.cp_dir, 'checkpoints')
        check_or_create(self.log_dir, 'logs(summaries)')
        if 1 == 0:  # Not used
            import pandas as pd
            check_or_create(self.excel_dir, 'excel')
            self.excel_writer = pd.ExcelWriter(self.excel_dir + '/data.xlsx')

        self.global_step = tf.Variable(
            0, name="global_step", trainable=False, dtype=tf.int64
        )  # in TF 2.x must be tf.int64, because function set_step need args to be tf.int64.
Пример #4
0
    def __init__(
            self,
            n_copies=1,
            is_save=True,
            base_dir='',
            device: str = 'cpu',
            max_train_step=sys.maxsize,
            max_frame_step=sys.maxsize,
            max_train_episode=sys.maxsize,
            save_frequency=100,
            save2single_file=False,
            n_step_value=4,
            gamma=0.999,
            logger_types=['none'],
            decay_lr=False,
            normalize_vector_obs=False,
            obs_with_pre_action=False,
            oplr_params=dict(),
            rep_net_params={
                'vector_net_params': {
                    'h_dim': 16,
                    'network_type': 'adaptive'  # rls.nn.represents.vectors
                },
                'visual_net_params': {
                    'h_dim': 128,
                    'network_type': 'simple'  # rls.nn.represents.visuals
                },
                'encoder_net_params': {
                    'h_dim': 16,
                    'network_type': 'identity'  # rls.nn.represents.encoders
                },
                'memory_net_params': {
                    'rnn_units': 16,
                    'network_type': 'lstm'
                }
            },
            **kwargs):
        """
        inputs:
            a_dim: action spaces
            base_dir: the directory that store data, like model, logs, and other data
        """
        self.n_copies = n_copies
        self._is_save = is_save
        self._base_dir = base_dir
        self._training_name = os.path.split(self._base_dir)[-1]
        self.device = device
        logger.info(colorize(f"PyTorch Tensor Device: {self.device}"))
        self._max_train_step = max_train_step

        self._should_learn_cond_train_step = Until(max_train_step)
        self._should_learn_cond_frame_step = Until(max_frame_step)
        self._should_learn_cond_train_episode = Until(max_train_episode)
        self._should_save_model = Every(save_frequency)

        self._save2single_file = save2single_file
        self.gamma = gamma
        self._logger_types = logger_types
        self._n_step_value = n_step_value
        self._decay_lr = decay_lr  # TODO: implement
        self._normalize_vector_obs = normalize_vector_obs  # TODO: implement
        self._obs_with_pre_action = obs_with_pre_action
        self._rep_net_params = dict(rep_net_params)
        self._oplr_params = dict(oplr_params)

        super().__init__()

        self.memory_net_params = rep_net_params.get('memory_net_params', {
            'rnn_units': 16,
            'network_type': 'lstm'
        })
        self.use_rnn = self.memory_net_params.get('network_type',
                                                  'identity') != 'identity'

        self.cp_dir, self.log_dir = [
            os.path.join(base_dir, i) for i in ['model', 'log']
        ]

        if self._is_save:
            check_or_create(self.cp_dir, 'checkpoints(models)')

        self._cur_interact_step = th.tensor(0).long().to(self.device)
        self._cur_train_step = th.tensor(0).long().to(self.device)
        self._cur_frame_step = th.tensor(0).long().to(self.device)
        self._cur_episode = th.tensor(0).long().to(self.device)

        self._trainer_modules = {
            '_cur_train_step': self._cur_train_step,
            '_cur_frame_step': self._cur_frame_step,
            '_cur_episode': self._cur_episode
        }

        self._buffer = self._build_buffer()
        self._loggers = self._build_loggers() if self._is_save else list()
Пример #5
0
 def _create_writer(self, log_dir: str) -> tf.summary.SummaryWriter:
     check_or_create(log_dir, 'logs(summaries)')
     return tf.summary.create_file_writer(log_dir)