def init(config_filename, log_verbosity): """ :param str config_filename: filename to config-file :param int log_verbosity: """ rnn.init_better_exchook() rnn.init_thread_join_hack() if config_filename: print("Using config file %r." % config_filename) assert os.path.exists(config_filename) rnn.init_config(config_filename=config_filename, command_line_options=[]) global config config = rnn.config config.set("log", None) config.set("log_verbosity", log_verbosity) config.set("use_tensorflow", True) rnn.init_log() print("Returnn compile-native-op starting up.", file=log.v1) rnn.returnn_greeting() rnn.init_backend_engine() assert util.BackendEngine.is_tensorflow_selected(), "this is only for TensorFlow" rnn.init_faulthandler() rnn.init_config_json_network() if 'network' in config.typed_dict: print("Loading network") from returnn.tf.network import TFNetwork network = TFNetwork( name="root", config=config, rnd_seed=1, train_flag=False, eval_flag=True, search_flag=False) network.construct_from_dict(config.typed_dict["network"])
def _run_returnn_standalone_net_dict(self): print(">>> Constructing RETURNN model, load TF checkpoint, run...") with tf.compat.v1.Session() as session: from returnn.config import Config from returnn.tf.network import TFNetwork config = Config({ "extern_data": { "data": self._returnn_in_data_dict }, "debug_print_layer_output_template": True, }) network = TFNetwork(config=config, name="root") network.construct_from_dict(self._returnn_net_dict) network.load_params_from_file( filename=self._tf_checkpoint_save_path, session=session) x = network.extern_data.get_default_input_data() y = network.get_default_output_layer().output feed_dict = self._make_tf_feed_dict(x) y_, y_size = session.run((y.placeholder, y.size_placeholder), feed_dict=feed_dict) assert isinstance(y_, numpy.ndarray) print("Output shape:", y_.shape) numpy.testing.assert_allclose(self._out_returnn_np, y_) print(">>>> Looks good!") print()
def test_Updater_simple_batch(): with make_scope() as session: from returnn.tf.network import TFNetwork, ExternData from returnn.config import Config from returnn.datasets.generating import Task12AXDataset dataset = Task12AXDataset() dataset.init_seq_order(epoch=1) extern_data = ExternData() extern_data.init_from_dataset(dataset) config = Config() network = TFNetwork(extern_data=extern_data, train_flag=True) network.construct_from_dict({ "layer1": { "class": "linear", "activation": "tanh", "n_out": 13, "from": "data:data" }, "layer2": { "class": "linear", "activation": "tanh", "n_out": 13, "from": ["layer1"] }, "output": { "class": "softmax", "loss": "ce", "target": "classes", "from": ["layer2"] } }) network.initialize_params(session=session) updater = Updater(config=config, network=network) updater.set_learning_rate(1.0, session=session) updater.set_trainable_vars(network.get_trainable_params()) updater.init_optimizer_vars(session=session) from returnn.tf.data_pipeline import FeedDictDataProvider batches = dataset.generate_batches( recurrent_net=network.recurrent, batch_size=100, max_seqs=10, max_seq_length=sys.maxsize, used_data_keys=network.used_data_keys) data_provider = FeedDictDataProvider(tf_session=session, extern_data=extern_data, data_keys=network.used_data_keys, dataset=dataset, batches=batches) feed_dict, _ = data_provider.get_feed_dict(single_threaded=True) session.run(updater.get_optim_op(), feed_dict=feed_dict)
def _run_returnn_standalone_python(self): print( ">>> Constructing RETURNN model via Python code, load TF checkpoint, run..." ) with tf.compat.v1.Session() as session: with Naming.make_instance( ) as naming: # we expect this to work with the default settings model_func = self._model_func # Wrap the model_func in a module. # We assume this would be flattened away in the namespace. # All named modules should thus have the same names. class DummyModule(torch_returnn.nn.Module): def get_returnn_name(self) -> str: return "" # also avoid that this name becomes a prefix anywhere def forward(self, *inputs): return model_func(wrapped_import_torch_returnn, *inputs) dummy_mod = DummyModule() net_dict = dummy_mod.as_returnn_net_dict( self._returnn_in_data_dict) from returnn.config import Config from returnn.tf.network import TFNetwork config = Config({ "extern_data": { "data": self._returnn_in_data_dict }, "debug_print_layer_output_template": True, }) network = TFNetwork(config=config, name="root") network.construct_from_dict(net_dict) network.load_params_from_file( filename=self._tf_checkpoint_save_path, session=session) x = network.extern_data.get_default_input_data() y = network.get_default_output_layer().output feed_dict = self._make_tf_feed_dict(x) y_, y_size = session.run((y.placeholder, y.size_placeholder), feed_dict=feed_dict) assert isinstance(y_, numpy.ndarray) print("Output shape:", y_.shape) numpy.testing.assert_allclose(self._out_returnn_np, y_) print(">>>> Looks good!") print()