File size: 1,285 Bytes
a0224ec
73104e4
 
817aeb7
 
 
 
 
 
1122469
817aeb7
 
 
 
 
 
 
1122469
 
 
 
 
817aeb7
1122469
817aeb7
 
 
 
 
 
 
 
73104e4
817aeb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73104e4
817aeb7
 
 
 
 
 
 
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow_probability as tfp
import pandas as pd
tfd = tfp.distributions
tfl = tfp.layers

st.title("1 dimensional normal distribution")
mean = st.slider('Mean', -5, 5, 0)
std = st.slider('Scale', 0, 5, 1)

p = tfd.Normal(2, 1)

z = f"""\\begin{{array}}{{cc}}
  \mu & {mean} \\\\
  \sigma & {std}
\\end{{array}}
"""

st.latex(z)


q = tfd.Normal(mean, std)
z_values = tf.linspace(-5, 5, 200)
z_values = tf.cast(z_values, tf.float32)
prob_values_p = p.prob(z_values)
prob_values_q = q.prob(z_values)

fig, ax = plt.subplots()
ax.plot(z_values, prob_values_p, label=r'p', linestyle='--', lw=5, alpha=0.5)
ax.plot(z_values, prob_values_q, label=r'q')

ax.set_xlabel("x")
ax.set_ylabel("PDF(x)")
ax.legend()
ax.set_ylim((0, 1))


kl = tfd.kl_divergence(q, p)
st.latex(f"D_{{KL}}(q||p) is : {kl:0.2f}")

#arr = np.random.normal(mean, std, size=10000)

##ax.hist(arr, bins=20)
#sns.despine()
st.pyplot(fig)
hide_streamlit_style = """
            <style>
            #MainMenu {visibility: hidden;}
            footer {visibility: hidden;}
            </style>
            """
st.markdown(hide_streamlit_style, unsafe_allow_html=True)