python - Tensorflow 混合两种多元分布
问题描述
我想在张量流中混合两个多元分布。例如:
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])
但是,它得到了如下错误:
ValueError:尺寸 2 和 3 不兼容
ValueError:形状 (2, 3) 和 (3, 4) 不兼容
我可以在张量流中混合两个多元分布吗?
解决方案
试试这是否能解决您的问题
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
输出
<bound method Distribution.event_shape_tensor of <tfp.distributions.Mixture 'Mixture_11/' batch_shape=(3, 4) event_shape=() dtype=float64>>
推荐阅读
- plotly-dash - 情节虚线图中随机缺少一些条形图
- c# - 单击运行宏的按钮时将更新的 C# 进度条
- java - 从工作表(不是工作簿)Apache POI 中检索“本地”命名范围
- amazon-s3 - 如何在 docker-compose 中将 S3v2 api 设置为 Minio(AWS S3 本地)
- ios - 为 MKPinAnnotationView 中的按钮添加操作
- git - 恢复分支 A 上的提交,该分支稍后将合并到 B。提交应该留在 B
- c - C 中多层实现的错误处理
- mysql - 如何将mysql cmd中的数据类型从int更改为date?
- amazon-cloudwatch - 是什么导致 cloudwatch 代理日志中的“值不能为负”?
- javascript - 在从 javascript 执行的 html 中循环添加图像的问题