stefan-m-lenz / JuliaConnectoR

A functionally oriented interface for calling Julia from R
Other
100 stars 6 forks source link

writebin error #12

Closed ghost closed 2 years ago

ghost commented 2 years ago

I don't know why but randomly the Julia session seems to crash for no specific reason and I get the following message:

Error in writeBin(length(utf8bytes), pkgLocal$con) : 
  ignoring SIGPIPE signal

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)

stefan-m-lenz commented 2 years ago

Thank you for sharing. Could you please send some code to reproduce the issue?

ghost commented 2 years ago

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)
stefan-m-lenz commented 2 years ago

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")
ghost commented 2 years ago

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!

ghost commented 2 years ago

Indeed now works perfectly! I close the issue

stefan-m-lenz commented 2 years ago

Great to hear! I will create a new release on CRAN soon!

stefan-m-lenz commented 2 years ago

The new verion 1.1.0, which is also available from CRAN, incorporates the fix now.

ghost commented 2 years ago

Good to know! Thanks Stefan!