本文整理匯總了Python中tensorflow.compat.v1.squeeze方法的典型用法代碼示例。如果您正苦於以下問題:Python v1.squeeze方法的具體用法?Python v1.squeeze怎麽用?Python v1.squeeze使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tensorflow.compat.v1
的用法示例。
在下文中一共展示了v1.squeeze方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: two_class_log_likelihood
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import squeeze [as 別名]
def two_class_log_likelihood(predictions, labels, weights_fn=None):
"""Log-likelihood for two class classification with 0/1 labels.
Args:
predictions: A float valued tensor of shape [`batch_size`]. Each
component should be between 0 and 1.
labels: An int valued tensor of shape [`batch_size`]. Each component
should either be 0 or 1.
weights_fn: unused.
Returns:
A pair, with the average log likelihood in the first component.
"""
del weights_fn
float_predictions = tf.cast(tf.squeeze(predictions), dtype=tf.float64)
batch_probs = tf.stack([1. - float_predictions, float_predictions], axis=-1)
int_labels = tf.cast(tf.squeeze(labels), dtype=tf.int32)
onehot_targets = tf.cast(tf.one_hot(int_labels, 2), dtype=tf.float64)
chosen_probs = tf.einsum(
"ij,ij->i", batch_probs, onehot_targets, name="chosen_probs")
avg_log_likelihood = tf.reduce_mean(tf.log(chosen_probs))
return avg_log_likelihood, tf.constant(1.0)
示例2: set_precision
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import squeeze [as 別名]
def set_precision(predictions, labels,
weights_fn=common_layers.weights_nonzero):
"""Precision of set predictions.
Args:
predictions : A Tensor of scores of shape [batch, nlabels].
labels: A Tensor of int32s giving true set elements,
of shape [batch, seq_length].
weights_fn: A function to weight the elements.
Returns:
hits: A Tensor of shape [batch, nlabels].
weights: A Tensor of shape [batch, nlabels].
"""
with tf.variable_scope("set_precision", values=[predictions, labels]):
labels = tf.squeeze(labels, [2, 3])
weights = weights_fn(labels)
labels = tf.one_hot(labels, predictions.shape[-1])
labels = tf.reduce_max(labels, axis=1)
labels = tf.cast(labels, tf.bool)
return tf.to_float(tf.equal(labels, predictions)), weights
示例3: set_recall
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import squeeze [as 別名]
def set_recall(predictions, labels, weights_fn=common_layers.weights_nonzero):
"""Recall of set predictions.
Args:
predictions : A Tensor of scores of shape [batch, nlabels].
labels: A Tensor of int32s giving true set elements,
of shape [batch, seq_length].
weights_fn: A function to weight the elements.
Returns:
hits: A Tensor of shape [batch, nlabels].
weights: A Tensor of shape [batch, nlabels].
"""
with tf.variable_scope("set_recall", values=[predictions, labels]):
labels = tf.squeeze(labels, [2, 3])
weights = weights_fn(labels)
labels = tf.one_hot(labels, predictions.shape[-1])
labels = tf.reduce_max(labels, axis=1)
labels = tf.cast(labels, tf.bool)
return tf.to_float(tf.equal(labels, predictions)), weights
示例4: rouge_l_fscore
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import squeeze [as 別名]
def rouge_l_fscore(predictions, labels, **unused_kwargs):
"""ROUGE scores computation between labels and predictions.
This is an approximate ROUGE scoring method since we do not glue word pieces
or decode the ids and tokenize the output.
Args:
predictions: tensor, model predictions
labels: tensor, gold output.
Returns:
rouge_l_fscore: approx rouge-l f1 score.
"""
outputs = tf.to_int32(tf.argmax(predictions, axis=-1))
# Convert the outputs and labels to a [batch_size, input_length] tensor.
outputs = tf.squeeze(outputs, axis=[-1, -2])
labels = tf.squeeze(labels, axis=[-1, -2])
rouge_l_f_score = tf.py_func(rouge_l_sentence_level, (outputs, labels),
tf.float32)
return rouge_l_f_score, tf.constant(1.0)
示例5: rouge_2_fscore
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import squeeze [as 別名]
def rouge_2_fscore(predictions, labels, **unused_kwargs):
"""ROUGE-2 F1 score computation between labels and predictions.
This is an approximate ROUGE scoring method since we do not glue word pieces
or decode the ids and tokenize the output.
Args:
predictions: tensor, model predictions
labels: tensor, gold output.
Returns:
rouge2_fscore: approx rouge-2 f1 score.
"""
outputs = tf.to_int32(tf.argmax(predictions, axis=-1))
# Convert the outputs and labels to a [batch_size, input_length] tensor.
outputs = tf.squeeze(outputs, axis=[-1, -2])
labels = tf.squeeze(labels, axis=[-1, -2])
rouge_2_f_score = tf.py_func(rouge_n, (outputs, labels), tf.float32)
return rouge_2_f_score, tf.constant(1.0)
示例6: bleu_score
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import squeeze [as 別名]
def bleu_score(predictions, labels, **unused_kwargs):
"""BLEU score computation between labels and predictions.
An approximate BLEU scoring method since we do not glue word pieces or
decode the ids and tokenize the output. By default, we use ngram order of 4
and use brevity penalty. Also, this does not have beam search.
Args:
predictions: tensor, model predictions
labels: tensor, gold output.
Returns:
bleu: int, approx bleu score
"""
outputs = tf.to_int32(tf.argmax(predictions, axis=-1))
# Convert the outputs and labels to a [batch_size, input_length] tensor.
outputs = tf.squeeze(outputs, axis=[-1, -2])
labels = tf.squeeze(labels, axis=[-1, -2])
bleu = tf.py_func(compute_bleu, (labels, outputs), tf.float32)
return bleu, tf.constant(1.0)
示例7: decode
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import squeeze [as 別名]
def decode(self, bottleneck):
"""Auto-decode from the bottleneck and return the result."""
# Get the shape from bottleneck and num channels.
shape = common_layers.shape_list(bottleneck)
try:
num_channels = self.hparams.problem.num_channels
except AttributeError:
num_channels = 1
dummy_targets = tf.zeros(shape[:-1] + [num_channels])
# Set the bottleneck to decode.
if len(shape) > 4:
bottleneck = tf.squeeze(bottleneck, axis=[1])
bottleneck = 2 * bottleneck - 1 # Be -1/1 instead of 0/1.
self._cur_bottleneck_tensor = bottleneck
# Run decoding.
res = self.infer({"targets": dummy_targets})
self._cur_bottleneck_tensor = None
return res
示例8: body
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import squeeze [as 別名]
def body(self, features):
# Remove dropout if not training
hparams = self._hparams
targets = features["targets"]
targets = tf.squeeze(targets, 2)
(decoder_input, decoder_self_attention_bias) = attention_lm_prepare_decoder(
targets, hparams)
decoder_input = tf.nn.dropout(decoder_input,
1.0 - hparams.layer_prepostprocess_dropout)
decoder_output = attention_lm_decoder(decoder_input,
decoder_self_attention_bias, hparams)
decoder_output = tf.expand_dims(decoder_output, 2)
return decoder_output
示例9: infer
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import squeeze [as 別名]
def infer(self,
features=None,
decode_length=1,
beam_size=1,
top_beams=1,
alpha=0.0,
use_tpu=False):
"""Predict."""
del decode_length, beam_size, top_beams, alpha, use_tpu
assert features is not None
logits, _ = self(features)
assert len(logits.get_shape()) == 5
logits = tf.squeeze(logits, [1, 2, 3])
log_probs = common_layers.log_prob_from_logits(logits)
predictions, scores = common_layers.argmax_with_score(log_probs)
return {
"outputs": predictions,
"scores": scores,
}
示例10: infer
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import squeeze [as 別名]
def infer(self,
features=None,
decode_length=50,
beam_size=1,
top_beams=1,
alpha=0.0,
use_tpu=False):
"""Predict."""
del decode_length, beam_size, top_beams, alpha, use_tpu
assert features is not None
logits, _ = self(features) # pylint: disable=not-callable
assert len(logits.get_shape()) == 5
logits = tf.squeeze(logits, [1, 2, 3])
log_probs = common_layers.log_prob_from_logits(logits)
predictions, scores = common_layers.argmax_with_score(log_probs)
return {
"outputs": predictions,
"scores": scores,
}
示例11: encode_knowledge_bottom
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import squeeze [as 別名]
def encode_knowledge_bottom(self, features):
tf.logging.info("Encoding knowledge " + str(self.triple_num))
# Make sure this is embeddings for triples
# <tf.float32>[batch_size, triple_num*max_triple_length, 1, emb_dim]
fact_embedding = features["encoded_triples"]
# [batch_size, triple_num*max_triple_length, emb_dim]
fact_embedding = tf.squeeze(fact_embedding, 2)
kb_shape = common_layers.shape_list(fact_embedding)
batch_size = kb_shape[0]
embed_dim = kb_shape[2]
# <tf.float32>[batch_size*triple_num, max_triple_length, emb_dim]
re_fact_embedding = tf.reshape(
fact_embedding, [batch_size * self.triple_num, -1, embed_dim],
name="reshape_fact_embedding")
# <tf.int64>[batch_size, triple_num]
input_fact_lengths = features["triple_lens"]
# Stack the fact lengths.
# <tf.int64>[batch_size*max_triple_num]
re_fact_lengths = tf.reshape(
input_fact_lengths, [batch_size * self.triple_num, 1],
name="reshape_fact_lengths")
return re_fact_embedding, re_fact_lengths
示例12: compute_last_embedding
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import squeeze [as 別名]
def compute_last_embedding(input_embeddings, input_lengths, hparams):
"""Computes average of last K embedding.
Args:
input_embeddings: <tf.float32>[bs, max_seq_len, emb_dim]
input_lengths: <tf.int64>[bs, 1]
hparams: model hparams
Returns:
last_k_embedding: <tf.float32>[bs, emb_dim]
"""
max_seq_len = tf.shape(input_embeddings)[1]
# <tf.float32>[bs, 1, max_seq_len]
mask = tf.sequence_mask(input_lengths, max_seq_len, dtype=tf.float32)
del_mask = tf.sequence_mask(
input_lengths - hparams.last_k, max_seq_len, dtype=tf.float32)
final_mask = mask - del_mask
# <tf.float32>[bs, 1, emb_dim]
sum_embedding = tf.matmul(final_mask, input_embeddings)
# <tf.float32>[bs, 1, emb_dim]
last_k_embedding = sum_embedding / tf.to_float(
tf.expand_dims(
tf.ones([tf.shape(input_embeddings)[0], 1]) * hparams.last_k, 2))
# <tf.float32>[bs, dim]
return tf.squeeze(last_k_embedding, 1)
示例13: compute_max_pool_embedding
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import squeeze [as 別名]
def compute_max_pool_embedding(input_embeddings, input_lengths):
"""Computes max pool embedding.
Args:
input_embeddings: <tf.float32>[bs, max_seq_len, emb_dim]
input_lengths: <tf.int64>[bs, 1]
Returns:
max_pool_embedding: <tf.float32>[bs, emb_dim]
"""
max_seq_len = tf.shape(input_embeddings)[1]
# <tf.float32>[bs, max_seq_len]
mask = 1.0 - tf.sequence_mask(input_lengths, max_seq_len, dtype=tf.float32)
mask = tf.squeeze(mask * (-1e-6), 1)
mask = tf.expand_dims(mask, 2)
# <tf.float32>[bs, emb_dim]
max_pool_embedding = tf.reduce_max(input_embeddings + mask, 1)
# <tf.float32>[bs, dim]
return max_pool_embedding
示例14: _apply_logic
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import squeeze [as 別名]
def _apply_logic(self, input_tensor, output_depth, hparams, var_scope_suffix,
nonpadding, mask_future, **unused_kwargs):
"""Applies conv logic to `input_tensor`."""
with tf.variable_scope("%s_conv_%s" % (self._conv_type, var_scope_suffix)):
if mask_future:
# Pad shift the inputs so that temporal information does not leak. This
# must be used in tandem with VALID padding.
pad_amount = int(self._conv_width - 1) * self._dilation_rate
logic_output = tf.pad(
input_tensor, paddings=[[0, 0], [pad_amount, 0], [0, 0]])
padding = "VALID"
else:
logic_output = input_tensor
padding = "SAME"
logic_output = tf.expand_dims(logic_output, 2)
logic_output = self._conv_function(logic_output, output_depth, padding)
logic_output = tf.squeeze(logic_output, 2)
return logic_output
示例15: _create_greedy_infer_model
# 需要導入模塊: from tensorflow.compat import v1 [as 別名]
# 或者: from tensorflow.compat.v1 import squeeze [as 別名]
def _create_greedy_infer_model(self):
"""Creates model for greedy inference testing.
Returns:
model: A t2t model.
features: An map of string to tensor.
"""
model, features = get_model(transformer.transformer_tiny())
out_logits, _ = model(features)
out_logits = tf.squeeze(out_logits, axis=[2, 3])
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=tf.reshape(out_logits, [-1, VOCAB_SIZE]),
labels=tf.reshape(features["targets"], [-1]))
loss = tf.reduce_mean(loss)
apply_grad = tf.train.AdamOptimizer(0.001).minimize(loss)
with self.test_session():
tf.global_variables_initializer().run()
for _ in range(10):
apply_grad.run()
model.set_mode(tf.estimator.ModeKeys.PREDICT)
return model, features