Example #1
0
def evaluate_encoder_matrix(A, Y, X, feature_indices):
    """
    Run l1-min and model-based CoSaMP using the sensing matrix A.
    Args:
        A: 2-D array, shape=(emb_dim, input_dim)
        Y: 2-D array, shape=(num_sample, emb_dim)
        X: 2-D csr_matrix, shape=(num_sample, input_dim)
        feature_indices: 1-D array, defines the onehot-sparsity model.
    """
    l1ae_l1_err, l1ae_l1_exact, _ = l1_min_avg_err(A, Y, X, use_pos=False)
    l1ae_l1_err_pos, l1ae_l1_exact_pos, _ = l1_min_avg_err(A,
                                                           Y,
                                                           X,
                                                           use_pos=True)
    l1ae_cosamp_err, l1ae_cosamp_exact, _ = CoSaMP_onehot_avg_err(
        A, Y, X, feature_indices, use_pos=False)
    l1ae_cosamp_err_pos, l1ae_cosamp_exact_pos, _ = CoSaMP_onehot_avg_err(
        A, Y, X, feature_indices, use_pos=True)
    res = {}
    res['l1ae_l1_err'] = l1ae_l1_err
    res['l1ae_l1_exact'] = l1ae_l1_exact
    res['l1ae_l1_err_pos'] = l1ae_l1_err_pos
    res['l1ae_l1_exact_pos'] = l1ae_l1_exact_pos
    res['l1ae_cosamp_err'] = l1ae_cosamp_err
    res['l1ae_cosamp_exact'] = l1ae_cosamp_exact
    res['l1ae_cosamp_err_pos'] = l1ae_cosamp_err_pos
    res['l1ae_cosamp_exact_pos'] = l1ae_cosamp_exact_pos
    return res
Example #2
0
def PCA_l1(X_train, X_test, input_dim, emb_dim):
    """
    Args:
        X_train: csr_matrix, shape=(num_train_sample, input_dim)
        X_test: csr_matrix, shape=(num_test_sample, input_dim)
    """
    svd = TruncatedSVD(n_components=emb_dim)
    svd.fit(X_train)
    G = svd.components_
    Y = svd.transform(X_test)
    # p_err, p_exact, _ = l1_min_avg_err(G, Y, X_test, use_pos=False)
    p_err_pos, p_exact_pos, _ = l1_min_avg_err(G, Y, X_test, use_pos=True)
    pca_err = 0  # sqaured err after inverse transform
    batch_size = np.amin([256, X_test.shape[0]])  # compute error in batches
    num_batches = int(X_test.shape[0] / batch_size)
    for batch_i in xrange(num_batches):
        start_idx = batch_i * batch_size
        end_idx = (batch_i + 1) * batch_size
        X_hat = svd.inverse_transform(Y[start_idx:end_idx, :])  # dense array
        X_true = X_test[start_idx:end_idx, :].toarray()
        pca_err += np.linalg.norm(X_true - X_hat)**2  # squared error
    pca_err = np.sqrt(pca_err / (num_batches * batch_size))  # RMSE
    res = {}
    # res['l1_p_err'] = p_err
    # res['l1_p_exact'] = p_exact
    res['l1_p_err_pos'] = p_err_pos
    res['l1_p_exact_pos'] = p_exact_pos
    res['pca_err'] = pca_err
    return res
def evaluate_encoder_matrix(A, Y, X):
    """
    Run l1-min using the sensing matrix A.
    Args:
        A: 2-D array, shape=(emb_dim, input_dim)
        Y: 2-D array, shape=(num_sample, emb_dim)
        X: 2-D csr_matrix, shape=(num_sample, input_dim)
    """
    l1ae_l1_err, l1ae_l1_exact, _ = l1_min_avg_err(A, Y,
                                                   X, use_pos=False)
    l1ae_l1_err_pos, l1ae_l1_exact_pos, _ = l1_min_avg_err(
                                                       A, Y,
                                                       X, use_pos=True)
    res = {}
    res['l1ae_l1_err'] = l1ae_l1_err
    res['l1ae_l1_exact'] = l1ae_l1_exact
    res['l1ae_l1_err_pos'] = l1ae_l1_err_pos
    res['l1ae_l1_exact_pos'] = l1ae_l1_exact_pos
    return res
Example #4
0
def l1_min(X, input_dim, emb_dim):
    """
    Args:
        X: csr_matrix, shape=(num_sample, input_dim)
    """
    # random Gaussian matrix
    G = np.random.randn(input_dim, emb_dim) / np.sqrt(emb_dim)
    Y = X.dot(G)  # sparse.csr_matrix.dot
    # g_err, g_exact, _ = l1_min_avg_err(np.transpose(G), Y, X, use_pos=False)
    g_err_pos, g_exact_pos, _ = l1_min_avg_err(np.transpose(G),
                                               Y,
                                               X,
                                               use_pos=True)
    # random discrete Fourier transform matrix
    # F = np.zeros((input_dim, emb_dim))
    # # select emb_dim/2 rows since the DFT matrix is complex
    # for col in xrange(int(emb_dim/2)):
    #     k = np.random.choice(input_dim, 1)
    #     for row in xrange(input_dim):
    #         F[row, 2*col] = np.cos(-(row*k*2*np.pi)/input_dim)
    #         F[row, 2*col+1] = np.sin(-(row*k*2*np.pi)/input_dim)
    # F = F/np.sqrt(emb_dim/2)
    # Y = X.dot(F)  # sparse.csr_matrix.dot
    # f_err, f_exact, _ = l1_min_avg_err(np.transpose(F), Y, X, use_pos=False)
    # f_err_pos, f_exact_pos, _ = l1_min_avg_err(np.transpose(F), Y, X,
    #                                            use_pos=True)
    res = {}
    # res['l1_g_err'] = g_err
    # res['l1_g_exact'] = g_exact
    res['l1_g_err_pos'] = g_err_pos
    res['l1_g_exact_pos'] = g_exact_pos
    # res['l1_f_err'] = f_err
    # res['l1_f_exact'] = f_exact
    # res['l1_f_err_pos'] = f_err_pos
    # res['l1_f_exact_pos'] = f_exact_pos
    return res
Example #5
0
    for experiment_i in xrange(num_experiment):
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        sess = tf.Session(config=config)
        print("---Dataset: %d, Experiment: %d---" % (dataset_i, experiment_i))
        sparse_AE = L1AE(sess, input_dim, emb_dim, decoder_num_steps)

        print("Start training......")
        sparse_AE.train(X_train, X_valid, batch_size, learning_rate,
                        max_training_epochs, display_interval,
                        validation_interval, max_steps_not_improve)
        # evaluate the autoencoder
        test_sq_loss = sparse_AE.inference(X_test, batch_size)
        G = sparse_AE.sess.run(sparse_AE.encoder_weight)
        Y = X_test.dot(G)
        l1ae_l1_err, l1ae_l1_exact, _ = l1_min_avg_err(np.transpose(G), Y,
                                                       X_test, use_pos=False)
        l1ae_l1_err_pos, l1ae_l1_exact_pos, _ = l1_min_avg_err(
                                                       np.transpose(G), Y,
                                                       X_test, use_pos=True)
        l1ae_cosamp_err, l1ae_cosamp_exact, _ = CoSaMP_block_avg_err(
                                                       np.transpose(G), Y,
                                                       X_test, block_dim,
                                                       sparsity_level,
                                                       use_pos=False)
        l1ae_cosamp_err_pos, l1ae_cosamp_exact_pos, _ = CoSaMP_block_avg_err(
                                                       np.transpose(G), Y,
                                                       X_test, block_dim,
                                                       sparsity_level,
                                                       use_pos=True)
        res = {}
        res['l1ae_err'] = np.sqrt(test_sq_loss)  # RMSE