Last active
May 19, 2021 09:28
-
-
Save 18182324/4e744538354967a1ce5be66dc5b780b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Execute this cell to be sure to have a compatible TF (2.0) and TFP (0.8) version. | |
# If you are bold you can skip this cell. | |
try: #If running in colab | |
import google.colab | |
!pip install tensorflow==2.0.0 | |
!pip install tensorflow_probability==0.8.0 | |
except: | |
print('Not running in colab') | |
try: #If running in colab | |
import google.colab | |
IN_COLAB = True | |
%tensorflow_version 2.x | |
except: | |
IN_COLAB = False | |
#Imports | |
import tensorflow as tf | |
if (not tf.__version__.startswith('2')): #Checking if tf 2.0 is installed | |
print('Please install tensorflow 2.0 to run this notebook') | |
print('Tensorflow version: ',tf.__version__, ' running in colab?: ', IN_COLAB) | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from sklearn.model_selection import train_test_split | |
import tensorflow_probability as tfp | |
%matplotlib inline | |
plt.style.use('default') | |
tfd = tfp.distributions | |
tfb = tfp.bijectors | |
print("TFP Version", tfp.__version__) | |
print("TF Version",tf.__version__) | |
np.random.seed(42) | |
tf.random.set_seed(42) | |
#Working with a TFP normal distribution | |
# 10 20 30 40 50 55 | |
#123456789012345678901234567890123456789012345678901234 | |
import tensorflow_probability as tfp | |
tfd = tfp.distributions | |
d = tfd.Normal(loc=3, scale=1.5) #A | |
x = d.sample(2) # Draw two random points. #B | |
px = d.prob(x) # Compute density/mass. #C | |
print(x) | |
print(px) | |
#A create a 1D Normal distribution with mean 3 and standard deviation 1.5 | |
#B sample 2 realizations from the Normal distribution | |
#C compute the likelihood for each of the two sampled values under the defined Normal distribution | |
dist = tfd.Normal(loc=1.0, scale=0.1) | |
print('sample :', dist.sample(3).numpy()) #Samples 3 numbers | |
print('prob :',dist.prob((0,1,2)).numpy()) #Calculates the probabilities for positions 0,1,2 | |
print('log_prob :',dist.log_prob((0,1,2)).numpy()) #Same as above just log | |
print('cdf :',dist.cdf((0,1,2)).numpy()) #Calculates the cummulative distributions | |
print('mean :',dist.mean().numpy()) #Returns the mean of the distribution | |
print('stddev :',dist.stddev().numpy()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment