Exemple #1
0
        thresholds = np.arange(n_levels, step=8)
        thresholds += 10
        encoder = MultiThresholdEncoder(thresholds=thresholds)
        Xencode = encoder.transform(X)
        del X

        X = opu_mapping.transform(Xencode)
        del Xencode

    print(f"NEWMA OPU: Sketch took {time.time() - startingtime:.2f}s.")

    mult = 1.5
    detector = algos.NEWMA(X[0],
                           forget_factor=big_Lambda,
                           feat_func=lambda x: x.astype('float32'),
                           forget_factor2=small_lambda,
                           adapt_forget_factor=thres_ff * mult,
                           thresholding_quantile=0.95,
                           dist_func=lambda z1, z2: np.linalg.norm(z1 - z2))
    detector.apply_to_data(X)

    n = 0
    detection_stat = np.array([i[0] for i in detector.stat_stored
                               ])[int(10 * n):]  # padding
    online_th = np.array([i[1] for i in detector.stat_stored])[int(10 * n):]

    computation_duration = time.time() - startingtime
    print(f'NEWMA RP on OPU took {computation_duration:.2f}s.')

    # ----------
    # NEWMA FF on CPU
    # Newma config
    big_Lambda, small_lambda = algos.select_optimal_parameters(B)  # forget factors chosen with heuristic in the paper
    print('Chose Lambda = {:.4f} and lambda = {:.4f}'.format(big_Lambda, small_lambda))
    thres_ff = small_lambda
    # number of random features is set automatically with this criterion
    m = int((1 / 4) / (small_lambda + big_Lambda) ** 2)
    print('Number of RFs: {}'.format(m))
    W, sigmasq = feat.generate_frequencies(m, d, data=data_sigma_estimate, choice_sigma=choice_sigma)

    print('Start algo ', algo, ' with fixed threshold')

    def feat_func(x):
        return feat.fourier_feat(x, W)

    detector95 = algos.NEWMA(X[0], forget_factor=big_Lambda, forget_factor2=small_lambda, feat_func=feat_func,
                             adapt_forget_factor=thres_ff)
    detector95.apply_to_data(X)

    # compute performance metrics
    detection_stat95 = np.array([i[0] for i in detector95.stat_stored])[int(10 * n):]  # padding
    online_th95 = np.array([i[1] for i in detector95.stat_stored])[int(10 * n):]
    ground_truth = ground_truth[int(10 * n):]

    if args.show:
        import matplotlib.pyplot as plt
        plt.figure()
        plt.plot(detection_stat95[:4 * n])
        plt.plot(online_th95[:4 * n])
        plt.plot(1.2 * np.max(detection_stat95[:4 * n]) * ground_truth[:4 * n], '--', color='k')
        plt.legend([algo, 'Threshold', 'True Changes'], framealpha=1, ncol=1, handletextpad=0.1)
        plt.show()
Exemple #3
0
            X[0],
            kernel_func=lambda x, y: feat.gauss_kernel(x, y, np.sqrt(sigmasq)),
            window_size=window_size,
            nbr_windows=N,
            adapt_forget_factor=thresh_ff,
            store_values=False)
        t = time.time()
        ocpobj.apply_to_data(X)  # actual computations
        time_method = time.time() - t
        print('time:', time_method)
    else:
        print('RFF')
        W, _ = feat.generate_frequencies(m, d, data=X[:100])
        updt_func = lambda x: feat.fourier_feat(x, W)
        t = time.time()
        ocpobj = algos.NEWMA(X[0],
                             forget_factor=ff,
                             forget_factor2=ff2,
                             feat_func=updt_func,
                             adapt_forget_factor=thresh_ff,
                             store_values=False)
        ocpobj.apply_to_data(X)  # actual computations
        time_method = time.time() - t
        print('Execution time: ', time_method)
    running_times.append(time_method)

# save results for later plot
np.savez('test_B_runningtime_algo_{}.npz'.format(algo),
         windows=window_size_list,
         running_times=np.array(running_times))