def infer(raw, flags): """ :param raw: :param flags: :return: """ data_processor = DataProcessor.load(flags.checkpoint_path) model = LSTM(data_processor.num_features, flags.num_units, output_dim=flags.output_dim, num_layers=flags.num_layers, batch_first=True, dropout=flags.dropout) model, _, _ = load_checkpoint(flags.checkpoint_path, model, None) model.eval() # predict results = {} loader, ts_types = data_processor.get_forecast_data(raw) with torch.no_grad(): for type, data in zip(ts_types, loader): scale = data[4] _, outputs = _infer(data, model, flags.window, flags.forecast_length) results[type] = [(output * scale).detach().numpy()[0] for output in outputs] print(results) return results
def run_k_folds(self, n_runs=5, n_folds=2): dp = DataProcessor() dp.load('data/SQuAD/squad-v7.file') model = LogRegModel() model.load_vectors(dp.articles, n_folds=n_folds) baseline_results = [] sentiment_results = [] for run in range(n_runs): print("k-fold run:", run) baseline_results.append(model.run_k_fold(with_sentiment=False)) sentiment_results.append(model.run_k_fold()) model.create_new_folds(n_folds=n_folds) self.save_results(baseline_results, "results/5x2_baseline") self.save_results(sentiment_results, "results/5x2_sentiment")
result["conf_matrices"].append( confusion_matrix( self.model.predict(test_vectors), test_targets )) result["roc_curves"].append( roc_curve( test_targets, self.model.predict_proba(test_vectors)[:,1]) ) result["roc_auc_scores"].append( roc_auc_score( test_targets, self.model.predict(test_vectors)) ) result["coefficients"].append( self.model.coef_[0] ) return result if __name__ == "__main__": dp = DataProcessor() dp.load('data/SQuAD/squad-v7.file') model = LogRegModel() model.load_vectors(dp.articles) model.run_k_fold()