コード例 #1
0
 def test_normal_mixture_hard(self):
     np.random.seed(0)
     size_batch = 1000
     competition = AdversarialCompetition(
         size_batch=size_batch,
         true_model=GenerativeNormalMixtureModel(
             np.arange(-3, 4),
             np.random.uniform(1, 2, 7).round(2)),
         discriminative=pipeline.make_pipeline(
             preprocessing.PolynomialFeatures(4),
             linear_model.LogisticRegression()),
         generative=GenerativeNormalMixtureModel(np.arange(-3, 4) * 0.1,
                                                 np.ones(7),
                                                 updates=["mu", "sigma"]),
         gradient_descent=GradientDescent(np.array([0.3, 0.1, 0.3]).reshape(
             (-1, 1)),
                                          inertia=0.9,
                                          annealing=2000,
                                          last_learning_rate=0.001),
     )
     for i in range(5000):
         competition.iteration()
     params = competition.generatives[-1]._params
     print params.shape
     true_params = competition.true_model._params
     np.testing.assert_allclose(params, true_params, 0, 0.2)
コード例 #2
0
 def test_normal_1000(self):
     np.random.seed(0)
     size_batch = 1000
     adversarials = AdversarialCompetition(
         size_batch=size_batch,
         true_model=GenerativeNormalModel(1, 2),
         discriminative=pipeline.make_pipeline(
             preprocessing.PolynomialFeatures(4),
             linear_model.LogisticRegression()),
         generative=GenerativeNormalModel(0, 1, updates=["mu", "sigma"]),
         gradient_descent=GradientDescent(0.03, 0.9),
     )
     for i in range(200):
         adversarials.iteration()
     params = adversarials.generatives[-1]._params
     true_params = adversarials.true_model._params
     np.testing.assert_allclose(params, true_params, 0, 0.02)
コード例 #3
0
 def test_normal_1000(self):
     np.random.seed(0)
     size_batch = 1000
     adversarials = AdversarialCompetition(
         size_batch=size_batch,
         true_model=GenerativeNormalModel(1, 2),
         discriminative=pipeline.make_pipeline(
             preprocessing.PolynomialFeatures(4),
             linear_model.LogisticRegression()),
         generative=GenerativeNormalModel(0, 1, updates=["mu", "sigma"]),
         gradient_descent=GradientDescent(0.03, 0.9),
     )
     for i in range(200):
         adversarials.iteration()
     params = adversarials.generatives[-1]._params
     true_params = adversarials.true_model._params
     np.testing.assert_allclose(params, true_params, 0, 0.02)
コード例 #4
0
 def test_normal_mixture(self):
     np.random.seed(0)
     size_batch = 1000
     competition = AdversarialCompetition(
         size_batch=size_batch,
         true_model=GenerativeNormalMixtureModel([-3, 3], [1, 1]),
         discriminative=pipeline.make_pipeline(
             preprocessing.PolynomialFeatures(4),
             linear_model.LogisticRegression()),
         generative=GenerativeNormalMixtureModel(
             [-1, 1], [1, 1], updates=["mu", "sigma"]),
         gradient_descent=GradientDescent(
             0.1, inertia=0.9, annealing=1000, last_learning_rate=0.01),
     )
     for i in range(2000):
         competition.iteration()
     params = competition.generatives[-1]._params
     true_params = competition.true_model._params
     np.testing.assert_allclose(params, true_params, 0, 0.1)
コード例 #5
0
 def test_normal_mixture(self):
     np.random.seed(0)
     size_batch = 1000
     competition = AdversarialCompetition(
         size_batch=size_batch,
         true_model=GenerativeNormalMixtureModel([-3, 3], [1, 1]),
         discriminative=pipeline.make_pipeline(
             preprocessing.PolynomialFeatures(4),
             linear_model.LogisticRegression()),
         generative=GenerativeNormalMixtureModel([-1, 1], [1, 1],
                                                 updates=["mu", "sigma"]),
         gradient_descent=GradientDescent(0.1,
                                          inertia=0.9,
                                          annealing=1000,
                                          last_learning_rate=0.01),
     )
     for i in range(2000):
         competition.iteration()
     params = competition.generatives[-1]._params
     true_params = competition.true_model._params
     np.testing.assert_allclose(params, true_params, 0, 0.1)
コード例 #6
0
 def test_normal_mixture_hard(self):
     np.random.seed(0)
     size_batch = 1000
     competition = AdversarialCompetition(
         size_batch=size_batch,
         true_model=GenerativeNormalMixtureModel(
             np.arange(-3, 4), np.random.uniform(1, 2, 7).round(2)),
         discriminative=pipeline.make_pipeline(
             preprocessing.PolynomialFeatures(4),
             linear_model.LogisticRegression()),
         generative=GenerativeNormalMixtureModel(
             np.arange(-3, 4) * 0.1, np.ones(7), updates=["mu", "sigma"]),
         gradient_descent=GradientDescent(
             np.array([0.3, 0.1, 0.3]).reshape((-1, 1)), inertia=0.9,
             annealing=2000, last_learning_rate=0.001),
     )
     for i in range(5000):
         competition.iteration()
     params = competition.generatives[-1]._params
     print params.shape
     true_params = competition.true_model._params
     np.testing.assert_allclose(params, true_params, 0, 0.2)
コード例 #7
0
from competition import AdversarialCompetition
from models import GenerativeNormalMixtureModel
from gradient_descent import GradientDescent

logging.basicConfig(
    format="[%(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s",
    level="INFO")

np.random.seed(0)
size_batch = 1000

competition = AdversarialCompetition(
    size_batch=size_batch,
    true_model=GenerativeNormalMixtureModel([-3, 3], [1, 1]),
    discriminative=pipeline.make_pipeline(preprocessing.PolynomialFeatures(4),
                                          linear_model.LogisticRegression()),
    generative=GenerativeNormalMixtureModel([-1, 1], [1, 1],
                                            updates=["mu", "sigma"]),
    gradient_descent=GradientDescent(0.01, 0.5),
)

print(competition)

for i in range(1000):
    if i % 200 == 0:
        competition.plot()
        plt.show()
        pass
    competition.iteration()

print("final model %s" % competition.generatives[-1])
コード例 #8
0
from gradient_descent import GradientDescent

pyplot.ioff()

logging.basicConfig(
    format=
    "[%(filename)s:%(lineno)s - %(funcName)15s() %(asctime)-15s ] %(message)s",
    level=logging.DEBUG)

np.random.seed(0)
size_batch = 1000

competition = AdversarialCompetition(
    size_batch=size_batch,
    true_model=GenerativeNormalModel(1, 2),
    discriminative=pipeline.make_pipeline(preprocessing.PolynomialFeatures(4),
                                          linear_model.LogisticRegression()),
    generative=GenerativeNormalModel(0, 1, updates=["mu", "sigma"]),
    gradient_descent=GradientDescent(0.03, inertia=0.0, annealing=100),
)

print(competition)

for i in range(500):
    if i % 50 == 0:
        competition.plot()
        pyplot.savefig('file.png')
        pyplot.close()
        pass
    competition.iteration()

print("final model %s" % competition.generatives[-1])
コード例 #9
0
from competition import AdversarialCompetition
from models import GenerativeNormalModel
from gradient_descent import GradientDescent

logging.basicConfig(format="[%(filename)s:%(lineno)s - %(funcName)15s() %(asctime)-15s ] %(message)s", level=logging.DEBUG)

np.random.seed(0)
size_batch = 1000

competition = AdversarialCompetition(
    size_batch=size_batch,
    true_model=GenerativeNormalModel(1, 2),
    discriminative=pipeline.make_pipeline(
        preprocessing.PolynomialFeatures(4),
        linear_model.LogisticRegression()),
    generative=GenerativeNormalModel(
        0, 1, updates=["mu", "sigma"]),
    gradient_descent=GradientDescent(
        0.03, inertia=0.0, annealing=100),
)

print(competition)

for i in range(200):
    if i % 50 == 0:
        competition.plot()
        plt.show()
        pass
    competition.iteration()
コード例 #10
0
np.random.seed(0)
size_batch = 100
mu_param = 3
sigma_param = 1.0
sample_size = 1000
true_data1 = GenerativeNormalModel(mu_param,sigma_param).random(sample_size).reshape(-1)
true_data2 = GenerativeNormalModel(-mu_param,sigma_param).random(sample_size).reshape(-1)
true_data = np.concatenate((true_data1,true_data2))

competition1 = AdversarialCompetition(
    size_batch=size_batch,
    true_model=GenerativeNormalModel(mu_param, sigma_param),
    discriminative=pipeline.make_pipeline(
        preprocessing.PolynomialFeatures(4),
        linear_model.LogisticRegression()),
    generative=GenerativeNormalModel(
        2.5,2.1,updates=["mu", "sigma"]),
    gradient_descent=GradientDescent(
        0.03, inertia=0.0, annealing=100),
    x_dataset = true_data1
)

competition2 = AdversarialCompetition(
    size_batch=size_batch,
    true_model=GenerativeNormalModel(-mu_param, sigma_param),
    discriminative=pipeline.make_pipeline(
        preprocessing.PolynomialFeatures(4),
        linear_model.LogisticRegression()),
    generative=GenerativeNormalModel(
        -2.5,2.1,updates=["mu", "sigma"]),
    gradient_descent=GradientDescent(