Skip to content

Commit

Permalink
Embedding seed parameter. Not utilized by default (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolay-r committed Apr 20, 2021
1 parent 661e196 commit aad6dc4
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion common/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def IO(self):
return self.__io

# TODO. Remove epochs count, since it is related to NeuralNetworks only.
def run_training(self, epochs_count):
def run_training(self, epochs_count, seed):
raise NotImplementedError()

def predict(self, data_type=DataType.Test):
Expand Down
6 changes: 5 additions & 1 deletion contrib/networks/context/architectures/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,19 @@ def compile_hidden_states_only(self, config):
self.__init_embedding_hidden_states()
self.init_body_dependent_hidden_states()

def compile(self, config, reset_graph):
def compile(self, config, reset_graph, graph_seed=None):
assert(isinstance(config, DefaultNetworkConfig))
assert(isinstance(reset_graph, bool))
assert(isinstance(graph_seed, int) or graph_seed is None)

self.__cfg = config

if reset_graph:
tf.reset_default_graph()

if graph_seed is not None:
tf.set_random_seed(graph_seed)

self.init_input()
self.__init_embedding_hidden_states()
self.init_body_dependent_hidden_states()
Expand Down
4 changes: 2 additions & 2 deletions contrib/networks/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def __dispose_session(self):
"""
self.__sess.close()

def run_training(self, epochs_count):
self.__network.compile(self.Config, reset_graph=True)
def run_training(self, epochs_count, seed):
self.__network.compile(self.Config, reset_graph=True, graph_seed=seed)
self.set_optimiser()
self.__notify_initialized()

Expand Down
2 changes: 1 addition & 1 deletion contrib/networks/core/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def iter_input_dependent_hidden_parameters(self):
return
yield

def compile(self, config, reset_graph):
def compile(self, config, reset_graph, graph_seed):
raise NotImplementedError()

def create_feed_dict(self, input, data_type):
Expand Down
6 changes: 5 additions & 1 deletion contrib/networks/multi/architectures/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ def DropoutKeepProb(self):

# region body

def compile(self, config, reset_graph):
def compile(self, config, reset_graph, graph_seed=None):
assert(isinstance(config, BaseMultiInstanceConfig))
assert(isinstance(graph_seed, int) or graph_seed is None)

self.__cfg = config
tf.reset_default_graph()

if graph_seed is not None:
tf.set_random_seed(graph_seed)

with tf.variable_scope(self.__ctx_network_scope):
self.__context_network.compile_hidden_states_only(config=config.ContextConfig)

Expand Down
8 changes: 6 additions & 2 deletions contrib/networks/run_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ class NetworksTrainingEngine(ExperimentEngine):
def __init__(self, bags_collection_type, experiment,
load_model, config,
create_network_func,
prepare_model_root=True):
prepare_model_root=True,
seed=None):
assert(callable(create_network_func))
assert(isinstance(config, DefaultNetworkConfig))
assert(issubclass(bags_collection_type, BagsCollection))
assert(isinstance(load_model, bool))
assert(isinstance(seed, int) or seed is None)

super(NetworksTrainingEngine, self).__init__(experiment)

self.__clear_model_root_before_experiment = prepare_model_root
self.__config = config
self.__create_network_func = create_network_func
self.__bags_collection_type = bags_collection_type
self.__load_model = load_model
self.__seed = seed

def __get_model_dir(self):
return self._experiment.DataIO.ModelIO.get_model_dir()
Expand Down Expand Up @@ -93,7 +97,7 @@ def _handle_iteration(self, it_index):

# Run model
with callback:
model.run_training(epochs_count=callback.Epochs)
model.run_training(epochs_count=callback.Epochs, seed=self.__seed)

del network
del model
Expand Down
2 changes: 1 addition & 1 deletion contrib/networks/tests/test_tf_ctx_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test(self):
logger.info("Clases count: {}".format(config.ClassesCount))

init_config(config)
network.compile(config, reset_graph=True)
network.compile(config, reset_graph=True, graph_seed=42)


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion contrib/networks/tests/test_tf_ctx_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def run_feeding(network, network_config, create_minibatch_func, logger,
labels_scaler = ThreeLabelScaler()
init_config(network_config)
# Init network.
network.compile(config=network_config, reset_graph=True)
network.compile(config=network_config, reset_graph=True, graph_seed=42)
minibatch = create_minibatch_func(config=network_config,
labels_scaler=labels_scaler)

Expand Down
2 changes: 1 addition & 1 deletion contrib/networks/tests/test_tf_mi_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def mpmi(context_config, context_network):

network = MaxPoolingOverSentences(context_network=context_network)
init_config(config)
network.compile(config, reset_graph=True)
network.compile(config, reset_graph=True, graph_seed=42)

def test(self):
logging.basicConfig(level=logging.INFO)
Expand Down

0 comments on commit aad6dc4

Please sign in to comment.