Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
############################################################ | ||
# Identity Layer | ||
############################################################ | ||
@defstruct IdentityLayer Layer ( | ||
name :: String = "identity", | ||
(tops :: Vector{Symbol} = Symbol[], length(tops) > 0), | ||
(bottoms :: Vector{Symbol} = Symbol[], length(bottoms) == length(tops)), | ||
) | ||
@characterize_layer(IdentityLayer, | ||
can_do_bp => true | ||
) | ||
|
||
type IdentityLayerState <: LayerState | ||
layer :: IdentityLayer | ||
blobs :: Vector{Blob} | ||
blobs_diff :: Vector{Blob} | ||
end | ||
|
||
function setup(backend::Backend, layer::IdentityLayer, inputs::Vector{Blob}, diffs::Vector{Blob}) | ||
blobs = inputs[:] # shallow copy | ||
blobs_diff = diffs[:] # shallow_copy | ||
|
||
IdentityLayerState(layer, blobs, blobs_diff) | ||
end | ||
function shutdown(backend::Backend, state::IdentityLayerState) | ||
end | ||
|
||
function forward(backend::Backend, state::IdentityLayerState, inputs::Vector{Blob}) | ||
# do nothing | ||
end | ||
|
||
function backward(backend::Backend, state::IdentityLayerState, inputs::Vector{Blob}, diffs::Vector{Blob}) | ||
# do nothing | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
function test_identity_layer(backend::Backend, T) | ||
println("-- Testing IdentityLayer on $(typeof(backend)){$T}...") | ||
|
||
eps = 1e-10 | ||
input = rand(T, 3, 4, 5) | ||
input_blob = make_blob(backend, input) | ||
|
||
println(" > Setup") | ||
layer = IdentityLayer(tops=[:foo], bottoms=[:bar]) | ||
state = setup(backend, layer, Blob[input_blob], Blob[NullBlob()]) | ||
@test all(map(x -> isa(x, NullBlob), state.blobs_diff)) | ||
|
||
println(" > Forward") | ||
forward(backend, state, Blob[input_blob]) | ||
got_output = to_array(state.blobs[1]) | ||
@test all(abs(got_output-input) .< eps) | ||
|
||
shutdown(backend, state) | ||
end | ||
|
||
function test_identity_layer(backend::Backend) | ||
test_identity_layer(backend, Float32) | ||
test_identity_layer(backend, Float64) | ||
end | ||
|
||
if test_cpu | ||
test_identity_layer(backend_cpu) | ||
end | ||
if test_gpu | ||
test_identity_layer(backend_gpu) | ||
end | ||
|