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


Python partitioned_variables.fixed_size_partitioner函数代码示例

本文整理汇总了Python中tensorflow.python.ops.partitioned_variables.fixed_size_partitioner函数的典型用法代码示例。如果您正苦于以下问题:Python fixed_size_partitioner函数的具体用法?Python fixed_size_partitioner怎么用?Python fixed_size_partitioner使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


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

示例1: _GenerateTestInputs

  def _GenerateTestInputs(self):
    np.random.seed(0)
    weights = np.random.randn(self._num_classes, self._dim).astype(np.float32)
    biases = np.random.randn(self._num_classes).astype(np.float32)
    hidden_acts = np.random.randn(self._batch_size,
                                  self._dim).astype(np.float32)

    with ops.Graph().as_default() as g:
      sharded_weights = variable_scope.get_variable(
          "w",
          partitioner=partitioned_variables.fixed_size_partitioner(
              self._num_shards),
          initializer=constant_op.constant(weights))
      sharded_biases = variable_scope.get_variable(
          "b",
          partitioner=partitioned_variables.fixed_size_partitioner(
              self._num_shards),
          initializer=constant_op.constant(biases))
      with self.test_session(graph=g) as sess:
        variables.global_variables_initializer().run()

        sharded_weights_v, sharded_biases_v = sess.run(
            [list(sharded_weights), list(sharded_biases)])

    return weights, biases, hidden_acts, sharded_weights_v, sharded_biases_v
开发者ID:AlbertXiebnu,项目名称:tensorflow,代码行数:25,代码来源:nn_test.py

示例2: test_load_and_remap_linear_multiclass_initializer_default_init

  def test_load_and_remap_linear_multiclass_initializer_default_init(self):
    """Tests where the zeros_initializer default is used for linear."""
    loading_initializer = (checkpoint_ops._load_and_remap_matrix_initializer(
        new_row_vocab_size=5,
        new_col_vocab_file=self.new_class_vocab_file,
        old_col_vocab_file=self.old_class_vocab_file,
        new_col_vocab_size=4,
        old_tensor_name='some_scope/embeddings',
        ckpt_path=[self.checkpoint_file],
        new_row_vocab_file=self.new_feature_vocab_file,
        old_row_vocab_file=self.old_feature_vocab_file,
        num_row_oov_buckets=1,
        num_col_oov_buckets=1))

    expected_remapped_matrix = np.concatenate(
        [
            np.reshape([2, 18, 34, 50, 0, 0], [6, 1]),
            np.reshape([0, 16, 32, 48, 0, 0], [6, 1]),
            np.reshape([0] * 6, [6, 1]),
            np.reshape([1, 17, 33, 49, 0, 0], [6, 1]),
            np.reshape([0] * 6, [6, 1])
        ],
        axis=1)

    remapped_matrix = variable_scope.get_variable(
        name='linear_init_fallback/obtained_weight_matrix',
        shape=[6, 5],
        initializer=loading_initializer,
        partitioner=partitioned_variables.fixed_size_partitioner(2))

    with self.test_session():
      variables.global_variables_initializer().run()
      self.assertAllClose(expected_remapped_matrix,
                          remapped_matrix.as_tensor().eval())
开发者ID:1000sprites,项目名称:tensorflow,代码行数:34,代码来源:checkpoint_ops_test.py

示例3: test_load_and_remap_output_layer_weight_initializer_dnn_output

  def test_load_and_remap_output_layer_weight_initializer_dnn_output(self):
    """Tests for the output layer initializer in the DNN output case."""
    loading_initializer = (checkpoint_ops._load_and_remap_matrix_initializer(
        new_row_vocab_size=5,
        new_col_vocab_file=self.new_class_vocab_file,
        old_col_vocab_file=self.old_class_vocab_file,
        new_col_vocab_size=4,
        old_tensor_name='some_scope/embeddings',
        ckpt_path=[self.checkpoint_file],
        num_col_oov_buckets=1,
        initializer=self.initializer))

    expected_remapped_matrix = np.concatenate(
        [
            np.reshape([2, 18, 34, 50, 66], [5, 1]),
            np.reshape([0, 16, 32, 48, 64], [5, 1]),
            np.reshape([self.init_val] * 5, [5, 1]),
            np.reshape([1, 17, 33, 49, 65], [5, 1]),
            np.reshape([self.init_val] * 5, [5, 1])
        ],
        axis=1)

    # The new weight matrix is of size
    # [5-sized input layer, 4 class vocab + 1 class OOV].
    remapped_matrix = variable_scope.get_variable(
        name='dnn_output/obtained_weight_matrix',
        shape=[5, 5],
        initializer=loading_initializer,
        partitioner=partitioned_variables.fixed_size_partitioner(2))

    with self.test_session():
      variables.global_variables_initializer().run()
      self.assertAllClose(expected_remapped_matrix,
                          remapped_matrix.as_tensor().eval())
开发者ID:1000sprites,项目名称:tensorflow,代码行数:34,代码来源:checkpoint_ops_test.py

示例4: testFixedSizePartitionerInt64

 def testFixedSizePartitionerInt64(self):
   with self.test_session():
     partitioner = partitioned_variables.fixed_size_partitioner(4, axis=0)
     with variable_scope.variable_scope("root", partitioner=partitioner):
       v0 = variable_scope.get_variable("v0", dtype=dtypes.int64, shape=[20])
       v0_list = v0._get_variable_list()
       self.assertEqual(len(v0_list), 4)
开发者ID:AnishShah,项目名称:tensorflow,代码行数:7,代码来源:partitioned_variables_test.py

示例5: test_load_linear_multiclass_bias_initializer

  def test_load_linear_multiclass_bias_initializer(self):
    """Tests for the bias initializer wrapper."""
    bias_loading_initializer = (
        contrib_framework.load_linear_multiclass_bias_initializer(
            new_class_vocab_file=self.new_class_vocab_file,
            old_class_vocab_file=self.old_class_vocab_file,
            new_class_vocab_size=4,
            bias_tensor_name='some_scope/bias',
            ckpt_path=[self.bundle_file],
            num_class_oov_buckets=1,
            initializer=self.initializer))

    expected_remapped_bias_vector = np.reshape(
        [2, 0, self.init_val, 1, self.init_val], [5, 1])

    # The new bias vector is of size [4 class vocab + 1 class OOV, 1].
    remapped_bias_vector = variable_scope.get_variable(
        name='bias/obtained_bias_vector',
        shape=[5, 1],
        initializer=bias_loading_initializer,
        partitioner=partitioned_variables.fixed_size_partitioner(3))

    with self.test_session():
      variables.global_variables_initializer().run()
      self.assertAllClose(expected_remapped_bias_vector,
                          remapped_bias_vector.as_tensor().eval())
开发者ID:1000sprites,项目名称:tensorflow,代码行数:26,代码来源:checkpoint_ops_test.py

示例6: _testMultiplePartitionedVariables

  def _testMultiplePartitionedVariables(self, is_training):
    # When weights are partitioned into multiple partitions the weights variable
    # is followed by a identity -> concat -> identity to group the partitions.
    partitioner = partitioned_variables.fixed_size_partitioner(2)
    graph = ops.Graph()
    with graph.as_default():
      with variable_scope.variable_scope('part', partitioner=partitioner):
        batch_size, height, width, depth = 5, 128, 128, 3
        input1 = array_ops.zeros((batch_size, height, width, depth))
        input2 = array_ops.zeros((batch_size, height / 2, width / 2, 32))
        conv = conv2d(
            input1,
            32, [5, 5],
            stride=2,
            padding='SAME',
            weights_initializer=self._WeightInit(0.09),
            activation_fn=None,
            scope='test/test')
        node = math_ops.add(conv, input2, name='test/add')
        node = nn_ops.relu6(node, name='test/relu6')

      quantize.Quantize(graph, is_training, weight_bits=8, activation_bits=8)
      # Check that the weight's quant node was added.
      op_names = [op.name for op in graph.get_operations()]
      self.assertTrue(
          'part/test/test/weights_quant/FakeQuantWithMinMaxVars' in op_names)
开发者ID:AnishShah,项目名称:tensorflow,代码行数:26,代码来源:quantize_test.py

示例7: testMetaGraphSaveLoad

  def testMetaGraphSaveLoad(self):
    save_prefix = os.path.join(self.get_temp_dir(), "ckpt")
    save_graph = ops.Graph()
    with save_graph.as_default(), self.test_session(
        graph=save_graph) as session:
      partitioner = partitioned_variables.fixed_size_partitioner(5, axis=0)
      with variable_scope.variable_scope("root", partitioner=partitioner):
        v0 = variable_scope.get_variable(
            "v0", dtype=dtypes.float32, shape=(10, 10))
        v0_list = v0._get_variable_list()
        v0_part = v0._get_partitions()
        self.assertEqual(len(v0_list), 5)
        self.assertAllEqual(v0_part, (5, 1))
        variables.global_variables_initializer().run()

        save_graph.get_collection_ref("partvar").append(v0)
        saver = saver_lib.Saver()
        save_graph.finalize()
        save_path = saver.save(sess=session, save_path=save_prefix)
        previous_value = session.run(
            save_graph.get_tensor_by_name(v0.name + ":0"))

    restore_graph = ops.Graph()
    with restore_graph.as_default(), self.test_session(
        graph=restore_graph) as session:
      saver = saver_lib.import_meta_graph(save_path + ".meta")
      saver.restore(sess=session, save_path=save_path)
      v0, = save_graph.get_collection_ref("partvar")
      self.assertIsInstance(v0, variables.PartitionedVariable)
      self.assertAllEqual(
          previous_value,
          session.run(restore_graph.get_tensor_by_name(v0.name + ":0")))
开发者ID:AnishShah,项目名称:tensorflow,代码行数:32,代码来源:partitioned_variables_test.py

示例8: test_load_embedding_initializer

  def test_load_embedding_initializer(self):
    """Tests for the load_embedding_initializer wrapper."""
    embedding_loading_initializer = (checkpoint_ops._load_embedding_initializer(
        new_vocab_file=self.new_feature_vocab_file,
        old_vocab_file=self.old_feature_vocab_file,
        new_vocab_size=5,
        embedding_dim=16,
        embedding_tensor_name='some_scope/embeddings',
        ckpt_path=[self.checkpoint_file],
        num_oov_buckets=1,
        initializer=self.initializer))

    expected_remapped_embeddings = np.concatenate(
        [
            np.reshape(range(64), [4, 16]),
            np.reshape([self.init_val] * 32, [2, 16]),
        ],
        axis=0)

    # The new weight matrix is of size
    # [5 feature vocab + 1 feature OOV, 16 (embedding dimension)], where the
    # last vocab row (2nd last row) is newly initialized (wasn't found in
    # previous vocab) and the actual last row is OOV and also newly initialized.
    # Use a partitioned variable to confirm that the offset logic works.
    remapped_embeddings = variable_scope.get_variable(
        name='embedding/obtained_embedding_matrix',
        shape=[6, 16],
        initializer=embedding_loading_initializer,
        partitioner=partitioned_variables.fixed_size_partitioner(2))

    with self.test_session():
      variables.global_variables_initializer().run()
      self.assertAllClose(expected_remapped_embeddings,
                          remapped_embeddings.as_tensor().eval())
开发者ID:1000sprites,项目名称:tensorflow,代码行数:34,代码来源:checkpoint_ops_test.py

示例9: test_loading_partitions_greater_than_max_rows

 def test_loading_partitions_greater_than_max_rows(self):
   """Tests loading partitioned var with more slices than partitions."""
   self._test_loading_variable_with_max_rows(
       np_value=np.reshape(list(range(0, 36)), (9, 4)),
       partitioner=partitioned_variables.fixed_size_partitioner(3),
       # Even though each partition has 3 rows, we'll only load the tensor one
       # row at a time.
       max_rows_in_memory=1)
开发者ID:HughKu,项目名称:tensorflow,代码行数:8,代码来源:checkpoint_ops_test.py

示例10: test_loading_partitions_equals_max_rows

 def test_loading_partitions_equals_max_rows(self):
   """Tests loading partitioned var sliced on partition boundary."""
   self._test_loading_variable_with_max_rows(
       np_value=np.reshape(list(range(0, 36)), (9, 4)),
       partitioner=partitioned_variables.fixed_size_partitioner(3),
       # With a tensor of shape [9, 3] and 3 partitions, each partition has
       # exactly 3 rows.
       max_rows_in_memory=3)
开发者ID:HughKu,项目名称:tensorflow,代码行数:8,代码来源:checkpoint_ops_test.py

示例11: test_loading_partitions_less_than_max_rows

  def test_loading_partitions_less_than_max_rows(self):
    """Tests loading partitioned var as a single slice.

    (When the specified max_rows_in_memory is larger than the number of rows)
    """
    self._test_loading_variable_with_max_rows(
        np_value=np.reshape(list(range(0, 36)), (9, 4)),
        partitioner=partitioned_variables.fixed_size_partitioner(3),
        max_rows_in_memory=10)
开发者ID:HughKu,项目名称:tensorflow,代码行数:9,代码来源:checkpoint_ops_test.py

示例12: testFixedSizePartitioner

 def testFixedSizePartitioner(self):
   with self.test_session():
     partitioner = partitioned_variables.fixed_size_partitioner(5, axis=0)
     with variable_scope.variable_scope("root", partitioner=partitioner):
       v0 = variable_scope.get_variable(
           "v0", dtype=dtypes.float32, shape=(10, 10))
       v0_list = v0._get_variable_list()
       v0_part = v0._get_partitions()
       self.assertEqual(len(v0_list), 5)
       self.assertAllEqual(v0_part, (5, 1))
开发者ID:AnishShah,项目名称:tensorflow,代码行数:10,代码来源:partitioned_variables_test.py

示例13: build

  def build(self, inputs_shape):
    if inputs_shape[1].value is None:
      raise ValueError("Expected inputs.shape[-1] to be known, saw shape: %s"
                       % inputs_shape)

    input_depth = inputs_shape[1].value
    h_depth = self._num_units if self._num_proj is None else self._num_proj
    maybe_partitioner = (
        partitioned_variables.fixed_size_partitioner(self._num_unit_shards)
        if self._num_unit_shards is not None
        else None)
    self._kernel = self.add_variable(
        _WEIGHTS_VARIABLE_NAME,
        shape=[input_depth + h_depth, 4 * self._num_units],
        initializer=self._initializer,
        partitioner=maybe_partitioner)
    self._bias = self.add_variable(
        _BIAS_VARIABLE_NAME,
        shape=[4 * self._num_units],
        initializer=init_ops.zeros_initializer(dtype=self.dtype))
    if self._use_peepholes:
      self._w_f_diag = self.add_variable("w_f_diag", shape=[self._num_units],
                                         initializer=self._initializer)
      self._w_i_diag = self.add_variable("w_i_diag", shape=[self._num_units],
                                         initializer=self._initializer)
      self._w_o_diag = self.add_variable("w_o_diag", shape=[self._num_units],
                                         initializer=self._initializer)

    if self._num_proj is not None:
      maybe_proj_partitioner = (
          partitioned_variables.fixed_size_partitioner(self._num_proj_shards)
          if self._num_proj_shards is not None
          else None)
      self._proj_kernel = self.add_variable(
          "projection/%s" % _WEIGHTS_VARIABLE_NAME,
          shape=[self._num_units, self._num_proj],
          initializer=self._initializer,
          partitioner=maybe_proj_partitioner)

    self.built = True
开发者ID:AbhinavJain13,项目名称:tensorflow,代码行数:40,代码来源:rnn_cell_impl.py

示例14: make_graph_with_partitioned_variables

 def make_graph_with_partitioned_variables(use_resource):
   variable_scope.get_variable(
       name="weights",
       partitioner=partitioned_variables.fixed_size_partitioner(3, axis=0),
       initializer=random_ops.truncated_normal([100, 10]),
       use_resource=use_resource)
   # The next variable illustrates the necessity of restoring collections
   # in a deterministic fashion when using ResourceVariables.
   variable_scope.get_variable(
       name="another",
       shape=[],
       collections=["a", "b", "z", "f", "e", "d", "g"],
       use_resource=use_resource)
开发者ID:aeverall,项目名称:tensorflow,代码行数:13,代码来源:meta_graph_test.py

示例15: testVariablesNotParitioned_MovingAvg

 def testVariablesNotParitioned_MovingAvg(self):
   # Variables added should not use a default partiioner since they are
   # scalar. There would be a tensorflow error thrown if the partitioner was
   # respected by the rewrite.
   with ops.Graph().as_default():
     with variable_scope.variable_scope(
         'part', partitioner=partitioned_variables.fixed_size_partitioner(2)):
       x = array_ops.placeholder(dtypes.float32, shape=[2])
       _ = quant_ops.MovingAvgQuantize(
           x,
           init_min=0.0,
           init_max=0.0,
           is_training=True,
           vars_collection=_MIN_MAX_VARS)
开发者ID:BhaskarNallani,项目名称:tensorflow,代码行数:14,代码来源:quant_ops_test.py


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