I am trying to train a gated graph neural network to predict molecular properties with GeometricFlux.jl: https://github.com/FluxML/GeometricFlux.jl
The inputs are FeaturedGraph objects of various morphologies. After quite a bit of trial-and-error, I have reached a point where the exception message is opaque to me and I do not know how to proceed. Code:
using Flux, GeometricFlux, MetaGraphs
# convert a MetaGraph to FeaturedGraph
mg = MetaGraph()
adj_mat = adjacency_matrix(mg)
g = FeaturedGraph(adj_mat; nf=ones(2, nv(mg)))
# train the model
model = Chain(GatedGraphConv(2, 1, aggr=:mean), softmax)
loss(x, y) = Flux.mse(model(x), y)
#Flux.epochs 1 Flux.train!(
loss,
Flux.params(model),
[(g, 1.0)],
ADAM()
)
The error message is: LoadError: MethodError: _pullback(::Zygote.Context, ::typeof(hcat)) is ambiguous.
Can anyone suggest what is wrong?
Related
I have TensorFlow version 2.4 and work with the R packages tensorflow (2.2.0) and keras (2.3.0.0.9000).
I would like to convert tensors to R arrays in a loss function (don't ask why).
Here is an example when such a conversion (outside a loss function) works:
library(tensorflow)
library(keras)
x.R <- matrix(1:12, ncol = 3) # dummy R object
x.tensor <- keras_array(x.R) # converting the R object to a tensor
as.array(x.tensor) # converting it back to an R array. This works because...
stopifnot(tf$executing_eagerly()) # ... eager execution is enabled
During training of a model, eager execution is FALSE though and thus
the as.array() call fails. To see this, let's first define a dummy
neural network model and training data.
d <- 2 # input and output dimension
in.lay <- layer_input(shape = d)
hid.lay <- layer_dense(in.lay, units = 300, activation = "relu")
out.lay <- layer_dense(hid.lay, units = d, activation = "sigmoid")
model <- keras_model(in.lay, out.lay)
n <- 1200 # number of training samples
data <- matrix(runif(n * d), ncol = d) # training data
Now let's define the loss function and compile the model with it.
myloss <- function(x, y) { # x and y are tensors here
stopifnot(!tf$executing_eagerly()) # confirms that eager execution is disabled
x. <- as.array(x) # ... fails with "RuntimeError: Evaluation error: invalid first argument, must be vector (list or atomic)." How can we convert x to an R array?
loss_mean_squared_error(x, y) # just a dummy return value (the MSE)
}
compile(model, optimizer = "adam", loss = myloss)
Let's try and fit this model (to see that it fails to convert the tensor x to an R array via as.array()).
prior <- matrix(rexp(n * d), ncol = d) # input sample to train the NN on
n.epoch <- 5 # number of epochs to train
batch.size <- 400 # batch size
fit(model, x = prior, y = data, batch_size = batch.size, epochs = n.epoch) # fails with error message given above
The R package tensorflow provides tfe_enable_eager_execution() to enable eager execution
in a session. But if I call it with TensorFlow 2.4, then I obtain:
tfe_enable_eager_execution() # "Error in py_get_attr_impl(x, name, silent) : AttributeError: module 'tensorflow' has no attribute 'contrib'"
Ideally, I wouldn't want to mess with eager execution much (not sure about the side effects),
just converting a tensor to an array. My guess is that there is no other way than eager execution as
only then the pointers are resolved and the R package tensorflow finds the data
in the tensor and is able to convert it to an array.
Other ideas to enable/disable eager execution are mentioned here but that's all in Python
and not available in R it seems. And this this post seems to ask the same question but in a different context.
I encounter a strange problem when trying to train a model in R using caret :
> bart <- train(x = cor_data, y = factor(outcome), method = "bartMachine")
Error in tuneGrid[!duplicated(tuneGrid), , drop = FALSE] :
nombre de dimensions incorrect
However, when using rf, xgbTree, glmnet, or svmRadial instead of bartMachine, no error is raised.
Moreover, dim(cor_data) and length(outcome) return [1] 3056 134 and [1] 3056 respectively, which indicates that there is indeed no issue with the dimensions of my dataset.
I have tried changing the tuneGrid parameter in train, which resolved the problem but caused this issue instead :
Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "pool-89-thread-1"
My dataset includes no NA, and all variables are either numerical or binary.
My goal is to extract the most important variables in the bart model. For example, I use for random forests:
rf <- train(x = cor_data, y = factor(outcome), method = "rf")
rfImp <- varImp(rf)
rf_select <- row.names(rfImp$importance[order(- rfImp$importance$Overall)[1:43], , drop = FALSE])
Thank you in advance for your help.
Since your goal is to extract the most important variables in the bart model, I will assume you are willing to bypass the caret wrapper and do it directly in R bartMachine, which is the only way I could successfully run it.
For my system, solving the memory issue required 2 further things:
Restart R and before loading anything, allocate 8Gb memory as so:
options(java.parameters = "-Xmx8g")
When running bartMachineCV, turn off mem_cache_for_speed:
library(bartMachine)
set_bart_machine_num_cores(16)
bart <- bartMachineCV(X = cor_data, y = factor(outcome), mem_cache_for_speed = F)
This will iterate through 3 values of k (2, 3 and 5) and 2 values of m (50 and 200) running 5 cross-validations each time, then builds a bartMachine using the best hyperparameter combination. You may also have to reduce the number of cores depending on your system, but this took about an hour on a 20,000 observation x 12 variable training set on 16 cores. You could also reduce the number of hyperparameter combinations it tests using the k_cvs and num_tree_cvs arguments.
Then to get the variable importance:
vi <- investigate_var_importance(bart, num_replicates_for_avg = 20)
print(vi)
You can also use it as a predictive model with predict(bart, new_data=new) similar to the object normally returned by caret::train(). This worked on R4.0.5, bartMachine_1.2.6 and rJava_1.0-4
I have 9,150 polygons in my dataset. I was trying to run a spatial autoregressive model (SAR) in spdep to test spatial dependence of my outcome variable. After running the model, I wanted to examine the direct/indirect impacts, but encountered an error that seems to have something to do with the length of neighbors in the weights matrix not being equal to n.
I tried running the very same equation as SLX model (Spatial Lag X), and impacts() worked fine, even though there were some polygons in my set that had no neighbors. I Googled and looked at spdep documentation, but couldn't find a clue on how to solve this error.
# Defining queen contiguity neighbors for polyset and storing the matrix as list
q.nbrs <- poly2nb(polyset)
listweights <- nb2listw(q.nbrs, zero.policy = TRUE)
# Defining the model
model.equation <- TIME ~ A + B + C
# Run SAR model
reg <- lagsarlm(model.equation, data = polyset, listw = listweights, zero.policy = TRUE)
# Run impacts() to show direct/indirect impacts
impacts(reg, listw = listweights, zero.policy = TRUE)
Error in intImpacts(rho = rho, beta = beta, P = P, n = n, mu = mu, Sigma = Sigma, :
length(listweights$neighbours) == n is not TRUE
I know that this is a question from 2019, but maybe it can help people dealing with the same problem. I found out that in my case the problem was the type of dataset, your data=polyset should be of type "SpatialPolygonsDataFrame". Which can be achieved by converting your data:
polyset_spatial_sf <- sf::as_Spatial(polyset, IDs = polyset$ID)
Then rerun your code.
I am using spatstat v.1.59-0 to build some point process models, but I am having problems with some of the validation tools, specifically effectfun and and the residual plot parres
The components of the model are rings_pp which consists of point locations for 52 archaeological sites, and elev which is a DEM converted to a spatstat pixel image. I am trying to evaluate the fit of a Gibbs point process model with an Area Interaction parameter (AreaInter) and elev
I fit the model to the data using the following code:
rr1 <- data.frame(r=seq(100, 2000, by=50))
p1 <- profilepl(rr1, AreaInter, rings_pp~elev, aic=T)
ppm5 <- as.ppm(p1)
Everything seems to be working fine (e.g., other diagnostics show the model is a reasonable fit to the data) except when I try to use effectfun and parres to evaluate the effect of the elevation covariate, they wont work.
parres gives the error Error in m[, d$relevant, drop = FALSE] : (subscript) logical subscript too long When I traceback the error I get the following:
`> trace(parres(ppm5, "elev"))
Error in m[, d$relevant, drop = FALSE] :
(subscript) logical subscript too long
> traceback()
4: effectFun.can(x)
3: effectFun(xxx)
2: parres(ppm5, "elev")
1: trace(parres(ppm5, "elev"))`
effectfun works when se.fit=FALSE but will not work when se.fit=TRUE and gives the following error: Error in quadform(mm, vc) : ncol(x) == nrow(v) is not TRUE When I traceback the error I get the following:
`> trace(plot(effectfun(ppm5, "elev", se.fit=T)))
Error in quadform(mm, vc) : ncol(x) == nrow(v) is not TRUE
> traceback()
8: stop(simpleError(msg, call = sys.call(-1)))
7: stopifnot(ncol(x) == nrow(v))
6: quadform(mm, vc)
5: predict.ppm(orig.model, locations = fakeloc, covariates = fakecov,
se = se.fit)
4: predict(orig.model, locations = fakeloc, covariates = fakecov,
se = se.fit)
3: effectfun(ppm5, "elev", se.fit = T)
2: plot(effectfun(ppm5, "elev", se.fit = T))
1: trace(plot(effectfun(ppm5, "elev", se.fit = T)))`
When I fit an inhomogeneous model ppm1 <- ppm(rings_pp, ~elev) both effectfun and parres work fine and show a good fit (though using the residual K function Kres suggest the model isn't accounting for clustering). So it seems to be something with how I've fit the Gibbs AreaInter model.
Any advice would be much appreciated.
Thanks again for a clear question.
This is a bug, which can be demonstrated by the simple example
fit <- ppm(cells ~ x, AreaInter(0.07))
plot(effectfun(fit, se.fit=TRUE))
plot(parres(fit))
I will investigate and fix it soon.
Postscript: this has now been fixed in the development version of spatstat (version 1.59-0.020) available on the GitHub repository
.
Trying to plot ROC curve for H2O Model Object in R, however, I keep receiving the following error message:
"Error in as.double(y) :
cannot coerce type 'S4' to vector of type 'double'"
My code is as follows:
drf1 <- h2o.randomForest(x=x,y=y,training_frame = train,validation_frame = valid, nfolds = nfolds, fold_assignment = "Modulo",keep_cross_validation_predictions = TRUE,seed = 1)
plot((h2o.performance(drf1,valid = T)), type = "roc")
I followed suggestions found here: How to directly plot ROC of h2o model object in R
Any help would be greatly appreciated!
From the error, I think your response variable is not binary. You can change your response variable to factor before putting it into model. i.e.
df$y <- as.factor(df$y)
"ROC is a graphical plot that illustrates the diagnostic ability of a binary classifier system as its discrimination threshold is varied".
source:
ROC wiki