Skip to content

User defined model does not work #597

@rs1909

Description

@rs1909

I have defined a model that is a multivariate polynomial. For some reason Flux.jacobian returns zero no matter what. ForwardDiff does not work either.

ForwardDiff gives:
ERROR: LoadError: TypeError: in typeassert, expected Float64, got ForwardDiff.Dual{Nothing,Float64,4}
I believe something is not being tracked, but I have no clue what.

I have posted the issue with a slightly different code here: https://discourse.julialang.org/t/flux-user-defined-layer-problem/20455?u=fastwave

The code is

using Flux
using LinearAlgebra
using DynamicPolynomials
using MultivariatePolynomials

struct PolyModel{S,T}
    mexp::S
    W::T
end

function PolyModel(in::Integer, out::Integer, order::Integer)
    @polyvar x[1:in]
    mx0 = monomials(x, 0:order)
    mx1 = [exponents(mx0[k]) for k=1:length(mx0)]
    return PolyModel(mx1, param(randn(out, length(mx0))))
end

function (a::PolyModel)(z::AbstractArray)
    mon = ones(length(a.mexp))
    for j=1:length(a.mexp)
        for k=1:length(z)
            for p=1:a.mexp[j][k]
                mon[j] *= z[k]
            end
        end
    end
    return a.W*mon
end

Flux.@treelike PolyModel

a = PolyModel(2,2,2)
j1 = Flux.jacobian(a,zeros(2))
j2 = ForwardDiff.jacobian(a,zeros(2))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions