Skip to content

Commit 24f40b6

Browse files
authored
Merge pull request #1945 from KronosTheLate/add_firstindex_method_to_chain
Add Base.firstindex(c::Chain) = 1
2 parents 6def768 + d92ed90 commit 24f40b6

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/layers/basic.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function Chain(; kw...)
4444
end
4545

4646
@forward Chain.layers Base.getindex, Base.length, Base.first, Base.last,
47-
Base.iterate, Base.lastindex, Base.keys
47+
Base.iterate, Base.lastindex, Base.keys, Base.firstindex
4848

4949
@functor Chain
5050

@@ -68,7 +68,6 @@ end
6868
Base.getindex(c::Chain, i::AbstractArray) = Chain(c.layers[i])
6969
Base.getindex(c::Chain{<:NamedTuple}, i::AbstractArray) =
7070
Chain(NamedTuple{Base.keys(c)[i]}(Tuple(c.layers)[i]))
71-
7271
function Base.show(io::IO, c::Chain)
7372
print(io, "Chain(")
7473
_show_layers(io, c.layers)

test/layers/basic.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ import Flux: activations
3131
@test_throws ArgumentError Chain(layers = Dense(10, 10), two = identity) # reserved name
3232

3333
@test_nowarn Chain([Dense(10, 5, σ), Dense(5, 2)])(randn(Float32, 10)) # vector of layers
34+
35+
c = Chain(Dense(10, 5, σ), Dense(5, 2), Dense(2, 1, relu))
36+
@test c[1] == c[begin]
37+
@test c[3] == c[end]
3438
end
3539

3640
@testset "Activations" begin

0 commit comments

Comments
 (0)