def set_train_func(self): write('\nBuilding a pi train func...') prd_true = T.imatrix('prd_true') prd_proba = self.model.calc_proba(self.model.inputs) prd_pred = self.model.binary_prediction(prd_proba) crr, true_total, pred_total = self.model.calc_correct_predictions(y_true=prd_true, y_pred=prd_pred) loss = self.model.get_binary_loss(prd_true, prd_proba) cost = loss + L2Regularizer()(alpha=self.argv.lr, params=self.model.params) grads = T.grad(cost=cost, wrt=self.model.params) self.optimizer = get_optimizer(argv=self.argv) updates = self.optimizer(grads=grads, params=self.model.params) self.train_func = theano.function( inputs=self.model.inputs + [prd_true], outputs=[cost, crr, true_total, pred_total ], updates=updates, mode='FAST_RUN', )
def evaluate(): print(' * Collecting Real Data') (x_train, y_train), (x_test, y_test) = load_data() print(' * Collecting Optimizer') adam = get_optimizer() print(' * Collecting Generator Model') generator = load_model('models/generator', adam) print(' * Collecting Discriminator Model') discriminator = load_model('models/discriminator', adam) print(' * Generating 100 examples') noise = np.random.normal(0, 1, size=[100, Config['gen_in_dim']]) generated_images = generator.predict(noise) print(' * Concating 100 real and 100 fake images') X = np.concatenate([ x_train[np.random.randint(x_train.shape[0], size=100)], generated_images ]) y = np.zeros(X.shape[0]) y[:100] = 1 print(' * Evaluating Discriminator') indices = np.arange(X.shape[0]) np.random.shuffle(indices) res = discriminator.evaluate(X[indices], y[indices]) print('Loss: ' + str(res[0])) print('Accuracy: ' + str(res[1]))
def __init__(self, options, train_sentences=None): self.model = dn.Model() self.statistics = Statistics.from_sentences(train_sentences) self.container = nn.Container(self.model) self.network = EdgeEvaluationNetwork(self.container, self.statistics, options) self.optimizer = nn.get_optimizer(self.model, options) self.decoder = decoders[options.decoder] self.label_decoder = label_decoders[options.label_decoder] self.labelsFlag = options.labelsFlag self.options = options if "func" in options: del options.func
def load(cls, prefix, # type: str new_options=None): """ :param prefix: model file name prefix :rtype: MaxSubGraphParser """ model = dn.Model() parser, savable = nn.model_load_helper(None, prefix, model) parser.options.__dict__.update(new_options.__dict__) parser.model = model parser.container = savable parser.network = parser.container.components[0] parser.optimizer = nn.get_optimizer(model, parser.options) return parser
def __init__(self, options, data_train): self.model = dn.Model() (span_model_options, statistics), self.container = nn.model_load_helper( options.span_model_format, options.span_model_prefix, self.model) logger.info(statistics) self.statistics = statistics self.options = options self.options.__dict__ = AttrDict(chain(span_model_options.__dict__.items(), options.__dict__.items())) logger.info(pformat(self.options.__dict__)) self.optimizer = nn.get_optimizer(self.model, options) with open(options.derivations, "rb") as f: self.derivations = pickle.load(f, encoding="latin1") self.hrg_statistics = HRGStatistics.from_derivations(self.derivations) print(self.hrg_statistics) self.span_ebd_network, self.span_eval_network, self.label_eval_network = self.container.components self.scorer_network = self.scorers[options.scorer](self.container, self.hrg_statistics, self.options) self.edge_eval = EdgeEvaluation(self.container, self.hrg_statistics.structural_edges, options) self.category_embedding = nn.EmbeddingFromDictionary(self.container, self.hrg_statistics.categories, options.category_embedding) self.category_scorer = POSTagClassification(self.container, self.hrg_statistics.categories, options, options.lstm_dims * options.span_lstm_layers ) with open(options.grammar, "rb") as f: self.grammar = pickle.load(f, encoding="latin1") # type: Mapping[str, Mapping[CFGRule, int]] self.terminal_mapping = defaultdict(Counter) # type: Mapping[str, typing.Counter] for (cfg_lhs, cfg_rhs_list), value in self.grammar.items(): if all(isinstance(i, HLexicon) for i in cfg_rhs_list): self.terminal_mapping[cfg_lhs] += value self.lexicon_mapping = defaultdict(Counter) # type: Mapping[Tuple[HLexicon, int], typing.Counter] for (cfg_lhs, cfg_rhs_list), value in self.grammar.items(): rule_name, main_node_count = cfg_lhs.rsplit("#", 1) main_node_count = int(main_node_count) if all(isinstance(i, HLexicon) for i in cfg_rhs_list): lexicon = cfg_rhs_list[0] self.lexicon_mapping[lexicon, main_node_count] += value self.lemmatizer = WordNetLemmatizer() if options.unlexicalized_rules is not None: with open(options.unlexicalized_rules, "rb") as f: self.unlexicalized_rules = pickle.load(f)
def __init__(self, options, train_sentences=None, restore_file=None): self.model = dn.Model() if restore_file: old_options, self.network = nn.model_load_helper(None, restore_file, self.model) if options is not None: old_options.__dict__.update(options.__dict__) options = old_options else: statistics = Statistics.from_sentences(train_sentences) self.network = EdgeEvaluationNetwork(self.model, statistics, options) self.optimizer = nn.get_optimizer(self.model, options) self.decoder = decoders[options.decoder] self.labelsFlag = options.labelsFlag self.options = options if "func" in options: del options.func
def set_train_func(self): write('\nBuilding an lp train func...') y_label = T.imatrix('y') label_proba = self.model.calc_label_proba(self.model.inputs) label_pred = self.model.argmax_label_proba(label_proba) true_label_path_score = self.model.calc_label_path_score(label_proba, y_label) cost = - T.mean(true_label_path_score) + L2Regularizer()(alpha=self.argv.reg, params=self.model.params) grads = T.grad(cost=cost, wrt=self.model.params) self.optimizer = get_optimizer(argv=self.argv) updates = self.optimizer(grads=grads, params=self.model.params) self.train_func = theano.function( inputs=self.model.inputs + [y_label], outputs=[cost, categorical_accuracy(y_true=y_label, y_pred=label_pred), label_pred.flatten(), y_label.flatten() ], updates=updates, mode='FAST_RUN' )
def __init__(self, options, train_sentences=None, restore_file=None, statistics=None): self.model = dn.Model() random.seed(1) self.optimizer = nn.get_optimizer(self.model, options) self.activation = activations[options.activation] # self.decoder = decoders[options.decoder] self.labelsFlag = options.labelsFlag self.costaugFlag = options.cost_augment self.options = options if "func" in options: del options.func self.container = nn.Container(self.model) self.statistics = statistics = StatisticsWithEmpty.from_sentences( train_sentences) self.has_emptys = len(statistics.emptys) > 0 self.network = EdgeEvaluationNetwork(self.container, statistics, options) if self.has_emptys: self.network_for_emptys = EdgeEvaluation(self.container, options) self.label_network_for_emptys = ToEmptyLabelEvaluation( self.container, self.statistics.emptys, options) if options.use_2nd: self.network3 = EdgeSiblingEvaluation(self.container, options) if self.has_emptys: self.network3_for_emptys_mid = EdgeSiblingEvaluation( self.container, options) self.network3_for_emptys_out = EdgeSiblingEvaluation( self.container, options)
seed_everything(Config.seed) device = torch.device(Config.device) start_epoch = 0 logging.info('Define Models') model = build_model(Config).to(device) tokenizer = Tokenizer.from_pretrained(Config.model_name) logging.info('Define Loss and Optimizer') criterion = LabelSmoothing(tokenizer.vocab_size, pad_id=tokenizer.pad_token_id, smoothing=Config.smoothing) _opt = optim.Adam(model.parameters(), lr=0, betas=(0.9, 0.98), eps=1e-9) optimizer = get_optimizer(_opt, factor=Config.factor, warmup=Config.warmup) logging.info('Preparing training data') if Config.use_pickle: with open(f'{Config.pickle_path}', 'rb') as f: train_data = pickle.load(f) else: train_data = make_train_data_from_txt(Config, tokenizer) dataset = DialogDataset(train_data, tokenizer) logging.info('Start Training') for epoch in range(start_epoch, Config.n_epoch): one_cycle(epoch, Config, model, optimizer, criterion, BalancedDataLoader(dataset, tokenizer.pad_token_id), tokenizer, device) evaluate(Config, 'もう疲れたー', tokenizer, model, device)
def generate(): print(' * Collecting Model') adam = get_optimizer() generator = load_model('models/generator', adam) print(' * Generating and Ploting 100 examples') plot_generated_images(0, generator, show=True)