py_get_attr_impl(x,name,silent)中的R错误:AttributeError:模块'tensorflow'没有属性'placeholder'

Posted

技术标签:

【中文标题】py_get_attr_impl(x,name,silent)中的R错误:AttributeError:模块\'tensorflow\'没有属性\'placeholder\'【英文标题】:R Error in py_get_attr_impl(x, name, silent) : AttributeError: module 'tensorflow' has no attribute 'placeholder'py_get_attr_impl(x,name,silent)中的R错误:AttributeError:模块'tensorflow'没有属性'placeholder' 【发布时间】:2021-09-25 22:10:15 【问题描述】:

我正在尝试从 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 module 'tensorflow' has no attribute '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.0 - AttributeError: module 'tensorflow' has no attribute 'Session' 中的说明禁用 Tensorflow 2 行为,因此我尝试使用 reticulate 并通过此示例抑制错误:

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

【问题讨论】:

【参考方案1】:

我更深入地发现tf充当R tensorflow模块,因为?tf在使用library(tensorflow)之后是一个有效命令,然后由于Tensorflow更新到版本2+,而不是使用tf$placeholder,所以使用@987654325 @,所以我有一个想法,将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 不复存在,降维自动编码器成功执行

【讨论】:

以上是关于py_get_attr_impl(x,name,silent)中的R错误:AttributeError:模块'tensorflow'没有属性'placeholder'的主要内容,如果未能解决你的问题,请参考以下文章

List分组和排序

yield表达式 三元表达式

x多进程

函数--廖雪峰视频

[UVA]UVA1401 remember the name(Trie)

函数调用