Skip to content

Commit

Permalink
Fix invalid-beta error when using BatchNormalization layer inside Tim…
Browse files Browse the repository at this point in the history
…eDistribution

* Add test case for BatchNormalization as inner layer of TimeDistributed

* Fix rank of slices passed to inner layer of TimeDistribution
  • Loading branch information
Dobiasd authored Jul 3, 2021
1 parent c7587bb commit d2b5758
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/fdeep/layers/time_distributed_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class time_distributed_layer : public layer
else
raise_error("invalid input dim for TimeDistributed");

for (auto& slice: slices)
{
slice.shrink_rank();
}

if (td_output_len_ == 2)
concat_axis = 2;
else if (td_output_len_ == 3)
Expand Down
4 changes: 4 additions & 0 deletions include/fdeep/tensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ class tensor
{
return shape_;
}
void shrink_rank()
{
shape_.shrink_rank();
}
std::size_t depth() const
{
return shape().depth_;
Expand Down
18 changes: 18 additions & 0 deletions include/fdeep/tensor_shape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,24 @@ class tensor_shape
return rank_;
}

std::size_t minimal_rank() const
{
if (size_dim_5_ > 1)
return 5;
if (size_dim_4_ > 1)
return 4;
if (height_ > 1)
return 3;
if (width_ > 1)
return 2;
return 1;
}

void shrink_rank()
{
rank_ = minimal_rank();
}

std::vector<std::size_t> dimensions() const
{
if (rank() == 5)
Expand Down
1 change: 1 addition & 0 deletions keras_export/generate_test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ def get_test_model_recurrent():

outputs.append(TimeDistributed(MaxPooling2D(2, 2))(inputs[3]))
outputs.append(TimeDistributed(AveragePooling2D(2, 2))(inputs[3]))
outputs.append(TimeDistributed(BatchNormalization())(inputs[3]))

model = Model(inputs=inputs, outputs=outputs, name='test_model_recurrent')
model.compile(loss='mse', optimizer='nadam')
Expand Down

0 comments on commit d2b5758

Please sign in to comment.