Mozzie
Mozzie

Reputation: 353

Tensorflow mix two multivariate distribution

I would like to mix two multivariate distribution in tensorflow. For example:

import tensorflow_probability as tfp
import tensorflow as tf
import numpy as np
tfd = tfp.distributions

#mean,var,pi have the same shape(3,4).
mean = tf.convert_to_tensor(np.arange(12.0).reshape(3,4))
var = mean
dist = tfd.Normal(loc=mean,scale=var)
pi = tf.ones_like(mean)
mix = tfd.Mixture(cat = tfd.Categorical(probs=[pi,1-pi]),components=[dist,dist])

However, it got the error as follows:

ValueError: Dimensions 2 and 3 are not compatible

ValueError: Shapes (2, 3) and (3, 4) are not compatible

Can I mix two multivariate distribution in tensorflow?

Upvotes: 2

Views: 491

Answers (2)

Anatoly
Anatoly

Reputation: 113

the trick is, it appears, that number of categories needs to be the last dimension in probs, this code works for me:

In:

mix = tfd.Mixture(cat = tfd.Categorical(probs=tf.stack([pi,1-pi],axis=-1)),components=[dist,dist])
mix

Out:

<tfp.distributions.Mixture 'Mixture' batch_shape=[3, 4] event_shape=[] dtype=float64>

Upvotes: 0

MPękalski
MPękalski

Reputation: 7103

Try if this solves your issue

import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
tfd = tfp.distributions 

#mean,var,pi have the same shape(3,4).
mean = tf.convert_to_tensor(np.arange(12.0).reshape(3,4))
var = mean
dist = tfd.Normal(loc=-1., scale=0.1)

pi = tf.transpose(tf.ones_like(mean))

mix = tfd.Mixture(cat = tfd.Categorical(probs=[pi/3,
                                               pi/3,
                                               pi/3]), 
                  components=[tfd.Normal(loc=mean,scale=var), 
                              tfd.Normal(loc=mean,scale=var), 
                              tfd.Normal(loc=mean,scale=var)]
                 )

mix.event_shape_tensor

output

<bound method Distribution.event_shape_tensor of <tfp.distributions.Mixture 'Mixture_11/' batch_shape=(3, 4) event_shape=() dtype=float64>>

Upvotes: 1

Related Questions