コード例 #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())
コード例 #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"])
コード例 #3
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"])
コード例 #4
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())
コード例 #5
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
コード例 #6
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)
コード例 #7
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)
コード例 #8
0
ファイル: Config.py プロジェクト: rwth-i6/returnn
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