Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ModelZoo
ResNet50_tensorflow
Commits
dab5ff9d
Commit
dab5ff9d
authored
Jul 25, 2022
by
Hongkun Yu
Committed by
A. Unique TensorFlower
Jul 25, 2022
Browse files
Internal change
PiperOrigin-RevId: 463166393
parent
e096ecf4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
6 deletions
+9
-6
official/nlp/modeling/layers/per_dim_scale_attention.py
official/nlp/modeling/layers/per_dim_scale_attention.py
+9
-6
No files found.
official/nlp/modeling/layers/per_dim_scale_attention.py
View file @
dab5ff9d
...
@@ -13,10 +13,12 @@
...
@@ -13,10 +13,12 @@
# limitations under the License.
# limitations under the License.
"""Keras-based attention layer with learnable per dim scaling."""
"""Keras-based attention layer with learnable per dim scaling."""
import
gin
import
numpy
as
np
import
numpy
as
np
import
tensorflow
as
tf
import
tensorflow
as
tf
@
gin
.
configurable
@
tf
.
keras
.
utils
.
register_keras_serializable
(
package
=
'Text'
)
@
tf
.
keras
.
utils
.
register_keras_serializable
(
package
=
'Text'
)
class
PerDimScaleAttention
(
tf
.
keras
.
layers
.
MultiHeadAttention
):
class
PerDimScaleAttention
(
tf
.
keras
.
layers
.
MultiHeadAttention
):
"""Learn scales for individual dims.
"""Learn scales for individual dims.
...
@@ -27,12 +29,13 @@ class PerDimScaleAttention(tf.keras.layers.MultiHeadAttention):
...
@@ -27,12 +29,13 @@ class PerDimScaleAttention(tf.keras.layers.MultiHeadAttention):
def
_build_from_signature
(
self
,
query
,
value
,
key
=
None
):
def
_build_from_signature
(
self
,
query
,
value
,
key
=
None
):
super
().
_build_from_signature
(
query
=
query
,
value
=
value
,
key
=
key
)
# pytype: disable=attribute-error
super
().
_build_from_signature
(
query
=
query
,
value
=
value
,
key
=
key
)
# pytype: disable=attribute-error
self
.
_scale_dim
=
self
.
_key_dim
self
.
_scale_dim
=
self
.
_key_dim
self
.
per_dim_scale
=
self
.
add_weight
(
with
tf
.
init_scope
():
name
=
'per_dim_scale'
,
self
.
per_dim_scale
=
self
.
add_weight
(
shape
=
(
self
.
_scale_dim
,),
name
=
'per_dim_scale'
,
initializer
=
'zeros'
,
shape
=
(
self
.
_scale_dim
,),
dtype
=
self
.
dtype
,
initializer
=
'zeros'
,
trainable
=
True
)
dtype
=
self
.
dtype
,
trainable
=
True
)
def
_scale_query
(
self
,
query
):
def
_scale_query
(
self
,
query
):
# 1.0/tf.nn.softplus(0.0) = 1.442695041. Hard code this number so that we
# 1.0/tf.nn.softplus(0.0) = 1.442695041. Hard code this number so that we
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment