def predict(self, input_data): row1d = is_row1d(self.input_layer) result = format_data(input_data, row1d=row1d) for layer in self.layers: result = layer.output(result) return result
def _train(self, input_train, target_train=None, input_test=None, target_test=None, epochs=100, epsilon=None): """ Main method for the Neural Network training. """ # ----------- Pre-format target data ----------- # input_row1d = is_row1d(self.input_layer) input_train = format_data(input_train, row1d=input_row1d) target_row1d = is_row1d(self.output_layer) target_train = format_data(target_train, row1d=target_row1d) if input_test is not None: input_test = format_data(input_test, row1d=input_row1d) if target_test is not None: target_test = format_data(target_test, row1d=target_row1d) # ----------- Validate input values ----------- # if epsilon is not None and epochs <= 2: raise ValueError("Network should train at teast 3 epochs before " "check the difference between errors") # ----------- Predefine parameters ----------- # self.epoch = 1 show_epoch = self.show_epoch logs = self.logs compute_error_out = input_test is not None and target_test is not None predict = self.predict last_epoch_shown = 0 if epsilon is not None: iterepochs = iter_until_converge(self, epsilon, max_epochs=epochs) if isinstance(show_epoch, six.string_types): show_epoch = 100 logs.warning( "Can't use `show_epoch` value in converging " "mode. Set up 100 to `show_epoch` property " "by default." ) else: iterepochs = range(1, epochs + 1) show_epoch = parse_show_epoch_property(show_epoch, epochs) epoch_summary = show_epoch_summary(self, show_epoch) # ----------- Train process ----------- # logs.header("Start train") logs.log("TRAIN", "Train data size: {}".format(input_train.shape[0])) if input_test is not None: logs.log("TRAIN", "Validation data size: {}" "".format(input_test.shape[0])) if epsilon is None: logs.log("TRAIN", "Total epochs: {}".format(epochs)) else: logs.log("TRAIN", "Max epochs: {}".format(epochs)) logs.empty() # Optimizations for long loops errors = self.errors_in errors_out = self.errors_out shuffle_data = self.shuffle_data error_func = self.error train_epoch = self.train_epoch train_epoch_end_signal = self.train_epoch_end_signal train_end_signal = self.train_end_signal self.input_train = input_train self.target_train = target_train for epoch in iterepochs: self.epoch = epoch epoch_start_time = time() if shuffle_data: input_train, target_train = shuffle_train_data(input_train, target_train) self.input_train = input_train self.target_train = target_train try: error = train_epoch(input_train, target_train) if compute_error_out: predicted_test = predict(input_test) error_out = error_func(predicted_test, target_test) errors_out.append(error_out) errors.append(error) self.train_epoch_time = time() - epoch_start_time if epoch % show_epoch == 0 or epoch == 1: show_epoch = next(epoch_summary) last_epoch_shown = epoch if train_epoch_end_signal is not None: train_epoch_end_signal(self) except StopIteration as err: logs.log("TRAIN", "Epoch #{} stopped. {}" "".format(epoch, str(err))) break if epoch != last_epoch_shown: show_training_summary(self) if train_end_signal is not None: train_end_signal(self) logs.log("TRAIN", "End train")
def _train(self, input_train, target_train=None, input_test=None, target_test=None, epochs=None, epsilon=None): # ----------- Pre-format target data ----------- # input_row1d = is_row1d(self.input_layer) input_train = format_data(input_train, row1d=input_row1d) target_row1d = is_row1d(self.output_layer) target_train = format_data(target_train, row1d=target_row1d) if input_test is not None: input_test = format_data(input_test, row1d=input_row1d) if target_test is not None: target_test = format_data(target_test, row1d=target_row1d) # ----------- Validation ----------- # if epochs is None and epsilon is None: epochs = 100 if epochs is not None and epsilon is not None: raise ValueError("You can't user `epochs` and `epsilon` " "attributes in one train process.") # ----------- Predefine parameters ----------- # if epochs is not None: self.epoch = 0 iterepochs = range(self.epoch, epochs) last_epoch = epochs - 1 predict = self.predict compute_error_out = (input_test is not None and target_test is not None) if epsilon is not None: iterepochs = iter_until_converge(self, epsilon) last_epoch = None predict = None compute_error_out = None # ----------- Train process ----------- # logs = self.logs logs.header("Start train") logs.log("TRAIN", "Train data size: {}".format(input_train.shape[0])) logs.log("TRAIN", "Number of input features: {}".format( input_train.shape[1] )) if epochs is not None: logs.log("TRAIN", "Total epochs: {}".format(epochs)) logs.empty() # Optimizations for long loops. Set constant properties to # variables. errors = self.errors_in errors_out = self.errors_out show_epoch = self.show_epoch shuffle_data = self.shuffle_data # Methods error_func = self.error train_epoch = self.train_epoch train_epoch_end_signal = self.train_epoch_end_signal train_end_signal = self.train_end_signal for epoch in iterepochs: epoch_start_time = time() if shuffle_data: if target_train is not None: input_train, target_train = shuffle(input_train, target_train) else: input_train, = shuffle(input_train) self.input_train = input_train self.target_train = target_train try: error = train_epoch(input_train, target_train) if compute_error_out: error_out = error_func(predict(input_test), target_test) errors_out.append(error_out) errors.append(error) self.train_epoch_time = time() - epoch_start_time if epoch % show_epoch == 0 or epoch == last_epoch: logs.data(""" Epoch {epoch} Error in: {error} Error out: {error_out} Epoch time: {epoch_time} sec """.format( epoch=self.epoch, error=self.last_error_in() or '-', error_out=self.last_error_out() or '-', epoch_time=round(self.train_epoch_time, 5) )) if train_epoch_end_signal is not None: train_epoch_end_signal(self) self.epoch = epoch + 1 except StopIteration as err: logs.log("TRAIN", "Epoch #{} stopped. {}".format(self.epoch, str(err))) break if train_end_signal is not None: train_end_signal(self) logs.log("TRAIN", "End train")
def _train(self, input_train, target_train=None, input_test=None, target_test=None, epochs=None, epsilon=None): # ----------- Pre-format target data ----------- # input_row1d = is_row1d(self.input_layer) input_train = format_data(input_train, row1d=input_row1d) target_row1d = is_row1d(self.output_layer) target_train = format_data(target_train, row1d=target_row1d) if input_test is not None: input_test = format_data(input_test, row1d=input_row1d) if target_test is not None: target_test = format_data(target_test, row1d=target_row1d) # ----------- Validation ----------- # if epochs is None and epsilon is None: epochs = 100 if epochs is not None and epsilon is not None: raise ValueError("You can't user `epochs` and `epsilon` " "attributes in one train process.") # ----------- Predefine parameters ----------- # if epochs is not None: self.epoch = 0 iterepochs = range(self.epoch, epochs) last_epoch = epochs - 1 predict = self.predict compute_error_out = (input_test is not None and target_test is not None) if epsilon is not None: iterepochs = iter_until_converge(self, epsilon) last_epoch = None predict = None compute_error_out = None # ----------- Train process ----------- # logs = self.logs logs.header("Start train") logs.log("TRAIN", "Train data size: {}".format(input_train.shape[0])) logs.log("TRAIN", "Number of input features: {}".format(input_train.shape[1])) if epochs is not None: logs.log("TRAIN", "Total epochs: {}".format(epochs)) logs.empty() # Optimizations for long loops. Set constant properties to # variables. errors = self.errors_in errors_out = self.errors_out show_epoch = self.show_epoch shuffle_data = self.shuffle_data # Methods error_func = self.error train_epoch = self.train_epoch train_epoch_end_signal = self.train_epoch_end_signal train_end_signal = self.train_end_signal for epoch in iterepochs: epoch_start_time = time() if shuffle_data: if target_train is not None: input_train, target_train = shuffle( input_train, target_train) else: input_train, = shuffle(input_train) self.input_train = input_train self.target_train = target_train try: error = train_epoch(input_train, target_train) if compute_error_out: error_out = error_func(predict(input_test), target_test) errors_out.append(error_out) errors.append(error) self.train_epoch_time = time() - epoch_start_time if epoch % show_epoch == 0 or epoch == last_epoch: logs.data(""" Epoch {epoch} Error in: {error} Error out: {error_out} Epoch time: {epoch_time} sec """.format(epoch=self.epoch, error=self.last_error_in() or '-', error_out=self.last_error_out() or '-', epoch_time=round(self.train_epoch_time, 5))) if train_epoch_end_signal is not None: train_epoch_end_signal(self) self.epoch = epoch + 1 except StopIteration as err: logs.log("TRAIN", "Epoch #{} stopped. {}".format(self.epoch, str(err))) break if train_end_signal is not None: train_end_signal(self) logs.log("TRAIN", "End train")
def _train(self, input_train, target_train=None, input_test=None, target_test=None, epochs=100, epsilon=None): """ Main method for the Neural Network training. """ # ----------- Pre-format target data ----------- # input_row1d = is_row1d(self.input_layer) input_train = format_data(input_train, row1d=input_row1d) target_row1d = is_row1d(self.output_layer) target_train = format_data(target_train, row1d=target_row1d) if input_test is not None: input_test = format_data(input_test, row1d=input_row1d) if target_test is not None: target_test = format_data(target_test, row1d=target_row1d) # ----------- Validate input values ----------- # if epsilon is not None and epochs <= 2: raise ValueError("Network should train at teast 3 epochs before " "check the difference between errors") # ----------- Predefine parameters ----------- # self.epoch = 1 show_epoch = self.show_epoch logs = self.logs compute_error_out = (input_test is not None and target_test is not None) predict = self.predict last_epoch_shown = 0 if epsilon is not None: iterepochs = iter_until_converge(self, epsilon, max_epochs=epochs) if isinstance(show_epoch, six.string_types): show_epoch = 100 logs.warning("Can't use `show_epoch` value in converging " "mode. Set up 100 to `show_epoch` property " "by default.") else: iterepochs = range(1, epochs + 1) show_epoch = parse_show_epoch_property(show_epoch, epochs) epoch_summary = show_epoch_summary(self, show_epoch) # ----------- Train process ----------- # logs.header("Start train") logs.log("TRAIN", "Train data size: {}".format(input_train.shape[0])) if input_test is not None: logs.log("TRAIN", "Validation data size: {}" "".format(input_test.shape[0])) if epsilon is None: logs.log("TRAIN", "Total epochs: {}".format(epochs)) else: logs.log("TRAIN", "Max epochs: {}".format(epochs)) logs.empty() # Optimizations for long loops errors = self.errors_in errors_out = self.errors_out shuffle_data = self.shuffle_data error_func = self.error train_epoch = self.train_epoch train_epoch_end_signal = self.train_epoch_end_signal train_end_signal = self.train_end_signal self.input_train = input_train self.target_train = target_train for epoch in iterepochs: self.epoch = epoch epoch_start_time = time() if shuffle_data: input_train, target_train = shuffle_train_data(input_train, target_train) self.input_train = input_train self.target_train = target_train try: error = train_epoch(input_train, target_train) if compute_error_out: predicted_test = predict(input_test) error_out = error_func(predicted_test, target_test) errors_out.append(error_out) errors.append(error) self.train_epoch_time = time() - epoch_start_time if epoch % show_epoch == 0 or epoch == 1: show_epoch = next(epoch_summary) last_epoch_shown = epoch if train_epoch_end_signal is not None: train_epoch_end_signal(self) except StopIteration as err: logs.log("TRAIN", "Epoch #{} stopped. {}" "".format(epoch, str(err))) break if epoch != last_epoch_shown: show_training_summary(self) if train_end_signal is not None: train_end_signal(self) logs.log("TRAIN", "End train")