Esempio n. 1
0
def Q1_3():
    hashtags = [
        '#gohawks', '#nfl', '#sb49', '#gopatriots', '#patriots', '#superbowl'
    ]
    for tag in hashtags:
        X = load_obj(tag + '_Q13')[:-1, :]
        y = load_obj(tag + '_numTweetsInHour')[1:]
        model = stats_api.OLS(y, X)
        res = model.fit()
        y_pred = res.predict(X)
        y_resid = y - y_pred
        sum_err = pow(y_resid, 2)
        sum_err = np.sum(sum_err)
        print(res.summary())
        #     print(sum_err)
        rmse = sqrt(sum_err / len(y_resid))
        print('%s has RMSE of %.3f' % (tag, rmse))

        features = [
            'mentionCount', 'rankScore', 'passitivity',
            'co-occurrence_of_tags', 'unique_author'
        ]
        for i in [0, 2, 3]:
            x_plt = X[:, i]
            ys = [[y, 'Predictant']]
            x_label = features[i]
            y_label = 'number of tweets for next hour'
            title = tag + ', ' + x_label
            make_plot(x_plt,
                      ys,
                      scatter=True,
                      xlabel=x_label,
                      ylabel=y_label,
                      title=title)
        print('=============================')
Esempio n. 2
0
def Q3():
    import nltk
    nltk.download('vader_lexicon')
    from nltk.sentiment.vader import SentimentIntensityAnalyzer
    sid = SentimentIntensityAnalyzer()

    hashtags = ['#gohawks', '#gopatriots', '#patriots']
    for tag in hashtags:
        with open(fileLocation(tag), encoding="utf8") as f:
            print('tag: ', tag)

            firstTs = FIRST_TS[tag]
            firstTs = firstTs // 3600 * 3600
            lastTs = LAST_TS[tag]
            totalHours = tsDiffHour(firstTs, lastTs) + 1

            neg_sentiments = [0] * totalHours
            pos_sentiments = [0] * totalHours
            neu_sentiments = [0] * totalHours
            hourCount = [0] * totalHours

            tweets = f.readlines()
            for tweet in tweets:
                t = json.loads(tweet)
                ts = t['citation_date']
                date = datetime.datetime.fromtimestamp(t['citation_date'])
                title = t['title']
                hourDiff = tsDiffHour(firstTs, ts)
                hourCount[hourDiff] += 1
                ss = sid.polarity_scores(title)
                h_r = hourCount[hourDiff]
                neg_sentiments[hourDiff] = (neg_sentiments[hourDiff] *
                                            (h_r - 1) + ss['neg']) / h_r
                pos_sentiments[hourDiff] = (pos_sentiments[hourDiff] *
                                            (h_r - 1) + ss['pos']) / h_r
                neu_sentiments[hourDiff] = (neu_sentiments[hourDiff] *
                                            (h_r - 1) + ss['neu']) / h_r

            # plot sentiments vs time (hour from beginning)
            ys = [[neg_sentiments, 'negative sentiment'],
                  [pos_sentiments, 'positive sentiment']]
            x_plt = range(0, totalHours)
            x_label = 'time'
            y_label = 'sentiment'
            title = 'sentiment vs time for ' + tag
            make_plot(x_plt,
                      ys,
                      scatter=False,
                      xlabel=x_label,
                      ylabel=y_label,
                      title=title)
    def evaluate(self, predicted_adj_mat: np.array, make_plots: bool = True):
        """
        Evaluate the performance of the link prediction algorithm with the generated graph
        fill the
        :return:
        """
        true_edges, false_edges = [], []
        y_score = []

        for i, (u, v) in enumerate(self.test_edges):
            score = predicted_adj_mat[u, v]
            y_score.append(score)  # if there's an edge - it'll be 1 or close to 1
            true_edges.append(1)  # actual edge

        for i, (u, v) in enumerate(self.test_edges_false):
            score = predicted_adj_mat[u, v]
            y_score.append(score)  # the numbers should be 0 or close to 0
            false_edges.append(0)  # actual non-edge

        y_true = true_edges + false_edges
        assert len(y_score) == len(y_true), f'Lengths of y_score: {len(y_score)!r} y_true: {len(y_true)!r} not equal'

        fpr, tpr, _ = roc_curve(y_true=y_true, y_score=y_score)
        auroc = roc_auc_score(y_true=y_true, y_score=y_score)

        prec, recall, _ = precision_recall_curve(y_true=y_true, probas_pred=y_score)
        aupr = auc(recall, prec, reorder=True)

        self.performance['AUROC'] = auroc
        self.performance['AUPR'] = aupr
        self.performance['AP'] = average_precision_score(y_true=y_true, y_score=y_score)
        # self.performance['ACC'] = accuracy_score(y_true=y_true, y_pred=y_score)
        # self.performance['F1'] = f1_score(y_true=y_true, y_pred=y_score)

        if make_plots:
            fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1)
            make_plot(x=fpr, y=tpr, xlabel='False Positive Rate', ylabel='True Positive Rate',
                      c='darkorange', label=f'AUROC {round(auroc, 2)}',
                      title=f'{self.method!r} {self.dataset!r} ROC curve', ax=ax1)

            make_plot(x=recall, y=prec, xlabel='Recall', ylabel='Precision', c='darkorange',
                      label=f'AUPR {round(aupr, 2)}',
                      title=f'{self.method!r} {self.dataset!r} PR curve', ax=ax2, kind='pr')
            plt.show()
        return
Esempio n. 4
0
def plot(search):
    checkin_date = search.checkin_date.data
    star_rating = search.star_rating.data
    predictions = estimator.predict(checkin_date, star_rating)
    plot = make_plot(predictions)

    # Embed plot into HTML via Flask Render
    script, div = components(plot)
    return render_template("index.html", script=script, div=div, form=search)
Esempio n. 5
0
def train_vae(model, train_iter, test_iter, num_epochs, test_interval, save_model=False):
    logger.info('Training VAE begins.')
    all_loss = []
    optimizer = torch.optim.Adam(model.parameters(), lr=LR_VAE)  #, betas=(beta1, 0.999))
    fixed_noise = torch.randn(size=(32, N_LATENT), device=dev)
    test_vae(model, test_iter, fixed_noise, 0)

    for epoch in range(1, num_epochs+1):
        model.train()
        epoch_loss = .0
        n_batches = 0
        n_images = 0
        start = time.time()
        for batch, data in enumerate(train_iter):
            X = data[0].to(dev)
            n_batches += 1
            n_images += X.shape[0]
            optimizer.zero_grad()

            y, mu, logvar = model(X)
            loss = model.loss_compute(X, y, mu, logvar)

            loss.backward()
            optimizer.step()
            epoch_loss += loss.item() * X.shape[0]
        epoch_loss /= len(train_iter.dataset)
        all_loss.append(epoch_loss)
        logger.info('Epoch {}, Loss: {:.4f}, ELBO: {:.4f}, Time: {:.4f}, Img processed: {}'.format(
            epoch, epoch_loss, -epoch_loss, time.time()-start, len(train_iter.dataset)))
        if epoch % test_interval == 0:
            test_vae(model, test_iter, fixed_noise, epoch)

    if save_model:
        utils.model_save(model, 'P3_VAE.pt')
    utils.make_plot(all_loss, save_name='P3_vae_learning_curve.png', title='VAE Learning Curve', tickets=['Epochs', 'Loss'])
    return all_loss
Esempio n. 6
0
def index():
    # Determine the selected feature
    current_feature_name = request.args.get("feature_name")
    search = request.args.get("search")
    date = request.args.get("date")

    if date:
        start_date = pd.to_datetime(date)
        end_date = start_date + pd.DateOffset(months=1) - pd.DateOffset(days=1)
    else:
        start_date = pd.to_datetime(
            pd.datetime.now().strftime("%Y-%m"))  # - pd.DateOffset(months=1)
        end_date = start_date + pd.DateOffset(months=1) - pd.DateOffset(days=1)
    start_date_str = start_date.strftime("%m/%Y")

    if search == None:
        return render_template("index.html",
                               ticker_exists=False,
                               search=search,
                               feature_names=feature_names,
                               current_feature_name=current_feature_name,
                               start_date=start_date_str)

    data = get_data(search, start_date, end_date)
    if data.empty:
        #search = None
        return render_template("index.html",
                               ticker_exists=False,
                               search=search,
                               feature_names=feature_names,
                               current_feature_name=current_feature_name,
                               start_date=start_date_str)

    plot = make_plot(data, column=current_feature_name, ticker=search)

    # Embed plot into HTML via Flask Render
    script, div = components(plot)
    print(feature_names)
    return render_template("index.html",
                           script=script,
                           div=div,
                           ticker_exists=True,
                           search=search,
                           feature_names=feature_names,
                           current_feature_name=current_feature_name,
                           start_date=start_date_str)
Esempio n. 7
0
def bokeh():

    quandl.ApiConfig.api_key = "xMwhitoyQ___b7Mb9pAt"
    data_dict ={}
    stock_list= ['ATVI','AAPL','GOOG','CSCO']
    for i in stock_list:
        data_dict[i] = quandl.get("WIKI/%s" %i)
        useful_cols = ['Open','High','Low','Close']
    for i in data_dict.keys():
        data_dict[i] = data_dict[i][useful_cols].reset_index()

    current_stock = 'AAPL'
    select_stock = Select(title="stock selection", value=current_stock, options=stock_list)

    source = make_dataset(data_dict,current_stock)
    plot = make_plot(source)

    select_stock.on_change('value', update_plot)

    controls = column(select_stock)
    full = row(plot,controls)
    curdoc().add_root(full)

    curdoc().title = 'Stock demo'


    # grab the static resources
    js_resources = INLINE.render_js()
    css_resources = INLINE.render_css()

    # render template
    script, div = components(full)
    html = render_template(
        'index.html',
        plot_script=script,
        plot_div=div,
        js_resources=js_resources,
        css_resources=css_resources,
    )
    return encode_utf8(html)
def main(device=torch.device('cuda:0')):
    # CLI arguments
    parser = arg.ArgumentParser(
        description='We all know what we are doing. Fighting!')
    parser.add_argument("--datasize",
                        "-d",
                        default="small",
                        type=str,
                        help="data size you want to use, small, medium, total")
    # Parsing
    args = parser.parse_args()
    # Data loaders
    datasize = args.datasize
    pathname = "data/nyu.zip"
    tr_loader, va_loader, te_loader = getTrainingValidationTestingData(
        datasize, pathname, batch_size=config("unet.batch_size"))

    # Model
    model = Net()

    # TODO: define loss function, and optimizer
    learning_rate = utils.config("unet.learning_rate")
    criterion = DepthLoss(0.1)
    optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
    number_of_epoches = 10
    #

    # print("Number of float-valued parameters:", util.count_parameters(model))

    # Attempts to restore the latest checkpoint if exists
    print("Loading unet...")
    model, start_epoch, stats = utils.restore_checkpoint(
        model, utils.config("unet.checkpoint"))

    # axes = utils.make_training_plot()

    # Evaluate the randomly initialized model
    # evaluate_epoch(
    #     axes, tr_loader, va_loader, te_loader, model, criterion, start_epoch, stats
    # )
    # loss = criterion()

    # initial val loss for early stopping
    # prev_val_loss = stats[0][1]

    running_va_loss = []
    running_va_acc = []
    running_tr_loss = []
    running_tr_acc = []
    # TODO: define patience for early stopping
    # patience = 1
    # curr_patience = 0
    #
    tr_acc, tr_loss = utils.evaluate_model(model, tr_loader, device)
    acc, loss = utils.evaluate_model(model, va_loader, device)
    running_va_acc.append(acc)
    running_va_loss.append(loss)
    running_tr_acc.append(tr_acc)
    running_tr_loss.append(tr_loss)

    # Loop over the entire dataset multiple times
    # for epoch in range(start_epoch, config('cnn.num_epochs')):
    epoch = start_epoch
    # while curr_patience < patience:
    while epoch < number_of_epoches:
        # Train model
        utils.train_epoch(tr_loader, model, criterion, optimizer)
        tr_acc, tr_loss = utils.evaluate_model(model, tr_loader, device)
        va_acc, va_loss = utils.evaluate_model(model, va_loader, device)
        running_va_acc.append(va_acc)
        running_va_loss.append(va_loss)
        running_tr_acc.append(tr_acc)
        running_tr_loss.append(tr_loss)
        # Evaluate model
        # evaluate_epoch(
        #     axes, tr_loader, va_loader, te_loader, model, criterion, epoch + 1, stats
        # )

        # Save model parameters
        utils.save_checkpoint(model, epoch + 1,
                              utils.config("unet.checkpoint"), stats)

        # update early stopping parameters
        """
        curr_patience, prev_val_loss = early_stopping(
            stats, curr_patience, prev_val_loss
        )
        """

        epoch += 1
    print("Finished Training")
    # Save figure and keep plot open
    # utils.save_training_plot()
    # utils.hold_training_plot()
    utils.make_plot(running_tr_loss, running_tr_acc, running_va_loss,
                    running_va_acc)
Esempio n. 9
0
def main(device, tr_loader, va_loader, te_loader, modelSelection):
    """Train CNN and show training plots."""
    # Model
    if modelSelection.lower() == 'res50':
        model = Res50()
    elif modelSelection.lower() == 'dense121':
        model = Dense121()
    elif modelSelection.lower() == 'dense161':
        model = Dense161()
    elif modelSelection.lower() == 'mobv2':
        model = Mob_v2()
    elif modelSelection.lower() == 'dense169':
        model = Dense169()
    elif modelSelection.lower() == 'mob':
        model = Net()
    elif modelSelection.lower() == 'squeeze':
        model = Squeeze()
    else:
        assert False, 'Wrong type of model selection string!'
    model = model.to(device)

    # TODO: define loss function, and optimizer
    learning_rate = utils.config(modelSelection + ".learning_rate")
    criterion = DepthLoss(0.1).to(device)
    optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
    number_of_epoches = 10
    #

    # Attempts to restore the latest checkpoint if exists
    print("Loading unet...")
    model, start_epoch, stats = utils.restore_checkpoint(
        model, utils.config(modelSelection + ".checkpoint"))

    running_va_loss = [] if 'va_loss' not in stats else stats['va_loss']
    running_va_acc = [] if 'va_err' not in stats else stats['va_err']
    running_tr_loss = [] if 'tr_loss' not in stats else stats['tr_loss']
    running_tr_acc = [] if 'tr_err' not in stats else stats['tr_err']
    tr_acc, tr_loss = utils.evaluate_model(model, tr_loader, device)
    acc, loss = utils.evaluate_model(model, va_loader, device)
    running_va_acc.append(acc)
    running_va_loss.append(loss)
    running_tr_acc.append(tr_acc)
    running_tr_loss.append(tr_loss)
    stats = {
        'va_err': running_va_acc,
        'va_loss': running_va_loss,
        'tr_err': running_tr_acc,
        'tr_loss': running_tr_loss,
    }
    # Loop over the entire dataset multiple times
    # for epoch in range(start_epoch, config('cnn.num_epochs')):
    epoch = start_epoch
    # while curr_patience < patience:
    while epoch < number_of_epoches:
        # Train model
        utils.train_epoch(device, tr_loader, model, criterion, optimizer)
        # Save checkpoint
        utils.save_checkpoint(model, epoch + 1,
                              utils.config(modelSelection + ".checkpoint"),
                              stats)
        # Evaluate model
        tr_acc, tr_loss = utils.evaluate_model(model, tr_loader, device)
        va_acc, va_loss = utils.evaluate_model(model, va_loader, device)
        running_va_acc.append(va_acc)
        running_va_loss.append(va_loss)
        running_tr_acc.append(tr_acc)
        running_tr_loss.append(tr_loss)
        epoch += 1
    print("Finished Training")
    utils.make_plot(running_tr_loss, running_tr_acc, running_va_loss,
                    running_va_acc)
Esempio n. 10
0
def main(device, tr_loader, va_loader, te_loader, modelSelection):
    """Train CNN and show training plots."""
    # CLI arguments
    # parser = arg.ArgumentParser(description='We all know what we are doing. Fighting!')
    # parser.add_argument("--datasize", "-d", default="small", type=str,
    #                     help="data size you want to use, small, medium, total")
    # Parsing
    # args = parser.parse_args()
    # Data loaders
    # datasize = args.datasize
    # Model
    if modelSelection.lower() == 'res50':
        model = Res50()
    elif modelSelection.lower() == 'dense121':
        model = Dense121()
    elif modelSelection.lower() == 'mobv2':
        model = Mob_v2()
    elif modelSelection.lower() == 'dense169':
        model = Dense169()
    elif modelSelection.lower() == 'mob':
        model = Net()
    elif modelSelection.lower() == 'squeeze':
        model = Squeeze()
    else:
        assert False, 'Wrong type of model selection string!'
    # Model
    # model = Net()
    # model = Squeeze()
    model = model.to(device)

    # TODO: define loss function, and optimizer
    learning_rate = utils.config(modelSelection + ".learning_rate")
    criterion = DepthLoss(0.1).to(device)
    optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
    number_of_epoches = 10
    #

    # Attempts to restore the latest checkpoint if exists
    print("Loading unet...")
    model, start_epoch, stats = utils.restore_checkpoint(
        model, utils.config(modelSelection + ".checkpoint"))

    running_va_loss = [] if 'va_loss' not in stats else stats['va_loss']
    running_va_acc = [] if 'va_err' not in stats else stats['va_err']
    running_tr_loss = [] if 'tr_loss' not in stats else stats['tr_loss']
    running_tr_acc = [] if 'tr_err' not in stats else stats['tr_err']
    tr_acc, tr_loss = utils.evaluate_model(model, tr_loader, device)
    acc, loss = utils.evaluate_model(model, va_loader, device)
    running_va_acc.append(acc)
    running_va_loss.append(loss)
    running_tr_acc.append(tr_acc)
    running_tr_loss.append(tr_loss)
    stats = {
        'va_err': running_va_acc,
        'va_loss': running_va_loss,
        'tr_err': running_tr_acc,
        'tr_loss': running_tr_loss,
        # 'num_of_epoch': 0
    }
    # Loop over the entire dataset multiple times
    # for epoch in range(start_epoch, config('cnn.num_epochs')):
    epoch = start_epoch
    # while curr_patience < patience:
    while epoch < number_of_epoches:
        # Train model
        utils.train_epoch(device, tr_loader, model, criterion, optimizer)
        # Save checkpoint
        utils.save_checkpoint(model, epoch + 1,
                              utils.config(modelSelection + ".checkpoint"),
                              stats)
        # Evaluate model
        tr_acc, tr_loss = utils.evaluate_model(model, tr_loader, device)
        va_acc, va_loss = utils.evaluate_model(model, va_loader, device)
        running_va_acc.append(va_acc)
        running_va_loss.append(va_loss)
        running_tr_acc.append(tr_acc)
        running_tr_loss.append(tr_loss)
        epoch += 1
    print("Finished Training")
    utils.make_plot(running_tr_loss, running_tr_acc, running_va_loss,
                    running_va_acc)
Esempio n. 11
0
PATH = sys.argv[1] if len(sys.argv) == 2 else str(Path(__file__).parent.absolute())[:-3]+'imgs/'
NAME = 'huge'

fst = Param(
    CLUSTERS = 12**2,
    CLUST_SIDE_LEN = 7,
    DAYS = 100,

    INFECTION_TIME = 20,
    DEATH_RATE = 0.4,
    INFECT_RATE = 0.3,
    MIGRATIONS_PER_DAY = 100,
    FEAR_RATE = 0.4,
    HEALTHCARE_CAPACITY = 2500
)


start = time()
cumulative, active, healed, dead, migrations, real_mig = run_simulation(fst, PATH)

print('time elapsed:', time() - start, flush=True) 
make_gif(PATH, NAME)

# plt.plot(range(len(migrations)), migrations, label='migrations', color='blue')
plt.plot(range(len(real_mig)), real_mig, label='migrations', color='blue', ls=':')
make_plot(fst, cumulative, active, healed, dead, label='', color='red')

plt.savefig(PATH+NAME+'.png')
print("The visualization of the simulation is available at './imgs/'", flush=True)

    logging.info("Eval metrics for dataset {}".format(','.join(datasets_test)))

    test_logs = []

    logging.info("Restoring parameters from {}".format(restore_path))
    utils.load_checkpoint(restore_path, model, meta_optimizer)

    # train_metrics = evaluate(model, loss_fn, meta_train_classes,
    #                                  task_lr, task_type, metrics, params, args,
    #                                  'train')
    test_metrics = evaluate(model, loss_fn, meta_test_classes, task_lr,
                            task_type, metrics, params, args, 'test')
    test_logs.append(test_metrics)

    save_dir = "experiments/emodb_ravdess_savee_iemocap/shemo"
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)

    with open(os.path.join(save_dir, "shemo_eval.pkl"), 'wb') as f:
        pickle.dump(test_logs, f)

    plt_x = [x / 10.0 for x in supports]
    plt_y = [t['f1_score'] for t in test_logs]

    with open(os.path.join(save_dir, "shemo_eval.txt"), 'w') as f:
        f.write("\n".join(map(str, plt_y)))

    print(plt_y)
    utils.make_plot(save_dir, plt_x, plt_y, 'shemo')
Esempio n. 13
0
def train_gan(model, train_iter, test_iter, num_epochs, G_update_iterval, test_interval, save_model=False):
    all_loss_g = []
    all_loss_d = []
    all_wd = []
    fixed_noise = torch.randn(64, N_LATENT).to(dev)

    optimizerG = torch.optim.Adam(model.generator.parameters(), lr=LR_G, betas=(beta1, 0.999))
    optimizerD = torch.optim.Adam(model.discriminator.parameters(), lr=LR_D, betas=(beta1, 0.999))

    for epoch in range(1, num_epochs+1):
        model.train()
        epoch_ls_g = []
        epoch_ls_d = []
        epoch_wd = []
        epoch_gp = []
        start = time.time()
        for batch, (X, y) in enumerate(train_iter):

            # 1. update discriminator
            optimizerD.zero_grad()
            X = X.to(dev)
            noise = torch.randn(size=(X.shape[0], N_LATENT)).to(dev)
            X_fake = model.generator(noise)
            outp_real = model.discriminator(X).view(-1)
            outp_fake = model.discriminator(X_fake.detach()).view(-1)
            gradient_penalty = LAMBDA * model.gradient_penalty(X.data, X_fake.data)
            wd_distance = outp_real.mean() - outp_fake.mean()
            d_loss = -wd_distance + gradient_penalty

            d_loss.backward()
            optimizerD.step()
            epoch_ls_d.append(d_loss.item())
            epoch_wd.append(wd_distance.item())
            epoch_gp.append(gradient_penalty.item())

            # 2. update generator
            if (batch+1) % G_update_iterval == 0:
                optimizerG.zero_grad()
                noise = torch.randn(size=(X.shape[0], N_LATENT)).to(dev)
                X_fake = model.generator(noise)
                outp_fake = model.discriminator(X_fake).view(-1)
                g_loss = -outp_fake.mean()

                g_loss.backward()
                optimizerG.step()
                epoch_ls_g.append(g_loss.item())

        all_loss_g.append(sum(epoch_ls_g) / len(epoch_ls_g))
        all_loss_d.append(sum(epoch_ls_d) / len(epoch_ls_d))
        all_wd.append(sum(epoch_wd) / len(epoch_wd))
        template = 'Epoch {}, G Loss:{:.2f}, D Loss:{:.2f}, G WD:{:.2f}, gp:{:.2f}, ' \
                   'Time: {:.2f}.'
        logger.info(template.format(epoch, all_loss_g[-1], all_loss_d[-1],
                              all_wd[-1], sum(epoch_gp) / len(epoch_gp), time.time() - start)
        )

        if epoch % test_interval == 0:
            model.eval()
            with torch.no_grad():
                x = model.generator(fixed_noise).cpu()
                x = utils.de_normalize(x)
            save_image(x, filename='figure/P3_GAN_generated_samples_epoch_{}.png'.format(epoch))

    if save_model:
        utils.model_save(model, 'P3_GAN_Epoch{}.pt'.format(num_epochs))
    utils.make_plot(all_loss_d, save_name='P3_gan_learning_curve.png', title='GAN Learning Curve', tickets=['Epochs', 'D Loss'])
    return all_loss_d
Esempio n. 14
0
    train_loss, train_acc = train_net(model, loss, config, train_data,
                                      train_label, config['batch_size'],
                                      config['disp_freq'])

    # save loss & accuracy data for training epoch
    train_loss_list.append(train_loss)
    train_acc_list.append(train_acc)

    LOG_INFO('Testing @ %d epoch...' % (epoch))
    test_loss, test_acc = test_net(model, loss, test_data, test_label,
                                   config['batch_size'])

    # save loss & accuracy data for test epoch
    test_loss_list.append(test_loss)
    test_acc_list.append(test_acc)

end_time = time.time()

make_plot(config['max_epoch'],
          train_loss_list,
          test_loss_list,
          model.name,
          loss.name,
          loss=True)
make_plot(config['max_epoch'],
          train_acc_list,
          test_acc_list,
          model.name,
          loss.name,
          loss=False)
print('Time per epoch = ' + str((end_time - start_time) / config['max_epoch']))
Esempio n. 15
0
def Q1_1_plot(category):
    hourCount = load_obj(category + '_numTweetsInHour')
    hours = [x for x in range(0, len(hourCount))]
    ys = [[hourCount, 'Number of tweets per hour']]
    make_plot(hours, ys, bar=True, xlabel='hours', ylabel='Number of tweets')
Esempio n. 16
0
def Q3_jack():
    hashtags = ['#gohawks', '#patriots']
    num_of_positive_hawks = [0]
    num_of_negative_hawks = [0]
    num_of_positive_patriots = [0]
    num_of_negative_patriots = [0]
    totalHoursMin = 999999

    for category in hashtags:
        with open(fileLocation(category), encoding="utf8") as f:
            tweets = f.readlines()  #read all lines
            firstTs = FIRST_TS[category]
            firstTs = firstTs // 3600 * 3600
            lastTs = LAST_TS[category]
            totalHours = tsDiffHour(firstTs, lastTs) + 1
            if totalHours < totalHoursMin:
                totalHoursMin = totalHours
            hourCount = [0] * totalHours
            if category == '#gohawks':
                num_of_positive_hawks = [0] * totalHours
                num_of_negative_hawks = [0] * totalHours
            else:
                num_of_positive_patriots = [0] * totalHours
                num_of_negative_patriots = [0] * totalHours

            #for tweet in tweets:
            for i in range(0, len(tweets)):
                tweet = tweets[i]
                t = json.loads(tweet)
                ts = t['citation_date']
                date = datetime.datetime.fromtimestamp(t['citation_date'])
                title = t['title']
                hourDiff = tsDiffHour(firstTs, ts)
                hourCount[hourDiff] += 1  # number
                if category == '#gohawks':
                    if sentiment(tweet) is 'positive':
                        num_of_positive_hawks[hourDiff] += 1
                    elif sentiment(tweet) is 'negative':
                        num_of_negative_hawks[hourDiff] += 1
                else:
                    if sentiment(tweet) is 'positive':
                        num_of_positive_patriots[hourDiff] += 1
                    elif sentiment(tweet) is 'negative':
                        num_of_negative_patriots[hourDiff] += 1
    result_positive = [0] * totalHours
    result_negative = [0] * totalHours

    #the perspective of hawks
    for i in range(0, totalHoursMin):  #totalHours
        result_positive[
            i] = num_of_positive_hawks[i] + num_of_negative_patriots[i]
        result_negative[
            i] = num_of_positive_patriots[i] + num_of_negative_hawks[i]

    ys = [[result_positive, 'positive sentiment'],
          [result_negative, 'negative sentiment']]
    x_plt = range(0, totalHours)
    x_label = 'time'
    y_label = 'sentiment'
    title = 'sentiment vs time for hawks'
    make_plot(x_plt,
              ys,
              scatter=False,
              xlabel=x_label,
              ylabel=y_label,
              title=title)

    s = """for tag in hashtags: