tensorflow - 从张量流中的分布中采样非大小张量
问题描述
以下代码:
import tensorflow as tf
tfd = tf.contrib.distributions
mean = [0.0, 0.0]
scale = [1.0, 1.0]
dist = tfd.MultivariateNormalDiag(loc=mean, scale_diag=scale)
samp = dist.sample([None])
给出错误:
TypeError: Expected int32, got None of type '_Message' instead.
但是如果将 None 替换为整数 n,则从分布中生成 n 个样本。有没有办法从分布中获取未知数量的样本?
编辑:最初的问题可能措辞不当;我想对形状 (None, ...) 的张量进行采样,以与这种形状的其他张量结合。很明显,需要在某个地方输入来在运行时修复大小。
解决方案
你可以做
num_samples = tf.placeholder(dtype=tf.int32, shape=())
sampl = dist.sample(num_samples)
然后输入样本数。同样,如果您有一个表示样本数量的标量张量,您可以将其传入。
推荐阅读
- reactjs - 不必要地使用 React.forwardRef() 会带来什么不利吗?
- ios - Xcode Debugger Swift:无法在调试器中查看变量的值,lldb 命令给出错误
- python - Pandas 有没有办法根据某个行值修改以前的行?
- python - VS Code 无法识别新的 Conda 环境
- python - 将数组的列切片为最小大小
- apache-spark - 如何在单行中左连接两个结构数组?
- node.js - 使用 mongodb 和 node js 从 Collection 中获取所有数据
- python - 如何使用熊猫计算excel中特定文本的出现次数
- mysql - .Net Mysql Entity Framework - 创建mysql哈希索引
- reactjs - 如何在没有 useMutation 的情况下在 Next.js 中使用 GraphQL 突变