R Error in py_get_attr_impl(x, name, silent) : AttributeError: module 'tensorflow' has no attribute 'placeholder'

R Error in py_get_attr_impl(x, name, silent) : AttributeError: module 'tensorflow' has no attribute 'placeholder'

我正在尝试从 R 中的 Tensorflow 实现自动编码器降维,在这个例子中:

library(dimRed)
library(tensorflow)
fraud_data <- read.csv("fraud_data")
data_label <- fraud_data["class"]
my_formula <- as.formula("class ~ .")
dat <- as.dimRedData(my_formula, fraud_data)
dimen <- NULL
dimension_params <- NULL
dimen <- dimRed::AutoEncoder()
dimension_params <- dimen@stdpars
dimension_params$ndim <- 2
emb <- dimen@fun(fraud_data, dimension_params) 
dimensional_data <- data.frame(emb@data@data)
x11()
plot(x=dimensional_data[,1], y=dimensional_data[,2], col=data_label, main="Laplacian Eigenmaps Projection")
legend(x=legend_pos, legend = unique(data_label), col=unique(data_label), pch=1)

我不断收到 AttributeError 模块 'tensorflow' 没有属性 'placeholder'”,如此追溯中所述:

14. stop(structure(list(message = "AttributeError: module 'tensorflow' has no attribute 'placeholder'", 
    call = py_get_attr_impl(x, name, silent), cppstack = NULL), class = c("Rcpp::exception", 
"C++Error", "error", "condition"))) 
13. py_get_attr_impl(x, name, silent) 
12. py_get_attr(x, name) 
11. py_get_attr_or_item(x, name, TRUE) 
10. `$.python.builtin.object`(x, name) 
9. `$.python.builtin.module`(tf, "placeholder") 
8. tf$placeholder 
7. graph_params(d_in = ncol(indata), n_hidden = n_hidden, activation = activation, 
    weight_decay = weight_decay, learning_rate = learning_rate, 
    n_steps = n_steps, ndim = ndim) 
6. eval(substitute(expr), data, enclos = parent.frame()) 
5. eval(substitute(expr), data, enclos = parent.frame()) 
4. with.default(pars, {
    graph_params(d_in = ncol(indata), n_hidden = n_hidden, activation = activation, 
        weight_decay = weight_decay, learning_rate = learning_rate, 
        n_steps = n_steps, ndim = ndim) ... 
3. with(pars, {
    graph_params(d_in = ncol(indata), n_hidden = n_hidden, activation = activation, 
        weight_decay = weight_decay, learning_rate = learning_rate, 
        n_steps = n_steps, ndim = ndim) ... 
2. dimen@fun(dat, dimension_params) 

Error in py_get_attr_impl(x, name, silent) : 
  AttributeError: module 'tensorflow' has no attribute 'placeholder' 

由于常见的解决方案是禁用 Tensorflow 2 行为,如 中所述,我尝试使用网状结构并通过此示例抑制错误:

library(reticulate)
x <- import("tensorflow.compat.v1", as="tf") 
x$disable_v2_behavior()

但这并没有改变任何东西..我仍然得到 AttributeError,我想知道,在这种情况下,我应该如何从 R 中对 Tensorflow 进行适当的更改?

这里是用于示例的示例数据:https://drive.google.com/file/d/1Yt4V1Ir00fm1vQ9futziWbwjUE9VvYK7/view?usp=sharing

我更深入地发现 tf 作为 R tensorflow 模块,因为 ?tf 在使用 library(tensorflow) 后是一个有效的命令,然后因为 Tensorflow 更新到版本 2+,而不是使用 tf$placeholder,使用 tf$compat$v1$placeholder,所以我想将 tf$compat$v1 中可用的功能添加到 tf

tf_synchronize <- function(){
  library(tensorflow)
  rm(list=c("tf")) #Delete first if there any tf variable in Global Environment
  tf_compat_names <- names(tf$compat$v1)  
  for(x in 2:length(tf_compat_names)){
    tf[[tf_compat_names[x]]] <- tf$compat$v1[[tf_compat_names[x]]]
  }
}

执行完后,AttributeError就没有了,降维自动编码器成功执行