Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

juliacall.JuliaError: MethodError: Cannot convert an object of type PythonCall.Py to an object of type Tuple #183

Closed
HerClau opened this issue Jun 14, 2022 · 5 comments
Labels
documentation Improvements or additions to documentation

Comments

@HerClau
Copy link

HerClau commented Jun 14, 2022

Example 2: Calling Julia from Python

Traceback (most recent call last):
  File "F:/Python/Python Scripts/Python module JuliaCall/Example_2_Calling_Julia_from_Python.py", line 22, in <module>
    jl.Vector[jl.Tuple]([batch(100) for _ in range(2000)]),
  File "C:\Users\user\.julia\packages\PythonCall\4eoCM\src\jlwrap\any.jl", line 201, in __call__
    return self._jl_callmethod($(pyjl_methodnum(pyjlany_call)), args, kwargs)
juliacall.JuliaError: MethodError: Cannot `convert` an object of type PythonCall.Py to an object of type Tuple
Closest candidates are:
  convert(::Type{T}, !Matched::T) where T<:Tuple at c:\users\user\appdata\local\programs\julia-1.7.2\share\julia\base\essentials.jl:336
  convert(::Type{T}, !Matched::Tuple{Vararg{Any, N}}) where {N, T<:Tuple} at c:\users\user\appdata\local\programs\julia-1.7.2\share\julia\base\essentials.jl:337
  convert(::Type{T}, !Matched::CartesianIndex) where T<:Tuple at c:\users\user\appdata\local\programs\julia-1.7.2\share\julia\base\multidimensional.jl:137

julia> versioninfo()
Julia Version 1.7.3
Commit 742b9abb4d (2022-05-06 12:58 UTC)
Platform Info:
OS: Windows (x86_64-w64-mingw32)
CPU: AMD Ryzen 7 5700X 8-Core Processor
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-12.0.1 (ORCJIT, znver3)

(@v1.7) pkg> st Flux
Status C:\Users\user\.julia\environments\v1.7\Project.toml
[587475ba] Flux v0.13.3

(@v1.7) pkg> st PyCall
Status C:\Users\user\.julia\environments\v1.7\Project.toml
[438e738f] PyCall v1.93.1

Installed Pythons found by C:\Windows\py.exe Launcher for Windows
-3.8-64 C:\Users\user\AppData\Local\Programs\Python\Python38\python.exe *
pip list
juliacall 0.9.0
pip freeze
juliacall==0.9.0

@cjdoris
Copy link
Collaborator

cjdoris commented Jun 14, 2022

Oh I need to update that example. The issue is this line:

jl.Vector[jl.Tuple]([batch(100) for _ in range(2000)])

you need to do

from juliacall import Main as jl, convert as jlconvert

then replace that line with

jlconvert(jl.Vector[jl.Tuple], [batch(100) for _ in range(2000)])

@cjdoris cjdoris added the documentation Improvements or additions to documentation label Jun 14, 2022
@HerClau
Copy link
Author

HerClau commented Jun 14, 2022

I made the changes, but now another alert appears:

[juliapkg] Locating Julia ^1.6
[juliapkg] Using Julia 1.7.3 at C:\Users\user\AppData\Local\Programs\Julia-1.7.3\bin\julia.EXE
[juliapkg] Using Julia project at C:\Users\user\.julia\environments\pyjuliapkg
[juliapkg] Installing packages:
           julia> import Pkg
           julia> Pkg.add([Pkg.PackageSpec(name="PythonCall", uuid="6099a3de-0909-46bc-b1f4-468b9a2dfc0d")])
           julia> Pkg.resolve()

Traceback (most recent call last):
  File "F:\Python\Python Scripts\Python module JuliaCall\Example_2_Calling_Julia_from_Python.py", line 19, in <module>
    jl.Flux.train_b(
  File "C:\Users\user\.julia\packages\PythonCall\4eoCM\src\jlwrap\any.jl", line 201, in __call__
    return self._jl_callmethod($(pyjl_methodnum(pyjlany_call)), args, kwargs)
juliacall.JuliaError: UndefVarError: me not defined
Stacktrace:
  [1] adjoint
    @ C:\Users\user\.julia\packages\Zygote\ytjqm\src\lib\lib.jl:226 [inlined]
  [2] _pullback(__context__::Zygote.Context, 485::typeof(ZygoteRules.literal_getfield), x::Module, 486::Val{:me})
    @ Zygote C:\Users\user\.julia\packages\ZygoteRules\AIbCs\src\adjoint.jl:65
  [3] _pullback(cx::Zygote.Context, #unused#::typeof(getfield), x::Module, field_name::Symbol)
    @ Zygote C:\Users\user\.julia\packages\Zygote\ytjqm\src\lib\lib.jl:241
  [4] _pullback
    @ .\Base.jl:26 [inlined]
  [5] _pullback(::Zygote.Context, ::typeof(getproperty), ::Module, ::Symbol)
    @ Zygote C:\Users\user\.julia\packages\Zygote\ytjqm\src\compiler\interface2.jl:0
  [6] macro expansion
    @ C:\Users\user\.julia\packages\Zygote\ytjqm\src\lib\literal_getproperty.jl:83 [inlined]
  [7] _pullback
    @ C:\Users\user\.julia\packages\Zygote\ytjqm\src\lib\literal_getproperty.jl:44 [inlined]
  [8] _pullback
    @ .\none:1 [inlined]
  [9] _pullback(::Zygote.Context, ::var"#2#4"{Chain{Tuple{Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(identity), Matrix{Float32}, Vector{Float32}}}}}, ::PythonCall.PyArray{Float64, 2, true, false, Float64}, ::PythonCall.PyArray{Float64, 2, true, false, Float64})
    @ Zygote C:\Users\user\.julia\packages\Zygote\ytjqm\src\compiler\interface2.jl:0
 [10] _apply
    @ .\boot.jl:804 [inlined]
 [11] adjoint
    @ C:\Users\user\.julia\packages\Zygote\ytjqm\src\lib\lib.jl:200 [inlined]
 [12] _pullback
    @ C:\Users\user\.julia\packages\ZygoteRules\AIbCs\src\adjoint.jl:65 [inlined]
 [13] _pullback
    @ C:\Users\user\.julia\packages\Flux\js6mP\src\optimise\train.jl:120 [inlined]
 [14] _pullback(::Zygote.Context, ::Flux.Optimise.var"#37#40"{var"#2#4"{Chain{Tuple{Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(identity), Matrix{Float32}, Vector{Float32}}}}}, Tuple{PythonCall.PyArray{Float64, 2, true, false, Float64}, PythonCall.PyArray{Float64, 2, true, false, Float64}}})
    @ Zygote C:\Users\user\.julia\packages\Zygote\ytjqm\src\compiler\interface2.jl:0
 [15] pullback(f::Function, ps::Zygote.Params{Zygote.Buffer{Any, Vector{Any}}})
    @ Zygote C:\Users\user\.julia\packages\Zygote\ytjqm\src\compiler\interface.jl:352
 [16] gradient(f::Function, args::Zygote.Params{Zygote.Buffer{Any, Vector{Any}}})
    @ Zygote C:\Users\user\.julia\packages\Zygote\ytjqm\src\compiler\interface.jl:75
 [17] macro expansion
    @ C:\Users\user\.julia\packages\Flux\js6mP\src\optimise\train.jl:119 [inlined]
 [18] macro expansion
    @ C:\Users\user\.julia\packages\ProgressLogging\6KXlp\src\ProgressLogging.jl:328 [inlined]
 [19] train!(loss::Function, ps::Zygote.Params{Zygote.Buffer{Any, Vector{Any}}}, data::Vector{Tuple}, opt::ADAM; cb::Flux.Optimise.var"#38#41")
    @ Flux.Optimise C:\Users\user\.julia\packages\Flux\js6mP\src\optimise\train.jl:117
 [20] train!(loss::Function, ps::Zygote.Params{Zygote.Buffer{Any, Vector{Any}}}, data::Vector{Tuple}, opt::ADAM)
    @ Flux.Optimise C:\Users\user\.julia\packages\Flux\js6mP\src\optimise\train.jl:114
 [21] pyjlany_call(self::typeof(Flux.Optimise.train!), args_::PythonCall.Py, kwargs_::PythonCall.Py)
    @ PythonCall C:\Users\user\.julia\packages\PythonCall\4eoCM\src\jlwrap\any.jl:31
 [22] _pyjl_callmethod(f::Any, self_::Ptr{PythonCall.C.PyObject}, args_::Ptr{PythonCall.C.PyObject}, nargs::Int64)
    @ PythonCall C:\Users\user\.julia\packages\PythonCall\4eoCM\src\jlwrap\base.jl:69
 [23] _pyjl_callmethod(o::Ptr{PythonCall.C.PyObject}, args::Ptr{PythonCall.C.PyObject})
    @ PythonCall.C C:\Users\user\.julia\packages\PythonCall\4eoCM\src\cpython\jlwrap.jl:47

@cjdoris
Copy link
Collaborator

cjdoris commented Jun 14, 2022

I don't know what that error is, it works fine for me - check you haven't made a typo. I have fixed the example.

@HerClau
Copy link
Author

HerClau commented Jun 15, 2022

Reading the alert carefully, it seems that the error comes from calling the loss function:
"juliacall.JuliaError: UndefVarError: me not defined"
when changing the line:

loss = jl.seval("m -> (x, y) -> Flux.Losses.**ms**(m(x), y)")(model);

[my typo] by:

loss = jl.seval("m -> (x, y) -> Flux.Losses.mse(m(x), y)")(model)

The expected results are obtained:
loss = 0.015011342489806354 and the graph

@cjdoris
Copy link
Collaborator

cjdoris commented Jun 15, 2022

Awesome 👍

@cjdoris cjdoris closed this as completed Jun 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants