tf.multinomial
TensorFlow
machine learning
probability distributions
deep learning

How does tf.multinomial work?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

TensorFlow is a widely used open-source machine learning platform that provides a comprehensive library for building machine learning models. One of the functionalities it offers is `tf.multinomial`, a function used to sample random values from a multinomial distribution. Understanding how `tf.multinomial` works can be advantageous in a myriad of applications, particularly where probabilistic sampling is necessary, such as in reinforcement learning, generative models, and simulation tasks.

What is a Multinomial Distribution?

Before delving into `tf.multinomial`, it is crucial to understand the concept of a multinomial distribution. A multinomial distribution is a generalization of the binomial distribution. While a binomial distribution deals with binary outcomes (success/failure), a multinomial distribution handles scenarios where each trial results in one of more than two possible outcomes.

Mathematically, if nn is the number of trials and p1,p2,...,pkp_1, p_2, ..., p_k are the probabilities of the kk possible outcomes, then a random vector X=(X1,X2,...,Xk)X = (X_1, X_2, ..., X_k) has a multinomial distribution if:

P(X=x)=n!x_1!x_2!...x_k!p_1x_1p_2x_2...p_kx_kP(X = x) = \frac{n!}{x\_1!x\_2!...x\_k!} p\_1^{x\_1} p\_2^{x\_2}...p\_k^{x\_k}

where xi0x_i \geq 0 is the number of times outcome ii is observed and xi=n\sum x_i = n.

Understanding `tf.multinomial`

Purpose and Usage

`tf.multinomial` is used to draw samples from a multinomial distribution. The function operates on logits, which are the unnormalized log probabilities of the distribution, and converts them to probabilities to perform the sampling.

Function Syntax

The basic syntax of `tf.multinomial` in TensorFlow v1.x (as it has been deprecated and replaced in TensorFlow 2.x) is:

logits: A 2D tensor of shape `[batch_size, num_classes]`. These are the unnormalized log probabilities. • num_samples: An integer representing the number of independent samples to draw from each row of logits. • seed: An optional integer, which sets the random seed for reproducibility. • name: An optional name for the operation.

• Two different distributions are defined by the logits provided in a 2D tensor. • Five samples are drawn independently from each distribution.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.