2
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

When I run this

drb = DenseResidualBlock(64)

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

4

2 回答 2

2

try

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

instead of

function DenseResidualBlock(inc,c = 32,residual_beta = 0.2)
于 2021-07-19T12:55:01.450 回答
2

There is an ambiguity in your code when DenseResidualBlock is called with two arguments. It could either construct the structure DenseResidualBlock directly or call DenseResidualBlock(inc,c) with residual_beta = 0.2. If you use keyword arguments for DenseResidualBlock(inc; c = 32,residual_beta = 0.2) this ambiguity is lifted.

The error message indicated that at the line in_channels = inc + c*i the parameter c is not a number as expected but a Flux.Chain which cannot be multiplied by a number.

于 2021-07-19T12:59:27.507 回答