Julia Flux error: MethodError: no method matching *(::Chain{Tuple{Conv{2,4,typeof(identity),Array{Float32,4},Array{Float32,1}},var"#13#15"}}, ::Int64)

Julia Flux error: MethodError: no method matching *(::Chain{Tuple{Conv{2,4,typeof(identity),Array{Float32,4},Array{Float32,1}},var"#13#15"}}, ::Int64)

using Flux
using Flux:@functor
function ConvBlock(inc,out,k,s,p,use_act)
    return Chain(
        Conv((k,k),inc=>out,stride = s,pad = p,bias=true),
        use_act ? x -> leakyrelu.(x,0.2) : x -> x
    )
end
mutable struct DenseResidualBlock
    residual_beta
    blocks
end

@functor DenseResidualBlock

function DenseResidualBlock(inc,c = 32,residual_beta = 0.2)
    blocks = []
    for i in 0:4
        in_channels = inc + c*i
        out_channels = i<=3 ? c : inc
        use_act = i<=3 ? true : false
        push!(blocks,ConvBlock(in_channels,out_channels,3,1,1,use_act))
    end

    return DenseResidualBlock(residual_beta,blocks)
end

function (m::DenseResidualBlock)(x) 
    new_inputs = x
    local out,new_inputs
    for block in m.blocks
        out = block(new_inputs)
        new_inputs = cat(new_inputs,out,dims=3)
    end
    return m.residual_beta * out + x
end

当我运行这个

drb = DenseResidualBlock(64)

我收到这个错误 错误:方法错误:没有方法匹配 *(::Chain{Tuple{Conv{2,4,typeof(identity),Array{Float32,4},Array{Float32,1}},var"#13#15"}} , ::Int64)

尝试

function DenseResidualBlock(inc;c = 32,residual_beta = 0.2)

而不是

function DenseResidualBlock(inc,c = 32,residual_beta = 0.2)

当使用两个参数调用 DenseResidualBlock 时,您的代码存在歧义。它可以直接构造 DenseResidualBlock 结构,也可以用 residual_beta = 0.2 调用 DenseResidualBlock(inc,c)。如果您为 DenseResidualBlock(inc; c = 32,residual_beta = 0.2) 使用关键字参数,则会消除这种歧义。

错误消息表明在行 in_channels = inc + c*i 中,参数 c 不是预期的数字,而是一个不能与数字相乘的 Flux.Chain