C.W.K.
Stream
Lesson 01 of 05 · published

TF Probability and TF Decision Forests

~11 min · tfp, tf-df, tabular, uncertainty

Level 0Level 0
0 XP0/78 lessons0/17 achievements
0/100 XP to next level100 XP to go0% complete

The libraries that fix what neural networks don't do well

Standard neural networks output a single point estimate. Probabilistic models output distributions, capturing uncertainty. Critical for medical diagnosis (need confidence intervals, not just "tumor/not"), financial forecasting, safety-critical robotics.

TensorFlow Probability (TFP) provides distributions, bijectors (for normalizing flows), MCMC samplers, and DenseVariational layers for Bayesian neural networks.

TensorFlow Decision Forests (TF-DF) brings random forests and gradient boosted trees into the Keras API. For tabular data (the most common data type in enterprise ML), GBT typically outperforms neural networks unless you have millions of rows. TF-DF gives you that power with Keras conventions, TFX integration, and TF Serving compatibility — no toolkit-switching.

Code

TFP — distributions, Bayesian NN·python
import tensorflow as tf
import tensorflow_probability as tfp

tfd = tfp.distributions

# Distributions
normal = tfd.Normal(loc=0., scale=1.)
samples = normal.sample(100)
log_prob = normal.log_prob(0.5)

# Mixture of Gaussians
mixture = tfd.MixtureSameFamily(
    mixture_distribution=tfd.Categorical(probs=[0.3, 0.7]),
    components_distribution=tfd.Normal(loc=[-2., 2.], scale=[0.5, 0.5]),
)

# Bayesian neural network for uncertainty
model = tf.keras.Sequential([
    tfp.layers.DenseVariational(
        units=64,
        make_prior_fn=prior,
        make_posterior_fn=posterior,
    ),
    tf.keras.layers.Dense(1),
])
TF Decision Forests — tabular's best friend·python
import tensorflow_decision_forests as tfdf
import pandas as pd

# pip install tensorflow_decision_forests
df = pd.read_csv("adult_income.csv")
train_df, test_df = df[:800], df[800:]

train_ds = tfdf.keras.pd_dataframe_to_tf_dataset(train_df, label="income")
test_ds  = tfdf.keras.pd_dataframe_to_tf_dataset(test_df,  label="income")

# Random Forest — no LR tuning, no feature normalization needed
model = tfdf.keras.RandomForestModel()
model.fit(train_ds)

model.compile(metrics=["accuracy"])
print(model.evaluate(test_ds, return_dict=True))
model.summary()                 # built-in feature importance

# Save as TF SavedModel for deployment
model.save("decision_forest_model/")

External links

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.