# Copyright 2019 The Keras Tuner Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import gc
import numpy as np
from tensorflow.python import Session, ConfigProto
from tensorflow.python.keras import backend as K


def compute_model_size(model):
    "comput the size of a given model"
    params = [K.count_params(p) for p in set(model.trainable_weights)]
    return int(np.sum(params))


def clear_tf_session():
    "Clear tensorflow graph to avoid OOM issues"
    K.clear_session()
    # K.get_session().close() # unsure if it is needed
    gc.collect()

    if hasattr(K, 'set_session'):
        cfg = ConfigProto()
        cfg.gpu_options.allow_growth = True  # pylint: disable=no-member
        K.set_session(Session(config=cfg))

# Copyright 2019 The Keras Tuner Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import gc
import numpy as np
from tensorflow.python import Session, ConfigProto
from tensorflow.python.keras import backend as K


def compute_model_size(model):
    "comput the size of a given model"
    params = [K.count_params(p) for p in model.trainable_weights]
    return int(np.sum(params))


def clear_tf_session():
    "Clear tensorflow graph to avoid OOM issues"
    K.clear_session()
    # K.get_session().close() # unsure if it is needed
    gc.collect()

    if hasattr(K, 'set_session'):
        cfg = ConfigProto()
        cfg.gpu_options.allow_growth = True  # pylint: disable=no-member
        K.set_session(Session(config=cfg))

Fix bugs for updating from tf 2.0b0 to rc0
Fix bugs for updating from tf 2.0b0 to rc0

Updating the dependency of tensorflow from 2.0.0b1 to 2.0.0rc0 is causing crashes in keras-tuner.
Not sure the "set" operation is really useful, but it is causing the crash because it tries to hash a tensor with equality enabled.

```
../../.virtualenvs/ak/lib/python3.6/site-packages/kerastuner/utils.py:23: in compute_model_size
    params = [K.count_params(p) for p in set(model.trainable_weights)]
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <tf.Variable 'dense/kernel:0' shape=(32, 32) dtype=float32, numpy=
array([[ 0.03147817, -0.30117437, -0.08552131, ...,...],
       [ 0.23273078, -0.10847142,  0.0151484 , ...,  0.16468436,
        -0.21210298, -0.1087181 ]], dtype=float32)>

    def __hash__(self):
      if ops.Tensor._USE_EQUALITY and ops.executing_eagerly_outside_functions():  # pylint: disable=protected-access
>       raise TypeError("Variable is unhashable if Tensor equality is enabled. "
                        "Instead, use tensor.experimental_ref() as the key.")
E       TypeError: Variable is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key.

../../.virtualenvs/ak/lib/python3.6/site-packages/tensorflow_core/python/ops/variables.py:1085: TypeError
```