Tensorflow while_loop;模型不在循环内收敛,但在循环外收敛

时间:2019-02-27 18:22:05

标签: r tensorflow while-loop

我正在尝试使用while_loop调用优化器来训练我的模型。从某种意义上说,循环似乎在循环内成功进行,尽管这不是我想要的,并且模型开始迅速偏离最佳解决方案。当我在while_loop之外调用优化器时,一切都会按我的预期进行。

对于我做错了事,我将不胜感激。

这是我正在尝试的代码:

rm(list=ls())

library(tensorflow)
library(ggplot2)

tf$reset_default_graph()

set.seed(123)
df <- data.frame(x=runif(500000,-3,3))
df$y <- dnorm(df$x) * runif(df$x, .95, 1.05)

sess <- tf$Session()

X_input <- tf$placeholder(dtype=tf$float32, shape=list(NULL,1))
X <- tf$Variable(X_input, trainable=FALSE, validate_shape=FALSE, collections=list())
sess$run(tf$variables_initializer(list(X)), feed_dict=dict(X_input=matrix(df$x, ncol=1)))

Y_input <- tf$placeholder(dtype=tf$float32, shape=shape(NULL,1))
Y <- tf$Variable(Y_input, trainable=FALSE, validate_shape=FALSE, collections=list())
sess$run(tf$variables_initializer(list(Y)), feed_dict=dict(Y_input=matrix(df$y,ncol=1)))

fc1 <- tf$contrib$layers$fully_connected(inputs=tf$reshape(X, shape=list(-1L, 1L)), num_outputs=50L)
fc2 <- tf$contrib$layers$fully_connected(inputs=fc1, num_outputs=50L)
fc3 <- tf$contrib$layers$fully_connected(inputs=fc2, num_outputs=50L)
fc4 <- tf$contrib$layers$fully_connected(inputs=fc3, num_outputs=50L)
fc5 <- tf$contrib$layers$fully_connected(inputs=fc4, num_outputs=50L)
fc6 <- tf$contrib$layers$fully_connected(inputs=fc5, num_outputs=50L)
fc7 <- tf$contrib$layers$fully_connected(inputs=fc6, num_outputs=50L)
fc8 <- tf$contrib$layers$fully_connected(inputs=fc7, num_outputs=50L)
model <- tf$contrib$layers$fully_connected(inputs=fc8, num_outputs=1L,  activation_fn=NULL)

i <- tf$Variable(0, trainable=FALSE, validate_shape=FALSE, collections=list())
sess$run(tf$variables_initializer(list(i)))

loss <- tf$losses$mean_squared_error(model, Y)
optimizer <- tf$train$AdamOptimizer(learning_rate = 0.001)

loop_condition <- function(i) {
    return(tf$less(i,400))
}

loop_body <- function(i) {
    train_op = optimizer$minimize(loss)

    return( tf$tuple(list(tf$add(i,1)), control_inputs=list(train_op, loss)) )
}

loop <- tf$while_loop(cond=loop_condition, body=loop_body, loop_vars=list(i), parallel_iterations=1L)

#Model doesn't converge using this approach
sess$run(tf$global_variables_initializer())
sess$run(loop)
print(sess$run(loss))

#Model does converges using this approach
train_op <- optimizer$minimize(loss)
sess$run(tf$global_variables_initializer())
for(i in 1:400) {
    sess$run(train_op)
}
print(sess$run(loss))

#Plot results
ef <- data.frame(x=seq(-3,3,.05))
ef$y <- sess$run(model, feed_dict=dict(X=ef$x))
ef$class = "2 Predicted"

ff <- data.frame(x=seq(-3,3,.05))
ff$y <- dnorm(ff$x)
ff$class = "3 Actual"

ef <- rbind(ef, ff)

s <- which(runif(nrow(df),0,1) < .05)
ff <- df[s,]
ff$class <- "1 Training"

ef <- rbind(ef, ff)

ggplot(data=ef,aes(x=x, y=y, colour=class)) + geom_line()

0 个答案:

没有答案