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
chenpangpang
transformers
Commits
8513741b
Unverified
Commit
8513741b
authored
Nov 13, 2018
by
Thomas Wolf
Committed by
GitHub
Nov 13, 2018
Browse files
Merge pull request #17 from lukovnikov/master
activation function in BERTIntermediate
parents
5cd8d7ad
470076e4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
2 deletions
+11
-2
modeling.py
modeling.py
+11
-2
No files found.
modeling.py
View file @
8513741b
...
@@ -25,6 +25,7 @@ import six
...
@@ -25,6 +25,7 @@ import six
import
torch
import
torch
import
torch.nn
as
nn
import
torch.nn
as
nn
from
torch.nn
import
CrossEntropyLoss
from
torch.nn
import
CrossEntropyLoss
from
six
import
string_types
def
gelu
(
x
):
def
gelu
(
x
):
"""Implementation of the gelu activation function.
"""Implementation of the gelu activation function.
...
@@ -34,6 +35,13 @@ def gelu(x):
...
@@ -34,6 +35,13 @@ def gelu(x):
return
x
*
0.5
*
(
1.0
+
torch
.
erf
(
x
/
math
.
sqrt
(
2.0
)))
return
x
*
0.5
*
(
1.0
+
torch
.
erf
(
x
/
math
.
sqrt
(
2.0
)))
def
swish
(
x
):
return
x
*
torch
.
sigmoid
(
x
)
ACT2FN
=
{
"gelu"
:
gelu
,
"relu"
:
torch
.
nn
.
functional
.
relu
,
"swish"
:
swish
}
class
BertConfig
(
object
):
class
BertConfig
(
object
):
"""Configuration class to store the configuration of a `BertModel`.
"""Configuration class to store the configuration of a `BertModel`.
"""
"""
...
@@ -60,7 +68,7 @@ class BertConfig(object):
...
@@ -60,7 +68,7 @@ class BertConfig(object):
intermediate_size: The size of the "intermediate" (i.e., feed-forward)
intermediate_size: The size of the "intermediate" (i.e., feed-forward)
layer in the Transformer encoder.
layer in the Transformer encoder.
hidden_act: The non-linear activation function (function or string) in the
hidden_act: The non-linear activation function (function or string) in the
encoder and pooler.
encoder and pooler.
If string, "gelu", "relu" and "swish" are supported.
hidden_dropout_prob: The dropout probabilitiy for all fully connected
hidden_dropout_prob: The dropout probabilitiy for all fully connected
layers in the embeddings, encoder, and pooler.
layers in the embeddings, encoder, and pooler.
attention_probs_dropout_prob: The dropout ratio for the attention
attention_probs_dropout_prob: The dropout ratio for the attention
...
@@ -237,7 +245,8 @@ class BERTIntermediate(nn.Module):
...
@@ -237,7 +245,8 @@ class BERTIntermediate(nn.Module):
def
__init__
(
self
,
config
):
def
__init__
(
self
,
config
):
super
(
BERTIntermediate
,
self
).
__init__
()
super
(
BERTIntermediate
,
self
).
__init__
()
self
.
dense
=
nn
.
Linear
(
config
.
hidden_size
,
config
.
intermediate_size
)
self
.
dense
=
nn
.
Linear
(
config
.
hidden_size
,
config
.
intermediate_size
)
self
.
intermediate_act_fn
=
gelu
self
.
intermediate_act_fn
=
ACT2FN
[
config
.
hidden_act
]
\
if
isinstance
(
config
.
hidden_act
,
string_types
)
else
config
.
hidden_act
def
forward
(
self
,
hidden_states
):
def
forward
(
self
,
hidden_states
):
hidden_states
=
self
.
dense
(
hidden_states
)
hidden_states
=
self
.
dense
(
hidden_states
)
...
...
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