示例#1
0
 def testUnknownSize(self):
   value = array_ops.placeholder(dtype=dtypes.float32)
   sparsity = nn_impl.zero_fraction(value)
   with self.cached_session() as sess:
     self.assertAllClose(
         0.25,
         sess.run(sparsity, {value: [[0., 1.], [0.3, 2.]]}))
示例#2
0
 def testUnknownSize(self):
   value = array_ops.placeholder(dtype=dtypes.float32)
   sparsity = nn_impl.zero_fraction(value)
   with self.cached_session() as sess:
     self.assertAllClose(
         0.25,
         sess.run(sparsity, {value: [[0., 1.], [0.3, 2.]]}))
示例#3
0
 def add_pruning_summaries(self):
   """Adds summaries of weight sparsities and thresholds."""
   with ops.name_scope(self._spec.name + '_summaries'):
     summary.scalar('sparsity', self._sparsity)
     summary.scalar('last_mask_update_step', self._last_update_step)
     masks = get_masks()
     thresholds = get_thresholds()
     for mask, threshold in zip(masks, thresholds):
       summary.scalar(mask.op.name + '/sparsity', nn_impl.zero_fraction(mask))
       summary.scalar(threshold.op.name + '/threshold', threshold)
示例#4
0
 def add_pruning_summaries(self):
   """Adds summaries of weight sparsities and thresholds."""
   with ops.name_scope(self._spec.name + '_summaries'):
     summary.scalar('sparsity', self._sparsity)
     summary.scalar('last_mask_update_step', self._last_update_step)
     masks = get_masks()
     thresholds = get_thresholds()
     for mask, threshold in zip(masks, thresholds):
       summary.scalar(mask.op.name + '/sparsity', nn_impl.zero_fraction(mask))
       summary.scalar(threshold.op.name + '/threshold', threshold)
示例#5
0
def get_weight_sparsity():
    """Get sparsity of the weights.

  Args:
    None

  Returns:
    A list containing the sparsity of each of the weight tensors
  """
    masks = get_masks()
    return [nn_impl.zero_fraction(mask) for mask in masks]
示例#6
0
 def testZeroFraction(self):
     x_shape = [5, 17]
     x_np = np.random.randint(0, 2, size=x_shape).astype(np.float32)
     y_np = self._ZeroFraction(x_np)
     with self.test_session():
         x_tf = constant_op.constant(x_np)
         x_tf.set_shape(x_shape)
         y_tf = nn_impl.zero_fraction(x_tf)
         y_tf_np = y_tf.eval()
     eps = 1e-8
     self.assertAllClose(y_tf_np, y_np, eps)
示例#7
0
 def testZeroFraction(self):
   x_shape = [5, 17]
   x_np = np.random.randint(0, 2, size=x_shape).astype(np.float32)
   y_np = self._ZeroFraction(x_np)
   with self.test_session():
     x_tf = constant_op.constant(x_np)
     x_tf.set_shape(x_shape)
     y_tf = nn_impl.zero_fraction(x_tf)
     y_tf_np = y_tf.eval()
   eps = 1e-8
   self.assertAllClose(y_tf_np, y_np, eps)
示例#8
0
def get_weight_sparsity():
  """Get sparsity of the weights.

  Args:
    None

  Returns:
    A list containing the sparsity of each of the weight tensors
  """
  masks = get_masks()
  return [nn_impl.zero_fraction(mask) for mask in masks]
示例#9
0
    def add_pruning_summaries(self):
        """Adds summaries for this pruning spec.

    Args: none

    Returns: none
    """
        with ops.name_scope(self._spec.name + '_summaries'):
            summary.scalar('sparsity', self._sparsity)
            summary.scalar('last_mask_update_step', self._last_update_step)
            masks = get_masks()
            thresholds = get_thresholds()
            for mask, threshold in zip(masks, thresholds):
                if not self._exists_in_do_not_prune_list(mask.name):
                    summary.scalar(mask.op.name + '/sparsity',
                                   nn_impl.zero_fraction(mask))
                    summary.scalar(threshold.op.name + '/threshold', threshold)
示例#10
0
  def add_pruning_summaries(self):
    """Adds summaries for this pruning spec.

    Args: none

    Returns: none
    """
    with ops.name_scope(self._spec.name + '_summaries'):
      summary.scalar('sparsity', self._sparsity)
      summary.scalar('last_mask_update_step', self._last_update_step)
      masks = get_masks()
      thresholds = get_thresholds()
      for index, mask in enumerate(masks):
        if not self._exists_in_do_not_prune_list(mask.name):
          summary.scalar(mask.name + '/sparsity', nn_impl.zero_fraction(mask))
          summary.scalar(thresholds[index].op.name + '/threshold',
                         thresholds[index])
示例#11
0
def add_zero_fraction_summary(tensor, name=None, prefix=None,
                              print_summary=False):
  """Adds a summary for the percentage of zero values in the given tensor.

  Args:
    tensor: a variable or op tensor.
    name: the optional name for the summary.
    prefix: An optional prefix for the summary names.
    print_summary: If `True`, the summary is printed to stdout when the summary
      is computed.

  Returns:
    A scalar `Tensor` of type `string` whose contents are the serialized
    `Summary` protocol buffer.
  """
  name = _get_summary_name(tensor, name, prefix, 'Fraction of Zero Values')
  tensor = nn.zero_fraction(tensor)
  return add_scalar_summary(tensor, name, print_summary=print_summary)
示例#12
0
def add_zero_fraction_summary(tensor, name=None, prefix=None,
                              print_summary=False):
  """Adds a summary for the percentage of zero values in the given tensor.

  Args:
    tensor: a variable or op tensor.
    name: the optional name for the summary.
    prefix: An optional prefix for the summary names.
    print_summary: If `True`, the summary is printed to stdout when the summary
      is computed.

  Returns:
    A scalar `Tensor` of type `string` whose contents are the serialized
    `Summary` protocol buffer.
  """
  name = _get_summary_name(tensor, name, prefix, 'Fraction_of_Zero_Values')
  tensor = nn.zero_fraction(tensor)
  return add_scalar_summary(tensor, name, print_summary=print_summary)
示例#13
0
 def testZeroFractionEmpty(self):
   x = np.zeros(0)
   y = self.evaluate(nn_impl.zero_fraction(x))
   self.assertTrue(np.isnan(y))
示例#14
0
 def testZeroFractionEmpty(self):
   with self.test_session():
     x = np.zeros(0)
     y = nn_impl.zero_fraction(x).eval()
     self.assertTrue(np.isnan(y))
示例#15
0
 def testZeroFraction2_27Ones(self):
   sparsity = nn_impl.zero_fraction(
       array_ops.ones([int(2**27 * 1.01)], dtype=dtypes.int8))
   with self.cached_session():
     self.assertAllClose(0.0, sparsity.eval())
示例#16
0
 def testZeroFraction2_27Ones(self):
   sparsity = nn_impl.zero_fraction(
       array_ops.ones([int(2**27 * 1.01)], dtype=dtypes.int8))
   self.assertAllClose(0.0, self.evaluate(sparsity))
示例#17
0
 def testZeroFraction2_27Ones(self):
   sparsity = nn_impl.zero_fraction(
       array_ops.ones([int(2**27 * 1.01)], dtype=dtypes.int8))
   self.assertAllClose(0.0, self.evaluate(sparsity))
示例#18
0
 def testZeroFractionEmpty(self):
   x = np.zeros(0)
   y = self.evaluate(nn_impl.zero_fraction(x))
   self.assertTrue(np.isnan(y))
示例#19
0
 def testZeroFractionEmpty(self):
     with self.test_session():
         x = np.zeros(0)
         y = nn_impl.zero_fraction(x).eval()
         self.assertTrue(np.isnan(y))
示例#20
0
 def testZeroFraction2_27Ones(self):
   sparsity = nn_impl.zero_fraction(
       array_ops.ones([int(2**27 * 1.01)], dtype=dtypes.int8))
   with self.cached_session():
     self.assertAllClose(0.0, sparsity.eval())