denizyuret / AutoGrad.jl

Julia port of the Python autograd package.
Other
169 stars 26 forks source link

Cannot `convert` an object of type UnitRange{Int64} to an object of type Colon #117

Open obviousnic opened 4 years ago

obviousnic commented 4 years ago

I am trying to get the KnetML style transfer example https://github.com/KnetML/Neural-Style-Transfer to work under Julia >= 1.0.

In Julia1.3.0 + Knet132 (which I think obtains Autograd1.2) I made the obvious syntax changes to update the code to Julia1, then tried two things.

  1. Use the old autograd interface gfn = grad(fn) (used in the original code), which is said to still work now for backward compatibility.

  2. Update to the newer Param, @diff interface, with approximately these modifications

    img_var = Param(img_var) # new
    for t in 1:iterations
    
    # old
        #grads, loss_value = loss_gradient(img_var, content_weight,content_layer,content_target, 
        #    style_layers, style_targets, style_weights, tv_weight)
        #update!(img_var, grads, optim)
    
        # new style api
        loss_gradient = @diff loss(img_var, content_weight,content_layer,content_target, 
            style_layers, style_targets, style_weights, tv_weight)
        update!(img_var, grad(loss_gradient,img_var), optim)
        loss_value = value(loss_gradient)   # used below

In both approaches, the following error results

ERROR: LoadError: MethodError: Cannot `convert` an object of type UnitRange{Int64} to an object of type Colon
Closest candidates are:
  convert(::Type{T}, ::T) where T at essentials.jl:167
Stacktrace:
 [1] convert(::Type{Tuple{Colon,UnitRange{Int64},Colon,Colon}}, ::Tuple{UnitRange{Int64},Colon,Colon,Colon}) at ./essentials.jl:304
 [2] setindex!(::Array{Tuple{Colon,UnitRange{Int64},Colon,Colon},1}, ::Tuple{UnitRange{Int64},Colon,Colon,Colon}, ::Int64) at ./array.jl:766
 [3] copyto!(::Array{Tuple{Colon,UnitRange{Int64},Colon,Colon},1}, ::Int64, ::Array{Tuple{UnitRange{Int64},Colon,Colon,Colon},1}, ::Int64, ::Int64) at ./abstractarray.jl:842
 [4] append!(::Array{Tuple{Colon,UnitRange{Int64},Colon,Colon},1}, ::Array{Tuple{UnitRange{Int64},Colon,Colon,Colon},1}) at ./array.jl:895
 [5] addto!(::AutoGrad.Sparse{Float64,4}, ::AutoGrad.Sparse{Float64,4}) at /root/.julia/packages/AutoGrad/pTNVv/src/addto.jl:44
 [6] #differentiate#3(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::typeof(AutoGrad.differentiate), ::Function, ::Param{KnetArray{Float64,4}}, ::Vararg{Any,N} where N) at /root/.julia/packages/AutoGrad/pTNVv/src/core.jl:166
 [7] differentiate(::Function, ::Param{KnetArray{Float64,4}}, ::Vararg{Any,N} where N) at /root/.julia/packages/AutoGrad/pTNVv/src/core.jl:135
 [8] (::getfield(AutoGrad, Symbol("##gradfun#6#8")){typeof(loss),Int64,Bool})(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::getfield(AutoGrad, Symbol("#gradfun#7")){getfield(AutoGrad, Symbol("##gradfun#6#8")){typeof(loss),Int64,Bool}}, ::KnetArray{Float64,4}, ::Vararg{Any,N} where N) at /root/.julia/packages/AutoGrad/pTNVv/src/core.jl:225
 [9] (::getfield(AutoGrad, Symbol("#gradfun#7")){getfield(AutoGrad, Symbol("##gradfun#6#8")){typeof(loss),Int64,Bool}})(::KnetArray{Float64,4}, ::Vararg{Any,N} where N) at /root/.julia/packages/AutoGrad/pTNVv/src/core.jl:221
 [10] style_transfer(::String, ::String, ::Int64, ::Int64, ::Int64, ::Float64, ::NTuple{5,Int64}, ::Array{Float64,1}, ::Float64, ::Bool) at /work/neural_style_transfer.jl:365
 [11] style_transfer(::String, ::String, ::Int64, ::Int64, ::Int64, ::Float64, ::NTuple{5,Int64}, ::Array{Float64,1}, ::Float64) at /work/neural_style_transfer.jl:329
 [12] top-level scope at util.jl:156
 [13] include at ./boot.jl:328 [inlined]
 [14] include_relative(::Module, ::String) at ./loading.jl:1094
 [15] include(::Module, ::String) at ./Base.jl:31
 [16] include(::String) at ./client.jl:431
 [17] top-level scope at REPL[1]:1
in expression starting at /work/neural_style_transfer.jl:397