当前位置: 首页>>代码示例>>Python>>正文


Python Device.update_data方法代码示例

本文整理汇总了Python中Device.Device.update_data方法的典型用法代码示例。如果您正苦于以下问题:Python Device.update_data方法的具体用法?Python Device.update_data怎么用?Python Device.update_data使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Device.Device的用法示例。


在下文中一共展示了Device.update_data方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_multi_target_init

# 需要导入模块: from Device import Device [as 别名]
# 或者: from Device.Device import update_data [as 别名]
def test_multi_target_init():
  config = Config()
  config.update({
    "multiprocessing": False,
    "blocking": True,
    "device": "cpu",
    "num_epochs": 1,
    "num_inputs": 3,
    "num_outputs": {"t1": 4, "t2": 5},
    "learning_rate": 1.0,
  })
  config.network_topology_json = """
  {
  "fw0": {"class": "hidden", "activation": "identity", "n_out": 3},
  "out1": {"class": "softmax", "loss": "ce", "target": "t1", "from": ["fw0"]},
  "out2": {"class": "softmax", "loss": "ce", "target": "t2", "from": ["fw0"]}
  }
  """

  device = Device("cpu", config=config, blocking=True)
  assert_true(device.trainnet, "train network initialized")
  assert_true(device.testnet, "test network initialized")
  param_vars = device.trainnet.get_all_params_vars()
  print "params:", param_vars
  assert_equal(len(param_vars), 6, "W, b vars for each out, and fw")
  num_params = get_num_params(param_vars)
  assert_equal(num_params, (3 * 3 + 3) + (3 * 4 + 4) + (3 * 5 + 5), "W, b for each out, and fw")
  assert_in("fw0", device.testnet.hidden)
  assert_in("out1", device.testnet.output)
  assert_in("out2", device.testnet.output)
  assert_is(device.testnet.j["t1"], device.testnet.output["out1"].index)
  assert_true(device.updater)
  update_list = device.updater.getUpdateList()
  print "update list:"
  pprint(update_list)
  update_dict = dict(update_list)
  assert_equal(len(update_dict), len(update_list), "all params in update list only once")
  assert_in("fw0", device.trainnet.hidden)
  assert_equal(len(device.trainnet.hidden), 1)
  assert_in("W_in_data_fw0", device.trainnet.hidden["fw0"].params)
  assert_in("b_fw0", device.trainnet.hidden["fw0"].params)
  assert_equal(len(device.trainnet.hidden["fw0"].params), 2)
  assert_in("out1", device.trainnet.output)
  assert_equal(len(device.trainnet.output), 2)
  assert_in("W_in_fw0_out1", device.trainnet.output["out1"].params)
  assert_in("b_out1", device.trainnet.output["out1"].params)
  assert_equal(len(device.trainnet.output["out1"].params), 2)
  assert_in(device.trainnet.hidden["fw0"].params["W_in_data_fw0"], update_dict)
  assert_in(device.trainnet.hidden["fw0"].params["b_fw0"], update_dict)
  assert_in(device.trainnet.output["out1"].params["W_in_fw0_out1"], update_dict)
  assert_in(device.trainnet.output["out1"].params["b_out1"], update_dict)
  assert_in(device.trainnet.output["out2"].params["W_in_fw0_out2"], update_dict)
  assert_in(device.trainnet.output["out2"].params["b_out2"], update_dict)
  assert_equal(len(update_dict), 6)

  # Set net params.
  net_params = {
    "fw0": {"W_in_data_fw0": numpy.identity(3, dtype="float32"),
            "b_fw0": numpy.zeros((3,), dtype="float32")},
    "out1": {"W_in_fw0_out1": numpy.arange(0.0, 1.2, 0.1, dtype="float32").reshape((3, 4)),
             "b_out1": numpy.arange(0.0, 4, dtype="float32")},
    "out2": {"W_in_fw0_out2": numpy.arange(0.0, 1.5, 0.1, dtype="float32").reshape((3, 5)),
             "b_out2": numpy.arange(0.0, 5, dtype="float32")}
  }
  device.trainnet.set_params_by_dict(net_params)
  device.testnet.set_params_by_dict(net_params)

  # Show params.
  for p in param_vars:
    print "init %s:" % p
    pprint(p.get_value())

  # Init dataset.
  dataset = StaticDataset(data=[{
    "data": numpy.array([[0.1, 0.2, -0.3]], dtype="float32"),
    "t1": numpy.array([2]),
    "t2": numpy.array([4])
  }], output_dim=config.typed_value("num_outputs"))
  dataset.init_seq_order()
  assert_equal(dataset.is_data_sparse("data"), False)
  assert_equal(dataset.is_data_sparse("t1"), True)
  assert_equal(dataset.is_data_sparse("t2"), True)

  # Copy to device allocation.
  success = assign_dev_data_single_seq(device, dataset, 0)
  assert_true(success, "failed to allocate & assign data")

  # Check allocated data.
  assert_equal(device.targets["data"].shape, (1, 1, 3))  # input shape. (time,batch,dim)
  assert_in("t1", device.targets)
  assert_in("t2", device.targets)
  assert_equal(device.targets["t1"].shape, (1, 1))
  assert_equal(device.targets["t2"].shape, (1, 1))
  assert_equal(device.output_index["data"].shape, (1, 1))
  numpy.testing.assert_equal(device.output_index["data"], numpy.array([[1]]))
  assert_equal(device.output_index["t1"].shape, (1, 1))
  numpy.testing.assert_equal(device.output_index["t1"], numpy.array([[1]]))

  # Forward test.
  device.update_data()
#.........这里部分代码省略.........
开发者ID:atuxhe,项目名称:returnn,代码行数:103,代码来源:test_multi_target.py


注:本文中的Device.Device.update_data方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。