def test_NetworkDescription_to_json_config1(): config = Config() config.update(config1_dict) desc = LayerNetworkDescription.from_config(config) desc_json_content = desc.to_json_content() pprint(desc_json_content) assert_in("hidden_0", desc_json_content) assert_equal(desc_json_content["hidden_0"]["class"], "forward") assert_in("hidden_1", desc_json_content) assert_in("output", desc_json_content) orig_network = LayerNetwork.from_description(desc) assert_in("hidden_0", orig_network.hidden) assert_in("hidden_1", orig_network.hidden) assert_equal(len(orig_network.hidden), 2) assert_is_instance(orig_network.hidden["hidden_0"], ForwardLayer) assert_equal(orig_network.hidden["hidden_0"].layer_class, "hidden") orig_json_content = orig_network.to_json_content() pprint(orig_json_content) assert_in("hidden_0", orig_json_content) assert_equal(orig_json_content["hidden_0"]["class"], "hidden") assert_in("hidden_1", orig_json_content) assert_in("output", orig_json_content) new_network = LayerNetwork.from_json( desc_json_content, config1_dict["num_inputs"], {"classes": (config1_dict["num_outputs"], 1)}) new_json_content = new_network.to_json_content() if orig_json_content != new_json_content: print(dict_diff_str(orig_json_content, new_json_content)) assert_equal(orig_json_content, new_network.to_json_content())
def test_config1_to_json_network_copy(): config = Config() config.update(config1_dict) orig_network = LayerNetwork.from_config_topology(config) orig_json_content = orig_network.to_json_content() pprint(orig_json_content) new_network = LayerNetwork.from_json(orig_json_content, orig_network.n_in, orig_network.n_out) assert_equal(orig_network.n_in, new_network.n_in) assert_equal(orig_network.n_out, new_network.n_out) new_json_content = new_network.to_json_content() if orig_json_content != new_json_content: print(dict_diff_str(orig_json_content, new_json_content)) assert_equal(orig_json_content, new_network.to_json_content())
def maybe_init_new_network(self, net_desc): if self.network.layers_desc == net_desc: return from Util import dict_diff_str print("reinit because network description differs. Diff:", dict_diff_str(self.network.layers_desc, net_desc), file=log.v3) old_network_params = self.network.get_params_serialized(self.tf_session) self._init_network(net_desc) # Otherwise it's initialized randomly which is fine. # This copy will copy the old params over and leave the rest randomly initialized. # This also works if the old network has just the same topology, # e.g. if it is the initial model from self.init_network_from_config(). self.network.set_params_by_serialized(old_network_params, session=self.tf_session)
def demo(): """ Will print out the different network topologies of the specified pretraining scheme. """ import better_exchook better_exchook.install() import rnn import argparse from Util import dict_diff_str arg_parser = argparse.ArgumentParser() arg_parser.add_argument("config") arg_parser.add_argument("--diff", action="store_true", help="show diff only") arg_parser.add_argument('other_returnn_args', nargs=argparse.REMAINDER, help="config updates or so") args = arg_parser.parse_args() rnn.init_config(config_filename=args.config, command_line_options=args.other_returnn_args, extra_updates={"log": []}) # noinspection PyProtectedMember rnn.config._hack_value_reading_debug() rnn.init_log() if not rnn.config.value("pretrain", ""): print( "config option 'pretrain' not set, will set it for this demo to 'default'" ) rnn.config.set("pretrain", "default") pretrain = pretrain_from_config(rnn.config) print("pretrain: %s" % pretrain) num_pretrain_epochs = pretrain.get_train_num_epochs() last_net_json = None from pprint import pprint for epoch in range(1, 1 + num_pretrain_epochs): print("epoch %i (of %i) network json:" % (epoch, num_pretrain_epochs)) net_json = pretrain.get_network_json_for_epoch(epoch) if args.diff: if last_net_json is not None: print(dict_diff_str(last_net_json, net_json)) else: print("(initial)") else: pprint(net_json) last_net_json = net_json print("done.")