From 8a444260c99470218fe1b8030298602c329c0d3e Mon Sep 17 00:00:00 2001 From: Timo Kaufmann Date: Fri, 21 Aug 2020 15:12:23 +0200 Subject: [PATCH 1/2] Remove the clear memory function Its use is no longer clear (https://github.com/kiudee/cs-ranking/issues/148) and it introduces unnecessary complications and additional state. Its removal also allows us to remove the hash_file parameter. --- csrank/choicefunction/cmpnet_choice.py | 3 -- csrank/choicefunction/fate_choice.py | 4 -- csrank/choicefunction/feta_choice.py | 4 -- csrank/choicefunction/ranknet_choice.py | 4 -- csrank/core/cmpnet_core.py | 30 --------------- csrank/core/fate_network.py | 37 ------------------- csrank/core/feta_network.py | 30 --------------- csrank/core/ranknet_core.py | 30 --------------- .../discretechoice/cmpnet_discrete_choice.py | 3 -- csrank/discretechoice/fate_discrete_choice.py | 4 -- csrank/discretechoice/feta_discrete_choice.py | 4 -- .../discretechoice/ranknet_discrete_choice.py | 4 -- csrank/learner.py | 6 --- csrank/objectranking/cmp_net.py | 3 -- csrank/objectranking/fate_object_ranker.py | 4 -- csrank/objectranking/feta_object_ranker.py | 4 -- csrank/objectranking/list_net.py | 29 --------------- csrank/objectranking/rank_net.py | 3 -- 18 files changed, 206 deletions(-) diff --git a/csrank/choicefunction/cmpnet_choice.py b/csrank/choicefunction/cmpnet_choice.py index 3b9cd590..14a201da 100644 --- a/csrank/choicefunction/cmpnet_choice.py +++ b/csrank/choicefunction/cmpnet_choice.py @@ -187,6 +187,3 @@ def predict_for_scores(self, scores, **kwargs): def predict(self, X, **kwargs): return super().predict(X, **kwargs) - - def clear_memory(self, **kwargs): - super().clear_memory(**kwargs) diff --git a/csrank/choicefunction/fate_choice.py b/csrank/choicefunction/fate_choice.py index e42a4ae5..6ab78ec3 100644 --- a/csrank/choicefunction/fate_choice.py +++ b/csrank/choicefunction/fate_choice.py @@ -192,7 +192,3 @@ def predict_for_scores(self, scores, **kwargs): def predict(self, X, **kwargs): return super().predict(X, **kwargs) - - def clear_memory(self, **kwargs): - self.logger.info("Clearing memory") - super().clear_memory(**kwargs) diff --git a/csrank/choicefunction/feta_choice.py b/csrank/choicefunction/feta_choice.py index 6f74603e..688b63a7 100644 --- a/csrank/choicefunction/feta_choice.py +++ b/csrank/choicefunction/feta_choice.py @@ -364,7 +364,3 @@ def predict_for_scores(self, scores, **kwargs): def predict(self, X, **kwargs): return super().predict(X, **kwargs) - - def clear_memory(self, **kwargs): - self.logger.info("Clearing memory") - super().clear_memory(**kwargs) diff --git a/csrank/choicefunction/ranknet_choice.py b/csrank/choicefunction/ranknet_choice.py index 1b49efa2..5f79773e 100644 --- a/csrank/choicefunction/ranknet_choice.py +++ b/csrank/choicefunction/ranknet_choice.py @@ -181,7 +181,3 @@ def predict_for_scores(self, scores, **kwargs): def predict(self, X, **kwargs): return super().predict(X, **kwargs) - - def clear_memory(self, **kwargs): - self.logger.info("Clearing memory") - super().clear_memory(**kwargs) diff --git a/csrank/core/cmpnet_core.py b/csrank/core/cmpnet_core.py index 2bcc410f..a2fdbb51 100644 --- a/csrank/core/cmpnet_core.py +++ b/csrank/core/cmpnet_core.py @@ -1,7 +1,6 @@ from itertools import permutations import logging -from keras import backend as K from keras import Input from keras import Model from keras.layers import concatenate @@ -10,7 +9,6 @@ from keras.regularizers import l2 import numpy as np from sklearn.utils import check_random_state -import tensorflow as tf from csrank.constants import allowed_dense_kwargs from csrank.layers import NormalizedDense @@ -197,31 +195,3 @@ def _predict_scores_fixed(self, X, **kwargs): self.logger.info("Done predicting scores") return scores - - def clear_memory(self, **kwargs): - """ - Clear the memory, restores the currently fitted model back to prevent memory leaks. - - Parameters - ---------- - **kwargs : - Keyword arguments for the function - """ - if self.hash_file is not None: - self.model.save_weights(self.hash_file) - K.clear_session() - sess = tf.Session() - K.set_session(sess) - - self._initialize_optimizer() - self._initialize_regularizer() - self._construct_layers( - kernel_regularizer=self.kernel_regularizer_, - kernel_initializer=self.kernel_initializer, - activation=self.activation, - **self.kwargs, - ) - self.model = self.construct_model() - self.model.load_weights(self.hash_file) - else: - self.logger.info("Cannot clear the memory") diff --git a/csrank/core/fate_network.py b/csrank/core/fate_network.py index a8c5be9b..5fde1e16 100644 --- a/csrank/core/fate_network.py +++ b/csrank/core/fate_network.py @@ -1,6 +1,5 @@ import logging -import keras.backend as K from keras.layers import Dense from keras.layers import Input from keras.layers.merge import concatenate @@ -9,7 +8,6 @@ from keras.regularizers import l2 import numpy as np from sklearn.utils import check_random_state -import tensorflow as tf from csrank.constants import allowed_dense_kwargs from csrank.layers import create_input_lambda @@ -662,38 +660,3 @@ def _predict_scores_fixed(self, X, **kwargs): predicted_scores = joint_model.predict(X) self.logger.info("Done predicting scores") return predicted_scores - - def clear_memory(self, n_objects=5, **kwargs): - """ - Clear the memory, restores the currently fitted model back to prevent memory leaks. - - Parameters - ---------- - n_objects : int - float (n_instances, n_objects, n_features) - **kwargs : - Keyword arguments for the function - """ - if self.hash_file is not None: - self.model.save_weights(self.hash_file) - K.clear_session() - sess = tf.Session() - K.set_session(sess) - self._initialize_optimizer() - self._initialize_regularizer() - self._construct_layers( - activation=self.activation, - kernel_initializer=self.kernel_initializer, - kernel_regularizer=self.kernel_regularizer_, - **self.kwargs, - ) - self._create_set_layers( - activation=self.activation, - kernel_initializer=self.kernel_initializer, - kernel_regularizer=self.kernel_regularizer_, - **self.kwargs, - ) - self.model = self.construct_model(self.n_object_features_fit_, n_objects) - self.model.load_weights(self.hash_file) - else: - self.logger.info("Cannot clear the memory") diff --git a/csrank/core/feta_network.py b/csrank/core/feta_network.py index 3fdead3e..e05c4cfa 100644 --- a/csrank/core/feta_network.py +++ b/csrank/core/feta_network.py @@ -13,7 +13,6 @@ from keras.regularizers import l2 import numpy as np from sklearn.utils import check_random_state -import tensorflow as tf from csrank.constants import allowed_dense_kwargs from csrank.layers import NormalizedDense @@ -338,32 +337,3 @@ def _predict_scores_fixed(self, X, **kwargs): scores = self.model.predict(X, **kwargs) self.logger.info("Done predicting scores") return scores - - def clear_memory(self, **kwargs): - """ - Clear the memory, restores the currently fitted model back to prevent memory leaks. - - Parameters - ---------- - **kwargs : - Keyword arguments for the function - """ - if self.hash_file is not None: - self.model.save_weights(self.hash_file) - K.clear_session() - sess = tf.Session() - K.set_session(sess) - - self._pairwise_model = None - self._zero_order_model = None - self._initialize_optimizer() - self._construct_layers( - kernel_regularizer=self.kernel_regularizer_, - kernel_initializer=self.kernel_initializer, - activation=self.activation, - **self.kwargs, - ) - self.model = self.construct_model() - self.model.load_weights(self.hash_file) - else: - self.logger.info("Cannot clear the memory") diff --git a/csrank/core/ranknet_core.py b/csrank/core/ranknet_core.py index f7464091..e65d5b89 100644 --- a/csrank/core/ranknet_core.py +++ b/csrank/core/ranknet_core.py @@ -1,6 +1,5 @@ import logging -from keras import backend as K from keras import Input from keras import Model from keras.layers import add @@ -9,7 +8,6 @@ from keras.optimizers import SGD from keras.regularizers import l2 from sklearn.utils import check_random_state -import tensorflow as tf from csrank.constants import allowed_dense_kwargs from csrank.layers import NormalizedDense @@ -200,31 +198,3 @@ def _predict_scores_fixed(self, X, **kwargs): scores = scores.reshape(n_instances, n_objects) self.logger.info("Done predicting scores") return scores - - def clear_memory(self, **kwargs): - """ - Clear the memory, restores the currently fitted model back to prevent memory leaks. - - Parameters - ---------- - **kwargs : - Keyword arguments for the function - """ - if self.hash_file is not None: - self.model.save_weights(self.hash_file) - K.clear_session() - sess = tf.Session() - K.set_session(sess) - - self._scoring_model = None - self._initialize_optimizer() - self._construct_layers( - kernel_regularizer=self.kernel_regularizer_, - kernel_initializer=self.kernel_initializer, - activation=self.activation, - **self.kwargs, - ) - self.model = self.construct_model() - self.model.load_weights(self.hash_file) - else: - self.logger.info("Cannot clear the memory") diff --git a/csrank/discretechoice/cmpnet_discrete_choice.py b/csrank/discretechoice/cmpnet_discrete_choice.py index 6d22783a..10233cfd 100644 --- a/csrank/discretechoice/cmpnet_discrete_choice.py +++ b/csrank/discretechoice/cmpnet_discrete_choice.py @@ -117,6 +117,3 @@ def predict_for_scores(self, scores, **kwargs): def predict(self, X, **kwargs): return super().predict(X, **kwargs) - - def clear_memory(self, **kwargs): - super().clear_memory(**kwargs) diff --git a/csrank/discretechoice/fate_discrete_choice.py b/csrank/discretechoice/fate_discrete_choice.py index c72cab21..9be03c29 100644 --- a/csrank/discretechoice/fate_discrete_choice.py +++ b/csrank/discretechoice/fate_discrete_choice.py @@ -145,7 +145,3 @@ def predict_for_scores(self, scores, **kwargs): def predict(self, X, **kwargs): return super().predict(X, **kwargs) - - def clear_memory(self, **kwargs): - self.logger.info("Clearing memory") - super().clear_memory(**kwargs) diff --git a/csrank/discretechoice/feta_discrete_choice.py b/csrank/discretechoice/feta_discrete_choice.py index 2b868603..da604ff9 100644 --- a/csrank/discretechoice/feta_discrete_choice.py +++ b/csrank/discretechoice/feta_discrete_choice.py @@ -334,7 +334,3 @@ def predict_for_scores(self, scores, **kwargs): def predict(self, X, **kwargs): return super().predict(X, **kwargs) - - def clear_memory(self, **kwargs): - self.logger.info("Clearing memory") - super().clear_memory(**kwargs) diff --git a/csrank/discretechoice/ranknet_discrete_choice.py b/csrank/discretechoice/ranknet_discrete_choice.py index 68a782d0..c07b9221 100644 --- a/csrank/discretechoice/ranknet_discrete_choice.py +++ b/csrank/discretechoice/ranknet_discrete_choice.py @@ -113,7 +113,3 @@ def predict_for_scores(self, scores, **kwargs): def predict(self, X, **kwargs): return super().predict(X, **kwargs) - - def clear_memory(self, **kwargs): - self.logger.info("Clearing memory") - super().clear_memory(**kwargs) diff --git a/csrank/learner.py b/csrank/learner.py index db05a239..f3785164 100644 --- a/csrank/learner.py +++ b/csrank/learner.py @@ -95,12 +95,6 @@ def predict_scores(self, X, **kwargs): scores = dict() for ranking_size, x in X.items(): n_instances, n_objects, n_features = x.shape - if "clear_memory" in dir(self): - self.clear_memory( - n_instances=n_instances, - n_objects=n_objects, - n_features=n_features, - ) scores[ranking_size] = self._predict_scores_fixed(x, **kwargs) else: diff --git a/csrank/objectranking/cmp_net.py b/csrank/objectranking/cmp_net.py index 87c42c67..0b037ab1 100644 --- a/csrank/objectranking/cmp_net.py +++ b/csrank/objectranking/cmp_net.py @@ -165,6 +165,3 @@ def predict_for_scores(self, scores, **kwargs): def predict(self, X, **kwargs): return super().predict(X, **kwargs) - - def clear_memory(self, **kwargs): - super().clear_memory(**kwargs) diff --git a/csrank/objectranking/fate_object_ranker.py b/csrank/objectranking/fate_object_ranker.py index 34321636..2beb3854 100644 --- a/csrank/objectranking/fate_object_ranker.py +++ b/csrank/objectranking/fate_object_ranker.py @@ -110,7 +110,3 @@ def predict_for_scores(self, scores, **kwargs): def predict(self, X, **kwargs): return super().predict(X, **kwargs) - - def clear_memory(self, **kwargs): - self.logger.info("Clearing memory") - super().clear_memory(**kwargs) diff --git a/csrank/objectranking/feta_object_ranker.py b/csrank/objectranking/feta_object_ranker.py index e2528f4e..fb337472 100644 --- a/csrank/objectranking/feta_object_ranker.py +++ b/csrank/objectranking/feta_object_ranker.py @@ -149,7 +149,3 @@ def predict_for_scores(self, scores, **kwargs): def predict(self, X, **kwargs): return super().predict(X, **kwargs) - - def clear_memory(self, **kwargs): - self.logger.info("Clearing memory") - super().clear_memory(**kwargs) diff --git a/csrank/objectranking/list_net.py b/csrank/objectranking/list_net.py index 932decad..01e42f17 100644 --- a/csrank/objectranking/list_net.py +++ b/csrank/objectranking/list_net.py @@ -1,6 +1,5 @@ import logging -from keras import backend as K from keras import Input from keras.layers import concatenate from keras.layers import Dense @@ -8,7 +7,6 @@ from keras.optimizers import SGD from keras.regularizers import l2 from sklearn.utils import check_random_state -import tensorflow as tf from csrank.constants import allowed_dense_kwargs from csrank.layers import create_input_lambda @@ -262,30 +260,3 @@ def predict_for_scores(self, scores, **kwargs): def predict(self, X, **kwargs): return super().predict(X, **kwargs) - - def clear_memory(self, **kwargs): - """ - Clear the memory, restores the currently fitted model back to prevent memory leaks. - - Parameters - ---------- - **kwargs : - Keyword arguments for the function - """ - if self.hash_file is not None: - self.model.save_weights(self.hash_file) - K.clear_session() - sess = tf.Session() - K.set_session(sess) - self._scoring_model = None - self._initialize_optimizer() - self._construct_layers( - kernel_regularizer=self.kernel_regularizer_, - kernel_initializer=self.kernel_initializer, - activation=self.activation, - **self.kwargs, - ) - self.model = self.construct_model() - self.model.load_weights(self.hash_file) - else: - self.logger.info("Cannot clear the memory") diff --git a/csrank/objectranking/rank_net.py b/csrank/objectranking/rank_net.py index a2f8325d..822abbfd 100644 --- a/csrank/objectranking/rank_net.py +++ b/csrank/objectranking/rank_net.py @@ -156,6 +156,3 @@ def predict_for_scores(self, scores, **kwargs): def predict(self, X, **kwargs): return super().predict(X, **kwargs) - - def clear_memory(self, **kwargs): - super().clear_memory(**kwargs) From 76a9854a4ce765fa7e2d4ba9402d8207958dc1a8 Mon Sep 17 00:00:00 2001 From: Timo Kaufmann Date: Fri, 21 Aug 2020 15:13:46 +0200 Subject: [PATCH 2/2] Remove hash_file parameter and attribute Reduces complexity and brings us one step closer to removing state (attributes) of estimators that is not set through the init parameters. --- csrank/core/cmpnet_core.py | 1 - csrank/core/fate_network.py | 1 - csrank/core/feta_network.py | 3 --- csrank/core/ranknet_core.py | 1 - csrank/objectranking/list_net.py | 3 --- 5 files changed, 9 deletions(-) diff --git a/csrank/core/cmpnet_core.py b/csrank/core/cmpnet_core.py index a2fdbb51..aea5828f 100644 --- a/csrank/core/cmpnet_core.py +++ b/csrank/core/cmpnet_core.py @@ -34,7 +34,6 @@ def __init__( self.logger = logging.getLogger("CmpNet") self.batch_normalization = batch_normalization self.activation = activation - self.hash_file = None self.batch_size = batch_size diff --git a/csrank/core/fate_network.py b/csrank/core/fate_network.py index 5fde1e16..9d5c3cd8 100644 --- a/csrank/core/fate_network.py +++ b/csrank/core/fate_network.py @@ -183,7 +183,6 @@ def __init__(self, n_hidden_set_layers=1, n_hidden_set_units=1, **kwargs): kernel_regularizer=self.kernel_regularizer_, ) self.is_variadic = True - self.hash_file = None def _create_set_layers(self, **kwargs): """ diff --git a/csrank/core/feta_network.py b/csrank/core/feta_network.py index e05c4cfa..df35a3bd 100644 --- a/csrank/core/feta_network.py +++ b/csrank/core/feta_network.py @@ -50,7 +50,6 @@ def __init__( self.max_number_of_objects = max_number_of_objects self.num_subsample = num_subsample self.batch_size = batch_size - self.hash_file = None self.optimizer = optimizer self.add_zeroth_order_model = add_zeroth_order_model self.n_hidden = n_hidden @@ -306,8 +305,6 @@ def fit( verbose=verbose, **kwd, ) - if self.hash_file is not None: - self.model.save_weights(self.hash_file) def sub_sampling(self, X, Y): if self.n_objects_fit_ > self.max_number_of_objects: diff --git a/csrank/core/ranknet_core.py b/csrank/core/ranknet_core.py index e65d5b89..92e3da5d 100644 --- a/csrank/core/ranknet_core.py +++ b/csrank/core/ranknet_core.py @@ -48,7 +48,6 @@ def __init__( self.batch_size = batch_size self._scoring_model = None self.model = None - self.hash_file = None self.random_state = random_state def _construct_layers(self, **kwargs): diff --git a/csrank/objectranking/list_net.py b/csrank/objectranking/list_net.py index 01e42f17..62f444e7 100644 --- a/csrank/objectranking/list_net.py +++ b/csrank/objectranking/list_net.py @@ -50,8 +50,6 @@ def __init__( ---------- n_top : int Size of the top-k-subrankings to consider for training - hash_file: str - File path of the model where the weights are stored to get the predictions after clearing the memory n_hidden : int Number of hidden layers used in the scoring network n_units : int @@ -105,7 +103,6 @@ def __init__( self.batch_size = batch_size self.random_state = random_state - self.hash_file = None self.model = None self._scoring_model = None