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.]]}))
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)
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]
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)
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)
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])
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)
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)
def testZeroFractionEmpty(self): x = np.zeros(0) y = self.evaluate(nn_impl.zero_fraction(x)) self.assertTrue(np.isnan(y))
def testZeroFractionEmpty(self): with self.test_session(): x = np.zeros(0) y = nn_impl.zero_fraction(x).eval() self.assertTrue(np.isnan(y))
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())
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))