# Need help to translate this Pytorch part to Flux

**URL:** <https://discourse.julialang.org/t/need-help-to-translate-this-pytorch-part-to-flux/67032>\
**Category:** Machine Learning\
**Created:** [August 26, 2021, 12:01pm UTC](https://discourse.julialang.org/t/need-help-to-translate-this-pytorch-part-to-flux/67032 "2021-08-26T12:01:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![extremety1989](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/extremety1989/32/28184_2.png) [@extremety1989](https://discourse.julialang.org/u/extremety1989)\
**Post date:** [August 26, 2021, 12:01pm UTC](https://discourse.julialang.org/t/need-help-to-translate-this-pytorch-part-to-flux/67032/1 "2021-08-26T12:01:44Z")

</div>

i already made some progress

```julia
Pytorch code

class ResidualBlock(nn.Module):
    def __init__ (self, in_channels, channels=32, residual_beta=0.2):
        super(). __init__ ()
        self.residual_beta = residual_beta
        self.blocks = nn.ModuleList()

        for i in range(5):
            self.blocks.append(
                ConvBlock(
                    in_channels + channels * i,
                    channels if i <= 3 else in_channels,
                    kernel_size=3,
                    stride=1,
                    padding=1,
                    use_act=True if i <= 3 else False,
                )
            )

    def forward(self, x):
        new_inputs = x
        for block in self.blocks:
            out = block(new_inputs)
            new_inputs = torch.cat([new_inputs, out], dim=1)
        return self.residual_beta * out + x

Julia code

struct ResidualBlock
    residual_beta 
    blocks
end

@functor ResidualBlock
function ResidualBlock(
    in_channels::Int,
    channels::Int=32,
    residual_beta::Float32 = 0.2f0,
) 
return ResidualBlock(
   #this part is incorrect
    blocks = []
    for i in range(1, length=5)
        push!(blocks, 
            3,
            in_channels + channels * i,
            i <= 3 ? channels : in_channels,
            i <= 3 ? use_act=true : false,
            1,
            1)
    end
)  
end

function (net::ResidualBlock)(x)
    new_inputs = x
    for block in net.blocks
        out = block(new_inputs)
        new_inputs = cat([new_inputs, out], dims=1)
    end
    return net.residual_beta * out + x
end

```

---

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [August 26, 2021, 2:26pm UTC](https://discourse.julialang.org/t/need-help-to-translate-this-pytorch-part-to-flux/67032/2 "2021-08-26T14:26:05Z")

</div>

It would be good, if you more clearly state the problem, and create the minimal (not)working example, where people can just cut-and-paste it to start to work on the problem. That means, adding all libraries that you use, generate input data, how your struct is called, etc. As the request is stated by now, it is really difficult to help.

---

<div class="post-metadata">

**Author:** ![extremety1989](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/extremety1989/32/28184_2.png) [@extremety1989](https://discourse.julialang.org/u/extremety1989)\
**Post date:** [August 26, 2021, 2:32pm UTC](https://discourse.julialang.org/t/need-help-to-translate-this-pytorch-part-to-flux/67032/3 "2021-08-26T14:32:19Z")

</div>

sure

---

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [August 26, 2021, 4:41pm UTC](https://discourse.julialang.org/t/need-help-to-translate-this-pytorch-part-to-flux/67032/5 "2021-08-26T16:41:09Z")

</div>

Can you make it minimal to better see, what is causing a problem? I do not have a GPU, therefore I might not be able to help.

---

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [August 26, 2021, 6:45pm UTC](https://discourse.julialang.org/t/need-help-to-translate-this-pytorch-part-to-flux/67032/7 "2021-08-26T18:45:38Z")

</div>

You have a type in

```julia
Flux.indentity

```

but that is not the bug you are after

---

<div class="post-metadata">

**Author:** ![extremety1989](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/extremety1989/32/28184_2.png) [@extremety1989](https://discourse.julialang.org/u/extremety1989)\
**Post date:** [August 26, 2021, 6:53pm UTC](https://discourse.julialang.org/t/need-help-to-translate-this-pytorch-part-to-flux/67032/8 "2021-08-26T18:53:56Z")

</div>

i found example at flux-zoo how to build correctly models, i’am reimplementing it.  
thank you anyway

---

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [August 26, 2021, 6:56pm UTC](https://discourse.julialang.org/t/need-help-to-translate-this-pytorch-part-to-flux/67032/9 "2021-08-26T18:56:30Z")

</div>

Would this be of any help?

```julia
using Flux
using Zygote
using Functors

struct Prelu{T<:AbstractVector}
    slope::T
end

Prelu(dim::Int=1; init::Real=0.25f0) = Prelu(fill(float(init), dim))

@functor Prelu

function (p::Prelu)(x::AbstractArray)
	triv = x isa AbstractVector ? () : ntuple(_ -> 1, ndims(x) - 2)
	a = reshape(p.slope, triv..., :) # channel dim is 2nd-last, unless x is a vector
	leakyrelu.(x, a)
end

struct ConvBlock
    cnn
    act
end

@functor ConvBlock

function ConvBlock(
    kernel_size::Int,
    in_channels::Int,
    out_channels::Int,
    act::Bool,
    s::Int,
    p::Int,
) return ConvBlock(
    Conv((kernel_size, kernel_size), in_channels => out_channels; stride=s, pad=p,bias=true),
    if act 
        x -> leakyrelu.(x, 0.2)
    else 
        Flux.identity
    end
)  
end

function (net::ConvBlock)(x)
    return net.act(net.cnn(x))
end

in_channels = 4
channels = 1
kernel_size = 3

net = Chain([ ConvBlock(
		kernel_size,
        in_channels,
        in_channels,
        i <= 3,
        1,
        1,
    ) for i in 1:5]...)

net(randn(24,24,4,5))

```
