Beispiel #1
0
    def test_log_likelihood_sym(self, output_dim, input_shape):
        bmr = BernoulliMLPRegressor(input_shape=(input_shape[1], ),
                                    output_dim=output_dim)

        new_xs_var = tf.compat.v1.placeholder(tf.float32, input_shape)
        new_ys_var = tf.compat.v1.placeholder(dtype=tf.float32,
                                              name='ys',
                                              shape=(None, output_dim))

        data = np.full(input_shape, 0.5)
        one_hot_label = np.zeros((input_shape[0], output_dim))
        one_hot_label[np.arange(input_shape[0]), 0] = 1

        p = bmr._f_prob(np.asarray(data))
        ll = bmr._dist.log_likelihood(np.asarray(one_hot_label), dict(p=p))

        outputs = bmr.log_likelihood_sym(new_xs_var, new_ys_var, name='ll_sym')

        ll_from_sym = self.sess.run(outputs,
                                    feed_dict={
                                        new_xs_var: data,
                                        new_ys_var: one_hot_label
                                    })

        assert np.allclose(ll, ll_from_sym, rtol=0, atol=1e-5)
Beispiel #2
0
    def test_sample_predict(self):
        n_sample = 100
        input_dim = 50
        output_dim = 1
        bmr = BernoulliMLPRegressor(input_shape=(input_dim, ),
                                    output_dim=output_dim)

        xs = np.random.random((input_dim, ))
        p = bmr._f_prob([xs])
        ys = bmr.sample_predict([xs] * n_sample)
        p_predict = np.count_nonzero(ys == 1) / n_sample

        assert np.real_if_close(p, p_predict)
Beispiel #3
0
    def test_predict_log_likelihood(self):
        n_sample = 50
        input_dim = 50
        output_dim = 1
        bmr = BernoulliMLPRegressor(input_shape=(input_dim, ),
                                    output_dim=output_dim)

        xs = np.random.random((n_sample, input_dim))
        ys = np.random.randint(2, size=(n_sample, output_dim))
        p = bmr._f_prob(xs)
        ll = bmr.predict_log_likelihood(xs, ys)
        ll_true = np.sum(np.log(p * ys + (1 - p) * (1 - ys)), axis=-1)

        assert np.allclose(ll, ll_true)