Keras, LIME: No data provided for "lstm__input" in "explain" function - r

I receive ValueError: No data provided for "lstm__input". Need data for each key in: ['lstm__input'] while executing lime::explain
I have latest lime version and looked thourgh all related topics to my problem. I have LSTM regression network and I don't have any issues training it and predicting values.
As well I don't have any problems predicting values using lime:
class(model)
%keras.engine.sequential.Sequential
model_type.keras.engine.sequential.Sequential <- function(x, ...) {
"regression"}
predict_model.keras.engine.sequential.Sequential <- function (x, newdata, type, ...) {
pred <- predict(object = x, x = newdata)
data.frame (pred) }
predict_model(x = model, newdata = (testX_Matrix), type = 'raw')
explainer <- lime::lime (
x = trainX,
model = model,
bin_continuous = FALSE)
Up to this point everything works fine. When I try to run next lines:
explanation <- lime::explain (
testX,
explainer = explainer,
n_features = 4)
I receive an error:
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: No data provided for "lstm_8_input". Need data for each key in: ['lstm_8_input']
Detailed traceback:
File "C:\Soft\anaconda\envs\R-TENS~1\lib\site-packages\keras\engine\training.py", line 1147, in predict
x, _, _ = self._standardize_user_data(x)
File "C:\Soft\anaconda\envs\R-TENS~1\lib\site-packages\keras\engine\training.py", line 749, in _standardize_user_data
exception_prefix='input')
File "C:\Soft\anaconda\envs\R-TENS~1\lib\site-packages\keras\engine\training_utils.py", line 77, in standardize_input_data
'for each key in: ' + str(names))
I guess main problem comes from the fact that my NN requires only 3-dim matrix (the same I used in predict_model), but lime doesn't work't with objects of class 'array', 'double', 'numeric' so I can't use it.
Has anyone faced such issue? How can I fix it?

Related

External Cluster Validation - Categorical Data R

I've recently been attempting to evaluate output from k-modes (a cluster label), relative to a so-called True cluster label (labelled 'class' below).
In other words: I've been attempting to external validate the clustering output. However, when I tried external validation measures from the 'fpc' package, I was unsuccessful (error term posted below script).
I've attached my code for the mushroom dataset. I would appreciate if anyone could show me how to successful execute these external validation measures in the context of categorical data.
Any help appreciated.
# LIBRARIES
install.packages('klaR')
install.packages('fpc')
library(klaR)
library(fpc)
#MUSHROOM DATA
mushrooms <- read.csv(file = "https://raw.githubusercontent.com/miachen410/Mushrooms/master/mushrooms.csv", header = FALSE)
names(mushrooms) <- c("edibility", "cap-shape", "cap-surface", "cap-color",
"bruises", "odor", "gill-attachment", "gill-spacing",
"gill-size", "gill-color", "stalk-shape", "stalk-root",
"stalk-surface-above-ring", "stalk-surface-below-ring",
"stalk-color-above-ring", "stalk-color-below-ring", "veil-type",
"veil-color", "ring-number", "ring-type", "spore-print-color",
"population", "habitat")
names(mushrooms)[names(mushrooms)=="edibility"] <- "class"
indexes <- apply(mushrooms, 2, function(x) any(is.na(x) | is.infinite(x)))
colnames(mushrooms)[indexes]
table(mushrooms$class)
str(mushrooms)
#REMOVING CLASS VARIABLE
mushroom.df <- subset(mushrooms, select = -c(class))
#KMODES ANALYSIS
result.kmode <- kmodes(mushroom.df, 2, iter.max = 50, weighted = FALSE)
#EXTERNAL VALIDATION ATTEMPT
mushrooms$class <- as.factor(mushrooms$class)
class <- as.numeric(mushrooms$class))
clust_stats <- cluster.stats(d = dist(mushroom.df),
class, result.kmode$cluster)
#ERROR TERM
Error in silhouette.default(clustering, dmatrix = dmat) :
NA/NaN/Inf in foreign function call (arg 1)
In addition: Warning message:
In dist(mushroom.df) : NAs introduced by coercion

Simple regression linreg error in R: argument 'modeltest'must be either TRUE or FALSE

Hello everyone I am having an issue with my R software. When I try to run a simple regression i get this error:
model1 <- linReg(data = dat, dep = 'Intent', blocks = list(c('Enjoy')), modelTest = 'f', stdEst = TRUE)
model1
Error: Argument 'modelTest' must be either TRUE or FALSE
I have tried changing the to a capital and typing in FALSE instead but when I type in FALSE I get this error:
model1 <- linReg(data = dat, dep = 'Intent', blocks = list(c('Enjoy')), modelTest = FALSE, stdEst = TRUE)
Error in eval(predvars, data, env) : object 'XRW5qb3k' not found
Can anyone please help me with this? All o the descriptives, graphs, and plots run just fine. I have the packages jmv, psych, and car loaded
Thank you for your help!

Error in eval(expr, p): object 'X' not found; predict (BayesARIMAX)

I am trying to use BayesARIMAX to model and predict us gdp (you can find the data here: https://fred.stlouisfed.org/series/GDP).I followed the example (https://cran.r-project.org/web/packages/BayesARIMAX/BayesARIMAX.pdf) to build my model. I didnt have any major issue to build the model(used error handling to overcome Getting chol.default error when using BayesARIMAX in R issue). However could not get the prediction of the model. I tried to look for solution and there is no example of predicting the model that is build using BayesARIMAX. Every time that I run the "predict" I get the following error:
"Error in eval(expr, p) : object 'X' not found"
Here is my code.
library(xts)
library(zoo)
library(tseries)
library(tidyverse)
library(fpp2)
gdp <- read.csv("GDP.csv", head = T)
date.q <- as.Date(gdp[, 1], "%Y-%m-%d")
gdp <- xts(gdp[,2],date.q)
train.row <- 248
number.row <- dim(merge.data)[1]
gdp.train <- gdp[1:train.row]
gdp.test <- gdp[(train.row+1):number.row]
date.test <- date.q[(train.row+1):number.row]
library(BayesARIMAX)
#wrote this function to handle randomly procuded error due to MCMC simulation
test_function <- function(a,b,P=1,Q=1,D=1,error_count = 0)
{
tryCatch(
{
model = BayesARIMAX(Y=a,X = b,p=P,q=Q,d=D)
return(model)
},
error = function(cond)
{
error_count=error_count+1
if (error_count <40)
{
test_function(a,b,P,Q,D,error_count = error_count)
}
else
{
print(paste("Model doesnt converge for ARIMA(",P,D,Q,")"))
print(cond)
}
}
)
}
set.seed(1)
x = rnorm(length(gdp.train),4,1)
bayes_arima_model <- test_function(a = gdp.train,b=x,P = 3,D = 2,Q = 2)
bayes_arima_pred <- xts(predict(bayes_arima_model[[1]],newxreg = x[1:3])$pred,date.test)
and here is the error code
Error in eval(expr, p) : object 'X' not found
Here is how I resolve the issue after reading through the BayesARIMAX code (https://rdrr.io/cran/BayesARIMAX/src/R/BayesianARIMAX.R) . I basically created the variable "X" and passed it to predict function to get the result. You just have to set the length of X variable equal to number of prediction.
here is the solution code for prediction.
X <- c(1:3)
bayes_arima_pred <- xts(predict(bayes_arima_model[[1]],newxreg = X[1:3])$pred,date.test)
which gave me the following results.
bayes_arima_pred
[,1]
2009-01-01 14462.24
2009-04-01 14459.73
2009-07-01 14457.23

R function slda.em: error when setting the logistic = T argument

I am trying to run supervised LDA on a set of annotated tweets. I keep getting this error msg:
Error in structure(.Call("collapsedGibbsSampler", documents, as.integer(K), : This should not have happened (nan).
How do I fix it?
It only happens when I set logistic = T.
Code below. s is a sample of the data.
corpus1 <- lexicalize(tweets[s], lower=TRUE)
to.keep <- corpus1$vocab[word.counts(corpus1$documents, corpus1$vocab) >= 1]
documents <- lexicalize(tweets[s], lower=TRUE, vocab=to.keep)
params <- sample(c(-1, 1), num.topics, replace=TRUE)
result <- slda.em(documents=documents,
K=num.topics,
vocab=to.keep,
num.e.iterations=10,
num.m.iterations=4,
alpha=1.0, eta=0.1,
annotations = as.integer(annotations[s]),
params,
variance=0.25,
lambda=1.0,
logistic=T)

Unknown variable error in JAGS model

I'm currently trying to develop a model in JAGS, but I unfortunately keep getting the following error:
Error in jags.model(file = "Model.txt", n.adapt = MCMCAdapt) :
RUNTIME ERROR:
Compilation error on line 15.
Unknown variable precedentCount
Either supply values for this variable with the data
or define it on the left hand side of a relation.
The following is what is in the file titled, "Model.txt":
model {
for (i in 1:citationCount) {
Z[i] ~ dbern(Z.hat[i])
Z.hat[i] <- phi(kappa - lambda*pow(x.precedent[newPrecedentID[i]] - x.brief[newBriefID[i]], 2))
}
for (j in 1:briefCount) {
x.brief[newBriefID[j]] ~ dnorm(0,1)
}
for (k in 1:precedentCount) {
x.precedent[newPrecedentID[k]] ~ dnorm(0,1)
}
kappa ~ dunif(-10,10)
lambda ~ dunif(-10, 0)
}
I'm a little confused as to why the error is occurring, as precedentCount is already defined in R's environment:
> precedentCount
[1] 650
The same thing is true for briefCount and citationCount, yet these have not yielded the same error (yet, at least):
> briefCount
[1] 126
> citationCount
[1] 2948
If anyone can advise how I can address this problem, I would greatly appreciate it.
You need to give JAGS all data as the argument jags.model(data = list(...)). precedentCount came up because it was just the first error.
data_list <- list(precedentCount = 650,
briefCount = 126,
citationCount = 2948,
newPrecedentID = ...))
jags.model(file = "Model.txt", n.adapt = MCMCAdapt, data = data_list)

Resources