如何使用Tensorflow最小化功能

时间:2019-12-17 14:49:08

标签: r tensorflow optimization

我正在与TensorFlow一起尝试估计统计分布的参数。作为损失函数,我正在使用对数似然函数的负数,并且正在使用梯度下降优化器,但是估计确实很差。这就是我所拥有的:

alpha1 <- 1
sigma1 <- 2
beta1 <- 1.5
mu1 <- 1
k1 <- 1/(1-(1+1/beta1)^{-alpha1})
n <- 2000
set.seed(23)
p <- runif(n, 0, 1)
x <- sigma1*(-log(-beta1*((1-p/k1)^{-1/alpha1}-1)+1))^{1/mu1}

pdf <- function(x) ((k1*alpha1*mu1)/(beta1*sigma1))*((1+(1/beta1)*(1-exp(-(x/sigma1)^{mu1})))^{-alpha1-1})*((x/sigma1)^{mu1-1})*(exp(-(x/sigma1)^{mu1}))
curve(pdf, from = 0, col='blue', lwd=2)
integrate(pdf, lower = 0, upper = Inf)

alpha <- tf$Variable(tf$random_uniform(shape(1L), 1, 5), name="alpha")
sigma <- tf$Variable(tf$random_uniform(shape(1L), 1, 5), name="sigma")
beta <- tf$Variable(tf$random_uniform(shape(1L), 1, 5), name="beta")
mu <- tf$Variable(tf$random_uniform(shape(1L), 1, 5), name="mu")
X <- tf$placeholder(dtype=tf$float32, name = "x-data")

loss <- -tf$reduce_sum(log(((( 1/(1-(1+1/beta)^{-alpha}))*alpha*mu)/(beta*sigma))*((1+(1/beta)*(1-exp(-(X/sigma)^{mu})))^{-alpha-1})*((X/sigma)^{mu-1})*(exp(-(X/sigma)^{mu}))))


optimizer <- tf$train$GradientDescentOptimizer(0.00001)
train <- optimizer$minimize(loss)

# Launch the graph and initialize the variables.
sess = tf$Session()
sess$run(tf$global_variables_initializer())

#Create dictionary to feed the data to optimize the coefficients
fd <- dict(X = x)

# Fit the line
n_iter <- 50000
for (step in 1:n_iter) {
        sess$run(train, feed_dict=fd)
        if (step %% 10000 == 0 | step <=5)
                cat(step, "\t", sess$run(alpha), sess$run(sigma), sess$run(beta), sess$run(mu), "\n")
}

c(sess$run(alpha), sess$run(sigma), sess$run(beta), sess$run(mu))

sess$close()

如果有人可以帮助我,我将非常感谢。谢谢

0 个答案:

没有答案