Python tensorflow.compat.v1.multiply() Examples
The following are 30
code examples of tensorflow.compat.v1.multiply().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow.compat.v1
, or try the search function
.
Example #1
Source File: common_layers_test.py From tensor2tensor with Apache License 2.0 | 6 votes |
def testSpectralNorm(self): # Test that after 20 calls to apply_spectral_norm, the spectral # norm of the normalized matrix is close to 1.0 with tf.Graph().as_default(): weights = tf.get_variable("w", dtype=tf.float32, shape=[2, 3, 50, 100]) weights = tf.multiply(weights, 10.0) normed_weight, assign_op = common_layers.apply_spectral_norm(weights) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for _ in range(20): sess.run(assign_op) normed_weight, assign_op = common_layers.apply_spectral_norm( weights) normed_weight = sess.run(normed_weight).reshape(-1, 100) _, s, _ = np.linalg.svd(normed_weight) self.assertTrue(np.allclose(s[0], 1.0, rtol=0.1))
Example #2
Source File: preprocessor_test.py From models with Apache License 2.0 | 6 votes |
def testRandomHorizontalFlipWithEmptyBoxes(self): def graph_fn(): preprocess_options = [(preprocessor.random_horizontal_flip, {})] images = self.expectedImagesAfterNormalization() boxes = self.createEmptyTestBoxes() tensor_dict = {fields.InputDataFields.image: images, fields.InputDataFields.groundtruth_boxes: boxes} images_expected1 = self.expectedImagesAfterLeftRightFlip() boxes_expected = self.createEmptyTestBoxes() images_expected2 = images tensor_dict = preprocessor.preprocess(tensor_dict, preprocess_options) images = tensor_dict[fields.InputDataFields.image] boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] images_diff1 = tf.squared_difference(images, images_expected1) images_diff2 = tf.squared_difference(images, images_expected2) images_diff = tf.multiply(images_diff1, images_diff2) images_diff_expected = tf.zeros_like(images_diff) return [images_diff, images_diff_expected, boxes, boxes_expected] (images_diff_, images_diff_expected_, boxes_, boxes_expected_) = self.execute_cpu(graph_fn, []) self.assertAllClose(boxes_, boxes_expected_) self.assertAllClose(images_diff_, images_diff_expected_)
Example #3
Source File: visualization.py From tensor2robot with Apache License 2.0 | 6 votes |
def add_heatmap_summary(feature_query, feature_map, name): """Plots dot produce of feature_query on feature_map. Args: feature_query: Batch x embedding size tensor of goal embeddings feature_map: Batch x h x w x embedding size of pregrasp scene embeddings name: string to name tensorflow summaries Returns: Batch x h x w x 1 heatmap """ batch, dim = feature_query.shape reshaped_query = tf.reshape(feature_query, (int(batch), 1, 1, int(dim))) heatmaps = tf.reduce_sum( tf.multiply(feature_map, reshaped_query), axis=3, keep_dims=True) tf.summary.image(name, heatmaps) shape = tf.shape(heatmaps) softmaxheatmaps = tf.nn.softmax(tf.reshape(heatmaps, (int(batch), -1))) tf.summary.image( six.ensure_str(name) + 'softmax', tf.reshape(softmaxheatmaps, shape)) return heatmaps
Example #4
Source File: retrain.py From AudioNet with MIT License | 6 votes |
def should_distort_images(flip_left_right, random_crop, random_scale, random_brightness): """Whether any distortions are enabled, from the input flags. Args: flip_left_right: Boolean whether to randomly mirror images horizontally. random_crop: Integer percentage setting the total margin used around the crop box. random_scale: Integer percentage of how much to vary the scale by. random_brightness: Integer range to randomly multiply the pixel values by. Returns: Boolean value indicating whether any distortions should be applied. """ return (flip_left_right or (random_crop != 0) or (random_scale != 0) or (random_brightness != 0))
Example #5
Source File: deep_cnn.py From privacy with Apache License 2.0 | 6 votes |
def _variable_with_weight_decay(name, shape, stddev, wd): """Helper to create an initialized Variable with weight decay. Note that the Variable is initialized with a truncated normal distribution. A weight decay is added only if one is specified. Args: name: name of the variable shape: list of ints stddev: standard deviation of a truncated Gaussian wd: add L2Loss weight decay multiplied by this float. If None, weight decay is not added for this Variable. Returns: Variable Tensor """ var = _variable_on_cpu(name, shape, tf.truncated_normal_initializer(stddev=stddev)) if wd is not None: weight_decay = tf.multiply(tf.nn.l2_loss(var), wd, name='weight_loss') tf.add_to_collection('losses', weight_decay) return var
Example #6
Source File: preprocessor.py From models with Apache License 2.0 | 6 votes |
def convert_class_logits_to_softmax(multiclass_scores, temperature=1.0): """Converts multiclass logits to softmax scores after applying temperature. Args: multiclass_scores: float32 tensor of shape [num_instances, num_classes] representing the score for each box for each class. temperature: Scale factor to use prior to applying softmax. Larger temperatures give more uniform distruibutions after softmax. Returns: multiclass_scores: float32 tensor of shape [num_instances, num_classes] with scaling and softmax applied. """ # Multiclass scores must be stored as logits. Apply temp and softmax. multiclass_scores_scaled = tf.multiply( multiclass_scores, 1.0 / temperature, name='scale_logits') multiclass_scores = tf.nn.softmax(multiclass_scores_scaled, name='softmax') return multiclass_scores
Example #7
Source File: cost.py From super-resolution-videos with The Unlicense | 6 votes |
def cosine_similarity(v1, v2): """Cosine similarity [-1, 1], `wiki <https://en.wikipedia.org/wiki/Cosine_similarity>`_. Parameters ----------- v1, v2 : tensor of [batch_size, n_feature], with the same number of features. Returns ----------- a tensor of [batch_size, ] """ try: ## TF1.0 cost = tf.reduce_sum(tf.multiply(v1, v2), 1) / (tf.sqrt(tf.reduce_sum(tf.multiply(v1, v1), 1)) * tf.sqrt(tf.reduce_sum(tf.multiply(v2, v2), 1))) except: ## TF0.12 cost = tf.reduce_sum(tf.mul(v1, v2), reduction_indices=1) / (tf.sqrt(tf.reduce_sum(tf.mul(v1, v1), reduction_indices=1)) * tf.sqrt(tf.reduce_sum(tf.mul(v2, v2), reduction_indices=1))) return cost ## Regularization Functions
Example #8
Source File: transformer_memory.py From tensor2tensor with Apache License 2.0 | 6 votes |
def read(self, x): """Read from the memory. An external component can use the results via a simple MLP, e.g., fn(x W_x + retrieved_mem W_m). Args: x: a tensor in the shape of [batch_size, length, depth]. Returns: access_logits: the logits for accessing the memory in shape of [batch_size, length, memory_size]. retrieved_mem: the retrieved results in the shape of [batch_size, length, val_depth]. """ access_logits = self._address_content(x) weights = tf.nn.softmax(access_logits) retrieved_mem = tf.reduce_sum( tf.multiply(tf.expand_dims(weights, 3), tf.expand_dims(self.mem_vals, axis=1)), axis=2) return access_logits, retrieved_mem
Example #9
Source File: common_layers.py From tensor2tensor with Apache License 2.0 | 6 votes |
def kl_divergence(mu, log_var, mu_p=0.0, log_var_p=0.0): """KL divergence of diagonal gaussian N(mu,exp(log_var)) and N(0,1). Args: mu: mu parameter of the distribution. log_var: log(var) parameter of the distribution. mu_p: optional mu from a learned prior distribution log_var_p: optional log(var) from a learned prior distribution Returns: the KL loss. """ batch_size = shape_list(mu)[0] prior_distribution = tfp.distributions.Normal( mu_p, tf.exp(tf.multiply(0.5, log_var_p))) posterior_distribution = tfp.distributions.Normal( mu, tf.exp(tf.multiply(0.5, log_var))) kld = tfp.distributions.kl_divergence(posterior_distribution, prior_distribution) return tf.reduce_sum(kld) / to_float(batch_size)
Example #10
Source File: tf_mittens.py From mittens with Apache License 2.0 | 6 votes |
def _get_cost_function(self): """Compute the cost of the Mittens objective function. If self.mittens = 0, this is the same as the cost of GloVe. """ self.weights = tf.placeholder( tf.float32, shape=[self.n_words, self.n_words]) self.log_coincidence = tf.placeholder( tf.float32, shape=[self.n_words, self.n_words]) self.diffs = tf.subtract(self.model, self.log_coincidence) cost = tf.reduce_sum( 0.5 * tf.multiply(self.weights, tf.square(self.diffs))) if self.mittens > 0: self.mittens = tf.constant(self.mittens, tf.float32) cost += self.mittens * tf.reduce_sum( tf.multiply( self.has_embedding, self._tf_squared_euclidean( tf.add(self.W, self.C), self.original_embedding))) tf.summary.scalar("cost", cost) return cost
Example #11
Source File: bounds.py From interval-bound-propagation with Apache License 2.0 | 6 votes |
def apply_batch_norm(self, wrapper, mean, variance, scale, bias, epsilon): # Element-wise multiplier. multiplier = tf.rsqrt(variance + epsilon) if scale is not None: multiplier *= scale w = multiplier # Element-wise bias. b = -multiplier * mean if bias is not None: b += bias b = tf.squeeze(b, axis=0) # Because the scale might be negative, we need to apply a strategy similar # to linear. c = (self.lower + self.upper) / 2. r = (self.upper - self.lower) / 2. c = tf.multiply(c, w) + b r = tf.multiply(r, tf.abs(w)) return IntervalBounds(c - r, c + r)
Example #12
Source File: test_forward.py From incubator-tvm with Apache License 2.0 | 6 votes |
def test_forward_multi_input(): with tf.Graph().as_default(): in1 = tf.placeholder(tf.int32, shape=[3, 3], name='in1') in2 = tf.placeholder(tf.int32, shape=[3, 3], name='in2') in3 = tf.placeholder(tf.int32, shape=[3, 3], name='in3') in4 = tf.placeholder(tf.int32, shape=[3, 3], name='in4') out1 = tf.add(in1, in2, name='out1') out2 = tf.subtract(in3, in4, name='out2') out = tf.multiply(out1, out2, name='out') in_data = np.arange(9, dtype='int32').reshape([3, 3]) compare_tf_with_tvm([in_data, in_data, in_data, in_data], ['in1:0', 'in2:0', 'in3:0', 'in4:0'], 'out:0') ####################################################################### # Multi Output to Graph # ---------------------
Example #13
Source File: nq_long_utils.py From language with Apache License 2.0 | 6 votes |
def f1_metric(precision, precision_op, recall, recall_op): """Computes F1 based on precision and recall. Args: precision: <float> [batch_size] precision_op: Update op for precision. recall: <float> [batch_size] recall_op: Update op for recall. Returns: tensor and update op for F1. """ f1_op = tf.group(precision_op, recall_op) numerator = 2 * tf.multiply(precision, recall) denominator = tf.add(precision, recall) f1 = tf.divide(numerator, denominator) # <float> [batch_size] zero_vec = tf.zeros_like(f1) is_valid = tf.greater(denominator, zero_vec) f1 = tf.where(is_valid, x=f1, y=zero_vec) return f1, f1_op
Example #14
Source File: post_processing_builder.py From models with Apache License 2.0 | 5 votes |
def _score_converter_fn_with_logit_scale(tf_score_converter_fn, logit_scale): """Create a function to scale logits then apply a Tensorflow function.""" def score_converter_fn(logits): scaled_logits = tf.multiply(logits, 1.0 / logit_scale, name='scale_logits') return tf_score_converter_fn(scaled_logits, name='convert_scores') score_converter_fn.__name__ = '%s_with_logit_scale' % ( tf_score_converter_fn.__name__) return score_converter_fn
Example #15
Source File: test_forward.py From incubator-tvm with Apache License 2.0 | 5 votes |
def _test_reshape_with_call(): """ relay.expr.Call as shape """ data = np.zeros((6, 4, 2)) with tf.Graph().as_default(): in_data = array_ops.placeholder(shape=data.shape, dtype=data.dtype) out_shape = tf.constant([1, 2, 3], dtype="int32") out_shape = tf.multiply(out_shape, 2) array_ops.reshape(in_data, out_shape) compare_tf_with_tvm(data, 'Placeholder:0', 'Reshape:0')
Example #16
Source File: test_forward.py From incubator-tvm with Apache License 2.0 | 5 votes |
def _test_resize_nearest_neighbor_dynamic_shape(in_shape, scale): """ One iteration of resize nearest neighbor for graph with dynamic input shape """ data = np.random.uniform(size=in_shape).astype('float32') with tf.Graph().as_default(): in_data = array_ops.placeholder(shape=None, dtype=data.dtype) # multiply input shape by scale factor new_shape = tf.shape(in_data)[1:3] * tf.constant(scale, dtype=tf.int32) tf.image.resize_nearest_neighbor( in_data, new_shape, name='resize_nearest_neighbor') compare_tf_with_tvm(data, 'Placeholder:0', 'resize_nearest_neighbor:0')
Example #17
Source File: test_forward.py From incubator-tvm with Apache License 2.0 | 5 votes |
def _test_broadcast_to_from_tensor(in_shape): """ One iteration of broadcast_to with unknown shape at graph build""" data = np.random.uniform(size=in_shape).astype('float32') with tf.Graph().as_default(): in_data = array_ops.placeholder( shape=[None], dtype=data.dtype) shape_data = tf.multiply(tf.shape(in_data), 32) tf.broadcast_to(in_data, shape_data) compare_tf_with_tvm(data, 'Placeholder:0', 'BroadcastTo:0')
Example #18
Source File: rnn_decoder_test.py From models with Apache License 2.0 | 5 votes |
def test_rnn_decoder_single_unroll(self): batch_size = 2 num_unroll = 1 num_units = 64 width = 8 height = 10 input_channels = 128 initial_state = tf.random_normal((batch_size, width, height, num_units)) inputs = tf.random_normal([batch_size, width, height, input_channels]) rnn_cell = MockRnnCell(input_channels, num_units) outputs, states = rnn_decoder.rnn_decoder( decoder_inputs=[inputs] * num_unroll, initial_state=(initial_state, initial_state), cell=rnn_cell) self.assertEqual(len(outputs), num_unroll) self.assertEqual(len(states), num_unroll) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) results = sess.run((outputs, states, inputs, initial_state)) outputs_results = results[0] states_results = results[1] inputs_results = results[2] initial_states_results = results[3] self.assertEqual(outputs_results[0].shape, (batch_size, width, height, input_channels + num_units)) self.assertAllEqual( outputs_results[0], np.concatenate((inputs_results, initial_states_results), axis=3)) self.assertEqual(states_results[0][0].shape, (batch_size, width, height, num_units)) self.assertEqual(states_results[0][1].shape, (batch_size, width, height, num_units)) self.assertAllEqual(states_results[0][0], np.multiply(initial_states_results, 2.0)) self.assertAllEqual(states_results[0][1], initial_states_results)
Example #19
Source File: preprocessor_test.py From models with Apache License 2.0 | 5 votes |
def testRandomRotation90(self): def graph_fn(): preprocess_options = [(preprocessor.random_rotation90, {})] images = self.expectedImagesAfterNormalization() boxes = self.createTestBoxes() tensor_dict = { fields.InputDataFields.image: images, fields.InputDataFields.groundtruth_boxes: boxes } images_expected1 = self.expectedImagesAfterRot90() boxes_expected1 = self.expectedBoxesAfterRot90() images_expected2 = images boxes_expected2 = boxes tensor_dict = preprocessor.preprocess(tensor_dict, preprocess_options) images = tensor_dict[fields.InputDataFields.image] boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] boxes_diff1 = tf.squared_difference(boxes, boxes_expected1) boxes_diff2 = tf.squared_difference(boxes, boxes_expected2) boxes_diff = tf.multiply(boxes_diff1, boxes_diff2) boxes_diff_expected = tf.zeros_like(boxes_diff) images_diff1 = tf.squared_difference(images, images_expected1) images_diff2 = tf.squared_difference(images, images_expected2) images_diff = tf.multiply(images_diff1, images_diff2) images_diff_expected = tf.zeros_like(images_diff) return [ images_diff, images_diff_expected, boxes_diff, boxes_diff_expected ] (images_diff_, images_diff_expected_, boxes_diff_, boxes_diff_expected_) = self.execute_cpu(graph_fn, []) self.assertAllClose(boxes_diff_, boxes_diff_expected_) self.assertAllClose(images_diff_, images_diff_expected_)
Example #20
Source File: ssd_model.py From benchmarks with Apache License 2.0 | 5 votes |
def _localization_loss(self, pred_loc, gt_loc, gt_label, num_matched_boxes): """Computes the localization loss. Computes the localization loss using smooth l1 loss. Args: pred_loc: a flatten tensor that includes all predicted locations. The shape is [batch_size, num_anchors, 4]. gt_loc: a tensor representing box regression targets in [batch_size, num_anchors, 4]. gt_label: a tensor that represents the classification groundtruth targets. The shape is [batch_size, num_anchors, 1]. num_matched_boxes: the number of anchors that are matched to a groundtruth targets, used as the loss normalizater. The shape is [batch_size]. Returns: box_loss: a float32 representing total box regression loss. """ mask = tf.greater(tf.squeeze(gt_label), 0) float_mask = tf.cast(mask, tf.float32) smooth_l1 = tf.reduce_sum(tf.losses.huber_loss( gt_loc, pred_loc, reduction=tf.losses.Reduction.NONE ), axis=2) smooth_l1 = tf.multiply(smooth_l1, float_mask) box_loss = tf.reduce_sum(smooth_l1, axis=1) return tf.reduce_mean(box_loss / num_matched_boxes)
Example #21
Source File: preprocessor_test.py From models with Apache License 2.0 | 5 votes |
def testRandomVerticalFlip(self): def graph_fn(): preprocess_options = [(preprocessor.random_vertical_flip, {})] images = self.expectedImagesAfterNormalization() boxes = self.createTestBoxes() tensor_dict = { fields.InputDataFields.image: images, fields.InputDataFields.groundtruth_boxes: boxes } images_expected1 = self.expectedImagesAfterUpDownFlip() boxes_expected1 = self.expectedBoxesAfterUpDownFlip() images_expected2 = images boxes_expected2 = boxes tensor_dict = preprocessor.preprocess(tensor_dict, preprocess_options) images = tensor_dict[fields.InputDataFields.image] boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] boxes_diff1 = tf.squared_difference(boxes, boxes_expected1) boxes_diff2 = tf.squared_difference(boxes, boxes_expected2) boxes_diff = tf.multiply(boxes_diff1, boxes_diff2) boxes_diff_expected = tf.zeros_like(boxes_diff) images_diff1 = tf.squared_difference(images, images_expected1) images_diff2 = tf.squared_difference(images, images_expected2) images_diff = tf.multiply(images_diff1, images_diff2) images_diff_expected = tf.zeros_like(images_diff) return [ images_diff, images_diff_expected, boxes_diff, boxes_diff_expected ] (images_diff_, images_diff_expected_, boxes_diff_, boxes_diff_expected_) = self.execute_cpu(graph_fn, []) self.assertAllClose(boxes_diff_, boxes_diff_expected_) self.assertAllClose(images_diff_, images_diff_expected_)
Example #22
Source File: preprocessor_test.py From models with Apache License 2.0 | 5 votes |
def testRandomHorizontalFlip(self): def graph_fn(): preprocess_options = [(preprocessor.random_horizontal_flip, {})] images = self.expectedImagesAfterNormalization() boxes = self.createTestBoxes() tensor_dict = {fields.InputDataFields.image: images, fields.InputDataFields.groundtruth_boxes: boxes} images_expected1 = self.expectedImagesAfterLeftRightFlip() boxes_expected1 = self.expectedBoxesAfterLeftRightFlip() images_expected2 = images boxes_expected2 = boxes tensor_dict = preprocessor.preprocess(tensor_dict, preprocess_options) images = tensor_dict[fields.InputDataFields.image] boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] boxes_diff1 = tf.squared_difference(boxes, boxes_expected1) boxes_diff2 = tf.squared_difference(boxes, boxes_expected2) boxes_diff = tf.multiply(boxes_diff1, boxes_diff2) boxes_diff_expected = tf.zeros_like(boxes_diff) images_diff1 = tf.squared_difference(images, images_expected1) images_diff2 = tf.squared_difference(images, images_expected2) images_diff = tf.multiply(images_diff1, images_diff2) images_diff_expected = tf.zeros_like(images_diff) return [images_diff, images_diff_expected, boxes_diff, boxes_diff_expected] (images_diff_, images_diff_expected_, boxes_diff_, boxes_diff_expected_) = self.execute_cpu(graph_fn, []) self.assertAllClose(boxes_diff_, boxes_diff_expected_) self.assertAllClose(images_diff_, images_diff_expected_)
Example #23
Source File: test_forward.py From incubator-tvm with Apache License 2.0 | 5 votes |
def _test_spop_placeholder_with_shape_and_default_value(): with tf.Graph().as_default(): data = np.ones([1], dtype=int).astype(np.int32) dataVar = tf.Variable(data, shape=data.shape) pl1 = array_ops.placeholder_with_default(dataVar,shape=data.shape,name="pl1") tpl = tf.convert_to_tensor(pl1, dtype=tf.int32) @function.Defun(*[tf.int32]) def pl_with_default(pl): return tf.expand_dims(tf.multiply(pl, pl), 0) z = gen_functional_ops.StatefulPartitionedCall(args=[tpl], Tout=[tf.int32], f=pl_with_default) compare_tf_with_tvm(data, ['pl1:0'], 'StatefulPartitionedCall:0', mode='vm', init_global_variables=True)
Example #24
Source File: balanced_positive_negative_sampler.py From models with Apache License 2.0 | 5 votes |
def _get_values_from_start_and_end(self, input_tensor, num_start_samples, num_end_samples, total_num_samples): """slices num_start_samples and last num_end_samples from input_tensor. Args: input_tensor: An int32 tensor of shape [N] to be sliced. num_start_samples: Number of examples to be sliced from the beginning of the input tensor. num_end_samples: Number of examples to be sliced from the end of the input tensor. total_num_samples: Sum of is num_start_samples and num_end_samples. This should be a scalar. Returns: A tensor containing the first num_start_samples and last num_end_samples from input_tensor. """ input_length = tf.shape(input_tensor)[0] start_positions = tf.less(tf.range(input_length), num_start_samples) end_positions = tf.greater_equal( tf.range(input_length), input_length - num_end_samples) selected_positions = tf.logical_or(start_positions, end_positions) selected_positions = tf.cast(selected_positions, tf.float32) indexed_positions = tf.multiply(tf.cumsum(selected_positions), selected_positions) one_hot_selector = tf.one_hot(tf.cast(indexed_positions, tf.int32) - 1, total_num_samples, dtype=tf.float32) return tf.cast(tf.tensordot(tf.cast(input_tensor, tf.float32), one_hot_selector, axes=[0, 0]), tf.int32)
Example #25
Source File: retrain.py From AudioNet with MIT License | 5 votes |
def add_jpeg_decoding(input_width, input_height, input_depth, input_mean, input_std): """Adds operations that perform JPEG decoding and resizing to the graph.. Args: input_width: Desired width of the image fed into the recognizer graph. input_height: Desired width of the image fed into the recognizer graph. input_depth: Desired channels of the image fed into the recognizer graph. input_mean: Pixel value that should be zero in the image for the graph. input_std: How much to divide the pixel values by before recognition. Returns: Tensors for the node to feed JPEG data into, and the output of the preprocessing steps. """ jpeg_data = tf.placeholder(tf.string, name='DecodeJPGInput') decoded_image = tf.image.decode_jpeg(jpeg_data, channels=input_depth) decoded_image_as_float = tf.cast(decoded_image, dtype=tf.float32) decoded_image_4d = tf.expand_dims(decoded_image_as_float, 0) resize_shape = tf.stack([input_height, input_width]) resize_shape_as_int = tf.cast(resize_shape, dtype=tf.int32) resized_image = tf.image.resize_bilinear(decoded_image_4d, resize_shape_as_int) offset_image = tf.subtract(resized_image, input_mean) mul_image = tf.multiply(offset_image, 1.0 / input_std) return jpeg_data, mul_image
Example #26
Source File: policy_gradient_ops.py From trfl with Apache License 2.0 | 5 votes |
def policy_gradient(policies, actions, action_values, policy_vars=None, name="policy_gradient"): """Computes policy gradient losses for a batch of trajectories. See `policy_gradient_loss` for more information on expected inputs and usage. Args: policies: A distribution over a batch supporting a `log_prob` method, e.g. an instance of `tfp.distributions.Distribution`. For example, for a diagonal gaussian policy: `policies = tfp.distributions.MultivariateNormalDiag(mus, sigmas)` actions: An action batch Tensor used as the argument for `log_prob`. Has shape equal to the batch shape of the policies concatenated with the event shape of the policies (which may be scalar, in which case concatenation leaves shape just equal to batch shape). action_values: A Tensor containing estimates of the values of the `actions`. Has shape equal to the batch shape of the policies. policy_vars: An optional iterable of Tensors used by `policies`. If provided is used in scope checks. For the multivariate normal example above this would be `[mus, sigmas]`. name: Customises the name_scope for this op. Returns: loss: Tensor with same shape as `actions` containing the total loss for each element in the batch. Differentiable w.r.t the variables in `policies` only. """ policy_vars = list(policy_vars) if policy_vars else list() with tf.name_scope(values=policy_vars + [actions, action_values], name=name): actions = tf.stop_gradient(actions) action_values = tf.stop_gradient(action_values) log_prob_actions = policies.log_prob(actions) # Prevent accidental broadcasting if possible at construction time. action_values.get_shape().assert_is_compatible_with( log_prob_actions.get_shape()) return -tf.multiply(log_prob_actions, action_values)
Example #27
Source File: model.py From ocrd_anybaseocr with Apache License 2.0 | 5 votes |
def denorm_boxes_graph(boxes, shape): """Converts boxes from normalized coordinates to pixel coordinates. boxes: [..., (y1, x1, y2, x2)] in normalized coordinates shape: [..., (height, width)] in pixels Note: In pixel coordinates (y2, x2) is outside the box. But in normalized coordinates it's inside the box. Returns: [..., (y1, x1, y2, x2)] in pixel coordinates """ h, w = tf.split(tf.cast(shape, tf.float32), 2) scale = tf.concat([h, w, h, w], axis=-1) - tf.constant(1.0) shift = tf.constant([0., 0., 1., 1.]) return tf.cast(tf.round(tf.multiply(boxes, scale) + shift), tf.int32)
Example #28
Source File: conftest.py From utensor_cgen with Apache License 2.0 | 5 votes |
def subject_ugraph1_2(): graph = tf.Graph() with graph.as_default(): sub_input0 = tf.placeholder(name='sub_input0', dtype=tf.int32) sub_input1 = tf.placeholder(name='sub_input1', dtype=tf.int32) sub_input2 = tf.constant([i for i in range(10)], name='sub_input2') sub_add0 = tf.add(sub_input0, sub_input1, name='sub_add0') sub_add1 = tf.add(sub_input1, sub_add0, name='sub_add1') sub_output = tf.multiply(sub_add1, sub_input2, name='sub_output') ugraph = GraphDefParser(config={}).parse(graph.as_graph_def(), [sub_output.op.name]) # ugraph.ops_info[sub_input1.op.name].add_null_input_tensor() return ugraph
Example #29
Source File: conftest.py From utensor_cgen with Apache License 2.0 | 5 votes |
def subject_ugraph1_1(): graph = tf.Graph() with graph.as_default(): sub_input0 = tf.placeholder(name='sub_input0', dtype=tf.int32) sub_input1 = tf.placeholder(name='sub_input1', dtype=tf.int32) sub_input2 = tf.constant([i for i in range(10)], name='sub_input2') # permute sub_add0 = tf.add(sub_input1, sub_input0, name='sub_add0') sub_add1 = tf.add(sub_add0, sub_input1, name='sub_add1') sub_output = tf.multiply(sub_add1, sub_input2, name='sub_output') ugraph = GraphDefParser(config={}).parse(graph.as_graph_def(), [sub_output.op.name]) return ugraph
Example #30
Source File: slate_decomp_q_agent.py From recsim with Apache License 2.0 | 5 votes |
def score_documents_tf(user_obs, doc_obs, no_click_mass=1.0, is_mnl=False, min_normalizer=-1.0): """Computes unnormalized scores given both user and document observations. This implements both multinomial proportional model and multinormial logit model given some parameters. We also assume scores are based on inner products of user_obs and doc_obs. Args: user_obs: An instance of AbstractUserState. doc_obs: A numpy array that represents the observation of all documents in the candidate set. no_click_mass: a float indicating the mass given to a no click option is_mnl: whether to use a multinomial logit model instead of a multinomial proportional model. min_normalizer: A float (<= 0) used to offset the scores to be positive when using multinomial proportional model. Returns: A float tensor that stores unnormalzied scores of documents and a float tensor that represents the score for the action of picking no document. """ user_obs = tf.reshape(user_obs, [1, -1]) scores = tf.reduce_sum(input_tensor=tf.multiply(user_obs, doc_obs), axis=1) all_scores = tf.concat([scores, tf.constant([no_click_mass])], axis=0) if is_mnl: all_scores = tf.nn.softmax(all_scores) else: all_scores = all_scores - min_normalizer return all_scores[:-1], all_scores[-1]