tensorflow / tensorflow/probability

tfd.Categorical.log_prob returns wrong dtype

Open
#973 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Jupyter Notebook
Stars
4.4k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

In tfd.Categorical, the log_prob method derives its return type from the logits (or probs) constructor argument, as in the following:

logits32 = tf.constant([1,2,3,4], dtype=tf.float32)
tfd.Categorical(logits=logits).log_prob(2).dtype == tf.float32 # True

logits64 = tf.constant([1,2,3,4], dtype=tf.float64)
tfd.Categorical(logits=logits).log_prob(2).dtype == tf.float64 # True

However, if a list (batch) of [logits] is supplied, an unexpected type demotion occurs such that the return value of log_prob defaults tf.float32:

logits64 = tf.constant([1,2,3,4], dtype=tf.float64)
tfd.Categorical(logits=[logits]).log_prob(2).dtype == tf.float64 # False

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at tfd.Categorical.log_prob and reproduce the reported dtype difference between a direct float64 logits tensor and a one-element list containing it. Trace how the batched constructor argument determines the return dtype, then add a regression test showing that the list form preserves float64 and run the relevant Categorical tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
tensorflow
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.