def continue_training( self, training_trackers: List[DialogueStateTracker], domain: Domain, **kwargs: Any, ) -> None: """Continues training an already trained policy.""" # takes the new example labelled and learns it # via taking `epochs` samples of n_batch-1 parts of the training data, # inserting our new example and learning them. this means that we can # ask the network to fit the example without overemphasising # its importance (and therefore throwing off the biases) batch_size = kwargs.get("batch_size", 5) epochs = kwargs.get("epochs", 50) with self.graph.as_default(), self.session.as_default(): for _ in range(epochs): training_data = self._training_data_for_continue_training( batch_size, training_trackers, domain ) # fit to one extra example using updated trackers self.model.fit( training_data.X, training_data.y, epochs=self.current_epoch + 1, batch_size=len(training_data.y), verbose=obtain_verbosity(), initial_epoch=self.current_epoch, ) self.current_epoch += 1
def model_architecture( self, input_shape: Tuple[int, int], output_shape: Tuple[int, Optional[int]] ) -> tf.keras.models.Sequential: """Build a keras model and return a compiled model.""" # from tensorflow.keras.models import Sequential # from tensorflow.keras.layers import ( from keras.models import Sequential from keras.layers import ( Masking, LSTM, Dense, TimeDistributed, Activation, ) # Build Model model = Sequential() # the shape of the y vector of the labels, # determines which output from rnn will be used # to calculate the loss if len(output_shape) == 1: # y is (num examples, num features) so # only the last output from the rnn is used to # calculate the loss model.add(Masking(mask_value=-1, input_shape=input_shape)) model.add(LSTM(self.rnn_size, dropout=0.2)) model.add(Dense(input_dim=self.rnn_size, units=output_shape[-1])) elif len(output_shape) == 2: # y is (num examples, max_dialogue_len, num features) so # all the outputs from the rnn are used to # calculate the loss, therefore a sequence is returned and # time distributed layer is used # the first value in input_shape is max dialogue_len, # it is set to None, to allow dynamic_rnn creation # during prediction model.add(Masking(mask_value=-1, input_shape=(None, input_shape[1]))) model.add(LSTM(self.rnn_size, return_sequences=True, dropout=0.2)) model.add(TimeDistributed(Dense(units=output_shape[-1]))) else: raise ValueError( "Cannot construct the model because" "length of output_shape = {} " "should be 1 or 2." "".format(len(output_shape)) ) model.add(Activation("softmax")) model.compile( loss="categorical_crossentropy", optimizer="rmsprop", metrics=["accuracy"] ) if common_utils.obtain_verbosity() > 0: model.summary() return model
def train( self, training_trackers: List[DialogueStateTracker], domain: Domain, **kwargs: Any, ) -> None: # set numpy random seed np.random.seed(self.random_seed) training_data = self.featurize_for_training(training_trackers, domain, **kwargs) # noinspection PyPep8Naming shuffled_X, shuffled_y = training_data.shuffled_X_y() self.graph = tf.Graph() with self.graph.as_default(): # set random seed in tf tf.set_random_seed(self.random_seed) self.session = tf.compat.v1.Session(config=self._tf_config) with self.session.as_default(): if self.model is None: self.model = self.model_architecture( shuffled_X.shape[1:], shuffled_y.shape[1:] ) logger.info( "Fitting model with {} total samples and a " "validation split of {}" "".format(training_data.num_examples(), self.validation_split) ) # filter out kwargs that cannot be passed to fit self._train_params = self._get_valid_params( self.model.fit, **self._train_params ) self.model.fit( shuffled_X, shuffled_y, epochs=self.epochs, batch_size=self.batch_size, shuffle=False, verbose=obtain_verbosity(), **self._train_params, ) # the default parameter for epochs in keras fit is 1 self.current_epoch = self.defaults.get("epochs", 1) logger.info("Done fitting keras policy model")
def train( self, training_trackers: List[DialogueStateTracker], domain: Domain, **kwargs: Any, ) -> None: current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S") train_log_dir = 'logs/gradient_tape/' + current_time + '/train' test_log_dir = 'logs/gradient_tape/' + current_time + '/test' train_summary_writer = tf.summary.create_file_writer(train_log_dir) test_summary_writer = tf.summary.create_file_writer(test_log_dir) np.random.seed(self.random_seed) tf.random.set_seed(self.random_seed) training_data = self.featurize_for_training(training_trackers, domain, **kwargs) # noinspection PyPep8Naming shuffled_X, shuffled_y = training_data.shuffled_X_y() if self.model is None: self.model = self.model_architecture(shuffled_X.shape[1:], shuffled_y.shape[1:]) logger.debug( f"Fitting model with {training_data.num_examples()} total samples and a " f"validation split of {self.validation_split}.") # filter out kwargs that cannot be passed to fit self._train_params = self._get_valid_params(self.model.fit, **self._train_params) self.model.fit( shuffled_X, shuffled_y, epochs=self.epochs, batch_size=self.batch_size, shuffle=False, verbose=common_utils.obtain_verbosity(), **self._train_params, ) self.current_epoch = self.epochs logger.debug("Done fitting Keras Policy model.")
def train( self, training_trackers: List[DialogueStateTracker], domain: Domain, **kwargs: Any, ) -> None: np.random.seed(self.random_seed) tf.random.set_seed(self.random_seed) training_data = self.featurize_for_training(training_trackers, domain, **kwargs) # noinspection PyPep8Naming shuffled_X, shuffled_y = training_data.shuffled_X_y() if self.model is None: self.model = self.model_architecture( shuffled_X.shape[1:], shuffled_y.shape[1:] ) logger.debug( f"Fitting model with {training_data.num_examples()} total samples and a " f"validation split of {self.validation_split}." ) # filter out kwargs that cannot be passed to fit self._train_params = self._get_valid_params( self.model.fit, **self._train_params ) self.model.fit( shuffled_X, shuffled_y, epochs=self.epochs, batch_size=self.batch_size, shuffle=False, verbose=common_utils.obtain_verbosity(), **self._train_params, ) self.current_epoch = self.epochs logger.debug("Done fitting Keras Policy model.")