-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbayers_workflow.py
More file actions
29 lines (22 loc) · 910 Bytes
/
Copy pathbayers_workflow.py
File metadata and controls
29 lines (22 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import tensorflow_probability.python as tfp
tfd = tfp.distributions
p = tfp.distributions.Normal(loc=1., scale=1.)
q = tfp.distributions.Normal(loc=0., scale=2.)
# loss_p = tf.nn.sigmoid_cross_entropy_with_logits(
# labels=tf.zeros_like(sample_p), logits=sample_p)
sample_p = p.sample((5000, 1))
sample_q = q.sample((5000, 1))
models = tf.keras.models.Sequential([
tf.keras.layers.Dense(16, input_dim=1, activation='relu'),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(1)
])
def bin_loss(sample_p, sample_q):
loss_p = tf.nn.sigmoid_cross_entropy_with_logits(
labels=tf.ones_like(sample_p), logits=models(sample_p))
loss_q = tf.nn.sigmoid_cross_entropy_with_logits(
labels=tf.zeros_like(sample_q), logits=models(sample_q))
return tf.reduce_mean(loss_q + loss_q)