Ejemplo n.º 1
0
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())
Ejemplo n.º 2
0
def test_config1_basic():
  config = Config()
  config.update(config1_dict)
  desc = LayerNetworkDescription.from_config(config)
  assert_is_instance(desc.hidden_info, list)
  assert_equal(len(desc.hidden_info), len(config1_dict["hidden_size"]))
  assert_equal(desc.num_inputs, config1_dict["num_inputs"])
Ejemplo n.º 3
0
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())
Ejemplo n.º 4
0
def test_config1_basic():
    config = Config()
    config.update(config1_dict)
    desc = LayerNetworkDescription.from_config(config)
    assert_is_instance(desc.hidden_info, list)
    assert_equal(len(desc.hidden_info), len(config1_dict["hidden_size"]))
    assert_equal(desc.num_inputs, config1_dict["num_inputs"])
Ejemplo n.º 5
0
 def init_args_from_config(cls, config):
   """
   :rtype: dict[str]
   :returns the kwarg for cls.from_json()
   """
   num_inputs, num_outputs = LayerNetworkDescription.num_inputs_outputs_from_config(config)
   return {
     "n_in": num_inputs, "n_out": num_outputs,
     "sparse_input": config.bool("sparse_input", False),
     "target": config.value('target', 'classes')
   }
Ejemplo n.º 6
0
def test_num_inputs_outputs_special_dataset():
  config = Config()
  config.update({
    "train": {"class": "CopyTaskDataset", "num_seqs": 1000, "nsymbols": 80, "minlen": 100, "maxlen": 100},
    "num_outputs": {"data": [80, 1], "classes": [80, 1]}})
  num_inputs, num_outputs = LayerNetworkDescription.num_inputs_outputs_from_config(config)
  assert_equal(num_inputs, 80)
  assert_in("data", num_outputs)
  assert_in("classes", num_outputs)
  assert_equal(num_outputs["classes"], [80, 1])
  assert_equal(num_outputs["data"], [80, 1])
Ejemplo n.º 7
0
def test_num_inputs_outputs_old():
  n_in = 5
  n_out = 10
  config = Config()
  config.update({"num_inputs": n_in, "num_outputs": n_out})
  num_inputs, num_outputs = LayerNetworkDescription.num_inputs_outputs_from_config(config)
  assert_equal(num_inputs, n_in)
  assert_is_instance(num_outputs, dict)
  assert_equal(len(num_outputs), 1)
  assert_in("classes", num_outputs)
  assert_equal(num_outputs["classes"], [n_out, 1])
Ejemplo n.º 8
0
def test_init():
    n_in = 5
    n_out = {"classes": (10, 1)}
    desc = LayerNetworkDescription(num_inputs=n_in,
                                   num_outputs=n_out,
                                   hidden_info=[],
                                   output_info={},
                                   default_layer_info={})

    assert_equal(desc.num_inputs, n_in)
    assert_equal(desc.num_outputs, n_out)
Ejemplo n.º 9
0
def test_num_inputs_outputs_special_dataset():
  config = Config()
  config.update({
    "train": {"class": "CopyTaskDataset", "num_seqs": 1000, "nsymbols": 80, "minlen": 100, "maxlen": 100},
    "num_outputs": {"data": [80, 1], "classes": [80, 1]}})
  num_inputs, num_outputs = LayerNetworkDescription.num_inputs_outputs_from_config(config)
  assert_equal(num_inputs, 80)
  assert_in("data", num_outputs)
  assert_in("classes", num_outputs)
  assert_equal(num_outputs["classes"], [80, 1])
  assert_equal(num_outputs["data"], [80, 1])
Ejemplo n.º 10
0
def test_num_inputs_outputs_old():
  n_in = 5
  n_out = 10
  config = Config()
  config.update({"num_inputs": n_in, "num_outputs": n_out})
  num_inputs, num_outputs = LayerNetworkDescription.num_inputs_outputs_from_config(config)
  assert_equal(num_inputs, n_in)
  assert_is_instance(num_outputs, dict)
  assert_equal(len(num_outputs), 1)
  assert_in("classes", num_outputs)
  assert_equal(num_outputs["classes"], [n_out, 1])
Ejemplo n.º 11
0
 def init_args_from_config(cls, config):
   """
   :rtype: dict[str]
   :returns the kwarg for cls.from_json()
   """
   num_inputs, num_outputs = LayerNetworkDescription.num_inputs_outputs_from_config(config)
   return {
     "n_in": num_inputs, "n_out": num_outputs,
     "sparse_input": config.bool("sparse_input", False),
     "target": config.value('target', 'classes')
   }
Ejemplo n.º 12
0
 def init_from_config(self, config):
   """
   :param Config.Config config:
   """
   from NetworkDescription import LayerNetworkDescription
   data_dims = LayerNetworkDescription.tf_extern_data_types_from_config(config)
   for key, init_args in data_dims.items():
     # In Returnn with Theano, we usually have the shape (time,batch,feature).
     # In TensorFlow, the default is (batch,time,feature).
     # This is also what we use here, i.e.:
     # batch_dim_axis=0, time_dim_axis=1. See TFEngine.DataProvider._get_next_batch().
     self.data[key] = Data(name=key, auto_create_placeholders=True, **init_args)
   self.default_target = config.value('target', 'classes')
Ejemplo n.º 13
0
def network_json_from_config(config, mask=None):
    """
  :type config: Config
  :param str mask: "unity", "none" or "dropout"
  :rtype: dict[str]
  """
    from Log import log
    json_content = None
    if config.has("network") and config.is_typed("network"):
        json_content = config.typed_value("network")
        assert isinstance(json_content, dict)
        assert json_content
    elif config.network_topology_json:
        start_var = config.network_topology_json.find(
            '(config:', 0)  # e.g. ..., "n_out" : (config:var), ...
        while start_var > 0:
            end_var = config.network_topology_json.find(')', start_var)
            assert end_var > 0, "invalid variable syntax at " + str(start_var)
            var = config.network_topology_json[start_var + 8:end_var]
            assert config.has(var), "could not find variable " + var
            config.network_topology_json = (
                config.network_topology_json[:start_var] +
                config.value(var, "") +
                config.network_topology_json[end_var + 1:])
            print("substituting variable %s with %s" %
                  (var, config.value(var, "")),
                  file=log.v4)
            start_var = config.network_topology_json.find(
                '(config:', start_var + 1)
        try:
            import json
            json_content = json.loads(config.network_topology_json)
        except ValueError as e:
            print("----- BEGIN JSON CONTENT -----", file=log.v3)
            print(config.network_topology_json, file=log.v3)
            print("------ END JSON CONTENT ------", file=log.v3)
            assert False, "invalid json content, %r" % e
        assert isinstance(json_content, dict)
        if 'network' in json_content:
            json_content = json_content['network']
        assert json_content
    if not json_content:
        if not mask:
            if sum(config.float_list('dropout', [0])) > 0.0:
                mask = "dropout"
        from NetworkDescription import LayerNetworkDescription
        description = LayerNetworkDescription.from_config(config)
        json_content = description.to_json_content(mask=mask)
    return json_content
Ejemplo n.º 14
0
def test_config2_bidirect_lstm():
  config = Config()
  config.update(config2_dict)
  desc = LayerNetworkDescription.from_config(config)
  assert_true(desc.bidirectional)
  network = LayerNetwork.from_config_topology(config)
  net_json = network.to_json_content()
  pprint(net_json)
  assert_in("output", net_json)
  assert_in("hidden_0_fw", net_json)
  assert_in("hidden_0_bw", net_json)
  assert_in("hidden_1_fw", net_json)
  assert_in("hidden_1_bw", net_json)
  assert_in("hidden_2_fw", net_json)
  assert_in("hidden_2_bw", net_json)
  assert_equal(net_json["output"]["from"], ["hidden_2_fw", "hidden_2_bw"])
  assert_equal(len(net_json), 7)
Ejemplo n.º 15
0
def test_config2_bidirect_lstm():
    config = Config()
    config.update(config2_dict)
    desc = LayerNetworkDescription.from_config(config)
    assert_true(desc.bidirectional)
    network = LayerNetwork.from_config_topology(config)
    net_json = network.to_json_content()
    pprint(net_json)
    assert_in("output", net_json)
    assert_in("hidden_0_fw", net_json)
    assert_in("hidden_0_bw", net_json)
    assert_in("hidden_1_fw", net_json)
    assert_in("hidden_1_bw", net_json)
    assert_in("hidden_2_fw", net_json)
    assert_in("hidden_2_bw", net_json)
    assert_equal(net_json["output"]["from"], ["hidden_2_fw", "hidden_2_bw"])
    assert_equal(len(net_json), 7)
Ejemplo n.º 16
0
def network_json_from_config(config, mask=None):
  """
  :type config: Config
  :param str mask: "unity", "none" or "dropout"
  :rtype: dict[str]
  """
  from Log import log
  json_content = None
  if config.has("network") and config.is_typed("network"):
    json_content = config.typed_value("network")
    assert isinstance(json_content, dict)
    assert json_content
  elif config.network_topology_json:
    start_var = config.network_topology_json.find('(config:', 0)  # e.g. ..., "n_out" : (config:var), ...
    while start_var > 0:
      end_var = config.network_topology_json.find(')', start_var)
      assert end_var > 0, "invalid variable syntax at " + str(start_var)
      var = config.network_topology_json[start_var+8:end_var]
      assert config.has(var), "could not find variable " + var
      config.network_topology_json = (
        config.network_topology_json[:start_var] + config.value(var, "") + config.network_topology_json[end_var+1:])
      print("substituting variable %s with %s" % (var, config.value(var, "")), file=log.v4)
      start_var = config.network_topology_json.find('(config:', start_var+1)
    try:
      import json
      json_content = json.loads(config.network_topology_json)
    except ValueError as e:
      print("----- BEGIN JSON CONTENT -----", file=log.v3)
      print(config.network_topology_json, file=log.v3)
      print("------ END JSON CONTENT ------", file=log.v3)
      assert False, "invalid json content, %r" % e
    assert isinstance(json_content, dict)
    if 'network' in json_content:
      json_content = json_content['network']
    assert json_content
  if not json_content:
    if not mask:
      if sum(config.float_list('dropout', [0])) > 0.0:
        mask = "dropout"
    from NetworkDescription import LayerNetworkDescription
    description = LayerNetworkDescription.from_config(config)
    json_content = description.to_json_content(mask=mask)
  return json_content