Skip to content

Commit

Permalink
Update TensorFlow to version 2.3 (#238)
Browse files Browse the repository at this point in the history
* Update TensorFlow to version 2.2.0

* tf 2.3 now matches f-d on stateful tests

* Update TensorFlow from 2.2 to 2.3

* fixed gru issue in generate_test_models.py

* Reformat code

* Fix layer-type name for nested models. Model -> Functional

* Fix docstring

Co-authored-by: Dobiasd <[email protected]>
  • Loading branch information
keithchugg and Dobiasd authored Aug 13, 2020
1 parent 74eb39a commit ae6bc8b
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ install:
- sudo apt-get install libblas-dev liblapack-dev libatlas-base-dev gfortran
# python libs
- sudo pip3 install --upgrade pip
- sudo pip3 install numpy scipy h5py "tensorflow==2.1.1"
- sudo pip3 install numpy scipy h5py "tensorflow==2.3.0"
- echo "Version numbers of TensorFlow and Keras:"
- python3 -c "import tensorflow as tf; import tensorflow.keras; print(tf.__version__); print(tensorflow.keras.__version__)"
# FunctionalPlus
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Requirements and Installation

- A **C++14**-compatible compiler: Compilers from these versions on are fine: GCC 4.9, Clang 3.7 (libc++ 3.7) and Visual C++ 2015
- Python 3.7 or higher
- TensorFlow 2.1.1
- TensorFlow 2.3.0

Guides for different ways to install frugally-deep can be found in [`INSTALL.md`](INSTALL.md).

Expand Down
1 change: 1 addition & 0 deletions include/fdeep/import_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,7 @@ inline layer_ptr create_layer(const get_param_f& get_param,

const wrapper_layer_creators wrapper_creators = {
{"Model", create_model_layer},
{"Functional", create_model_layer},
{"TimeDistributed", create_time_distributed_layer},
};

Expand Down
7 changes: 4 additions & 3 deletions keras_export/convert_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def embedding_layer_names(model):
if isinstance(layer, Embedding):
result.add(layer.name)
layer_type = type(layer).__name__
if layer_type in ['Model', 'Sequential']:
if layer_type in ['Model', 'Sequential', 'Functional']:
result.union(embedding_layer_names(layer))
return result

Expand Down Expand Up @@ -602,7 +602,7 @@ def get_all_weights(model):
assert K.image_data_format() == 'channels_last'
for layer in layers:
layer_type = type(layer).__name__
if layer_type in ['Model', 'Sequential']:
if layer_type in ['Model', 'Sequential', 'Functional']:
result = merge_two_disjunct_dicts(result, get_all_weights(layer))
else:
if hasattr(layer, 'data_format'):
Expand Down Expand Up @@ -664,7 +664,8 @@ def convert_sequential_to_model(model):
model.inbound_nodes = inbound_nodes
assert model.layers
for i in range(len(model.layers)):
if type(model.layers[i]).__name__ in ['Model', 'Sequential']:
layer_type = type(model.layers[i]).__name__
if layer_type in ['Model', 'Sequential', 'Functional']:
# "model.layers[i] = ..." would not overwrite the layer.
model._layers[i] = convert_sequential_to_model(model.layers[i])
return model
Expand Down
5 changes: 3 additions & 2 deletions keras_export/generate_test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,9 @@ def get_test_model_gru_stateful_optional(stateful):

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

# fit to dummy data
training_data_size = 2
training_data_size = stateful_batch_size
data_in = generate_input_data(training_data_size, input_shapes)
initial_data_out = model.predict(data_in)
data_out = generate_output_data(training_data_size, initial_data_out)
Expand All @@ -653,7 +654,7 @@ def get_test_model_gru_stateful_optional(stateful):


def get_test_model_variable():
"""Returns a exhaustive model for variably shaped input tensors."""
"""Returns a model with variably shaped input tensors."""

input_shapes = [
(None, None, 1),
Expand Down
4 changes: 2 additions & 2 deletions test/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ RUN curl https://bazel.build/bazel-release.pub.gpg | apt-key add -
RUN echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | tee /etc/apt/sources.list.d/bazel.list
RUN apt update && apt install bazel-2.0.0

RUN git clone -b 'r2.1' --single-branch --depth 1 https://github.com/tensorflow/tensorflow.git
RUN git clone -b 'r2.3' --single-branch --depth 1 https://github.com/tensorflow/tensorflow.git
WORKDIR /tensorflow
RUN ./configure
RUN bazel-2.0.0 build -c opt //tensorflow/tools/pip_package:build_pip_package
RUN ./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
RUN pip install /tmp/tensorflow_pkg/tensorflow-2.1.1-cp37-cp37m-linux_x86_64.whl
RUN pip install /tmp/tensorflow_pkg/tensorflow-2.3.0-cp37-cp37m-linux_x86_64.whl
WORKDIR /

RUN git clone -b 'v0.2.9-p0' --single-branch --depth 1 https://github.com/Dobiasd/FunctionalPlus && cd FunctionalPlus && mkdir -p build && cd build && cmake .. && make && make install
Expand Down
36 changes: 17 additions & 19 deletions test/stateful_test/stateful_recurrent_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,16 @@ int main()
vec_append(all_results, *result[0].as_vector());

// ************************* BIDIRECTIONAL TESTS ************************* //
#define TF_BIDI_STATE_RESET_WORKS false

// *********** TEST 9: "bidi-GRU_nonstateful_no_init_state.json" ***********
model = load_model("./models/bidi-GRU_nonstateful_no_init_state.json");
/// state_reset = true
result = model.predict({test_in_0});
vec_append(all_results, *result[0].as_vector());
if(TF_BIDI_STATE_RESET_WORKS) model.reset_states();
model.reset_states();
result = model.predict({test_in_1});
vec_append(all_results, *result[0].as_vector());
if(TF_BIDI_STATE_RESET_WORKS) model.reset_states();
model.reset_states();
/// state_reset = false
result = model.predict({test_in_0});
vec_append(all_results, *result[0].as_vector());
Expand All @@ -189,10 +189,10 @@ int main()
/// state_reset = true
result = model.predict({test_in_0, test_state_0, test_state_1});
vec_append(all_results, *result[0].as_vector());
if(TF_BIDI_STATE_RESET_WORKS) model.reset_states();
model.reset_states();
result = model.predict({test_in_1, test_state_0, test_state_1});
vec_append(all_results, *result[0].as_vector());
if(TF_BIDI_STATE_RESET_WORKS) model.reset_states();
model.reset_states();
/// state_reset = false
result = model.predict({test_in_0, test_state_0, test_state_1});
vec_append(all_results, *result[0].as_vector());
Expand All @@ -204,10 +204,10 @@ int main()
/// state_reset = true
result = model.predict_stateful({test_in_0});
vec_append(all_results, *result[0].as_vector());
if(TF_BIDI_STATE_RESET_WORKS) model.reset_states();
model.reset_states();
result = model.predict_stateful({test_in_1});
vec_append(all_results, *result[0].as_vector());
if(TF_BIDI_STATE_RESET_WORKS) model.reset_states();
model.reset_states();
/// state_reset = false
result = model.predict_stateful({test_in_0});
vec_append(all_results, *result[0].as_vector());
Expand All @@ -219,10 +219,10 @@ int main()
/// state_reset = true
result = model.predict_stateful({test_in_0, test_state_0, test_state_1});
vec_append(all_results, *result[0].as_vector());
if(TF_BIDI_STATE_RESET_WORKS) model.reset_states();
model.reset_states();
result = model.predict_stateful({test_in_1, test_state_0, test_state_1});
vec_append(all_results, *result[0].as_vector());
if(TF_BIDI_STATE_RESET_WORKS) model.reset_states();
model.reset_states();
/// state_reset = false
result = model.predict_stateful({test_in_0, test_state_0, test_state_1});
vec_append(all_results, *result[0].as_vector());
Expand All @@ -234,10 +234,10 @@ int main()
/// state_reset = true
result = model.predict({test_in_0});
vec_append(all_results, *result[0].as_vector());
if(TF_BIDI_STATE_RESET_WORKS) model.reset_states();
model.reset_states();
result = model.predict({test_in_1});
vec_append(all_results, *result[0].as_vector());
if(TF_BIDI_STATE_RESET_WORKS) model.reset_states();
model.reset_states();
/// state_reset = false
result = model.predict({test_in_0});
vec_append(all_results, *result[0].as_vector());
Expand All @@ -249,10 +249,10 @@ int main()
/// state_reset = true
result = model.predict({test_in_0, test_state_0, test_state_1, test_state_2, test_state_3});
vec_append(all_results, *result[0].as_vector());
if(TF_BIDI_STATE_RESET_WORKS) model.reset_states();
model.reset_states();
result = model.predict({test_in_1, test_state_0, test_state_1, test_state_2, test_state_3});
vec_append(all_results, *result[0].as_vector());
if(TF_BIDI_STATE_RESET_WORKS) model.reset_states();
model.reset_states();
/// state_reset = false
result = model.predict({test_in_0, test_state_0, test_state_1, test_state_2, test_state_3});
vec_append(all_results, *result[0].as_vector());
Expand All @@ -264,10 +264,10 @@ int main()
/// state_reset = true
result = model.predict_stateful({test_in_0});
vec_append(all_results, *result[0].as_vector());
if(TF_BIDI_STATE_RESET_WORKS) model.reset_states();
model.reset_states();
result = model.predict_stateful({test_in_1});
vec_append(all_results, *result[0].as_vector());
if(TF_BIDI_STATE_RESET_WORKS) model.reset_states();
model.reset_states();
/// state_reset = false
result = model.predict_stateful({test_in_0});
vec_append(all_results, *result[0].as_vector());
Expand All @@ -279,18 +279,16 @@ int main()
/// state_reset = true
result = model.predict_stateful({test_in_0, test_state_0, test_state_1, test_state_2, test_state_3});
vec_append(all_results, *result[0].as_vector());
if(TF_BIDI_STATE_RESET_WORKS) model.reset_states();
model.reset_states();
result = model.predict_stateful({test_in_1, test_state_0, test_state_1, test_state_2, test_state_3});
vec_append(all_results, *result[0].as_vector());
if(TF_BIDI_STATE_RESET_WORKS) model.reset_states();
model.reset_states();
/// state_reset = false
result = model.predict_stateful({test_in_0, test_state_0, test_state_1, test_state_2, test_state_3});
vec_append(all_results, *result[0].as_vector());
result = model.predict_stateful({test_in_1, test_state_0, test_state_1, test_state_2, test_state_3});
vec_append(all_results, *result[0].as_vector());

#undef TF_BIDI_STATE_RESET_WORKS

if(verbose){
std::cout << "\n\nOUTPUT ***" << std::endl;
for(size_t idx = 0; idx < all_results.size(); ++ idx){
Expand Down

0 comments on commit ae6bc8b

Please sign in to comment.