tensorflow - 如何将此 Edward 代码转换为 TFP 代码?
问题描述
我在 Edward 中编写了概率矩阵分解模型。我正在尝试将其移植到 TFP,但我不确定如何定义对数似然和 KL 散度项。这是爱德华的代码 -
# MODEL
U = Normal(
loc=0.0,
scale=1.0,
sample_shape=[n_latent_dims, batch_size])
V = Normal(
loc=0.0,
scale=1.0,
sample_shape=[n_latent_dims, n_features])
R = Bernoulli(logits=tf.matmul(tf.transpose(U), V))
R_ph = tf.placeholder(tf.int32, [None, n_features])
# INFERENCE
qU = Normal(
loc=tf.get_variable("qU/loc",
[n_latent_dims, batch_size]),
scale=tf.nn.softplus(
tf.get_variable("qU/scale",
[n_latent_dims, batch_size])))
qV = Normal(
loc=tf.get_variable("qV/loc",
[n_latent_dims, n_features]),
scale=tf.nn.softplus(
tf.get_variable("qV/scale",
[n_latent_dims, n_features])))
qR = Bernoulli(logits=tf.matmul(tf.transpose(qU), qV))
qR_avg = Bernoulli(
logits=tf.reduce_mean(qR.parameters['logits'], 0))
log_likli = tf.reduce_mean(qR_avg.log_prob(R_ph), 1)
inference = ed.KLqp(
{
U: qU,
V: qV
}, data={self.R: self.R_ph})
inference_init = inference.initialize()
init = tf.global_variables_initializer()
解决方案
推荐阅读
- rust-cargo - 关于 Cargo.toml 依赖项中的 toml 换行符
- jquery - 用于日历的 jQuery UI 可拖放和可拖动
- emulation - 6502 个指令时钟周期背后的逻辑是什么?
- androidx - 替换已弃用的 BrowseFragment AndroidTV
- python - 根据另一列的掩码修改 Pandas 数据框列的符号?
- ios - 我试图让输入的数据进行计算并显示在标签中
- python - TFIDF 向量化器:具有多个元素的数组的真值是不明确的。使用 a.any() 或 a.all()
- c# - 过滤级联 CheckedListBox 并保留项目的检查状态
- html - 为什么我不能让 lang 成为一个元素?
- android - 如何使用 Retrofit2 处理嵌套 JsonObjects 的动态字段