def __init__(self, is_training=False, **kwargs): super(GCONVRNN, self).__init__(**kwargs) self._train_batch_size = int(kwargs['data'].get('batch_size')) self._test_batch_size = int(kwargs['data'].get('test_batch_size')) self._epoch = int(kwargs['train'].get('epoch')) self._logstep = int(kwargs['train'].get('logstep')) self._checkpoint_secs = int(kwargs['train'].get('checkpoint_secs')) self._data = utils.load_dataset_dcrnn( seq_len=self._model_kwargs.get('seq_len'), horizon=self._model_kwargs.get('horizon'), input_dim=self._model_kwargs.get('input_dim'), mon_ratio=self._mon_ratio, scaler_type=self._kwargs.get('scaler'), is_training=is_training, **self._data_kwargs) for k, v in self._data.items(): if hasattr(v, 'shape'): self._logger.info((k, v.shape)) # Build models. scaler = self._data['scaler'] W = self._data['adj_mx'] laplacian = W / W.max() laplacian = scipy.sparse.csr_matrix(laplacian, dtype=np.float32) lmax = graph.lmax(laplacian) if is_training: self._train_model = Model(is_training=True, laplacian=laplacian, lmax=lmax, batch_size=self._train_batch_size, reuse=True, **self._model_kwargs) else: self._test_model = Model(is_training=False, laplacian=laplacian, lmax=lmax, batch_size=self._test_batch_size, reuse=False, **self._model_kwargs) max_to_keep = self._train_kwargs.get('max_to_keep', 100) self.saver = tf.train.Saver(tf.global_variables(), max_to_keep=max_to_keep) self.summary_writer = tf.summary.FileWriter(self._log_dir) # Log model statistics. total_trainable_parameter = utils.get_total_trainable_parameter_size() self._logger.info('Total number of trainable parameters: {:d}'.format( total_trainable_parameter)) for var in tf.global_variables(): self._logger.debug('{}, {}'.format(var.name, var.get_shape()))
def __init__(self, **kwargs): self._kwargs = kwargs self._data_kwargs = kwargs.get('data') self._model_kwargs = kwargs.get('model') self._train_kwargs = kwargs.get('train') self._test_kwargs = kwargs.get('test') # logging. self._log_dir = self._get_log_dir(kwargs) log_level = self._kwargs.get('log_level', 'INFO') self._logger = utils.get_logger(self._log_dir, __name__, 'info.log', level=log_level) self._writer = tf.summary.FileWriter(self._log_dir) self._logger.info(kwargs) self._mon_ratio = float(self._kwargs.get('mon_ratio')) # Model's args self._seq_len = int(self._model_kwargs.get('seq_len')) self._horizon = int(self._model_kwargs.get('horizon')) self._input_dim = int(self._model_kwargs.get('input_dim')) self._nodes = int(self._model_kwargs.get('num_nodes')) # Test's args self._flow_selection = self._test_kwargs.get('flow_selection') self._test_size = self._test_kwargs.get('test_size') self._run_times = self._test_kwargs.get('run_times') # Data preparation self._day_size = self._data_kwargs.get('day_size') self._data = utils.load_dataset_dcrnn(seq_len=self._model_kwargs.get('seq_len'), horizon=self._model_kwargs.get('horizon'), input_dim=self._model_kwargs.get('input_dim'), mon_ratio=self._mon_ratio, **self._data_kwargs) for k, v in self._data.items(): if hasattr(v, 'shape'): self._logger.info((k, v.shape)) # Build models. scaler = self._data['scaler'] with tf.name_scope('Train'): with tf.variable_scope('DCRNN', reuse=False): self._train_model = DCRNNModel(is_training=True, scaler=scaler, batch_size=self._data_kwargs['batch_size'], adj_mx=self._data['adj_mx'], **self._model_kwargs) with tf.name_scope('Val'): with tf.variable_scope('DCRNN', reuse=True): self._val_model = DCRNNModel(is_training=False, scaler=scaler, batch_size=self._data_kwargs['val_batch_size'], adj_mx=self._data['adj_mx'], **self._model_kwargs) with tf.name_scope('Eval'): with tf.variable_scope('DCRNN', reuse=True): self._eval_model = DCRNNModel(is_training=False, scaler=scaler, batch_size=self._data_kwargs['eval_batch_size'], adj_mx=self._data['adj_mx'], **self._model_kwargs) with tf.name_scope('Test'): with tf.variable_scope('DCRNN', reuse=True): self._test_model = DCRNNModel(is_training=False, scaler=scaler, batch_size=self._data_kwargs['test_batch_size'], adj_mx=self._data['adj_mx'], **self._model_kwargs) # Learning rate. self._lr = tf.get_variable('learning_rate', shape=(), initializer=tf.constant_initializer(0.01), trainable=False) self._new_lr = tf.placeholder(tf.float32, shape=(), name='new_learning_rate') self._lr_update = tf.assign(self._lr, self._new_lr, name='lr_update') # Configure optimizer optimizer_name = self._train_kwargs.get('optimizer', 'adam').lower() epsilon = float(self._train_kwargs.get('epsilon', 1e-3)) optimizer = tf.train.AdamOptimizer(self._lr, epsilon=epsilon, ) if optimizer_name == 'sgd': optimizer = tf.train.GradientDescentOptimizer(self._lr, ) elif optimizer_name == 'amsgrad': optimizer = AMSGrad(self._lr, epsilon=epsilon) # Calculate loss output_dim = self._model_kwargs.get('output_dim') preds = self._train_model.outputs labels = self._train_model.labels[..., :output_dim] null_val = 0. self._loss_fn = masked_mse_loss(scaler, null_val) # self._loss_fn = masked_mae_loss(scaler, null_val) self._train_loss = self._loss_fn(preds=preds, labels=labels) tvars = tf.trainable_variables() grads = tf.gradients(self._train_loss, tvars) max_grad_norm = kwargs['train'].get('max_grad_norm', 1.) grads, _ = tf.clip_by_global_norm(grads, max_grad_norm) global_step = tf.train.get_or_create_global_step() self._train_op = optimizer.apply_gradients(zip(grads, tvars), global_step=global_step, name='train_op') max_to_keep = self._train_kwargs.get('max_to_keep', 100) self._epoch = 0 self._saver = tf.train.Saver(tf.global_variables(), max_to_keep=max_to_keep) # Log model statistics. total_trainable_parameter = utils.get_total_trainable_parameter_size() self._logger.info('Total number of trainable parameters: {:d}'.format(total_trainable_parameter)) for var in tf.global_variables(): self._logger.debug('{}, {}'.format(var.name, var.get_shape()))
def __init__(self, is_training=False, **kwargs): super(DCRNNSupervisor, self).__init__(**kwargs) self._data = utils.load_dataset_dcrnn( seq_len=self._model_kwargs.get('seq_len'), horizon=self._model_kwargs.get('horizon'), input_dim=self._model_kwargs.get('input_dim'), mon_ratio=self._mon_ratio, scaler_type=self._kwargs.get('scaler'), is_training=is_training, **self._data_kwargs) for k, v in self._data.items(): if hasattr(v, 'shape'): self._logger.info((k, v.shape)) # Build models. scaler = self._data['scaler'] if is_training: self.model = DCRNNModel(scaler=scaler, batch_size=self._data_kwargs['batch_size'], adj_mx=self._data['adj_mx'], **self._model_kwargs) else: self.model = DCRNNModel(scaler=scaler, batch_size=1, adj_mx=self._data['adj_mx'], **self._model_kwargs) # Learning rate. self._lr = tf.get_variable('learning_rate', shape=(), initializer=tf.constant_initializer(0.01), trainable=False) self._new_lr = tf.placeholder(tf.float32, shape=(), name='new_learning_rate') self._lr_update = tf.assign(self._lr, self._new_lr, name='lr_update') # Configure optimizer optimizer_name = self._train_kwargs.get('optimizer', 'adam').lower() epsilon = float(self._train_kwargs.get('epsilon', 1e-3)) optimizer = tf.train.AdamOptimizer( self._lr, epsilon=epsilon, ) if optimizer_name == 'sgd': optimizer = tf.train.GradientDescentOptimizer(self._lr, ) elif optimizer_name == 'amsgrad': optimizer = AMSGrad(self._lr, epsilon=epsilon) # Calculate loss output_dim = self._model_kwargs.get('output_dim') preds = self.model.outputs labels = self.model.labels[..., :output_dim] null_val = 0. self._loss_fn = masked_mse_loss(scaler, null_val) # self._loss_fn = masked_mae_loss(scaler, null_val) self._train_loss = self._loss_fn(preds=preds, labels=labels) tvars = tf.trainable_variables() grads = tf.gradients(self._train_loss, tvars) max_grad_norm = kwargs['train'].get('max_grad_norm', 1.) grads, _ = tf.clip_by_global_norm(grads, max_grad_norm) global_step = tf.train.get_or_create_global_step() self._train_op = optimizer.apply_gradients(zip(grads, tvars), global_step=global_step, name='train_op') max_to_keep = self._train_kwargs.get('max_to_keep', 100) self._epoch = 0 self._saver = tf.train.Saver(tf.global_variables(), max_to_keep=max_to_keep) # Log model statistics. total_trainable_parameter = utils.get_total_trainable_parameter_size() self._logger.info('Total number of trainable parameters: {:d}'.format( total_trainable_parameter)) for var in tf.global_variables(): self._logger.debug('{}, {}'.format(var.name, var.get_shape()))