在小轩哥的带领下,我也稍微看了一点点 Tensorflow,顺手把轩哥的蟒蛇代码“翻译”成了 R 代码(主要工作就是用 R 里面的 tensorflow 包,然后查找替换 .
为 $
):
数据生成部分:
p1 = .3; mu1 = 0; mu2 = 5; sigma1 = 1; sigma2 = 1.5
N = 1000
x = ifelse(rbinom(N, 1, p1), rnorm(N, mu1, sigma1), rnorm(N, mu2, sigma2))
hist(x, 30)
建模部分:
library(tensorflow)
# Define data
t_x = tf$placeholder(tf$float32)
# Define parameters
t_p1_ = tf$Variable(0, dtype=tf$float32)
t_p1 = tf$nn$softplus(t_p1_)
t_mu1 = tf$Variable(0, dtype=tf$float32)
t_mu2 = tf$Variable(1, dtype=tf$float32)
t_sigma1_ = tf$Variable(1, dtype=tf$float32)
t_sigma1 = tf$nn$softplus(t_sigma1_)
t_sigma2_ = tf$Variable(1, dtype=tf$float32)
t_sigma2 = tf$nn$softplus(t_sigma2_)
ds = tf$contrib$distributions
# Define model and objective function
t_gm = ds$Mixture(
cat=ds$Categorical(probs=c(t_p1, 1 - t_p1)),
components=list(
ds$Normal(t_mu1, t_sigma1),
ds$Normal(t_mu2, t_sigma2)
)
)
t_ll = tf$reduce_mean(t_gm$log_prob(t_x))
# Optimization
optimizer = tf$train$GradientDescentOptimizer(0.5)
train = optimizer$minimize(-t_ll)
# Run
sess = tf$Session()
init = tf$global_variables_initializer()
sess$run(init)
for (i in seq_len(500)) {
sess$run(train, dict(t_x = x))
}
cat('Estimated values:', unlist(sess$run(c(t_p1, t_mu1, t_mu2, t_sigma1, t_sigma2))))
cat('True values:', c(p1, mu1, mu2, sigma1, sigma2))