I try to use the R package JuliaConnectoR
to work with the Julia package EvoTrees
.
I see how I can pass numerical paramters defined in R to Julia (e.g., a double is translated to Float64), but I can't see how I can define a paramter in R that describes a type in Julia.
The following is an example:
n_obs = 100
n_features = 100
nrounds = 10L
set.seed(20221224)
x_train = matrix(ncol= n_features, rnorm(n_obs*n_features))
y_train = rnorm(n_obs)
library(JuliaConnectoR)
evoTrees = juliaImport("EvoTrees")
params_evo = evoTrees$EvoTreeRegressor(nrounds=nrounds , loss = as.symbol("linear"), alpha=0.5,lambda=0.0,gamma=0.0)
evoTrees_model = evoTrees$fit_evotree(params_evo, x_train, y_train, print_every_n = 50L)
The call to evoTrees$EvoTreeRegressor
works fine I e..g., can pass the julia symbol :linear
by as.symbol("linear")
and passing numerical parameters like nrounds
is clear too. However, I get the following error message for the call of the fit evoTrees$fit_evotree
:
Error: Evaluation in Julia failed. Original Julia error message: MethodError: no method matching fit_evotree(::EvoTrees.EvoTreeRegressor{EvoTrees.Linear, Float32}, ::Matrix{Float64}, ::Vector{Float64}; print_every_n=50) Closest candidates are: fit_evotree(::Union{EvoTrees.EvoTreeClassifier{L, T}, EvoTrees.EvoTreeCount{L, T}, EvoTrees.EvoTreeGaussian{L, T}, EvoTrees.EvoTreeMLE{L, T}, EvoTrees.EvoTreeRegressor{L, T}}; x_train, y_train, w_train, offset_train, x_eval, y_eval, w_eval, offset_eval, metric, early_stopping_rounds, print_every_n, verbosity, fnames, return_logger) where {L, T} at C:\Users\rwarn.julia\packages\EvoTrees\ayRL8\src\fit.jl:309 Stacktrace: [1] invokelatest(::Any, ::Any, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Int64, Tuple{Symbol}, NamedTuple{(:print_every_n,), Tuple{Int64}}}) @ Base .\essentials.jl:731 [2] evaluate!(call::Main.RConnector.Call, communicator::Main.RConnector.CommunicatoR{Sockets.TCPSocket}) @ Main.RConnector C:\Users\rwarn\Documents\R\win-l
I interpret this error as follows: Without passing a type via the Julia parameter T
, data types of Float32 are assumed. However, the train data in R type double is translated to Julia type Float64.
I think, the solution would be to pass a the Julia type Float64 to evoTrees$EvoTreeRegressor
. How can I do that? Thank you!
Infos: JuliaConnectoR: v1.1.1 EvoTrees v0.14.2
sessionInfo() R version 4.1.1 (2021-08-10) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 22621)
Matrix products: default
locale: [1] LC_COLLATE=German_Austria.1252 LC_CTYPE=German_Austria.1252 LC_MONETARY=German_Austria.1252 [4] LC_NUMERIC=C LC_TIME=German_Austria.1252
attached base packages: [1] stats graphics grDevices utils
datasets methods baseother attached packages: [1] JuliaConnectoR_1.1.1
loaded via a namespace (and not attached): [1] compiler_4.1.1 tictoc_1.0.1 tools_4.1.1
CodePudding user response:
Yes, you are right, the default type that is assumed is Float32
. It can be seen in the code here.
The constructor also accepts the keyword argument T
for defining the type.
If you want to pass the Julia type Float64
as a value for this argument, you can do this like shown below:
params_evo = evoTrees$EvoTreeRegressor(T = juliaExpr("Float64"),
rounds=nrounds, loss = as.symbol("linear"), alpha=0.5, lambda=0.0, gamma=0.0)
The juliaExpr
function of the JuliaConnectoR
allows to use arbitrary Julia expressions in calls from R.
The expressions are evaluated in Julia and the resulting values are used in this place.
This is useful for situations like this where an argument of a function cannot be expressed properly in R.
An alternative to this is using the juliaEval
function, which will give the same output as juliaExpr
in this case, but will make a round trip from Julia to R before making the function call.
The created EvoTreeRegressor
object now uses the Julia Float64
type:
> params_evo
<Julia object of type EvoTrees.EvoTreeRegressor{EvoTrees.Linear, Float64}>
EvoTrees.EvoTreeRegressor{EvoTrees.Linear, Float64}(10, 0.0, 0.0, 0.1, 5, 1.0, 1.0, 1.0, 32, 0.5, Dict{Int64, Int64}(), Random.TaskLocalRNG(), "cpu")