py_call_impl 中的索引错误

IndexError in py_call_impl

我是一名硕士生,目前在读R项目课程。在建立深度学习模型时,我遇到了一些问题。如果有人可以帮助我,我将不胜感激。我重新安装了 R、RStudio、Python、Anaconda,但无法解决问题。

library (keras);library(tensorflow);
install_tensorflow(); install_keras()
library(ggplot2); library(magrittr);
library(dplyr); library(tm); library(tidyr); 



clothing_reviews <- read.csv("C:/Users/Astrid/Documents/Master BWL/Data Mining mit R/R/Präsentation 2/Womens Clothing Reviews.csv") %>%
+   mutate(Liked = ifelse(Rating == 5, 1, 0),
+          text = paste("Title", "Review.Text"),
+          text = gsub("NA", "", text))
> 

> glimpse(clothing_reviews)
Observations: 28,222
Variables: 13
$ X                             <fct> "0", "1,1080,34,,\"Love this dress!  it's sooo pretty.  i happened to fi...
$ Clothing.ID                   <fct> 767, , , , 847, , , , 1077, , 1077, 1095, , ,  so i would say it's runni...
$ Age                           <fct> 33, , , , 47, , , , 24, , 53, 39, , , , , , 50, , , 41, , , , , , , , , ...
$ Title                         <fct> , , , , Flattering shirt, , , , Flattering, , Dress looks like it's made...
$ Review.Text                   <fct> Absolutely wonderful - silky and sexy and comfortable, , , , This shirt ...
$ Rating                        <fct> 4, , , , 5, , , , 5, , 3, 5, , , , , , 3, , , 5, , , , , , , , , , , , ,...
$ Recommended.IND               <fct> 1, , , , 1, , , , 1, , 0, 1, , , , , , 1, , , 1, , , , , , , , , , , , ,...
$ Positive.Feedback.Count       <fct> 0, , , , 6, , , , 0, , 14, 2, , , , , , 1, , , 0, , , , , , , , , , , , ...
$ Division.Name                 <fct> Initmates, , , , General, , , , General, , General, General Petite, , , ...
$ Department.Name               <fct> Intimate, , , , Tops, , , , Dresses, , Dresses, Dresses, , , , , , Dress...
$ Class.Name................... <fct> Intimates;;;;;;;;;;;;;;;;;;;, , , , Blouses;;;;;;;;;;;;;;;;;;;, , , , Dr...
$ Liked                         <dbl> 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, ...
$ text                          <chr> "Title Review.Text", "Title Review.Text", "Title Review.Text", "Title Re...
> 

> clothing_reviews %>%
+   ggplot(aes(x = factor(Liked), fill = Liked)) +
+   geom_bar(alpha = 0.8) + 
+   guides(fill = FALSE)
> 

> reviews_total<-rbind(clothing_reviews$Liked,
+                      clothing_reviews$text)
> 

> reviews_m<-as.matrix(reviews_total)
> dimnames(reviews_m)<-NULL
> 
> set.seed(123)
> ind<-sample(2,nrow(reviews_m),replace=TRUE,prob=c(0.7,0.3))
> reviews_train<-reviews_m[ind==1,1]
> reviews_test<-reviews_m[ind==2,1]
> reviews_trainlabel<-as.numeric(reviews_m[ind==1,2])
> reviews_testlabel<-as.numeric(reviews_m[ind==2,2])
Warning message:
NAs introduced by coercion 
> 
> maxlen<-100
> max_words<-10000
> tokenizer<-text_tokenizer(num_words=max_words)%>%
+   fit_text_tokenizer(reviews_train)
> sequences<-texts_to_sequences(tokenizer, reviews_train)
> word_index= tokenizer$word_index
> 

> x_train<-pad_sequences(sequences, maxlen=maxlen)
> y_train<-as.array(reviews_trainlabel)
> 
> batch_size <- 20
> epochs <- 12
> 
> model <- keras_model_sequential()%>%
+   layer_embedding(input_dim = max_words, output_dim = 100)%>%
+   layer_lstm(units = 16)%>%
+   layer_dense(1)%>%
+   layer_activation("sigmoid")%>% compile(
+     loss = "binary_crossentropy",
+     optimizer = "adam",
+     metrics = "accuracy"
+     
+   )
> 
> hist <- model %>%
+   fit(
+     x_train,
+     y_train,
+     batch_size = batch_size,
+     epochs = epochs,
+     validation_split = 0.3
+   )

2019-07-21 20:19:34.543600: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2

Error in py_call_impl(callable, dots$args, dots$keywords) : IndexError: list index out of range

虽然第一个 'error' 只是一个指示,但您安装的 TensorFlow 二进制文件不是 'optimal' 用于您的 CPU。除了安装默认的 TensorFlow,您还可以构建表单源,从而优化 CPU 用法等,但这不是错误(当我使用 Tensorflow / Keras 时,我也得到了这个。)

对于第二个错误,快速搜索显示:https://github.com/rstudio/tensorflow/issues/238#issuecomment-383189626 在讨论中,似乎 py_call_imb() 错误是由于与 RStudio / Keras 相关的依赖关系而发生的。

引用另一个 answer 的话,显然一个用户可以解决这个问题:

"Well - I used https://mran.microsoft.com/package/keras, where version 2.1.5, published 2018-03-25 is announced. However, upon installing as indicated, the whole process as described in my last post starts again. (I just repeated it, alas...). Only from devtools::install_github("rstudio/keras") 我能够得到一个有效的安装。"