本文整理汇总了Python中tensorflow.keras.backend.squeeze方法的典型用法代码示例。如果您正苦于以下问题:Python backend.squeeze方法的具体用法?Python backend.squeeze怎么用?Python backend.squeeze使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.keras.backend
的用法示例。
在下文中一共展示了backend.squeeze方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: call
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import squeeze [as 别名]
def call(self, inputs):
if self.data_mode == 'disjoint':
X, I = inputs
if K.ndim(I) == 2:
I = I[:, 0]
else:
X = inputs
attn_coeff = K.dot(X, self.attn_kernel)
attn_coeff = K.squeeze(attn_coeff, -1)
attn_coeff = K.softmax(attn_coeff)
if self.data_mode == 'single':
output = K.dot(attn_coeff[None, ...], X)
elif self.data_mode == 'batch':
output = K.batch_dot(attn_coeff, X)
else:
output = attn_coeff[:, None] * X
output = tf.math.segment_sum(output, I)
return output
示例2: build
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import squeeze [as 别名]
def build(self, input_layer):
last_layer = input_layer
input_shape = K.int_shape(input_layer)
if self.with_embedding:
if input_shape[-1] != 1:
raise ValueError("Only one feature (the index) can be used with embeddings, "
"i.e. the input shape should be (num_samples, length, 1). "
"The actual shape was: " + str(input_shape))
last_layer = Lambda(lambda x: K.squeeze(x, axis=-1),
output_shape=K.int_shape(last_layer)[:-1])(last_layer) # Remove feature dimension.
last_layer = Embedding(self.embedding_size, self.embedding_dimension,
input_length=input_shape[-2])(last_layer)
for _ in range(self.num_layers):
last_layer = Dense(self.num_units, activation=self.activation)(last_layer)
if self.with_bn:
last_layer = BatchNormalization()(last_layer)
if not np.isclose(self.p_dropout, 0):
last_layer = Dropout(self.p_dropout)(last_layer)
return last_layer
示例3: cat_acc
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import squeeze [as 别名]
def cat_acc(y_true, y_pred):
"""Keras loss function for sparse_categorical_accuracy.
:param y_true: tensor of true class labels.
:param y_pred: class output scores from network.
:returns: categorical accuracy.
"""
# sparse_categorical_accuracy is broken in keras 2.2.4
# https://github.com/keras-team/keras/issues/11348#issuecomment-439969957
# this is taken from e59570ae
from tensorflow.keras import backend as K
# reshape in case it's in shape (num_samples, 1) instead of (num_samples,)
if K.ndim(y_true) == K.ndim(y_pred):
y_true = K.squeeze(y_true, -1)
# convert dense predictions to labels
y_pred_labels = K.argmax(y_pred, axis=-1)
y_pred_labels = K.cast(y_pred_labels, K.floatx())
return K.cast(K.equal(y_true, y_pred_labels), K.floatx())
示例4: _build_tf_cosine_similarity
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import squeeze [as 别名]
def _build_tf_cosine_similarity(max_rank=0, offset=1, eps=1e-12):
# We build the graph (See utils.generic_utils.tf_recall_at_k for original implementation):
tf_db = K.placeholder(ndim=2, dtype=K.floatx()) # Where to find
tf_labels = K.placeholder(ndim=1, dtype=K.floatx()) # and their labels
tf_batch_query = K.placeholder(ndim=2, dtype=K.floatx()) # Used in case of memory issues
batch_labels = K.placeholder(ndim=2, dtype=K.floatx()) # and their labels
all_representations_T = K.expand_dims(tf_db, axis=0) # 1 x D x N
batch_representations = K.expand_dims(tf_batch_query, axis=0) # 1 x n x D
sim = K.batch_dot(batch_representations, all_representations_T) # 1 x n x N
sim = K.squeeze(sim, axis=0) # n x N
sim /= tf.linalg.norm(tf_batch_query, axis=1, keepdims=True) + eps
sim /= tf.linalg.norm(tf_db, axis=0, keepdims=True) + eps
if max_rank > 0: # computing r@K or mAP@K
index_ranking = tf.nn.top_k(sim, k=max_rank + offset).indices
else:
index_ranking = tf.contrib.framework.argsort(sim, axis=-1, direction='DESCENDING', stable=True)
top_k = index_ranking[:, offset:]
tf_ranking = tf.gather(tf_labels, top_k)
return tf_db, tf_labels, tf_batch_query, batch_labels, tf_ranking
示例5: _build_tf_l2_similarity
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import squeeze [as 别名]
def _build_tf_l2_similarity(max_rank=0, offset=1):
# We build the graph (See utils.generic_utils.tf_recall_at_k for original implementation):
tf_db = K.placeholder(ndim=2, dtype=K.floatx()) # Where to find
tf_labels = K.placeholder(ndim=1, dtype=K.floatx()) # and their labels
tf_batch_query = K.placeholder(ndim=2, dtype=K.floatx()) # Used in case of memory issues
batch_labels = K.placeholder(ndim=2, dtype=K.floatx()) # and their labels
all_representations_T = K.expand_dims(tf_db, axis=0) # 1 x D x N
batch_representations = K.expand_dims(tf_batch_query, axis=0) # 1 x n x D
dist = -2. * K.batch_dot(batch_representations, all_representations_T) # 1 x n x N
dist = K.squeeze(dist, axis=0) # n x N
dist += K.sum(tf_batch_query * tf_batch_query, axis=1, keepdims=True)
dist += K.sum(tf_db * tf_db, axis=0, keepdims=True)
if max_rank > 0: # computing r@K or mAP@K
# top_k finds the k greatest entries and we want the lowest. Note that distance with itself will be last ranked
dist = -dist
index_ranking = tf.nn.top_k(dist, k=max_rank + offset).indices
else:
index_ranking = tf.contrib.framework.argsort(dist, axis=-1, direction='ASCENDING', stable=True)
index_ranking = index_ranking[:, offset:]
tf_ranking = tf.gather(tf_labels, index_ranking)
return tf_db, tf_labels, tf_batch_query, batch_labels, tf_ranking
示例6: create_score_model
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import squeeze [as 别名]
def create_score_model(self) -> Model:
cr = self.model.inputs
if self.triplet_mode:
emb_c = self.model.get_layer("gru").output
emb_r = self.model.get_layer("pooling").get_output(-1)
dist_score = Lambda(lambda x: self.euclidian_dist(x), name="score_model")
score = dist_score([emb_c, emb_r])
else:
score = self.model.get_layer("score_model").output
score = Lambda(lambda x: 1. - K.squeeze(x, -1))(score)
score = Lambda(lambda x: 1. - x)(score)
model = Model(cr, score)
return model
示例7: create_score_model
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import squeeze [as 别名]
def create_score_model(self) -> Model:
cr = self.model.inputs
if self.triplet_mode:
emb_c = self.model.get_layer("sentence_embedding").get_output_at(0)
emb_r = self.model.get_layer("sentence_embedding").get_output_at(1)
dist_score = Lambda(lambda x: self._euclidian_dist(x), name="score_model")
score = dist_score([emb_c, emb_r])
else:
score = self.model.get_layer("score_model").output
score = Lambda(lambda x: 1. - K.squeeze(x, -1))(score)
score = Lambda(lambda x: 1. - x)(score)
model = Model(cr, score)
return model
示例8: _triplet_loss
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import squeeze [as 别名]
def _triplet_loss(self, labels: Tensor, pairwise_dist: Tensor) -> Tensor:
y_true = K.squeeze(labels, axis=1)
"""Triplet loss function"""
if self.hard_triplets:
triplet_loss = self._batch_hard_triplet_loss(y_true, pairwise_dist)
else:
triplet_loss = self._batch_all_triplet_loss(y_true, pairwise_dist)
return triplet_loss