Esempio n. 1
0
model = Sequential()
model.add(Dense(nb_neuron_input, input_shape = (1,) + input_shape))
model.add(Activation('tanh'))
model.add(Flatten())
model.add(Dense(nb_neuron_output, activation='softmax'))
model.summary()

# Create the DQN agent with the memory and policy hyper-parameters
memory = SequentialMemory(limit=50000, window_length=1)
policy = BoltzmannQPolicy()
dqn = DQNAgent(model=model, nb_actions=nb_neuron_output, memory=memory, nb_steps_warmup=10,
               target_model_update=1e-2, policy=policy)
dqn.compile(Adam(lr=1e-3), metrics=['mae', 'acc'])

# Create the metrics and pass it to the model as a callback for training
metrics = Metrics(dqn, env)
dqn.fit(env, nb_steps=1000000, visualize=False, verbose=2, callbacks=[metrics])

# the filename has the form modelName_memoryType|memorySize_policy_numberOfSteps
fileName = '1D__Sequential50000_BoltzmannQ_1000000steps(0)'

# Write the metrics into a file for post treatment
f1=open('./output/' + fileName + '.txt', 'w+')
f1.write(metrics.export_to_text())
f1.close()

# Optionally export figures
metrics.export_figs(fileName)

# Save network weights for later tests
dqn.save_weights('./output/' + fileName + '.h5f', overwrite=True)
Esempio n. 2
0
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "2"

import h5py
import numpy as np
import tensorflow as tf
import keras
import seaborn as sn
from matplotlib import pyplot as plt
from market_env import MarketEnv
from market_model_builder import MarketPolicyGradientModelBuilder
from model_builder import AbstractModelBuilder
from Metrics import Metrics
M = Metrics()


class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'


class PolicyGradient:
    def __init__(self,
                 env,
                 discount=0.99,
Esempio n. 3
0
    def display_metrics(classifier_list: list, test_labels_all, pred: list, proba: list, label_list: list):
        """
        Function that display some metrics in regard of the training results

        :arg
            self (Trainer): instance of the class
            classifier_list (list): List with selected classifier names
            test_labels_all (numpy array): Numpy array containing target values of the testing set
            pred (list): list of the prediction. Each index of the list contains a numpy array
            proba (list): list of the probability. Each index of the list contains a numpy array
            label_list (list): list of the name of pathologies

        :return
            None
        """
        metrics = Metrics()

        plt.figure()
        plt.title('ROC Curve')
        plt.xlabel('False Positive Rate')
        plt.ylabel('True Positive Rate')
        for i, value in enumerate(proba):
            fpr, tpr = metrics.roc_metrics(test_labels_all, value)
            plt.plot(fpr, tpr, label=classifier_list[i])

        if len(classifier_list) > 1:
            mean_proba = np.dstack(proba)
            mean_proba = np.mean(mean_proba, axis=2)
            fpr, tpr = metrics.roc_metrics(test_labels_all, mean_proba)
            plt.plot(fpr, tpr, label='Voting classifiers')

        plt.legend(loc='lower right')
        plt.show(block=False)

        plt.figure()
        plt.title('Precision-Recall Curve')
        plt.xlabel('Recall')
        plt.ylabel('Precision')
        for i, value in enumerate(proba):
            precision, recall = metrics.precision_recall(test_labels_all, value)
            plt.plot(precision, recall, label=classifier_list[i])

        if len(classifier_list) > 1:
            mean_proba = np.dstack(proba)
            mean_proba = np.mean(mean_proba, axis=2)
            fpr, tpr = metrics.precision_recall(test_labels_all, mean_proba)
            plt.plot(fpr, tpr, label='Voting classifiers')

        plt.legend(loc='lower left')
        plt.show(block=False)

        if len(classifier_list) > 1:
            mean_pred = np.dstack(pred)
            mean_pred = np.mean(mean_pred, axis=2)
            mean_pred[mean_pred >= 0.5] = 1
            mean_pred[mean_pred < 0.5] = 0
            pred = mean_pred
        else:
            pred = pred[0]

        cohen_kappa_score, kappa_class = metrics.cohen_kappa_score(test_labels_all, pred)
        f1_score, f1_class = metrics.f1_score(test_labels_all, pred)
        accuracy, accuracy_class = metrics.accuracy(test_labels_all, pred)
        precision, precision_class = metrics.precision(test_labels_all, pred)
        recall, recall_class = metrics.recall(test_labels_all, pred)
        #
        print('Cohen: {}'.format(cohen_kappa_score))
        print('F1: {}'.format(f1_score))
        print('Accuracy: {}'.format(accuracy))
        print('Precision: {}'.format(precision))
        print('Recall: {}'.format(recall))

        titles = ['names', 'Cohen', 'F1_score', 'Accuracy', 'Precision', 'Recall']
        kappa_class_disp = ['%.4f' % elem for elem in kappa_class]
        f1_class_disp = ['%.4f' % elem for elem in f1_class]
        accuracy_class_disp = ['%.4f' % elem for elem in accuracy_class]
        precision_class_disp = ['%.4f' % elem for elem in precision_class]
        recall_class_disp = ['%.4f' % elem for elem in recall_class]

        element = [titles] + list(
           zip(label_list, kappa_class_disp, f1_class_disp, accuracy_class_disp, precision_class_disp,
               recall_class_disp))
        for i, d in enumerate(element):
           line = '|'.join(str(x).ljust(19) for x in d)
           print(line)
           if i == 0:
               print('-' * (len(line)-10))
Esempio n. 4
0
# Settings
sampleEach = 1
dataset = "bentham"
useNGrams = True

# main
if __name__ == "__main__":

    # load dataset
    loader = DataLoader(dataset, sampleEach)
    print("Decoding " + str(loader.getNumSamples()) + " samples now.")
    print("")

    # metrics calculates CER and WER for dataset
    m = Metrics(loader.lm.getWordChars())

    # write results to csv
    csv = Utils.CSVWriter()

    # decode each sample from dataset
    for (idx, data) in enumerate(loader):
        # decode matrix
        res = wordBeamSearch(data.mat, 10, loader.lm, useNGrams)
        print("Sample: " + str(idx + 1))
        print("Filenames: " + data.fn)
        print('Result:       "' + res + '"')
        print('Ground Truth: "' + data.gt + '"')
        strEditDist = str(editdistance.eval(res, data.gt))
        print("Editdistance: " + strEditDist)
Esempio n. 5
0
    def computeMetrics(self, optimalPareto, solutions, functionName):
        self.solutionNames = [solution[0] for solution in solutions]
        solutionData = [solution[1] for solution in solutions]
        #solutionData = self._removeDominatedFromSolutionData([solution[1] for solution in solutions])
        self.dim = len(solutionData[0][0][0])
        self.nSolutions = len(self.solutionNames)
        metrics = Metrics(optimalPareto, solutionData)
        
        self.unaryMetricNames = ['Inverted Generational Distance', 'Delta P', 'Spacing', "Hypervolume"]
        self.unaryMetricOptType = [MetricsCalc.__MIN__, MetricsCalc.__MIN__, \
                              MetricsCalc.__MIN__, MetricsCalc.__MAX__]
        unaryMetricType = [MetricsCalc.__CONV__, MetricsCalc.__CONV__, \
                           MetricsCalc.__DIST__, [MetricsCalc.__CONV__, MetricsCalc.__DIST__]]
        unaryMetricFunction = [metrics.invertedGenerationalDistance, metrics.deltaP, \
                               metrics.spacing, metrics.hypervolume]
        self.nUnaryMetrics = len(self.unaryMetricNames)
        self.binaryMetricNames = ['Coverage', 'Additive Epsilon', 'Multiplicative Epsilon']
        self.binaryMetricOptType = [MetricsCalc.__MAX__, MetricsCalc.__MIN__, MetricsCalc.__MIN__]
        binaryMetricType = [MetricsCalc.__CONV__, MetricsCalc.__CONV__, MetricsCalc.__CONV__]
        self.nBinaryMetrics = len(self.binaryMetricNames)
        self.labels = []
        self.sublabels = []
        self.labels += self.unaryMetricNames
        self.sublabels += [None] * len(self.labels)
        for binaryMetric in self.binaryMetricNames:
            self.labels += [binaryMetric] * self.nSolutions
            self.sublabels += self.solutionNames
            
        self.labels.append("Points")
        self.sublabels.append("Convergence")
        self.labels.append("Points")
        self.sublabels.append("Distribution")
        
        nLabels = len(self.labels)
        self.metricMean = [[None] * (self.nSolutions) for _ in xrange(nLabels)]
        self.metricStd = [[None] * (self.nSolutions) for _ in xrange(nLabels)]
        self.metricMin = [[None] * (self.nSolutions) for _ in xrange(nLabels)]
        self.metricMax = [[None] * (self.nSolutions) for _ in xrange(nLabels)]
        self.metricQ1 = [[None] * (self.nSolutions) for _ in xrange(nLabels)]
        self.metricQ3 = [[None] * (self.nSolutions) for _ in xrange(nLabels)]
        self.metricIsBest = [[False] * (self.nSolutions) for _ in xrange(nLabels)]
        
        nadirPoint = [-(1<<30)] * self.dim
        for solution in solutionData:
            r = 0
            for run in solution:
                r += 1
                for point in run:
                    for d in xrange(self.dim):
                        nadirPoint[d] = max(nadirPoint[d], math.ceil(point[d] * 10 + 0.001) / 10.0)
        print "    Using Nadir point: " + str(nadirPoint)
        metrics.setHypervolumeReference(nadirPoint)
        maxHypervolume = metrics.maxHypervolume()
        
        mean = Utils.createListList(self.nUnaryMetrics)
        std = Utils.createListList(self.nUnaryMetrics)
        mmin = Utils.createListList(self.nUnaryMetrics)
        mmax = Utils.createListList(self.nUnaryMetrics)
        q1 = Utils.createListList(self.nUnaryMetrics)
        q3 = Utils.createListList(self.nUnaryMetrics)
        for solutionA in xrange(self.nSolutions):
            values = Utils.createListList(self.nUnaryMetrics)
            for runA in xrange(len(solutionData[solutionA])):
                metrics.setSolutionsToCompare(solutionA, runA, None, None)
                for i in xrange(self.nUnaryMetrics):
                    value = unaryMetricFunction[i]()
                    if math.isnan(value):
                        if self.unaryMetricNames[i] != "Spacing":
                            print >> sys.stderr, "Found one metric, besides Spacing, giving NaN: %s" % (self.unaryMetricNames[i])
                    else: # Good value
                        if self.unaryMetricNames[i] == "Hypervolume":
                            value /= maxHypervolume
                            if value > 1:
                                #print >> sys.stderr, "    Normalized hypervolume of %s for %s exceeds 1.0: %f" % \
                                #    (self.solutionNames[solutionA], functionName, value)
                                value = 1.0
                        values[i].append(value)
            
            for m in xrange(len(values)):
                mean[m].append(numpy.mean(values[m]))
                std[m].append(numpy.std(values[m]))
                mmin[m].append(numpy.min(values[m]))
                mmax[m].append(numpy.max(values[m]))
                q1[m].append(scoreatpercentile(values[m], 25))
                q3[m].append(scoreatpercentile(values[m], 75))

        for metric in [MetricsCalc.COVERAGE, MetricsCalc.ADDITIVE_EPSILON, MetricsCalc.MULTIPLICATIVE_EPSILON]:
            meanMetric, stdMetric = self._getMetric(solutionData, metric, metrics, self.solutionNames)
            mean += meanMetric
            std += stdMetric
        
        self.convPoints = [0] * self.nSolutions
        self.distPoints = [0] * self.nSolutions
        for row in xrange(len(mean)):
            for column in xrange(len(mean[row])):
                m = mean[row][column]
                s = std[row][column]
                if m is None or s is None:
                    continue
                
                if row < len(self.unaryMetricNames):
                    factor = self.unaryMetricOptType[row]
                    if abs(round(m*factor, Utils.__ROUND__) - 
                           round(min(x*factor for x in mean[row] if x is not None), Utils.__ROUND__)) < Utils.__EPS__:
                        self.metricIsBest[row][column] = True
                    for i in xrange(len(mean[row])):
                        if i != column and mean[row][i] is not None and \
                                round(mean[row][column]*factor, Utils.__ROUND__) <= round(mean[row][i]*factor, Utils.__ROUND__):
                            self._addMetricPoints(1.0 / (self.nSolutions - 1), column, unaryMetricType[row])
                elif row >= len(self.unaryMetricNames):
                    offset = row - (row - len(self.unaryMetricNames)) % self.nSolutions
                    metricIdx = int((row - len(self.unaryMetricNames)) / self.nSolutions)
                    factor = self.binaryMetricOptType[metricIdx]
                    if round(m*factor, Utils.__ROUND__) >= round(mean[offset + column][row - offset]*factor, Utils.__ROUND__):
                        self.metricIsBest[row][column] = True
                        self._addMetricPoints(1.0 / (self.nSolutions - 1), column, binaryMetricType[metricIdx])
                
                self.metricMean[row][column] = m
                self.metricStd[row][column] = s
                if row < len(self.unaryMetricNames):
                    self.metricMin[row][column] = mmin[row][column]
                    self.metricMax[row][column] = mmax[row][column]
                    self.metricQ1[row][column] = q1[row][column]
                    self.metricQ3[row][column] = q3[row][column]
        
        row = nLabels - 2
        for points in [self.convPoints, self.distPoints]:
            maxValue = max(points)
            for solutionIdx in xrange(self.nSolutions):
                value = points[solutionIdx]
                if abs(value - maxValue) < Utils.__EPS__:
                    self.metricIsBest[row][solutionIdx] = True
                self.metricMean[row][solutionIdx] = value
            row += 1
Esempio n. 6
0
    def __init__(self, args):
        super(NeuralNetwork, self).__init__()
        self.args = args
        self.patience = 0
        self.init_clip_max_norm = 5.0
        self.optimizer = None
        self.best_result = [0, 0, 0, 0, 0, 0]
        self.metrics = Metrics(self.args.score_file_path)
        self.device = torch.device(
            'cuda:0' if torch.cuda.is_available() else 'cpu')

        config_class, model_class, tokenizer_class = MODEL_CLASSES[
            args.model_type]

        self.bert_config = config_class.from_pretrained(
            args.config_name if args.config_name else args.model_name_or_path,
            finetuning_task="classification",
            num_labels=1)

        self.bert_tokenizer = BertTokenizer.from_pretrained(
            args.tokenizer_name
            if args.tokenizer_name else args.model_name_or_path,
            do_lower_case=args.do_lower_case)
        special_tokens_dict = {'eos_token': '[eos]'}
        num_added_toks = self.bert_tokenizer.add_special_tokens(
            special_tokens_dict)

        self.bert_model = model_class.from_pretrained(
            args.model_name_or_path,
            from_tf=bool('.ckpt' in args.model_name_or_path),
            config=self.bert_config)
        self.bert_model.resize_token_embeddings(len(self.bert_tokenizer))

        self.bert_model = self.bert_model.cuda()
        '''
        self.attn = nn.Linear(300, 300)
        self.rnn1 = nn.GRU(
            input_size=768, hidden_size=300,
            num_layers=1, batch_first=True, bidirectional=False
        )
        self.bilinear=nn.Bilinear(600,600,1)
        '''
        # multihop
        self.transformer_utt = TransformerBlock(input_size=768)
        self.transformer_eu = TransformerBlock(input_size=768)
        self.transformer_ru = TransformerBlock(input_size=768)

        self.transformer_ett = TransformerBlock(input_size=768)
        self.transformer_ue = TransformerBlock(input_size=768)
        self.transformer_re = TransformerBlock(input_size=768)

        self.transformer_rtt = TransformerBlock(input_size=768)
        self.transformer_ur = TransformerBlock(input_size=768)
        self.transformer_er = TransformerBlock(input_size=768)

        self._projection = nn.Sequential(nn.Linear(4 * 768, 200), nn.ReLU())

        self.rnn2 = nn.GRU(input_size=200,
                           hidden_size=200,
                           num_layers=1,
                           batch_first=True,
                           bidirectional=True)

        self._classification = nn.Sequential(nn.Dropout(p=0.2),
                                             nn.Linear(2 * 6 * 200, 200),
                                             nn.Tanh(), nn.Dropout(p=0.2),
                                             nn.Linear(200, 1))
Esempio n. 7
0
start_http_server(port)

while True:
    for db_instance_data in config.get("DBInstances"):
        db_instance_identifier = db_instance_data.get("DBInstanceIdentifier")
        region = "us-east-1" if db_instance_data.get("Region") is None or len(
            db_instance_data.get("Region")) == 0 else db_instance_data.get(
                "Region")
        db_engine = db_instance_data.get("DBEngine")
        availability_zone = db_instance_data.get("AvailabilityZone")

        db_metric_object = Metrics(period=period,
                                   db_name=db_instance_identifier,
                                   db_engine=db_engine,
                                   availability_zone=availability_zone,
                                   region=region,
                                   access_key=access_key,
                                   secret_access_key=secret_access_key,
                                   monitoring_type=monitoring_type)

        db_metrics = db_metric_object.get_metrics_from_cloud_watch()

        METRICS = db_metric_object.METRICS.get(monitoring_type).get(db_engine)
        LABELS = db_metric_object.LABELS

        for metric_result in db_metrics.get("MetricDataResults"):
            label = metric_result.get("Label")

            if len(metric_result.get("Values")) > 0:
                gauge = METRICS.get(label)
Esempio n. 8
0
    df5 = pd.DataFrame(data5, columns=x + y)
    plt.subplots_adjust(bottom=0.5)
    df1.plot(ax=axes[0], x=x[0], y=y, kind=kind)
    axes[0].set_title('@1')
    df3.plot(ax=axes[1], x=x[0], y=y, kind=kind)
    axes[1].set_title('@3')
    df5.plot(ax=axes[2], x=x[0], y=y, kind=kind)
    axes[2].set_title('@5')
    fig.suptitle(title, fontsize=24)


if __name__ == '__main__':
    file_name = sys.argv[1]
    marks_obj = extract_marks(file_name)
    metric_tensor = np.ndarray((len(marks_obj), 2, 3, 5))
    m1 = Metrics(1)
    m3 = Metrics(3)
    m5 = Metrics(5)
    queries = np.array(list(map(lambda x: x[2], marks_obj)))
    for i, mark in enumerate(marks_obj):
        my_mark, es_mark, query = mark

        metric_tensor[i, 0, 0, :] = m1.measure(my_mark[:1])
        metric_tensor[i, 0, 1, :] = m3.measure(my_mark[:3])
        metric_tensor[i, 0, 2, :] = m5.measure(my_mark[:5])

        metric_tensor[i, 1, 0, :] = m1.measure(es_mark[:1])
        metric_tensor[i, 1, 1, :] = m3.measure(es_mark[:3])
        metric_tensor[i, 1, 2, :] = m5.measure(es_mark[:5])

    partial_data_extractor = partial(data_to_plot_metric, queries,
Esempio n. 9
0
d.add_variable("cc3", 5000, 16)
pymt = d.calculate()
d.report()

e = Expense()
e.add("groceries", 1000)
e.add("utilities", 500)
e.add("household", 500)
e.add("housing", 2000)
e.add_yearly("property tax", 7000)
e.add_yearly("insurance", 3600)
e.add_yearly("maintenance", 2000)
e.add_yearly("vacation", 6000)
e.report()

b = Budget()
b.add(i, t, d, e)
b.report()

a = Assets()
a.add("Cash", 30000, 2)
a.add("Stocks", 80000, 8)
a.add("Bonds", 0, 4)
a.report()

m = Metrics(i, b, e, d, a)
m.calculate()
m.report()

ana = Analysis(p, i, d, b, e, a, m)
ana.do_all()
Esempio n. 10
0
    def __init__(self, scope, num_dirs, opt="adagrad", lr=0.025):
        print("num_dirs:", num_dirs)
        sub_dir = "ecb_" + scope + "/"

        # init stuff
        print("TORCH VERSION:", torch.__version__)
        global args
        self.args = config.parse_known_args()

        self.args.cuda = self.args.cuda and torch.cuda.is_available()
        device = torch.device("cuda:0" if self.args.cuda else "cpu")
        torch.manual_seed(self.args.seed)
        random.seed(self.args.seed)

        print("TREELSTM:", opt, "lr:", lr)

        # paths
        train_dir = os.path.join(self.args.data, str(num_dirs), 'train/',
                                 sub_dir)
        dev_dir = os.path.join(self.args.data, str(num_dirs), 'dev/', sub_dir)
        test_dir = os.path.join(self.args.data, str(num_dirs), 'test/',
                                sub_dir)

        print("train_dir:", train_dir)
        print("dev_dir:", dev_dir)

        # builds vocabulary
        sick_vocab_file = Helper.build_entire_vocab(
            os.path.join(self.args.data, str(num_dirs), 'sick.vocab'),
            train_dir, dev_dir, test_dir)
        vocab = Vocab(filename=sick_vocab_file,
                      data=[
                          Constants.PAD_WORD, Constants.UNK_WORD,
                          Constants.BOS_WORD, Constants.EOS_WORD
                      ])
        print('==> SICK vocabulary size : %d ' % vocab.size())

        # loads SICKDataset: Trees, sentences, and labels
        self.train_dataset = Helper.load_data(
            train_dir,
            os.path.join(self.args.data, str(num_dirs), 'sick_train.pth'),
            vocab, self.args.num_classes)
        self.dev_dataset = Helper.load_data(
            dev_dir, os.path.join(self.args.data, str(num_dirs),
                                  'sick_dev.pth'), vocab,
            self.args.num_classes)
        self.test_dataset = Helper.load_data(
            test_dir,
            os.path.join(self.args.data, str(num_dirs), 'sick_test.pth'),
            vocab, self.args.num_classes)

        # creates the TreeLSTM
        model = SimilarityTreeLSTM(vocab.size(), self.args.input_dim, self.args.mem_dim, self.args.hidden_dim, \
          self.args.num_classes, self.args.sparse, self.args.freeze_embed, vocab)
        criterion = nn.KLDivLoss()  #nn.CrossEntropyLoss()

        # loads glove embeddings
        emb = Helper.load_embeddings(
            self.args,
            os.path.join(self.args.data, str(num_dirs), 'sick_embed.pth'),
            vocab, device)

        # sets up the model
        model.emb.weight.data.copy_(
            emb)  # plug these into embedding matrix inside model
        model.to(device)
        criterion.to(device)
        opt = optim.Adagrad(filter(lambda p: p.requires_grad, \
           model.parameters()), lr=lr, weight_decay=self.args.wd)

        if opt == "adam":
            opt = optim.Adam(filter(lambda p: p.requires_grad, \
              model.parameters()), lr=lr)

        self.metrics = Metrics(self.args.num_classes)

        # create trainer object for training and testing
        self.trainer = Trainer(self.args, model, criterion, opt, device, vocab)
Esempio n. 11
0
def main(args):
    # make sure dec hidden units and layers are same
    FLAGS.dec_hidden_units = FLAGS.enc_hidden_units
    FLAGS.dec_layers = FLAGS.enc_layers

    # initialize the dataset and the data loader
    train_dataset = HagglingDataset(FLAGS.train, FLAGS)
    train_dataloader = DataLoader(train_dataset,
                                  batch_size=FLAGS.batch_size,
                                  shuffle=True,
                                  num_workers=10)
    test_dataset = HagglingDataset(FLAGS.test, FLAGS)
    test_dataloader = DataLoader(test_dataset,
                                 batch_size=FLAGS.batch_size,
                                 shuffle=False,
                                 num_workers=10)

    # set the wandb config
    config = FLAGS.flag_values_dict()
    run = wandb.init(project="Sell-It", config=config)

    # initialize the model, log it for visualization
    model = get_model()
    # try:
    #     torch.onnx.export(model, next(iter(train_dataloader)),
    #                       os.path.join(FLAGS.ckpt_dir, FLAGS.model + '/model.onnx'))
    #     wandb.save(os.path.join(FLAGS.ckpt_dir, FLAGS.model + '/model.onnx'))
    # except Exception as e:
    #     print(e)

    starting_epoch = 0

    # restore model if needed
    if FLAGS.resume_train:
        ckpt = os.path.join(FLAGS.ckpt_dir, FLAGS.model + '/')
        starting_epoch = model.load_model(ckpt, None)
        #starting_epoch = 130

    # get the loss function and optimizers
    criterion = get_loss_fn()
    optimizer = get_optimizer(model.get_trainable_parameters())
    p = 1.0

    metrics = Metrics(FLAGS)

    # run the training script
    for epoch in range(starting_epoch + 1, FLAGS.epochs + 1):

        print(epoch)

        # initialize the total epoch loss values
        train_loss_logs = {
            'Train/Total_Loss': 0,
            'Train/Reconstruction_Loss': 0,
            'Train/Regularization_Loss': 0,
            'Train/CrossEntropy_Loss': 0,
            'Train/VelocityRegularization': 0
        }

        train_metric_logs = {
            'Train/RightMSE': 0,
            'Train/LeftMSE': 0,
            'Train/RightNPSS': 0,
            'Train/LeftNPSS': 0,
            'Train/RightFrechet': 0,
            'Train/LeftFrechet': 0,
            'Train/RightSpeech': 0,
            'Train/LeftSpeech': 0,
            'Train/MSE': 0,
            'Train/NPSS': 0,
            'Train/Frechet': 0,
            'Train/Speech': 0
        }

        test_metric_logs = {
            'Test/RightMSE': 0,
            'Test/LeftMSE': 0,
            'Test/RightNPSS': 0,
            'Test/LeftNPSS': 0,
            'Test/RightFrechet': 0,
            'Test/LeftFrechet': 0,
            'Test/RightSpeech': 0,
            'Test/LeftSpeech': 0,
            'Test/MSE': 0,
            'Test/NPSS': 0,
            'Test/Frechet': 0,
            'Test/Speech': 0
        }

        # set model to train mode
        model.train()

        # decay factor set
        decay_p(p, epoch, model)

        # run through all the batches
        for i_batch, batch in enumerate(train_dataloader):
            # zero prev gradients
            optimizer.zero_grad()

            # forward pass through the net
            predictions, targets = model(batch)

            # calculate loss
            losses = criterion(predictions, targets, model.parameters(), FLAGS)
            total_loss = losses['Total_Loss']

            # calculate gradients
            total_loss.backward()
            optimizer.step()

            # compute train metrics
            with torch.no_grad():
                if not FLAGS.skip_train_metrics:
                    train_metrics = metrics.compute_and_save(
                        predictions, targets, batch, i_batch, None)
                    train_metric_logs = {
                        'Train/' + key:
                        train_metrics[key] + train_metric_logs['Train/' + key]
                        for key in train_metrics
                    }

                train_loss_logs = {
                    'Train/' + key: losses[key].detach().cpu().numpy().item() +
                    train_loss_logs['Train/' + key]
                    for key in losses
                }

        # set the model to evaluation mode
        model.eval()

        # calculate validation loss
        with torch.no_grad():
            for i_batch, batch in enumerate(test_dataloader):
                # forward pass through the net
                predictions, targets = model(batch)

                if FLAGS.model == 'bodyAE' or FLAGS.model == 'bmg':
                    test_metric_logs['Test/MSE'] += meanJointPoseError(
                        predictions, targets)
                else:
                    # consolidate metrics
                    test_metrics = metrics.compute_and_save(
                        predictions, targets, batch, i_batch, None)
                    test_metric_logs = {
                        'Test/' + key:
                        test_metrics[key] + test_metric_logs['Test/' + key]
                        for key in test_metrics
                    }

        # scale the metrics
        train_metric_logs = {
            key: train_metric_logs[key] / len(train_dataloader)
            for key in train_metric_logs
        }
        train_loss_logs = {
            key: train_loss_logs[key] / len(train_dataloader)
            for key in train_loss_logs
        }
        test_metric_logs = {
            key: test_metric_logs[key] / len(test_dataloader)
            for key in test_metric_logs
        }

        # log all the metrics
        run.log({**train_metric_logs, **train_loss_logs, **test_metric_logs})

        if epoch % FLAGS.ckpt == 0 and epoch > 0:
            ckpt = os.path.join(FLAGS.ckpt_dir,
                                FLAGS.model + '/' + wandb.run.name + '/')
            model.save_model(ckpt, epoch)

    run.finish()
Esempio n. 12
0
    def user(self):
        user_input = None
        print("0: generate actual maze\n"
              "1: generate blank maze\n"
              "2: generate random maze from the saved mazes\n"
              "3: Perform Repeated forward A* in favor of larger g value\n"
              "4: Perform Repeated forward A* in favor of smaller g value\n"
              "5: Perform Repeated backward A*\n"
              "6: Perform Repeated Adaptive A\n"
              "q: Quit\n")

        while user_input != "q":

            user_input = input("Choose one of the numbers above : ")

            if user_input == "0":
                size = int(input("Enter the size of the maze : "))
                actual_maze = Maze().generate_actual_maze(size)
                start_node_actual, goal_node_actual = Metrics().generate_random_start_and_goal_nodes(
                    actual_maze, size)
                Visual(7, start_node_actual, goal_node_actual, size).showMaze(actual_maze)

            elif user_input == "1":
                size = int(input("Enter the size of the maze : "))
                blank_maze = Maze().generate_blank_maze(size)
                start_node_actual, goal_node_actual = Metrics().generate_random_start_and_goal_nodes(
                    blank_maze, size)
                Visual(7, start_node_actual, goal_node_actual, size).showMaze(blank_maze)

            elif user_input == "2":
                self.actual_maze = self.actual_mazes[random.randint(0, 50)]
                self.start_node_actual, self.goal_node_actual = Metrics().generate_random_start_and_goal_nodes(
                    self.actual_maze, self.size)
                Visual(7, self.start_node_actual, self.goal_node_actual, self.size).showMaze(self.actual_maze)

            elif user_input == "3":
                agent_maze = Maze().generate_blank_maze(self.size)
                start_node = agent_maze[self.start_node_actual.x][self.start_node_actual.y]
                goal_node = agent_maze[self.goal_node_actual.x][self.goal_node_actual.y]
                RepeatedAlgo(self.size, self.actual_maze, agent_maze, start_node, goal_node, self.start_node_actual,
                             self.goal_node_actual, 0).repeated_algorithm()

            elif user_input == "4":
                agent_maze = Maze().generate_blank_maze(self.size)
                start_node = agent_maze[self.start_node_actual.x][self.start_node_actual.y]
                goal_node = agent_maze[self.goal_node_actual.x][self.goal_node_actual.y]
                RepeatedAlgo(self.size, self.actual_maze, agent_maze, start_node, goal_node, self.start_node_actual,
                             self.goal_node_actual, 1).repeated_algorithm()

            elif user_input == "5":
                agent_maze = Maze().generate_blank_maze(self.size)
                start_node = agent_maze[self.start_node_actual.x][self.start_node_actual.y]
                goal_node = agent_maze[self.goal_node_actual.x][self.goal_node_actual.y]
                RepeatedAlgo(self.size, self.actual_maze, agent_maze, start_node, goal_node, self.start_node_actual,
                             self.goal_node_actual, 2).repeated_algorithm()

            elif user_input == "6":
                agent_maze = Maze().generate_blank_maze(self.size)
                start_node = agent_maze[self.start_node_actual.x][self.start_node_actual.y]
                goal_node = agent_maze[self.goal_node_actual.x][self.goal_node_actual.y]
                RepeatedAlgo(self.size, self.actual_maze, agent_maze, start_node, goal_node, self.start_node_actual,
                             self.goal_node_actual, 3).repeated_algorithm()
Esempio n. 13
0
def pipeline(fileName, nameForFiles, protectedAttribute, trueLabels, feldman,
             bayes, dataName):
    # Load data into DataSet
    ds = DataSet()
    ds.loadData(fileName, protectedAttribute, trueLabels)

    # Open a file for writing results
    f = open("results/" + nameForFiles + ".txt", "w")

    print("Starting DI detection")
    DIresult = detectDI(ds)
    f.write("DI results on original data: " + DIresult)

    # Feldman repair algorithm
    currDataSet = ds
    if feldman == "yes":
        print("Starting Feldman")
        repair = RepairData()
        repair.runRepair(ds.fileName,
                         ds.protectedAttribute,
                         ds.trueLabels,
                         dataName,
                         noiseScale=.01)
        # Pickle the Feldman-repaired data
        repair.dataSetCopy.savePickle("pickledObjects/repairedData/" +
                                      nameForFiles)
        repair.dataSetCopy.saveToCsv("dataCSVs/repairedData/" + nameForFiles +
                                     ".csv")
        currDataSet = repair.dataSetCopy

        print("Starting post-Feldman DI detection")
        postFeldmanDIresult = detectDI(repair.dataSetCopy)
        f.write("DI results after Feldman: " + postFeldmanDIresult)

    #Split data into test and training set
    currDataSet.splitIntoTrainTest()
    print("Split into test train")

    #Bayes
    if bayes == "naive":
        print("Starting Naive Bayes")
        bayesObject = NaiveBayes()
        bayesObject.train(currDataSet, bayesObject.model)
        bayesObject.classify(currDataSet, "test")
        print("Completed Naive Bayes")

    elif bayes == "modified":
        bayesObject = ModifiedBayes()
        bayesObject.train(currDataSet, 1)
        bayesObject.classify(currDataSet, "test")

    else:
        bayesObject = TwoBayes()
        bayesObject.train(currDataSet, 1)
        bayesObject.classify(currDataSet, "test")

    currDataSet.savePickle("pickledObjects/classifiedData/" + nameForFiles)
    currDataSet.saveToCsv("dataCSVs/classifiedData/" + nameForFiles + ".csv")

    # Metrics
    print("Starting metrics")
    metrics = Metrics()
    metrics.runAllMetrics(f, currDataSet, bayes, bayesObject)
    print("Completed metrics")

    f.close()
Esempio n. 14
0
from time import sleep
import argparse
from datetime import datetime

from AlphaVantage_Backend import AlphaVantage_Backend
from Metrics import Metrics

import pandas as pd
from openpyxl import Workbook
from openpyxl.styles.numbers import FORMAT_NUMBER_COMMA_SEPARATED1, FORMAT_CURRENCY_USD
from openpyxl.utils.dataframe import dataframe_to_rows

av_key = os.getenv("ALPHA_VANTAGE")

backend = AlphaVantage_Backend(av_key)
metrics = Metrics(backend)


def analyze_ticker(ticker):
    assets = metrics.current_assets(ticker)
    liabilities = metrics.current_liabilities(ticker)
    al_ratio = {
        dt: total_assets / liabilities[dt]
        for dt, total_assets in assets.items()
    }

    metrics5years = pd.DataFrame([
        metrics.revenues(ticker),
        metrics.profits(ticker),
        metrics.fcfs(ticker),
        metrics.net_profit_margins(ticker), al_ratio