我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tensorflow.python.framework.ops.op_scope()。
def adjust_hue(image, delta, name=None): with ops.op_scope([image], name, 'adjust_hue') as name: # Remember original dtype to so we can convert back if needed orig_dtype = image.dtype flt_image = tf.image.convert_image_dtype(image, tf.float32) hsv = gen_image_ops.rgb_to_hsv(flt_image) hue = tf.slice(hsv, [0, 0, 0, 0], [-1, -1, -1, 1]) saturation = tf.slice(hsv, [0, 0, 0, 1], [-1, -1, -1, 1]) value = tf.slice(hsv, [0, 0, 0, 2], [-1, -1, -1, 1]) # Note that we add 2*pi to guarantee that the resulting hue is a positive # floating point number since delta is [-0.5, 0.5]. hue = math_ops.mod(hue + (delta + 1.), 1.) hsv_altered = tf.concat(3, [hue, saturation, value]) rgb_altered = gen_image_ops.hsv_to_rgb(hsv_altered) return tf.image.convert_image_dtype(rgb_altered, orig_dtype)
def adjust_saturation(image, saturation_factor, name=None): with ops.op_scope([image], name, 'adjust_saturation') as name: # Remember original dtype to so we can convert back if needed orig_dtype = image.dtype flt_image = tf.image.convert_image_dtype(image, tf.float32) hsv = gen_image_ops.rgb_to_hsv(flt_image) hue = tf.slice(hsv, [0, 0, 0, 0], [-1, -1, -1, 1]) saturation = tf.slice(hsv, [0, 0, 0, 1], [-1, -1, -1, 1]) value = tf.slice(hsv, [0, 0, 0, 2], [-1, -1, -1, 1]) saturation *= saturation_factor saturation = clip_ops.clip_by_value(saturation, 0.0, 1.0) hsv_altered = tf.concat(3, [hue, saturation, value]) rgb_altered = gen_image_ops.hsv_to_rgb(hsv_altered) return tf.image.convert_image_dtype(rgb_altered, orig_dtype)
def binary_cross_entropy(preds, targets, name=None): """Computes binary cross entropy given `preds`. For brevity, let `x = `, `z = targets`. The logistic loss is loss(x, z) = - sum_i (x[i] * log(z[i]) + (1 - x[i]) * log(1 - z[i])) Args: preds: A `Tensor` of type `float32` or `float64`. targets: A `Tensor` of the same type and shape as `preds`. """ eps = 1e-12 with ops.op_scope([preds, targets], name, "bce_loss") as name: preds = ops.convert_to_tensor(preds, name="preds") targets = ops.convert_to_tensor(targets, name="targets") return tf.reduce_mean(-(targets * tf.log(preds + eps) + (1. - targets) * tf.log(1. - preds + eps)))
def binary_cross_entropy(preds, targets, name=None): '''Computes binary cross entropy given `preds`. Let `x = `, `z = targets`. The logistic loss is loss(x, z) = - sum_i (x[i] * log(z[i]) + (1 - x[i]) * log(1 - z[i])) Args: preds: A `Tensor` of type `float32` or `float64`. targets: A `Tensor` of the same type and shape as `preds`. ''' eps = 1e-12 with ops.op_scope([preds, targets], name, 'bce_loss') as name: preds = ops.convert_to_tensor(preds, name='preds') targets = ops.convert_to_tensor(targets, name='targets') return tf.reduce_mean(-(targets * tf.log(preds + eps) + (1. - targets) * tf.log(1. - preds + eps))) # ================================== # ---------- LAYER MAPS --------- # # ==================================
def l1_l2_regularizer(scale_l1=1.0, scale_l2=1.0, scope=None): """Returns a function that can be used to apply L1 L2 regularizations. Args: scale_l1: A scalar multiplier `Tensor` for L1 regularization. scale_l2: A scalar multiplier `Tensor` for L2 regularization. scope: An optional op_scope name. Returns: A function with signature `l1_l2(weights)` that applies a weighted sum of L1 L2 regularization. Raises: ValueError: If scale is negative or if scale is not a float. """ scope = scope or 'l1_l2_regularizer' return sum_regularizer([l1_regularizer(scale_l1), l2_regularizer(scale_l2)], scope=scope)
def binary_cross_entropy(preds, targets, name=None): """Computes binary cross entropy given `preds`. For brevity, let `x = `, `z = targets`. The logistic loss is loss(x, z) = - sum_i (x[i] * log(z[i]) + (1 - x[i]) * log(1 - z[i])) Parameters ---------- preds : A `Tensor` of type `float32` or `float64`. targets : A `Tensor` of the same type and shape as `preds`. """ print("Undocumented") from tensorflow.python.framework import ops eps = 1e-12 with ops.op_scope([preds, targets], name, "bce_loss") as name: preds = ops.convert_to_tensor(preds, name="preds") targets = ops.convert_to_tensor(targets, name="targets") return tf.reduce_mean(-(targets * tf.log(preds + eps) + (1. - targets) * tf.log(1. - preds + eps)))
def binary_cross_entropy(output, target, name=None): """Computes binary cross entropy given `output`. For brevity, let `x = `, `z = targets`. The logistic loss is loss(x, z) = - sum_i (x[i] * log(z[i]) + (1 - x[i]) * log(1 - z[i])) Parameters ---------- output : A `Tensor` of type `float32` or `float64`. target : A `Tensor` of the same type and shape as `output`. """ # print("Undocumented") from tensorflow.python.framework import ops eps = 1e-12 with ops.op_scope([output, target], name, "bce_loss") as name: output = ops.convert_to_tensor(output, name="preds") target = ops.convert_to_tensor(targets, name="target") return tf.reduce_mean(-(target * tf.log(output + eps) + (1. - target) * tf.log(1. - output + eps)))
def sequence_loss_by_batch(logits, targets, weights, average_across_timesteps=True, softmax_loss_function=None, name=None): """Weighted cross-entropy loss for a sequence of logits, batch-collapsed (averaged). Args: logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols]. targets: List of 1D batch-sized int32 Tensors of the same length as logits. weights: List of 1D batch-sized float-Tensors of the same length as logits. average_across_timesteps: If set, divide the returned cost by the total label weight. average_across_batch: If set, divide the returned cost by the batch size. softmax_loss_function: Function (inputs-batch, labels-batch) -> loss-batch to be used instead of the standard softmax (the default if this is None). name: Optional name for this operation, defaults to "sequence_loss". Returns: A scalar float Tensor: The average log-perplexity per symbol (weighted). Raises: ValueError: If len(logits) is different from len(targets) or len(weights). """ with ops.op_scope(logits + targets + weights, name, "sequence_loss_by_batch"): cost = math_ops.reduce_sum(sequence_loss_by_example( logits, targets, weights, average_across_timesteps=average_across_timesteps, softmax_loss_function=softmax_loss_function)) batch_size = array_ops.shape(targets[0])[0] return cost / math_ops.cast(batch_size, dtypes.float32)
def sequence_loss(logits, targets, weights, name): """TODO(nh2tran): docstring. Weighted cross-entropy loss for a sequence of logits, batch-collapsed. Args: logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols]. targets: List of 1D batch-sized int32 Tensors of the same length as logits. weights: List of 1D batch-sized float-Tensors of the same length as logits. average_across_timesteps: If set, divide the returned cost by the total label weight. average_across_batch: If set, divide the returned cost by the batch size. softmax_loss_function: Function (inputs-batch, labels-batch) -> loss-batch to be used instead of the standard softmax (the default if this is None). name: Optional name for this operation, defaults to "sequence_loss". Returns: A scalar float Tensor: The average log-perplexity per symbol (weighted). Raises: ValueError: If len(logits) is different from len(targets) or len(weights). """ #~ with tf.name_scope(name=name, #~ values=logits + targets + weights): with ops.op_scope(logits + targets + weights, name): cost = math_ops.reduce_sum(sequence_loss_per_sample(logits, targets, weights)) batch_size = array_ops.shape(targets[0])[0] return cost / math_ops.cast(batch_size, dtypes.float32)
def sequence_loss(logits, targets, weights, average_across_timesteps=True, average_across_batch=True, softmax_loss_function=None, name=None): """Weighted cross-entropy loss for a sequence of logits, batch-collapsed. Args: logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols]. targets: List of 1D batch-sized int32 Tensors of the same length as logits. weights: List of 1D batch-sized float-Tensors of the same length as logits. average_across_timesteps: If set, divide the returned cost by the total label weight. average_across_batch: If set, divide the returned cost by the batch size. softmax_loss_function: Function (inputs-batch, labels-batch) -> loss-batch to be used instead of the standard softmax (the default if this is None). name: Optional name for this operation, defaults to "sequence_loss". Returns: A scalar float Tensor: The average log-perplexity per symbol (weighted). Raises: ValueError: If len(logits) is different from len(targets) or len(weights). """ with ops.op_scope(logits + targets + weights, name, "sequence_loss"): cost = math_ops.reduce_sum(sequence_loss_by_example( logits, targets, weights, average_across_timesteps=average_across_timesteps, softmax_loss_function=softmax_loss_function)) if average_across_batch: batch_size = array_ops.shape(targets[0])[0] return cost / math_ops.cast(batch_size, dtypes.float32) else: return cost
def tf_d_stepy(x,name=None): with ops.op_scope([x], name, "d_stepy") as name: y = tf.py_func(np_d_stepy_32, [x], [tf.float32], name=name, stateful=False) return y[0]
def tf_stepy(x, name=None): with ops.op_scope([x], name, "stepy") as name: y = py_func(np_stepy_32, [x], [tf.float32], name=name, grad=stepygrad) # <-- here's the call to the gradient return y[0]
def do_center_crop(value, size, name=None): """Randomly crops a tensor to a given size. Slices a shape `size` portion out of `value` at a uniformly chosen offset. Requires `value.shape >= size`. If a dimension should not be cropped, pass the full size of that dimension. For example, RGB images can be cropped with `size = [crop_height, crop_width, 3]`. Args: value: Input tensor to crop. size: 1-D tensor with size the rank of `value`. seed: Python integer. Used to create a random seed. See [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed) for behavior. name: A name for this operation (optional). Returns: A cropped tensor of the same rank as `value` and shape `size`. """ # TODO(shlens): Implement edge case to guarantee output size dimensions. # If size > value.shape, zero pad the result so that it always has shape # exactly size. from tensorflow.python.framework import dtypes with ops.op_scope([value, size], name, "center_crop") as name: value = ops.convert_to_tensor(value, name="value") size = ops.convert_to_tensor(size, dtype=dtypes.int32, name="size") shape = array_ops.shape(value) check = logging_ops.Assert( math_ops.reduce_all(shape >= size), ["Need value.shape >= size, got ", shape, size]) shape = control_flow_ops.with_dependencies([check], shape) limit = shape - size + 1 offset = tf.random_uniform( array_ops.shape(shape), dtype=size.dtype, maxval=size.dtype.max, seed=0) % limit offset2 = shape // 2 - size // 2 #import ipdb; ipdb.set_trace() return array_ops.slice(value, offset, size, name=name)
def assign_moving_average(variable, value, decay, name=None): """Compute the moving average of a variable. The moving average of 'variable' updated with 'value' is: variable * decay + value * (1 - decay) The returned Operation sets 'variable' to the newly computed moving average. The new value of 'variable' can be set with the 'AssignSub' op as: variable -= (1 - decay) * (variable - value) Args: variable: A Variable. value: A tensor with the same shape as 'variable' decay: A float Tensor or float value. The moving average decay. name: Optional name of the returned operation. Returns: An Operation that updates 'variable' with the newly computed moving average. """ with ops.op_scope([variable, value, decay], name, "AssignMovingAvg") as scope: with ops.colocate_with(variable): decay = ops.convert_to_tensor(1.0 - decay, name="decay") if decay.dtype != variable.dtype.base_dtype: decay = math_ops.cast(decay, variable.dtype.base_dtype) return state_ops.assign_sub(variable, (variable - value) * decay, name=scope)
def binary_cross_entropy(output, target, epsilon=1e-8, name='bce_loss'): """Computes binary cross entropy given `output`. For brevity, let `x = output`, `z = target`. The binary cross entropy loss is loss(x, z) = - sum_i (x[i] * log(z[i]) + (1 - x[i]) * log(1 - z[i])) Parameters ---------- output : tensor of type `float32` or `float64`. target : tensor of the same type and shape as `output`. epsilon : float A small value to avoid output is zero. name : string An optional name to attach to this layer. References ----------- - `DRAW <https://github.com/ericjang/draw/blob/master/draw.py#L73>`_ """ # from tensorflow.python.framework import ops # with ops.op_scope([output, target], name, "bce_loss") as name: # output = ops.convert_to_tensor(output, name="preds") # target = ops.convert_to_tensor(targets, name="target") with tf.name_scope(name): return tf.reduce_mean(tf.reduce_sum(-(target * tf.log(output + epsilon) + (1. - target) * tf.log(1. - output + epsilon)), axis=1))
def many2one_model_with_buckets(encoder_inputs_list, decoder_inputs, targets, weights, buckets, seq2seq, softmax_loss_function=None, per_example_loss=False, name=None, spscale=20): # Modified model with buckets to accept 2 encoders if len(encoder_inputs_list[0]) < buckets[-1][0]: raise ValueError("Length of encoder_inputs (%d) must be at least that of la" "st bucket (%d)." % (len(encoder_inputs), buckets[-1][0])) if len(targets) < buckets[-1][1]: raise ValueError("Length of targets (%d) must be at least that of last" "bucket (%d)." % (len(targets), buckets[-1][1])) if len(weights) < buckets[-1][1]: raise ValueError("Length of weights (%d) must be at least that of last" "bucket (%d)." % (len(weights), buckets[-1][1])) all_inputs = encoder_inputs_list + decoder_inputs + targets + weights losses = [] outputs = [] speech_buckets = [(x*spscale, y) for (x,y) in buckets] with ops.op_scope(all_inputs, name, "many2one_model_with_buckets"): for j, bucket in enumerate(buckets): with variable_scope.variable_scope(variable_scope.get_variable_scope(), reuse=True if j > 0 else None): #bucket_outputs, _ = seq2seq(encoder_inputs[:bucket[0]], decoder_inputs[:bucket[1]]) x = encoder_inputs_list[0][:bucket[0]] #print( x ) y = encoder_inputs_list[1][:speech_buckets[j][0]] bucket_outputs, _ = seq2seq([x, y], decoder_inputs[:bucket[1]]) outputs.append(bucket_outputs) if per_example_loss: losses.append(sequence_loss_by_example( outputs[-1], targets[:bucket[1]], weights[:bucket[1]], softmax_loss_function=softmax_loss_function)) else: losses.append(sequence_loss( outputs[-1], targets[:bucket[1]], weights[:bucket[1]], softmax_loss_function=softmax_loss_function)) return outputs, losses
def sequence_loss(logits, targets, weights, average_across_timesteps=True, average_across_batch=True, softmax_loss_function=None, name=None): """Weighted cross-entropy loss for a sequence of logits, batch-collapsed. Args: logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols]. targets: List of 1D batch-sized int32 Tensors of the same length as logits. weights: List of 1D batch-sized float-Tensors of the same length as logits. average_across_timesteps: If set, divide the returned cost by the total label weight. average_across_batch: If set, divide the returned cost by the batch size. softmax_loss_function: Function (inputs-batch, labels-batch) -> loss-batch to be used instead of the standard softmax (the default if this is None). name: Optional name for this operation, defaults to "sequence_loss". Returns: A scalar float Tensor: The average log-perplexity per symbol (weighted). Raises: ValueError: If len(logits) is different from len(targets) or len(weights). """ #with ops.op_scope(logits + targets + weights, name, "sequence_loss"): with tf.name_scope(name, "sequence_loss", logits + targets + weights): cost = math_ops.reduce_sum(sequence_loss_by_example( logits, targets, weights, average_across_timesteps=average_across_timesteps, softmax_loss_function=softmax_loss_function)) if average_across_batch: batch_size = array_ops.shape(targets[0])[0] return cost / math_ops.cast(batch_size, dtypes.float32) else: return cost