I'm having one error Graph Execution Error - graph

I'm having one error (Graph Execution Error) while running this multilayer perceptron code (code and errors shown below) don't know if its
please help me
for i in range(1):
(x_train,y_train),(x_test,y_test)=keras.datasets.fashion_mnist.load_data()
#x_train=x_train[:125]
x_train=x_train.reshape(60000,784)
x = tf.keras.layers.Input(shape=(784,))
x1 = DropConnectDense(units=256, prob=(0.8), activation="relu", use_bias=True)(x)
x2 = DropConnectDense(units=128, prob=(0.8), activation="relu", use_bias=True)(x1)
x3 = DropConnectDense(units=64, prob=(0.5), activation="relu", use_bias=True)(x2)
y = DropConnectDense(units=10, prob=(0.5), activation="softmax", use_bias=True)(x3)
model_dropconnect = tf.keras.models.Model(x, y)
# Hyperparameters
batch_size=128
epochs=30
print(history_list)
# Compile the model
model_dropconnect.compile(
optimizer=tf.keras.optimizers.Adam(0.0001), # Utilize optimizer
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=['accuracy'])
print(len(x_train))
# Train the network
history = model_dropconnect.fit(
np.array(x_train),
np.array(y_train),
batch_size=batch_size,
validation_split=0.2,
epochs=epochs,callbacks=[tf.keras.callbacks.TensorBoard(
log_dir="logs/image"+str('dropman')
)])
model_dropconnect.evaluate(x_test,y_test)

Related

R Differential Optimization produces error in checkForRemoteErrors(val): 4 nodes produced errors;

I am trying to use DEOptim package to tune gradient boosting (gbm) with caret. When I launch the job, I have an error: Error in checkForRemoteErrors(val): 4 nodes produced errors; first error: Stopping
I am working with R version 4.2.0 with Linux Centos 7.
May be I am doing something wrong in my code or with with cores + R.
tune parameters:
n.trees, interaction_min_max,shrinkage,minobsinnode
I provide few lines of my code:
library(caret)
library(DEoptim)
library(parallel)
fitControl <- trainControl(method = "repeatedcv",number = 5,repeats = 3)
#Differential
# Set parameter settings for search algorithm
max_iter <- 5 # maximum number of iterations
pop_size <- 10 # population size
# Create custom function for assessing solutions
eval_function_XGBoost_Linear <- function(x, data, train_settings) {
x1 <- x[1]; x2 <- x[2]; x3 <- x[3]; x4 <- x[4]
suppressWarnings(
XGBoost_Linear_model <- caret::train(isMut ~.,
data = data,
method = "gbm",
trControl = train_settings,
verbose = FALSE,
silent = 1,
tuneGrid = expand.grid(
interaction.depth = x1,
n.trees = x2,
shrinkage = x3,
n.minobsinnode = x4
)
)
)
return(XGBoost_Linear_model$results$Accuracy) # Accuracy
}
I Define minimum and maximum values for each input
interaction_min_max <- c(1,9)
ntrees_min_max <- c(1500,2000)
shrinkage <- c(0.1,0.1)
minobsinnode <- c(20,20)
When I run the differential evolution algorithm, I have the error described above.
set.seed(1)
n_cores <- detectCores()-1
​
DE_T0 <- Sys.time()
# Run differential evolution algorithm
DE_model_XGBoost_Linear <- DEoptim::DEoptim(
fn = eval_function_XGBoost_Linear,
lower = c(interaction_min_max[1], ntrees_min_max[1], shrinkage[1], minobsinnode[1]),
upper = c(interaction_min_max[2], ntrees_min_max[2], shrinkage[2], minobsinnode[2]),
control = DEoptim.control(
NP = pop_size, # population size
itermax = max_iter, # maximum number of iterations
CR = 0.5, # probability of crossover
storepopfreq = 1, # store every population
parallelType = 1 # run parallel processing
),
data = rose,
train_settings = fitControl
)
DE_T1 <- Sys.time()
DE_T1-DE_T0

How do I set the upper range mtry tuning value in mlr3, when I also conduct automated feature selection?

Date: 2022-08-17. R Version: 4.0.3. Platform: x86_64-apple-darwin17.0 (64-bit)
Problem: In mlr3 (classif.task, learner: random forest), I use automated hyperparameter optimization (HPO; mtry in the range between 1 and the number of features in the data), and automated feature selection (single criterion: msr = classif.auc).
I run into this ranger error message:
'mtry can not be larger than number of variables in data. Ranger will EXIT now.'
I am relatively sure that what happens is when a subset of features have been selected and HPO attempts to assess the performance for a higher number of features, that this produces the error. If this is true, then how do I set the upper range limit in HPO for the mtry parameter in such a case (see repex below)?
# Make data with binary outcome.
set.seed(123); n <- 500
for(i in 1:9) {
assign(paste0("x", i), rnorm(n=n, mean = 0, sd = sample(1:6,1)))
}
z <- 0 + (.02*x1) + .03*x2 - .06*x3 + .03*x4 + .1*x5 + .08*x6 + .09*x7 - .008*x8 + .045*x9
pr = 1/(1+exp(-z))
y = rbinom(n, 1, pr)
dat <- data.frame(y=factor(y), x1, x2, x3, x4, x5, x6, x7, x8, x9)
#
library(mlr3verse)
tskclassif <- TaskClassif$new(id="rangerCheck", backend=dat, target="y")
randomForest <- lrn("classif.ranger", predict_type = "prob")
# Question: How do I set the upper range limit for the mtry parameter, in order to not get the error message?
searchSpaceRANDOMFOREST <- ps(mtry=p_int(lower = 1, upper = (ncol(dat)-1)))
# Hyperparameter optimization
resamplingTuner <- rsmp("cv", folds=4)
tuner <-
atRANDOMFOREST <- AutoTuner$new(
learner=randomForest,
resampling = resamplingTuner,
measure = msr("classif.auc"),
search_space = searchSpaceRANDOMFOREST,
terminator = trm("evals", n_evals = 10),
tuner = tnr("random_search"))
# Feature selection
instance = FSelectInstanceSingleCrit$new(
task = tskclassif,
learner = atRANDOMFOREST,
resampling = rsmp("holdout", ratio = .8),
measure = msr("classif.auc"),
terminator = trm("evals", n_evals = 20)
)
fselector <- fs("random_search")
fselector$optimize(instance)
# Error message:
# Error: mtry can not be larger than number of variables in data. Ranger will EXIT now.
# Fehler in ranger::ranger(dependent.variable.name = task$target_names, data = task$data(), : User interrupt or internal error.
# This happened PipeOp classif.ranger.tuned's $train()
You should be able to use the mtry.ratio parameter in https://mlr3learners.mlr-org.com/reference/mlr_learners_classif.ranger.html instead of mtry to have a dynamic feature count selection during tuning which does not exceed the number of available features.

Invalid graphics state model4you

Following the model-based recursive partitioning in https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6015941/ I want to replicate the following code:
sim_data <- function(n=2000){
x1 <- rnorm(n)
x2 <- rbinom(n,1,0.3)
x3 <- runif(n)
x4 <- rnorm(n)
t <- rbinom(n,1,0.5)
z <- 1-x2+x1+2*(x1>=0)*x2*t-2*(x1<0)*x2*t
pr <- 1/(1+exp(-z))
y <- as.factor(rbinom(n,1,pr))
data.frame(x1,x3,x2=as.factor(x2),x4, t=factor(t,labels=c("C","A")),y,z)
}
dt <- sim_data()
dt.num = as.data.frame(sapply(dt, as.numeric))
dt.num$y <- dt.num$y-1 #only to convert outcome 1,2 into 0,1
mbase <- glm(y~t, data=dt.num,
family = binomial())
round(summary(mbase)$coefficients,3)
library("model4you")
pmtr <- pmtree(mbase, zformula = ~. ,
data = dt.num,
control = ctree_control(minbucket = 250))
plot(pmtr, terminal_panel = node_pmterminal(pmtr,
plotfun = binomial_glm_plot,
confint = TRUE))
However, the following inexplicable error occurs:
Error in .Call.graphics(C_palette2, .Call(C_palette2, NULL)) :
invalid graphics state
I was looking for a solution to this problem in the post Persistent invalid graphics state error when using ggplot2. But the problem persists.
Any clue?
Thank you in advance
When I tried to replicate this, I got a different error:
plot(pmtr, terminal_panel = node_pmterminal(pmtr, plotfun = binomial_glm_plot, confint = TRUE))
## Waiting for profiling to be done...
## Error in plotfun(mod = list(coefficients = c(`(Intercept)` = -0.16839363929017, :
## Plotting currently only works for models with a single factor covariate.
## We recommend using partykit or ggparty plotting functionalities!
The reason for this is that the panel function expects both the response and the treatment to be binary factors (as in dt). When you use binary numeric variables instead (as in dt.num) the model estimation in glm() leads to equivalent output but the plot() functionality is confused.
When I refit both the glm() and the pmtree() with dt rather than dt.num everything works as intended for me, yielding the following graphic:

plotting interaction effects for LASSO models in R

I fitted a lasso logistic model with interaction terms. Then i wanted to visualize those interactions using a interaction plot.
I tried to find some R function that will plot interactions for glmnet models and i couldnt find any .
Is there any R package that will plot interactions for LASSO ?
Since i couldnt find any, i tried to do it manually , by plotting the predicted values. But i am getting some errors.
My code is as follows,
require(ISLR)
require(glmnet)
y <- Smarket$Direction
x <- model.matrix(Direction ~ Lag1 + Lag4* Volume, Smarket)[, -1]
lasso.mod <- cv.glmnet(x, y, alpha=1,family="binomial",nfolds = 5, type.measure="class",
lambda = seq(0.001,0.1,by = 0.001))
lasso.mod$lambda.min
pred = expand.grid(Lag1 = median(Smarket$Lag1),
Lag4 = c(-0.64,0.0385,0.596750),
Volume = seq(min(Smarket$Volume), max(Smarket$Volume), length=100))
lasso.mod1 <- glmnet(x, y, alpha=1,family="binomial",
lambda = lasso.mod$lambda.min)
pred$Direction = predict(lasso.mod1, newx=pred,
type="response", s= lasso.mod$lambda.min)
i am getting this error :
Error in cbind2(1, newx) %*% nbeta :
not-yet-implemented method for <data.frame> %*% <dgCMatrix>
Can any suggest anything to fix this issue ?
Thank you
predict.glmnet says newx must be a matrix. And you need to give interaction value by yourself.
library(dplyr)
pred = expand.grid(Lag1 = median(Smarket$Lag1),
Lag4 = c(-0.64,0.0385,0.596750),
Volume = seq(min(Smarket$Volume), max(Smarket$Volume), length=100)) %>%
mutate(`Lag4:Volume` = Lag4 * Volume) # preparing interaction values
pred$Direction = predict(lasso.mod1, newx = as.matrix(pred), # convert to matrix
type = "link", s= lasso.mod$lambda.min)
[EDITED]
Oh, I overlooked more general, better way.
pred = expand.grid(Lag1 = median(Smarket$Lag1),
Lag4 = c(-0.64,0.0385,0.596750),
Volume = seq(min(Smarket$Volume), max(Smarket$Volume), length=100))
pred$Direction = predict(lasso.mod1,
newx = model.matrix( ~ Lag1 + Lag4* Volume, pred)[, -1],
type="response", s= lasso.mod$lambda.min)

Winbugs to Rjags beta binomial model translation

I am working through the textbook "Bayesian Ideas and Data Analysis" by Christensen et al.
There is a simple exercise in the book that involves cutting and pasting the following code to run in Winbugs:
model{ y ~ dbin(theta, n) # Model the data
ytilde ~ dbin(theta, m) # Prediction of future binomial
theta ~ dbeta(a, b) # The prior
prob <- step(ytilde - 20) # Pred prob that ytilde >= 20 }
list(n=100, m=100, y=10, a=1, b=1) # The data
list(theta=0.5, ytilde=10) # Starting/initial values
I am trying to translate the following into R2jags code and am running into some trouble. I thought I could fairly directly write my R2Jags code in this fashion:
model {
#Likelihoods
y ~ dbin(theta,n)
yt ~ dbin(theta,m)
#Priors
theta ~ dbeta(a,b)
prob <- step(yt - 20)
}
with the R code:
library(R2jags)
n <- 100
m <- 100
y <- 10
a <- 1
b <- 1
jags.data <- list(n = n,
m = m,
y = y,
a = a,
b = b)
jags.init <- list(
list(theta = 0.5, yt = 10), #Chain 1 init
list(theta = 0.5, yt = 10), #Chain 2 init
list(theta = 0.5, yt = 10) #Chain 3 init
)
jags.param <- c("theta", "yt")
jags.fit <- jags.model(data = jags.data,
inits = jags.inits,
parameters.to.save = jags.param,
model.file = "hw21.bug",
n.chains = 3,
n.iter = 5000,
n.burnin = 100)
print(jags.fit)
However, calling the R code brings about the following error:
Error in jags.model(data = jags.data, inits = jags.inits, parameters.to.save = jags.param, :
unused arguments (parameters.to.save = jags.param, model.file = "hw21.bug", n.iter = 5000, n.burnin = 100)
Is it because I am missing a necessary for loop in my R2Jags model code?
The error is coming from the R function jags.model (not from JAGS) - you are trying to use arguments parameters.to.save etc to the wrong function.
If you want to keep the model as similar to WinBUGS as possible, there is an easier way than specifying the data and initial values in R. Put the following into a text file called 'model.txt' in your working directory:
model{
y ~ dbin(theta, n) # Model the data
ytilde ~ dbin(theta, m) # Prediction of future binomial
theta ~ dbeta(a, b) # The prior
prob <- step(ytilde - 20) # Pred prob that ytilde >= 20
}
data{
list(n=100, m=100, y=10, a=1, b=1) # The data
}
inits{
list(theta=0.5, ytilde=10) # Starting/initial values
}
And then run this in R:
library('runjags')
results <- run.jags('model.txt', monitor='theta')
results
plot(results)
For more information on this method of translating WinBUGS models to JAGS see:
http://runjags.sourceforge.net/quickjags.html
Matt
This old blog post has an extensive example of converting BUGS to JAGS accessed via package rjags not R2jags. (I like the package runjags even better.) I know we're supposed to present self-contained answers here, not just links, but the post is rather long. It goes through each logical step of a script, including:
loading the package
specifying the model
assembling the data
initializing the chains
running the chains
examining the results

Resources