本文整理匯總了Python中tensorflow.compat.v1.multiply方法的典型用法代碼示例。如果您正苦於以下問題:Python v1.multiply方法的具體用法?Python v1.multiply怎麽用?Python v1.multiply使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tensorflow.compat.v1
的用法示例。
在下文中一共展示了v1.multiply方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: kl_divergence
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import multiply [as 別名]
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)
示例2: testSpectralNorm
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import multiply [as 別名]
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))
示例3: read
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import multiply [as 別名]
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
示例4: apply_batch_norm
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import multiply [as 別名]
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)
示例5: add_heatmap_summary
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import multiply [as 別名]
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
示例6: _variable_with_weight_decay
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import multiply [as 別名]
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
示例7: _get_cost_function
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import multiply [as 別名]
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
示例8: f1_metric
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import multiply [as 別名]
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
示例9: cosine_similarity
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import multiply [as 別名]
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
示例10: should_distort_images
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import multiply [as 別名]
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))
示例11: convert_class_logits_to_softmax
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import multiply [as 別名]
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
示例12: testRandomHorizontalFlipWithEmptyBoxes
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import multiply [as 別名]
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_)
示例13: test_forward_multi_input
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import multiply [as 別名]
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
# ---------------------
示例14: calc_iou_tensor
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import multiply [as 別名]
def calc_iou_tensor(boxes1, boxes2):
"""Calculation of IoU based on two boxes tensor.
Reference to https://github.com/kuangliu/pytorch-ssd
Args:
boxes1: shape (N, 4), four coordinates of N boxes
boxes2: shape (M, 4), four coordinates of M boxes
Returns:
IoU: shape (N, M), IoU of the i-th box in `boxes1` and j-th box in `boxes2`
"""
b1_left, b1_top, b1_right, b1_bottom = tf.split(boxes1, 4, axis=1)
b2_left, b2_top, b2_right, b2_bottom = tf.split(boxes2, 4, axis=1)
# Shape of intersect_* (N, M)
intersect_left = tf.maximum(b1_left, tf.transpose(b2_left))
intersect_top = tf.maximum(b1_top, tf.transpose(b2_top))
intersect_right = tf.minimum(b1_right, tf.transpose(b2_right))
intersect_bottom = tf.minimum(b1_bottom, tf.transpose(b2_bottom))
boxes1_area = (b1_right - b1_left) * (b1_bottom - b1_top)
boxes2_area = (b2_right - b2_left) * (b2_bottom - b2_top)
intersect = tf.multiply(tf.maximum((intersect_right - intersect_left), 0),
tf.maximum((intersect_bottom - intersect_top), 0))
union = boxes1_area + tf.transpose(boxes2_area) - intersect
iou = intersect / union
return iou
示例15: _localization_loss
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import multiply [as 別名]
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)