本文整理汇总了Python中edward.models.Normal.stddev方法的典型用法代码示例。如果您正苦于以下问题:Python Normal.stddev方法的具体用法?Python Normal.stddev怎么用?Python Normal.stddev使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类edward.models.Normal
的用法示例。
在下文中一共展示了Normal.stddev方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _test_normal_normal
# 需要导入模块: from edward.models import Normal [as 别名]
# 或者: from edward.models.Normal import stddev [as 别名]
def _test_normal_normal(self, Inference, default, *args, **kwargs):
with self.test_session() as sess:
x_data = np.array([0.0] * 50, dtype=np.float32)
mu = Normal(loc=0.0, scale=1.0)
x = Normal(loc=mu, scale=1.0, sample_shape=50)
if not default:
qmu_loc = tf.Variable(tf.random_normal([]))
qmu_scale = tf.nn.softplus(tf.Variable(tf.random_normal([])))
qmu = Normal(loc=qmu_loc, scale=qmu_scale)
# analytic solution: N(loc=0.0, scale=\sqrt{1/51}=0.140)
inference = Inference({mu: qmu}, data={x: x_data})
else:
inference = Inference([mu], data={x: x_data})
qmu = inference.latent_vars[mu]
inference.run(*args, **kwargs)
self.assertAllClose(qmu.mean().eval(), 0, rtol=0.1, atol=0.6)
self.assertAllClose(qmu.stddev().eval(), np.sqrt(1 / 51),
rtol=0.15, atol=0.5)
variables = tf.get_collection(
tf.GraphKeys.GLOBAL_VARIABLES, scope='optimizer')
old_t, old_variables = sess.run([inference.t, variables])
self.assertEqual(old_t, inference.n_iter)
sess.run(inference.reset)
new_t, new_variables = sess.run([inference.t, variables])
self.assertEqual(new_t, 0)
self.assertNotEqual(old_variables, new_variables)
示例2: test_normalnormal_run
# 需要导入模块: from edward.models import Normal [as 别名]
# 或者: from edward.models.Normal import stddev [as 别名]
def test_normalnormal_run(self):
with self.test_session() as sess:
x_data = np.array([0.0] * 50, dtype=np.float32)
mu = Normal(loc=0.0, scale=1.0)
x = Normal(loc=tf.ones(50) * mu, scale=1.0)
qmu_loc = tf.Variable(tf.random_normal([]))
qmu_scale = tf.nn.softplus(tf.Variable(tf.random_normal([])))
qmu = Normal(loc=qmu_loc, scale=qmu_scale)
# analytic solution: N(loc=0.0, scale=\sqrt{1/51}=0.140)
inference = ed.KLpq({mu: qmu}, data={x: x_data})
inference.run(n_samples=25, n_iter=100)
self.assertAllClose(qmu.mean().eval(), 0, rtol=1e-1, atol=1e-1)
self.assertAllClose(qmu.stddev().eval(), np.sqrt(1 / 51),
rtol=1e-1, atol=1e-1)
示例3: main
# 需要导入模块: from edward.models import Normal [as 别名]
# 或者: from edward.models.Normal import stddev [as 别名]
def main(_):
def ratio_estimator(data, local_vars, global_vars):
"""Takes as input a dict of data x, local variable samples z, and
global variable samples beta; outputs real values of shape
(x.shape[0] + z.shape[0],). In this example, there are no local
variables.
"""
# data[y] has shape (M,); global_vars[w] has shape (D,)
# we concatenate w to each data point y, so input has shape (M, 1 + D)
input = tf.concat([
tf.reshape(data[y], [FLAGS.M, 1]),
tf.tile(tf.reshape(global_vars[w], [1, FLAGS.D]), [FLAGS.M, 1])], 1)
hidden = tf.layers.dense(input, 64, activation=tf.nn.relu)
output = tf.layers.dense(hidden, 1, activation=None)
return output
ed.set_seed(42)
# DATA
w_true = np.ones(FLAGS.D) * 5.0
X_train, y_train = build_toy_dataset(FLAGS.N, w_true)
X_test, y_test = build_toy_dataset(FLAGS.N, w_true)
data = generator([X_train, y_train], FLAGS.M)
# MODEL
X = tf.placeholder(tf.float32, [FLAGS.M, FLAGS.D])
y_ph = tf.placeholder(tf.float32, [FLAGS.M])
w = Normal(loc=tf.zeros(FLAGS.D), scale=tf.ones(FLAGS.D))
y = Normal(loc=ed.dot(X, w), scale=tf.ones(FLAGS.M))
# INFERENCE
qw = Normal(loc=tf.get_variable("qw/loc", [FLAGS.D]) + 1.0,
scale=tf.nn.softplus(tf.get_variable("qw/scale", [FLAGS.D])))
inference = ed.ImplicitKLqp(
{w: qw}, data={y: y_ph},
discriminator=ratio_estimator, global_vars={w: qw})
inference.initialize(n_iter=5000, n_print=100,
scale={y: float(FLAGS.N) / FLAGS.M})
sess = ed.get_session()
tf.global_variables_initializer().run()
for _ in range(inference.n_iter):
X_batch, y_batch = next(data)
for _ in range(5):
info_dict_d = inference.update(
variables="Disc", feed_dict={X: X_batch, y_ph: y_batch})
info_dict = inference.update(
variables="Gen", feed_dict={X: X_batch, y_ph: y_batch})
info_dict['loss_d'] = info_dict_d['loss_d']
info_dict['t'] = info_dict['t'] // 6 # say set of 6 updates is 1 iteration
t = info_dict['t']
inference.print_progress(info_dict)
if t == 1 or t % inference.n_print == 0:
# Check inferred posterior parameters.
mean, std = sess.run([qw.mean(), qw.stddev()])
print("\nInferred mean & std:")
print(mean)
print(std)
示例4: Normal
# 需要导入模块: from edward.models import Normal [as 别名]
# 或者: from edward.models.Normal import stddev [as 别名]
qw = Normal(loc=tf.Variable(tf.random_normal([D]) + 1.0),
scale=tf.nn.softplus(tf.Variable(tf.random_normal([D]))))
inference = ed.ImplicitKLqp(
{w: qw}, data={y: y_ph},
discriminator=ratio_estimator, global_vars={w: qw})
inference.initialize(n_iter=5000, n_print=100, scale={y: float(N) / M})
sess = ed.get_session()
tf.global_variables_initializer().run()
for _ in range(inference.n_iter):
X_batch, y_batch = next(data)
for _ in range(5):
info_dict_d = inference.update(
variables="Disc", feed_dict={X: X_batch, y_ph: y_batch})
info_dict = inference.update(
variables="Gen", feed_dict={X: X_batch, y_ph: y_batch})
info_dict['loss_d'] = info_dict_d['loss_d']
info_dict['t'] = info_dict['t'] // 6 # say set of 6 updates is 1 iteration
t = info_dict['t']
inference.print_progress(info_dict)
if t == 1 or t % inference.n_print == 0:
# Check inferred posterior parameters.
mean, std = sess.run([qw.mean(), qw.stddev()])
print("\nInferred mean & std:")
print(mean)
print(std)