def __init__(self, model, alerts): SectionView.__init__(self) self._model = UpdateModel() self._model.connect('progress', self.__progress_cb) self.set_spacing(style.DEFAULT_SPACING) self.set_border_width(style.DEFAULT_SPACING * 2) self._top_label = Gtk.Label() self._top_label.set_line_wrap(True) self._top_label.set_justify(Gtk.Justification.LEFT) self._top_label.props.xalign = 0 self.pack_start(self._top_label, False, True, 0) self._top_label.show() separator = Gtk.HSeparator() self.pack_start(separator, False, True, 0) separator.show() bottom_label = Gtk.Label() bottom_label.set_line_wrap(True) bottom_label.set_justify(Gtk.Justification.LEFT) bottom_label.props.xalign = 0 bottom_label.set_markup( _('Software updates correct errors, eliminate security ' \ 'vulnerabilities, and provide new features.')) self.pack_start(bottom_label, False, True, 0) bottom_label.show() self._update_box = None self._progress_pane = None self._refresh()
def __init__(self, model, alerts): SectionView.__init__(self) self._model = UpdateModel() self._model.connect('progress', self.__progress_cb) self.set_spacing(style.DEFAULT_SPACING) self.set_border_width(style.DEFAULT_SPACING * 2) self._top_label = Gtk.Label() self._top_label.set_line_wrap(True) self._top_label.set_justify(Gtk.Justification.LEFT) self._top_label.props.xalign = 0 self.pack_start(self._top_label, False, True, 0) self._top_label.show() separator = Gtk.HSeparator() self.pack_start(separator, False, True, 0) separator.show() bottom_label = Gtk.Label() bottom_label.set_line_wrap(True) bottom_label.set_justify(Gtk.Justification.LEFT) bottom_label.props.xalign = 0 bottom_label.set_markup( _('Software updates correct errors, eliminate security ' 'vulnerabilities, and provide new features.')) self.pack_start(bottom_label, False, True, 0) bottom_label.show() self._update_box = None self._progress_pane = None self._refresh()
def train(args): paddle.set_device(args.device) data_path = args.data_path train_loader, valid_loader, test_loader, vocab_size = create_data_loader( batch_size=args.batch_size, num_steps=args.num_steps, data_path=data_path) network = RnnLm(vocab_size=vocab_size, hidden_size=args.hidden_size, batch_size=args.batch_size, num_layers=args.num_layers, init_scale=args.init_scale, dropout=args.dropout) gloabl_norm_clip = paddle.nn.ClipGradByGlobalNorm(args.max_grad_norm) cross_entropy = CrossEntropyLossForLm() ppl_metric = Perplexity() callback = UpdateModel() scheduler = paddle.callbacks.LRScheduler(by_step=False, by_epoch=True) model = paddle.Model(network) learning_rate = paddle.optimizer.lr.LambdaDecay( learning_rate=args.base_lr, lr_lambda=lambda x: args.lr_decay**max(x + 1 - args.epoch_start_decay, 0.0), verbose=True) optimizer = paddle.optimizer.SGD(learning_rate=learning_rate, parameters=model.parameters(), grad_clip=gloabl_norm_clip) model.prepare(optimizer=optimizer, loss=cross_entropy, metrics=ppl_metric) if args.init_from_ckpt: model.load(args.init_from_ckpt) print("Loaded checkpoint from %s" % args.init_from_ckpt) model.fit(train_data=train_loader, eval_data=valid_loader, epochs=args.max_epoch, shuffle=False, callbacks=[callback, scheduler], log_freq=max(1, len(train_loader) // 10)) model.save(path='checkpoint/test') # save for training print('Start to evaluate on test dataset...') model.evaluate(test_loader, log_freq=len(test_loader))
class ActivityUpdater(SectionView): def __init__(self, model, alerts): SectionView.__init__(self) self._model = UpdateModel() self._model.connect('progress', self.__progress_cb) self.set_spacing(style.DEFAULT_SPACING) self.set_border_width(style.DEFAULT_SPACING * 2) self._top_label = Gtk.Label() self._top_label.set_line_wrap(True) self._top_label.set_justify(Gtk.Justification.LEFT) self._top_label.props.xalign = 0 self.pack_start(self._top_label, False, True, 0) self._top_label.show() separator = Gtk.HSeparator() self.pack_start(separator, False, True, 0) separator.show() bottom_label = Gtk.Label() bottom_label.set_line_wrap(True) bottom_label.set_justify(Gtk.Justification.LEFT) bottom_label.props.xalign = 0 bottom_label.set_markup( _('Software updates correct errors, eliminate security ' 'vulnerabilities, and provide new features.')) self.pack_start(bottom_label, False, True, 0) bottom_label.show() self._update_box = None self._progress_pane = None self._refresh() def _switch_to_update_box(self): if self._update_box in self.get_children(): return if self._progress_pane in self.get_children(): self.remove(self._progress_pane) self._progress_pane = None if self._update_box is None: self._update_box = UpdateBox(self._model) self._update_box.refresh_button.connect( 'clicked', self.__refresh_button_clicked_cb) self._update_box.install_button.connect( 'clicked', self.__install_button_clicked_cb) self.pack_start(self._update_box, expand=True, fill=True, padding=0) self._update_box.show() def _switch_to_progress_pane(self): if self._progress_pane in self.get_children(): return if self._update_box in self.get_children(): self.remove(self._update_box) self._update_box = None if self._progress_pane is None: self._progress_pane = ProgressPane() self._progress_pane.cancel_button.connect( 'clicked', self.__cancel_button_clicked_cb) self.pack_start( self._progress_pane, expand=True, fill=False, padding=0) self._progress_pane.show() def _clear_center(self): if self._progress_pane in self.get_children(): self.remove(self._progress_pane) self._progress_pane = None if self._update_box in self.get_children(): self.remove(self._update_box) self._update_box = None def __progress_cb(self, model, action, bundle_name, current, total): if current == total and action == UpdateModel.ACTION_CHECKING: self._finished_checking() return elif current == total: self._finished_updating(int(current)) return if action == UpdateModel.ACTION_CHECKING: message = _('Checking %s...') % bundle_name elif action == UpdateModel.ACTION_DOWNLOADING: message = _('Downloading %s...') % bundle_name elif action == UpdateModel.ACTION_UPDATING: message = _('Updating %s...') % bundle_name self._switch_to_progress_pane() self._progress_pane.set_message(message) self._progress_pane.set_progress(current / float(total)) def _finished_checking(self): logging.debug('ActivityUpdater._finished_checking') available_updates = len(self._model.updates) if not available_updates: top_message = _('Your software is up-to-date') else: top_message = ngettext('You can install %s update', 'You can install %s updates', available_updates) top_message = top_message % available_updates top_message = GObject.markup_escape_text(top_message) self._top_label.set_markup('<big>%s</big>' % top_message) if not available_updates: self._clear_center() else: self._switch_to_update_box() self._update_box.refresh() def __refresh_button_clicked_cb(self, button): self._refresh() def _refresh(self): top_message = _('Checking for updates...') self._top_label.set_markup('<big>%s</big>' % top_message) self._model.check_updates() def __install_button_clicked_cb(self, button): text = '<big>%s</big>' % _('Installing updates...') self._top_label.set_markup(text) self._model.update(self._update_box.get_bundles_to_update()) def __cancel_button_clicked_cb(self, button): self._model.cancel() def _finished_updating(self, installed_updates): logging.debug('ActivityUpdater._finished_updating') top_message = ngettext('%s update was installed', '%s updates were installed', installed_updates) top_message = top_message % installed_updates top_message = GObject.markup_escape_text(top_message) self._top_label.set_markup('<big>%s</big>' % top_message) self._clear_center() def undo(self): self._model.cancel()
class ActivityUpdater(SectionView): def __init__(self, model, alerts): SectionView.__init__(self) self._model = UpdateModel() self._model.connect('progress', self.__progress_cb) self.set_spacing(style.DEFAULT_SPACING) self.set_border_width(style.DEFAULT_SPACING * 2) self._top_label = Gtk.Label() self._top_label.set_line_wrap(True) self._top_label.set_justify(Gtk.Justification.LEFT) self._top_label.props.xalign = 0 self.pack_start(self._top_label, False, True, 0) self._top_label.show() separator = Gtk.HSeparator() self.pack_start(separator, False, True, 0) separator.show() bottom_label = Gtk.Label() bottom_label.set_line_wrap(True) bottom_label.set_justify(Gtk.Justification.LEFT) bottom_label.props.xalign = 0 bottom_label.set_markup( _('Software updates correct errors, eliminate security ' \ 'vulnerabilities, and provide new features.')) self.pack_start(bottom_label, False, True, 0) bottom_label.show() self._update_box = None self._progress_pane = None self._refresh() def _switch_to_update_box(self): if self._update_box in self.get_children(): return if self._progress_pane in self.get_children(): self.remove(self._progress_pane) self._progress_pane = None if self._update_box is None: self._update_box = UpdateBox(self._model) self._update_box.refresh_button.connect( 'clicked', self.__refresh_button_clicked_cb) self._update_box.install_button.connect( 'clicked', self.__install_button_clicked_cb) self.pack_start(self._update_box, expand=True, fill=True, padding=0) self._update_box.show() def _switch_to_progress_pane(self): if self._progress_pane in self.get_children(): return if self._update_box in self.get_children(): self.remove(self._update_box) self._update_box = None if self._progress_pane is None: self._progress_pane = ProgressPane() self._progress_pane.cancel_button.connect( 'clicked', self.__cancel_button_clicked_cb) self.pack_start(self._progress_pane, expand=True, fill=False, padding=0) self._progress_pane.show() def _clear_center(self): if self._progress_pane in self.get_children(): self.remove(self._progress_pane) self._progress_pane = None if self._update_box in self.get_children(): self.remove(self._update_box) self._update_box = None def __progress_cb(self, model, action, bundle_name, current, total): if current == total and action == UpdateModel.ACTION_CHECKING: self._finished_checking() return elif current == total: self._finished_updating(int(current)) return if action == UpdateModel.ACTION_CHECKING: message = _('Checking %s...') % bundle_name elif action == UpdateModel.ACTION_DOWNLOADING: message = _('Downloading %s...') % bundle_name elif action == UpdateModel.ACTION_UPDATING: message = _('Updating %s...') % bundle_name self._switch_to_progress_pane() self._progress_pane.set_message(message) self._progress_pane.set_progress(current / float(total)) def _finished_checking(self): logging.debug('ActivityUpdater._finished_checking') available_updates = len(self._model.updates) if not available_updates: top_message = _('Your software is up-to-date') else: top_message = ngettext('You can install %s update', 'You can install %s updates', available_updates) top_message = top_message % available_updates top_message = GObject.markup_escape_text(top_message) self._top_label.set_markup('<big>%s</big>' % top_message) if not available_updates: self._clear_center() else: self._switch_to_update_box() self._update_box.refresh() def __refresh_button_clicked_cb(self, button): self._refresh() def _refresh(self): top_message = _('Checking for updates...') self._top_label.set_markup('<big>%s</big>' % top_message) self._model.check_updates() def __install_button_clicked_cb(self, button): text = '<big>%s</big>' % _('Installing updates...') self._top_label.set_markup(text) self._model.update(self._update_box.get_bundles_to_update()) def __cancel_button_clicked_cb(self, button): self._model.cancel() def _finished_updating(self, installed_updates): logging.debug('ActivityUpdater._finished_updating') top_message = ngettext('%s update was installed', '%s updates were installed', installed_updates) top_message = top_message % installed_updates top_message = GObject.markup_escape_text(top_message) self._top_label.set_markup('<big>%s</big>' % top_message) self._clear_center() def undo(self): self._model.cancel()