-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from JuliaReach/schillic/flux
Conversion from Flux networks
- Loading branch information
Showing
9 changed files
with
130 additions
and
18 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
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,8 @@ | ||
# optional dependencies | ||
function __init__() | ||
@require Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c" begin | ||
eval(load_Flux_activations()) | ||
eval(load_Flux_convert_layer()) | ||
eval(load_Flux_convert_network()) | ||
end | ||
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 |
---|---|---|
@@ -1,22 +1,16 @@ | ||
# always use the same instance of each activation function | ||
const _id = Id() | ||
const _relu = ReLU() | ||
const _sigmoid = Sigmoid() | ||
const _tanh = Tanh() | ||
|
||
const available_activations = Dict( | ||
# Id | ||
"Id" => _id, | ||
"linear" => _id, | ||
"Linear" => _id, | ||
"Affine" => _id, | ||
"Id" => Architecture._id, | ||
"linear" => Architecture._id, | ||
"Linear" => Architecture._id, | ||
"Affine" => Architecture._id, | ||
# ReLU | ||
"relu" => _relu, | ||
"ReLU" => _relu, | ||
"relu" => Architecture._relu, | ||
"ReLU" => Architecture._relu, | ||
# Sigmoid | ||
"sigmoid" => _sigmoid, | ||
"Sigmoid" => _sigmoid, | ||
"σ" => _sigmoid, | ||
"sigmoid" => Architecture._sigmoid, | ||
"Sigmoid" => Architecture._sigmoid, | ||
"σ" => Architecture._sigmoid, | ||
# Tanh | ||
"tanh" => _tanh, | ||
"Tanh" => _tanh) | ||
"tanh" => Architecture._tanh, | ||
"Tanh" => Architecture._tanh) |
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,44 @@ | ||
import Flux | ||
|
||
l1 = Flux.Dense(1, 2, Flux.relu) | ||
l1.weight .= 1, 2 | ||
l1.bias .= 3, 4 | ||
|
||
l2 = Flux.Dense(2, 3, Flux.sigmoid) | ||
l2.weight .= [1 2; 3 4; 5 6] | ||
|
||
l3 = Flux.Dense(3, 1) | ||
l3.weight .= [1 2 3;] | ||
|
||
l_unsupported = Flux.Dense(1 => 1, Flux.trelu) | ||
|
||
c = Flux.Chain(l1, l2, l3) | ||
|
||
activations = [ReLU(), Sigmoid(), Id()] | ||
|
||
# `==` is not defined for Flux types | ||
function compare_Flux_layer(l1, l2) | ||
return l1.weight == l2.weight && l1.bias == l2.bias && l1.σ == l2.σ | ||
end | ||
|
||
# layer conversion | ||
for (i, l) in enumerate(c.layers) | ||
op = convert(DenseLayerOp, l) | ||
@test op.weights == l.weight | ||
@test op.bias == l.bias | ||
@test op.activation == activations[i] | ||
|
||
l_back = convert(Flux.Dense, op) | ||
@test compare_Flux_layer(l, l_back) | ||
end | ||
@test_throws ArgumentError convert(DenseLayerOp, l_unsupported) | ||
|
||
# network conversion | ||
net = convert(FeedforwardNetwork, c) | ||
c_back = convert(Flux.Chain, net) | ||
@test length(net.layers) == length(c) | ||
for (i, l) in enumerate(c.layers) | ||
@test net.layers[i] == convert(DenseLayerOp, l) | ||
|
||
@test compare_Flux_layer(l, c_back.layers[i]) | ||
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
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