Esempio n. 1
0
    def test_mog_models2(self):
        def sample(n):
            x = np.random.uniform(size=2)
            return (n + 1) * x[0] if x[0]**n > x[1] else sample(n)

        n_comp = 20

        x = np.random.uniform(high=2, size=2000)
        t = np.array([sample(n) for n in x])

        x_network = keras.Sequential([
            keras.layers.Dense(10, activation='relu'),
            keras.layers.Dense(10, activation='relu'),
            keras.layers.Dense(10, activation='relu')
        ])

        x_input, t_input = [keras.layers.Input(shape=(d, )) for d in [1, 1]]

        pi, mu, sig = mog_model(n_comp, 10, 1)(x_network(x_input))
        ll = mog_loss_model(n_comp, 1)([pi, mu, sig, t_input])

        model = keras.engine.Model([x_input, t_input], [ll])
        model.add_loss(K.mean(ll))
        model.compile('nadam')
        model.fit([x, t], [], epochs=100)

        model2 = keras.engine.Model([x_input], [pi, mu, sig])
        import matplotlib
        matplotlib.use('Agg')
        import matplotlib.pyplot as plt
        for x in [0, 1, 2]:
            pi, mu, sig = model2.predict(np.array([[x]]))
            mu = mu.reshape(-1)

            def f(t):
                return np.sum(pi / (np.sqrt(2 * np.pi) * sig) *
                              np.exp(-np.square((t - mu) / sig) / 2))

            ts = np.linspace(-0.1, x + 1.1, 100)
            plt.figure()
            plt.plot(ts, [f(t) for t in ts])
            plt.plot(ts, [(t / (x + 1))**x for t in ts])
            plt.show()
Esempio n. 2
0
    def test_mog_models(self):
        d = 2
        n = 5

        theta = np.random.uniform(low=0.0, high=2 * np.pi, size=(5000, d))
        x = 10 * np.cos(theta) + np.random.normal(size=(5000, d))
        t = 10 * np.sin(theta) + np.random.normal(size=(5000, d))

        x_input = keras.layers.Input(shape=(d,))
        l1 = keras.layers.Dense(10, activation='relu')
        l2 = keras.layers.Dense(10, activation='relu')
        l3 = keras.layers.Dense(10, activation='relu')

        def norm(l):
            return l  # keras.layers.BatchNormalization()

        x_network = l3(norm(l2(norm(l1(x_input)))))

        t_input = keras.layers.Input(shape=(d,))

        pi, mu, sig = mog_model(n, 10, d)(x_network)
        ll = mog_loss_model(n, d)([pi, mu, sig, t_input])
        samp = mog_sample_model(n, d)

        samp2 = keras.layers.Concatenate()([samp([pi, mu, sig]), samp([pi, mu, sig])])

        # pi,mu,sig = MixtureOfGaussians(n, d)(x_network)
        # ll = MixtureOfGaussiansLogLoss(n, d)([pi,mu,sig,t_input])
        model = keras.engine.Model([x_input, t_input], [ll])
        model.add_loss(K.mean(ll))
        model.compile('nadam')
        model.fit([x, t], [], epochs=5)

        # For some reason this doesn't work at all when run against the CNTK backend...
        # model.compile('nadam', loss=lambda _,l:l)
        # model.fit([x,t], [np.zeros((5000,1))], epochs=500)

        model2 = keras.engine.Model([x_input], [pi, mu, sig])
        model3 = keras.engine.Model([x_input], [samp([pi, mu, sig])])
        model4 = keras.engine.Model([x_input], [samp2])

        print("samp2: {}".format(model4.predict(np.array([[0., 0.]]))))

        for x_i in [-10, -5, 0, 5, 10]:
            t = np.array([[np.sqrt(100 - x_i**2), -np.sqrt(100 - x_i**2)]])
            outs = model2.predict([np.array([[x_i, x_i]])])
            print(x_i, outs)

        # generate a valiation set
        x = 10 * np.cos(theta) + np.random.normal(size=(5000, d))
        t = 10 * np.sin(theta) + np.random.normal(size=(5000, d))
        pi, mu, sig = model2.predict([x])
        sampled_t = model3.predict([x])
        llm = model.predict([x, t])

        pi_o = np.tile([[0.25, 0.25, 0.25, 0.25, 0]], (5000, 1))
        x2 = np.sqrt(np.maximum(0, 100 - x**2)).reshape(-1, 1, 2)
        arrs = [np.array([f1, f2]) for f1 in [-1, 1] for f2 in [-1, 1]] + [np.zeros(2)]
        mu_o = np.concatenate([x2 * arr for arr in arrs], axis=1)
        sig_o = np.ones((5000, 5))

        print(pi[0], mu[0], sig[0], x[0], t[0])
        import io
        with io.open("sampled_{}.csv".format(K.backend()), 'w') as f:
            for (x1, x2), (t1, t2) in zip(x, sampled_t):
                f.write("{},{},{},{}\n".format(x1, t1, x2, t2))