コード例 #1
0
ファイル: run_cp.py プロジェクト: nahad3/semi_sup_cp
def check_cp_at_index(X, index, thresh_sim, thresh_dissim):
    '''Function to check if a change point in Signal X around the provided index'''
    p_win = 800
    f_win = 800
    win_pair = 600
    buff = 30
    cp = 0
    #X_temp = X[index - 2*p_win : index+2*p_win,:]
    X_temp = X
    #Y_temp = Y[index - 2*p_win : index+2*p_win,:]
    mmd = np.zeros(len(X_temp))
    var_array = np.zeros(len(X_temp))
    for i in range(p_win, len(X_temp) - f_win):
        past_np_median = X_temp[i - p_win:i, :]
        var_array[i] = np.var(np.mean(X_temp[i - p_win:i, :], axis=1))
        past = torch.from_numpy(X_temp[i - p_win:i, :]).cuda().float()
        past = past.unsqueeze(0)

        fut = torch.from_numpy(X_temp[i:i + f_win, :]).cuda().float()
        fut = fut.unsqueeze(0)
        # labels = labels[ i - p_win : i + f_win]

        sigma_list = mmd_util.median_heuristic(past_np_median, beta=5)
        sigma_var = torch.FloatTensor(sigma_list).cuda()

        mmd[i] = mmd_util.batch_mmd2_loss(past, fut,
                                          sigma_var).cpu().numpy()[0]
    mmd[-100:] = 0
    cp_mag = max(mmd)
    change_indices = np.argmax(mmd)
    #plot_cps(X_temp, 0 , mmd, change_indices)
    sim = 0
    X_pair_1 = X[index - win_pair - buff:index - buff, :]
    X_pair_2 = X[index + buff:index + buff + win_pair, :]
    X1_var = np.median(np.var(X_pair_1[0:500, :], axis=0))
    X2_var = np.median(np.var(X_pair_2[-300:, :], axis=0))
    if X1_var < 0.005 or X2_var < 0.005:  #'For 0 and 0 case '
        thresh_dissim = 2

    if max(mmd[:i - 0]) > thresh_dissim:
        sim = -1
    elif max(mmd) < thresh_sim:
        sim = 1
    else:
        sim = 0

    return sim
コード例 #2
0
def detect_cp_and_get_pairs(signal, win_size):
    #Reshape signal such that first dimension consists of number of points. 2nd dimension: signal dimension
    series = signal.reshape(-1, 1)
    mmd = np.zeros(series.shape[0])
    sigma_list = mmd_util.median_heuristic(series, beta=0.1)
    sigma_var = torch.FloatTensor(sigma_list).cuda()
    p_win = win_size
    f_win = win_size
    feat_temp = series
    for i in range(win_size, series.shape[0] - f_win):
        past = torch.from_numpy(feat_temp[i - p_win: i, :]).cuda().float()
        past = past.unsqueeze(0)
        fut = torch.from_numpy(feat_temp[i: i + f_win, :]).cuda().float()
        fut = fut.unsqueeze(0)
        mmd[i] = mmd_util.batch_mmd2_loss(past, fut, sigma_var).cpu().numpy()[0]
    change_indices = find_peaks(mmd, height=1.0)[0]
    X1_sim, X2_sim, X1_dis, X2_dis = get_paris_from_cp(series, change_indices, 5)
    return X1_sim, X2_sim, X1_dis, X2_dis
コード例 #3
0
    raise NotImplementedError(
        'Loss function %s is not support! Consider L1|L2|Huber' %
        (args.loss_func))
if args.cuda:
    cirterion = criterion.cuda()
optimizer = Optim(model.parameters(),
                  args.optim,
                  lr=args.lr,
                  grad_clip=args.grad_clip,
                  weight_decay=args.weight_decay,
                  momentum=args.momentum)

# sigma for mixture of RBF kernel in MMD
#sigma_list = [1.0]
#sigma_list = mmd_util.median_heuristic(Data.Y_subspace, beta=1.)
sigma_list = mmd_util.median_heuristic(Data.Y_subspace, beta=.5)
sigma_var = torch.FloatTensor(sigma_list).cuda()
print('sigma_list:', sigma_var)

# ========= Main loop for pretraining RNN with reconstruction loss  =========#
Y_val = Data.val_set['Y'].numpy()
L_val = Data.val_set['L'].numpy()
Y_tst = Data.tst_set['Y'].numpy()
L_tst = Data.tst_set['L'].numpy()

n_batchs = int(math.ceil(len(Data.trn_set['Y']) / float(args.batch_size)))
print('n_batchs', n_batchs, 'batch_size', args.batch_size)

update = 0
total_time = 0.
best_epoch = -1