Closed ghost closed 2 years ago
Thank you for sharing. Could you please send some code to reproduce the issue?
Of course! I did not shared the code because it happens in a completly random manner, for example when I modify the Juliaeval argument. It happened to me even with the example in the Github repository.
fit_mlp_net <- function(y, x, kappa, penalty){
## load libraries
JuliaConnectoR::juliaEval("using Flux")
Flux <- JuliaConnectoR::juliaImport("Flux")
## define model
model <- Flux$Chain(
Flux$Dense(ncol(x), 5L, Flux$sigmoid),
Flux$Dense(5L, 1L, Flux$sigmoid)
)
## define train function in Julia
train_network <- JuliaConnectoR::juliaEval("
function train_network!(model, x, y, e, κ, λ)
# define L2 penalty function
sqnorm(x) = sum(abs2, x)
# mini-batches
nndata = Flux.Data.DataLoader((x, y, e), batchsize=64,shuffle=true)
# collect-model params
ps = Flux.params(model)
# define multicriteria loss
function loss(x, y, e)
# data fitting
fit_loss = Flux.Losses.logitbinarycrossentropy(model(x), y)
# knowledge adherence
knowledge_loss = sum(1 .* sum(gradient(a -> sum(model(a)), x)))
# penalty
penalty = sum(sqnorm, Flux.params(model))
return (1-κ+λ)*fit_loss + κ*knowledge_loss + λ*penalty
end
opt = Flux.ADAM()
Flux.@epochs 10 Flux.train!(loss, ps, nndata, opt)
end
")
# convert x and y
x <- t(x)
# define effects
e <- (x > 0) * 1
y <- t(as.numeric(as.character(y)))
# train model
train_network(model, x, y, e, kappa, penalty)
return(model)
}
fit_mlp_net(y = as.factor(ifelse(c(iris[,4]) >= 1.8, 0, 1)),
x = as.matrix(iris[,1:3]),
kappa = 0.0,
penalty = 0.0)
I've recently found that the garbage collection wasn't fully thread safe and fixed this in the development version. Would it be possible for you to try the current development version and tell me whether the problem persists for you?
devtools::install_github("stefan-m-lenz/JuliaConnectoR")
Thanks, Stefan. I'm installing the dev version right now. I will let you know in a couple of hours whether it solved my problem. Thank you anyway for the quick reply, and I appreciate the work you did to create this package!
Indeed now works perfectly! I close the issue
Great to hear! I will create a new release on CRAN soon!
The new verion 1.1.0, which is also available from CRAN, incorporates the fix now.
Good to know! Thanks Stefan!
I don't know why but randomly the Julia session seems to crash for no specific reason and I get the following message:
Julia version: 1.7.0-rc2 R version: 4.1.1 Os version: Fedora 35
Is there any way to fix that? Thanks!
Ps: I'm using emacs as IDE (I do not think is related to the issue)