本文整理汇总了Python中tensor2tensor.models.transformer.features_to_nonpadding方法的典型用法代码示例。如果您正苦于以下问题:Python transformer.features_to_nonpadding方法的具体用法?Python transformer.features_to_nonpadding怎么用?Python transformer.features_to_nonpadding使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensor2tensor.models.transformer
的用法示例。
在下文中一共展示了transformer.features_to_nonpadding方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: encode
# 需要导入模块: from tensor2tensor.models import transformer [as 别名]
# 或者: from tensor2tensor.models.transformer import features_to_nonpadding [as 别名]
def encode(self, features, input_key):
hparams = self._hparams
inputs = common_layers.flatten4d3d(features[input_key])
(encoder_input, encoder_self_attention_bias, _) = (
transformer.transformer_prepare_encoder(inputs, problem.SpaceID.EN_TOK,
hparams))
encoder_input = tf.nn.dropout(encoder_input,
1.0 - hparams.layer_prepostprocess_dropout)
encoder_output = transformer.transformer_encoder(
encoder_input,
encoder_self_attention_bias,
hparams,
nonpadding=transformer.features_to_nonpadding(features, input_key))
encoder_output = tf.reduce_mean(encoder_output, axis=1)
return encoder_output
示例2: transformer_encoder
# 需要导入模块: from tensor2tensor.models import transformer [as 别名]
# 或者: from tensor2tensor.models.transformer import features_to_nonpadding [as 别名]
def transformer_encoder(inputs, target_space, hparams, features=None, losses=None):
encoder_input, self_attention_bias, encoder_decoder_attention_bias = (
transformer.transformer_prepare_encoder(
inputs, target_space, hparams, features=features))
encoder_input = tf.nn.dropout(encoder_input,
1.0 - hparams.layer_prepostprocess_dropout)
encoder_output = transformer.transformer_encoder(
encoder_input,
self_attention_bias,
hparams,
nonpadding=transformer.features_to_nonpadding(features, "inputs"),
save_weights_to=None,
losses=losses)
# encoder_output = tf.expand_dims(encoder_output, 2)
return encoder_output
示例3: encode
# 需要导入模块: from tensor2tensor.models import transformer [as 别名]
# 或者: from tensor2tensor.models.transformer import features_to_nonpadding [as 别名]
def encode(self, inputs, target_space, hparams, features=None, losses=None):
"""Encode transformer inputs.
Args:
inputs: Transformer inputs [batch_size, input_length, input_height,
hidden_dim] which will be flattened along the two spatial dimensions.
target_space: scalar, target space ID.
hparams: hyperparmeters for model.
features: optionally pass the entire features dictionary as well.
This is needed now for "packed" datasets.
losses: Unused.
Returns:
Tuple of:
encoder_output: Encoder representation.
[batch_size, input_length, hidden_dim]
encoder_extra_output: which is extra encoder output used in some
variants of the model (e.g. in ACT, to pass the ponder-time to body)
"""
del losses
inputs = common_layers.flatten4d3d(inputs)
(encoder_input, self_attention_bias, _) = (
transformer.transformer_prepare_encoder(inputs, target_space, hparams))
encoder_input = tf.nn.dropout(encoder_input,
1.0 - hparams.layer_prepostprocess_dropout)
(encoder_output, encoder_extra_output) = (
universal_transformer_util.universal_transformer_encoder(
encoder_input,
self_attention_bias,
hparams,
nonpadding=transformer.features_to_nonpadding(features, "inputs"),
save_weights_to=self.attention_weights))
return encoder_output, encoder_extra_output
示例4: encode
# 需要导入模块: from tensor2tensor.models import transformer [as 别名]
# 或者: from tensor2tensor.models.transformer import features_to_nonpadding [as 别名]
def encode(self, inputs, target_space, hparams, features=None, losses=None):
"""Encode inputs using _encoder().
This performs the same way as transformer.Transformer.encode with the
encoder portion replaced with _encoder().
Args:
inputs: Input [batch_size, input_length, input_height, hidden_dim] tensor
which will be flattened along the two spatial dimensions.
target_space: scalar, target space ID.
hparams: Hyperparmeters for model.
features: Optionally pass the entire features dictionary as well. This is
needed now for "packed" datasets.
losses: Unused list of losses.
Returns:
Tuple of:
encoder_output: Encoder representation.
[batch_size, input_length, hidden_dim]
encoder_decoder_attention_bias: Bias and mask weights for
encodre-decoder attention. [batch_size, input_length]
Raises:
ValueError: If encoder type not found.
"""
inputs = common_layers.flatten4d3d(inputs)
encoder_input, self_attention_bias, encoder_decoder_attention_bias = (
transformer.transformer_prepare_encoder(
inputs, target_space, hparams, features=features))
encoder_input = tf.nn.dropout(encoder_input,
1.0 - hparams.layer_prepostprocess_dropout)
encoder_output = self._encoder(
encoder_input,
self_attention_bias,
hparams,
nonpadding=transformer.features_to_nonpadding(features, "inputs"),
save_weights_to=self.attention_weights)
return encoder_output, encoder_decoder_attention_bias
示例5: __init__
# 需要导入模块: from tensor2tensor.models import transformer [as 别名]
# 或者: from tensor2tensor.models.transformer import features_to_nonpadding [as 别名]
def __init__(self, features_info=None, input_names=None, target_names=None,
hidden_size=512, filter_size=2048):
super(Transformer, self).__init__()
# TODO(lukaszkaiser): gin'ify and split into encoder/decoder classes.
self._has_input = True if input_names else False
self._input_name = input_names[0]
self._target_name = target_names[0]
try:
target_vocab_size = features_info[self._target_name].num_classes
except AttributeError:
target_vocab_size = features_info[self._target_name].encoder.vocab_size
hparams = transformer.transformer_base()
hparams.hidden_size = hidden_size
hparams.filter_size = filter_size
# Now the model.
self._embedding = tf.keras.layers.Embedding(
target_vocab_size, hidden_size, mask_zero=True)
def transformer_encoder(inputs, features):
return transformer.transformer_encode(
transformer_layers.transformer_encoder, inputs, None,
hparams, features=features)
def transformer_prepare_decoder(targets, features):
return transformer.transformer_prepare_decoder(targets, hparams, features)
def transformer_decoder(decoder_input, encoder_output,
encoder_decoder_attention_bias,
decoder_self_attention_bias,
features):
return transformer.transformer_decode(
transformer.transformer_decoder,
decoder_input,
encoder_output,
encoder_decoder_attention_bias,
decoder_self_attention_bias,
hparams,
nonpadding=transformer.features_to_nonpadding(features, "targets"))
if self._has_input:
self._encoder = keras_utils.FunctionLayer(transformer_encoder)
self._prepare_decoder = keras_utils.FunctionLayer(
transformer_prepare_decoder)
self._decoder = keras_utils.FunctionLayer(transformer_decoder)
self._logits = tf.keras.layers.Dense(
target_vocab_size, activation=None)