我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用tensorflow.python.ops.math_ops.logical_not()。
def _make_auc_histograms(boolean_labels, scores, score_range, nbins): """Create histogram tensors from one batch of labels/scores.""" with variable_scope.variable_scope( None, 'make_auc_histograms', [boolean_labels, scores, nbins]): # Histogram of scores for records in this batch with True label. hist_true = histogram_ops.histogram_fixed_width( array_ops.boolean_mask(scores, boolean_labels), score_range, nbins=nbins, dtype=dtypes.int64, name='hist_true') # Histogram of scores for records in this batch with False label. hist_false = histogram_ops.histogram_fixed_width( array_ops.boolean_mask(scores, math_ops.logical_not(boolean_labels)), score_range, nbins=nbins, dtype=dtypes.int64, name='hist_false') return hist_true, hist_false
def _apply_transform(self, input_tensors, **kwargs): """Applies the transformation to the `transform_input`. Args: input_tensors: a list of Tensors representing the input to the Transform. **kwargs: Additional keyword arguments, unused here. Returns: A namedtuple of Tensors representing the transformed output. """ d = input_tensors[0] if self.strip_value is np.nan: strip_hot = math_ops.is_nan(d) else: strip_hot = math_ops.equal(d, array_ops.constant([self.strip_value], dtype=d.dtype)) keep_hot = math_ops.logical_not(strip_hot) length = array_ops.reshape(array_ops.shape(d), []) indices = array_ops.boolean_mask(math_ops.range(length), keep_hot) values = array_ops.boolean_mask(d, keep_hot) sparse_indices = array_ops.reshape( math_ops.cast(indices, dtypes.int64), [-1, 1]) shape = math_ops.cast(array_ops.shape(d), dtypes.int64) # pylint: disable=not-callable return self.return_type(ops.SparseTensor(sparse_indices, values, shape))
def _mask_weights(mask=None, weights=None): """Mask a given set of weights. Elements are included when the corresponding `mask` element is `False`, and excluded otherwise. Args: mask: An optional, `bool` `Tensor`. weights: An optional `Tensor` whose shape matches `mask` if `mask` is not `None`. Returns: Masked weights if `mask` and `weights` are not `None`, weights equivalent to `mask` if `weights` is `None`, and otherwise `weights`. Raises: ValueError: If `weights` and `mask` are not `None` and have mismatched shapes. """ if mask is not None: check_ops.assert_type(mask, dtypes.bool) if weights is None: weights = array_ops.ones_like(mask, dtype=dtypes.float32) weights = math_ops.cast(math_ops.logical_not(mask), weights.dtype) * weights return weights
def kl(dist_a, dist_b, allow_nan=False, name=None): """Get the KL-divergence KL(dist_a || dist_b). Args: dist_a: The first distribution. dist_b: The second distribution. allow_nan: If `False` (default), a runtime error is raised if the KL returns NaN values for any batch entry of the given distributions. If `True`, the KL may return a NaN for the given entry. name: (optional) Name scope to use for created operations. Returns: A Tensor with the batchwise KL-divergence between dist_a and dist_b. Raises: NotImplementedError: If no KL method is defined for distribution types of dist_a and dist_b. """ kl_fn = _DIVERGENCES.get((type(dist_a), type(dist_b)), None) if kl_fn is None: raise NotImplementedError( "No KL(dist_a || dist_b) registered for dist_a type %s and dist_b " "type %s" % ((type(dist_a).__name__, type(dist_b).__name__))) with ops.name_scope("KullbackLeibler"): kl_t = kl_fn(dist_a, dist_b, name=name) if allow_nan: return kl_t # Check KL for NaNs kl_t = array_ops.identity(kl_t, name="kl") with ops.control_dependencies([ control_flow_ops.Assert( math_ops.logical_not( math_ops.reduce_any(math_ops.is_nan(kl_t))), ["KL calculation between %s and %s returned NaN values " "(and was called with allow_nan=False). Values:" % (dist_a.name, dist_b.name), kl_t])]): return array_ops.identity(kl_t, name="checked_kl")
def _apply_transform(self, input_tensors, **kwargs): """Applies the transformation to the `transform_input`. Args: input_tensors: a list of Tensors representing the input to the Transform. **kwargs: Additional keyword arguments, unused here. Returns: A namedtuple of Tensors representing the transformed output. """ d = input_tensors[0] if self.strip_value is np.nan: strip_hot = math_ops.is_nan(d) else: strip_hot = math_ops.equal(d, array_ops.constant([self.strip_value], dtype=d.dtype)) keep_hot = math_ops.logical_not(strip_hot) length = array_ops.reshape(array_ops.shape(d), []) indices = array_ops.boolean_mask(math_ops.range(length), keep_hot) values = array_ops.boolean_mask(d, keep_hot) sparse_indices = array_ops.reshape( math_ops.cast(indices, dtypes.int64), [-1, 1]) shape = math_ops.cast(array_ops.shape(d), dtypes.int64) # pylint: disable=not-callable return self.return_type( sparse_tensor.SparseTensor(sparse_indices, values, shape))
def __invert__(self): return logical_not(self)
def setUp(self): super(LogicalNotTest, self).setUp() self.ops = [('logical_not', operator.invert, math_ops.logical_not, core.logical_not),] self.test_lt = self.original_lt < 10
def testLogicalOps(self): self._testUnary( math_ops.logical_not, np.array([[True, False], [False, True]], dtype=np.bool), expected=np.array([[False, True], [True, False]], dtype=np.bool))
def next_inputs(self, time, outputs, state, sample_ids, name=None): with ops.name_scope(name, "ScheduledOutputTrainingHelperNextInputs", [time, outputs, state, sample_ids]): (finished, base_next_inputs, state) = ( super(ScheduledOutputTrainingHelper, self).next_inputs( time=time, outputs=outputs, state=state, sample_ids=sample_ids, name=name)) def maybe_sample(): """Perform scheduled sampling.""" def maybe_concatenate_auxiliary_inputs(outputs_, indices=None): """Concatenate outputs with auxiliary inputs, if they exist.""" if self._auxiliary_input_tas is None: return outputs_ next_time = time + 1 auxiliary_inputs = nest.map_structure( lambda ta: ta.read(next_time), self._auxiliary_input_tas) if indices is not None: auxiliary_inputs = array_ops.gather_nd(auxiliary_inputs, indices) return nest.map_structure( lambda x, y: array_ops.concat((x, y), -1), outputs_, auxiliary_inputs) if self._next_input_layer is None: return array_ops.where( sample_ids, maybe_concatenate_auxiliary_inputs(outputs), base_next_inputs) where_sampling = math_ops.cast( array_ops.where(sample_ids), dtypes.int32) where_not_sampling = math_ops.cast( array_ops.where(math_ops.logical_not(sample_ids)), dtypes.int32) outputs_sampling = array_ops.gather_nd(outputs, where_sampling) inputs_not_sampling = array_ops.gather_nd(base_next_inputs, where_not_sampling) sampled_next_inputs = maybe_concatenate_auxiliary_inputs( self._next_input_layer(outputs_sampling), where_sampling) base_shape = array_ops.shape(base_next_inputs) return (array_ops.scatter_nd(indices=where_sampling, updates=sampled_next_inputs, shape=base_shape) + array_ops.scatter_nd(indices=where_not_sampling, updates=inputs_not_sampling, shape=base_shape)) all_finished = math_ops.reduce_all(finished) next_inputs = control_flow_ops.cond( all_finished, lambda: base_next_inputs, maybe_sample) return (finished, next_inputs, state)
def save_state(self, state_name, value, name=None): """Returns an op to save the current batch of state `state_name`. Args: state_name: string, matches a key provided in `initial_states`. value: A `Tensor`. Its type must match that of `initial_states[state_name].dtype`. If we had at input: ```python initial_states[state_name].get_shape() == [d1, d2, ...]
then the shape of `value` must match: ```python tf.shape(value) == [batch_size, d1, d2, ...] ``` name: string (optional). The name scope for newly created ops. Returns: A control flow op that stores the new state of each entry into the state saver. This op must be run for every iteration that accesses data from the state saver (otherwise the state saver will never progress through its states and run out of capacity). Raises: KeyError: if `state_name` does not match any of the initial states declared in `initial_states`. """ if state_name not in self._state_saver._received_states.keys(): raise KeyError("state was not declared: %s" % state_name) default_name = "InputQueueingStateSaver_SaveState" with ops.name_scope(name, default_name, values=[value]): # Place all operations on the CPU. Barriers and queues are only # implemented for CPU, but all the other book-keeping operations # (reshape, shape, range, ...) would be placed on GPUs if available, # unless we explicitly tie them to CPU. with ops.colocate_with(self._state_saver._capacity_queue.queue_ref): indices_where_not_done = array_ops.reshape(array_ops.where( math_ops.logical_not(self._state_saver._sequence_is_done)), [-1]) keeping_next_key = array_ops.gather( self._state_saver._received_next_key, indices_where_not_done) value = _check_shape( array_ops.identity(value, name="convert_%s" % state_name), array_ops.shape(self._state_saver._received_states[state_name])) keeping_state = array_ops.gather(value, indices_where_not_done) return self._state_saver._barrier.insert_many( self._state_saver._get_barrier_index("state", state_name), keeping_next_key, keeping_state, name="BarrierInsertState_%s" % state_name)
```
def kl(dist_a, dist_b, allow_nan=False, name=None): """Get the KL-divergence KL(dist_a || dist_b). If there is no KL method registered specifically for `type(dist_a)` and `type(dist_b)`, then the class hierarchies of these types are searched. If one KL method is registered between any pairs of classes in these two parent hierarchies, it is used. If more than one such registered method exists, the method whose registered classes have the shortest sum MRO paths to the input types is used. If more than one such shortest path exists, the first method identified in the search is used (favoring a shorter MRO distance to `type(dist_a)`). Args: dist_a: The first distribution. dist_b: The second distribution. allow_nan: If `False` (default), a runtime error is raised if the KL returns NaN values for any batch entry of the given distributions. If `True`, the KL may return a NaN for the given entry. name: (optional) Name scope to use for created operations. Returns: A Tensor with the batchwise KL-divergence between dist_a and dist_b. Raises: NotImplementedError: If no KL method is defined for distribution types of dist_a and dist_b. """ kl_fn = _registered_kl(type(dist_a), type(dist_b)) if kl_fn is None: raise NotImplementedError( "No KL(dist_a || dist_b) registered for dist_a type %s and dist_b " "type %s" % ((type(dist_a).__name__, type(dist_b).__name__))) with ops.name_scope("KullbackLeibler"): kl_t = kl_fn(dist_a, dist_b, name=name) if allow_nan: return kl_t # Check KL for NaNs kl_t = array_ops.identity(kl_t, name="kl") with ops.control_dependencies([ control_flow_ops.Assert( math_ops.logical_not( math_ops.reduce_any(math_ops.is_nan(kl_t))), ["KL calculation between %s and %s returned NaN values " "(and was called with allow_nan=False). Values:" % (dist_a.name, dist_b.name), kl_t])]): return array_ops.identity(kl_t, name="checked_kl")
def _process_scale(self, scale, event_ndims): """Helper to __init__ which gets scale in batch-ready form. This function expands dimensions of `scale` according to the following table: event_ndims scale.ndims 0 1 0 [1]+S+[1,1] "silent error" 1 [ ]+S+[1,1] "silent error" 2 [ ]+S+[1,1] [1]+S+[ ] 3 [ ]+S+[1,1] [ ]+S+[ ] ... (same) (same) The idea is that we want to convert `scale` into something which can always work for, say, the left-hand argument of `batch_matmul`. Args: scale: `Tensor`. event_ndims: `Tensor` (0D, `int32`). Returns: scale: `Tensor` with dims expanded according to [above] table. batch_ndims: `Tensor` (0D, `int32`). The ndims of the `batch` portion. """ ndims = array_ops.rank(scale) left = math_ops.select( math_ops.reduce_any([ math_ops.reduce_all([ math_ops.equal(ndims, 0), math_ops.equal(event_ndims, 0) ]), math_ops.reduce_all([ math_ops.equal(ndims, 2), math_ops.equal(event_ndims, 1) ])]), 1, 0) right = math_ops.select(math_ops.equal(event_ndims, 0), 2, 0) pad = array_ops.concat(0, ( array_ops.ones([left], dtype=dtypes.int32), array_ops.shape(scale), array_ops.ones([right], dtype=dtypes.int32))) scale = array_ops.reshape(scale, pad) batch_ndims = ndims - 2 + right # For safety, explicitly zero-out the upper triangular part. scale = array_ops.matrix_band_part(scale, -1, 0) if self.validate_args: # matrix_band_part will fail if scale is not at least rank 2. shape = array_ops.shape(scale) assert_square = check_ops.assert_equal( shape[-2], shape[-1], message="Input must be a (batch of) square matrix.") # Assuming lower-triangular means we only need check diag != 0. diag = array_ops.matrix_diag_part(scale) is_non_singular = math_ops.logical_not( math_ops.reduce_any( math_ops.equal(diag, ops.convert_to_tensor(0, dtype=diag.dtype)))) assert_non_singular = control_flow_ops.Assert( is_non_singular, ["Singular matrix encountered", diag]) scale = control_flow_ops.with_dependencies( [assert_square, assert_non_singular], scale) return scale, batch_ndims