Ejemplo n.º 1
0
def perfect_features_test():
    from src.env.Amatrix_task import Amatrix

    n = 20
    m = 3
    env = Amatrix(n, m)

    features = env.Amatrix  # perfect features
    weights = np.random.rand(n)

    config = Config()
    config.parameter_size = n
    config.init_alpha = 0.001
    adam = Adam(config)

    sample_size = 100000
    for i in range(sample_size):
        rand_row = np.random.randint(n)
        target = env.sample_target(rand_row, noisy=True)

        pred_features = features[rand_row, :]
        prediction = np.dot(pred_features, weights)
        error = target - prediction
        gradient, new_stepsize, new_weight_vector = adam.update_weight_vector(
            error, pred_features, weights)
        weights = new_weight_vector
        if (i + 1) % 10000 == 0:
            print("Sample number: {0}".format(i + 1))
            print("\tPrediction error:{0}".format(error))

    print("Theta star:\n{0}".format(env.theta_star))
    print("Estimated theta:\n{0}".format(weights))
    difference = np.sqrt(np.sum(np.square(env.theta_star - weights)))
    print("L2 norm of difference:\n{0}".format(difference))
Ejemplo n.º 2
0
def imperfect_features_test():
    from src.env.Amatrix_task import Amatrix

    n = 4
    m = 2
    env = Amatrix(n, m)

    features = env.get_approx_A()  # first m features
    weights = np.random.rand(m)

    config = Config()
    config.parameter_size = m
    config.init_alpha = 0.001
    adam = Adam(config)

    sample_size = 50000
    for i in range(sample_size):
        rand_row = np.random.randint(n)
        target = env.sample_target(rand_row, noisy=True)

        pred_features = features[rand_row, :]
        prediction = np.dot(pred_features, weights)
        error = target - prediction
        gradient, new_stepsize, new_weight_vector = adam.update_weight_vector(
            error, pred_features, weights)
        weights = new_weight_vector
        print("Sample number: {0}".format(i + 1))
        print("\tPrediction error:{0}".format(error))

    print("Theta star:\n{0}".format(env.theta_star))
    print("Estimated theta:\n{0}".format(weights))
Ejemplo n.º 3
0
def adding_bad_features_test():
    from src.env.Amatrix_task import Amatrix

    n = 10
    m = 5
    env = Amatrix(n, m)

    features = env.get_approx_A()  # first m features
    weights = np.zeros(m)

    config = Config()
    config.parameter_size = m
    config.init_alpha = 0.001
    adam = Adam(config)

    sample_size = 50000
    additional_features = 5
    for k in range(additional_features + 1):
        print("Number of features in the representation: {0}".format(
            adam.parameter_size))
        for i in range(sample_size):
            rand_row = np.random.randint(n)
            target = env.sample_target(rand_row, noisy=True)

            pred_features = features[rand_row, :]
            prediction = np.dot(pred_features, weights)
            error = target - prediction
            gradient, new_stepsize, new_weight_vector = adam.update_weight_vector(
                error, pred_features, weights)
            weights = new_weight_vector
            if ((i + 1) % 50000) == 0:
                print("\tSample number: {0}".format(i + 1))
                print("\t\tPrediction error:{0}".format(error))

        print("Theta star:\n{0}".format(env.theta_star))
        print("Estimated theta:\n{0}".format(weights))

        if k < additional_features:
            print("Adding new feature...")
            new_feature = env.get_new_bad_features(1)
            features = np.hstack((features, new_feature))
            adam.increase_size(1)

            new_weights = np.zeros(m + 1)
            new_weights[:m] = weights
            m += 1
            weights = new_weights