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
9761aa48
"vscode:/vscode.git/clone" did not exist on "edbff1fd0075dc85b1431f8aa7d26dee2d618e05"
Commit
9761aa48
authored
Apr 15, 2019
by
thomwolf
Browse files
add to_json_file method to configuration classes
parent
b17963d8
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
56 additions
and
0 deletions
+56
-0
pytorch_pretrained_bert/modeling.py
pytorch_pretrained_bert/modeling.py
+5
-0
pytorch_pretrained_bert/modeling_gpt2.py
pytorch_pretrained_bert/modeling_gpt2.py
+5
-0
pytorch_pretrained_bert/modeling_openai.py
pytorch_pretrained_bert/modeling_openai.py
+5
-0
pytorch_pretrained_bert/modeling_transfo_xl.py
pytorch_pretrained_bert/modeling_transfo_xl.py
+5
-0
tests/modeling_gpt2_test.py
tests/modeling_gpt2_test.py
+9
-0
tests/modeling_openai_test.py
tests/modeling_openai_test.py
+9
-0
tests/modeling_test.py
tests/modeling_test.py
+9
-0
tests/modeling_transfo_xl_test.py
tests/modeling_transfo_xl_test.py
+9
-0
No files found.
pytorch_pretrained_bert/modeling.py
View file @
9761aa48
...
...
@@ -220,6 +220,11 @@ class BertConfig(object):
"""Serializes this instance to a JSON string."""
return
json
.
dumps
(
self
.
to_dict
(),
indent
=
2
,
sort_keys
=
True
)
+
"
\n
"
def
to_json_file
(
self
,
json_file_path
):
""" Save this instance to a json file."""
with
open
(
json_file_path
,
"w"
,
encoding
=
'utf-8'
)
as
writer
:
writer
.
write
(
self
.
to_json_string
())
try
:
from
apex.normalization.fused_layer_norm
import
FusedLayerNorm
as
BertLayerNorm
except
ImportError
:
...
...
pytorch_pretrained_bert/modeling_gpt2.py
View file @
9761aa48
...
...
@@ -180,6 +180,11 @@ class GPT2Config(object):
"""Serializes this instance to a JSON string."""
return
json
.
dumps
(
self
.
to_dict
(),
indent
=
2
,
sort_keys
=
True
)
+
"
\n
"
def
to_json_file
(
self
,
json_file_path
):
""" Save this instance to a json file."""
with
open
(
json_file_path
,
"w"
,
encoding
=
'utf-8'
)
as
writer
:
writer
.
write
(
self
.
to_json_string
())
class
Conv1D
(
nn
.
Module
):
def
__init__
(
self
,
nf
,
nx
):
...
...
pytorch_pretrained_bert/modeling_openai.py
View file @
9761aa48
...
...
@@ -225,6 +225,11 @@ class OpenAIGPTConfig(object):
"""Serializes this instance to a JSON string."""
return
json
.
dumps
(
self
.
to_dict
(),
indent
=
2
,
sort_keys
=
True
)
+
"
\n
"
def
to_json_file
(
self
,
json_file_path
):
""" Save this instance to a json file."""
with
open
(
json_file_path
,
"w"
,
encoding
=
'utf-8'
)
as
writer
:
writer
.
write
(
self
.
to_json_string
())
class
Conv1D
(
nn
.
Module
):
def
__init__
(
self
,
nf
,
rf
,
nx
):
...
...
pytorch_pretrained_bert/modeling_transfo_xl.py
View file @
9761aa48
...
...
@@ -316,6 +316,11 @@ class TransfoXLConfig(object):
"""Serializes this instance to a JSON string."""
return
json
.
dumps
(
self
.
to_dict
(),
indent
=
2
,
sort_keys
=
True
)
+
"
\n
"
def
to_json_file
(
self
,
json_file_path
):
""" Save this instance to a json file."""
with
open
(
json_file_path
,
"w"
,
encoding
=
'utf-8'
)
as
writer
:
writer
.
write
(
self
.
to_json_string
())
class
PositionalEmbedding
(
nn
.
Module
):
def
__init__
(
self
,
demb
):
...
...
tests/modeling_gpt2_test.py
View file @
9761aa48
...
...
@@ -16,6 +16,7 @@ from __future__ import absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
os
import
unittest
import
json
import
random
...
...
@@ -176,6 +177,14 @@ class GPT2ModelTest(unittest.TestCase):
self
.
assertEqual
(
obj
[
"vocab_size"
],
99
)
self
.
assertEqual
(
obj
[
"n_embd"
],
37
)
def
test_config_to_json_file
(
self
):
config_first
=
GPT2Config
(
vocab_size_or_config_json_file
=
99
,
n_embd
=
37
)
json_file_path
=
"/tmp/config.json"
config_first
.
to_json_file
(
json_file_path
)
config_second
=
GPT2Config
.
from_json_file
(
json_file_path
)
os
.
remove
(
json_file_path
)
self
.
assertEqual
(
config_second
.
to_dict
(),
config_first
.
to_dict
())
def
run_tester
(
self
,
tester
):
config_and_inputs
=
tester
.
prepare_config_and_inputs
()
output_result
=
tester
.
create_gpt2_model
(
*
config_and_inputs
)
...
...
tests/modeling_openai_test.py
View file @
9761aa48
...
...
@@ -16,6 +16,7 @@ from __future__ import absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
os
import
unittest
import
json
import
random
...
...
@@ -188,6 +189,14 @@ class OpenAIGPTModelTest(unittest.TestCase):
self
.
assertEqual
(
obj
[
"vocab_size"
],
99
)
self
.
assertEqual
(
obj
[
"n_embd"
],
37
)
def
test_config_to_json_file
(
self
):
config_first
=
OpenAIGPTConfig
(
vocab_size_or_config_json_file
=
99
,
n_embd
=
37
)
json_file_path
=
"/tmp/config.json"
config_first
.
to_json_file
(
json_file_path
)
config_second
=
OpenAIGPTConfig
.
from_json_file
(
json_file_path
)
os
.
remove
(
json_file_path
)
self
.
assertEqual
(
config_second
.
to_dict
(),
config_first
.
to_dict
())
def
run_tester
(
self
,
tester
):
config_and_inputs
=
tester
.
prepare_config_and_inputs
()
output_result
=
tester
.
create_openai_model
(
*
config_and_inputs
)
...
...
tests/modeling_test.py
View file @
9761aa48
...
...
@@ -16,6 +16,7 @@ from __future__ import absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
os
import
unittest
import
json
import
random
...
...
@@ -251,6 +252,14 @@ class BertModelTest(unittest.TestCase):
self
.
assertEqual
(
obj
[
"vocab_size"
],
99
)
self
.
assertEqual
(
obj
[
"hidden_size"
],
37
)
def
test_config_to_json_file
(
self
):
config_first
=
BertConfig
(
vocab_size_or_config_json_file
=
99
,
hidden_size
=
37
)
json_file_path
=
"/tmp/config.json"
config_first
.
to_json_file
(
json_file_path
)
config_second
=
BertConfig
.
from_json_file
(
json_file_path
)
os
.
remove
(
json_file_path
)
self
.
assertEqual
(
config_second
.
to_dict
(),
config_first
.
to_dict
())
def
run_tester
(
self
,
tester
):
config_and_inputs
=
tester
.
prepare_config_and_inputs
()
output_result
=
tester
.
create_bert_model
(
*
config_and_inputs
)
...
...
tests/modeling_transfo_xl_test.py
View file @
9761aa48
...
...
@@ -16,6 +16,7 @@ from __future__ import absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
os
import
unittest
import
json
import
random
...
...
@@ -186,6 +187,14 @@ class TransfoXLModelTest(unittest.TestCase):
self
.
assertEqual
(
obj
[
"n_token"
],
96
)
self
.
assertEqual
(
obj
[
"d_embed"
],
37
)
def
test_config_to_json_file
(
self
):
config_first
=
TransfoXLConfig
(
vocab_size_or_config_json_file
=
96
,
d_embed
=
37
)
json_file_path
=
"/tmp/config.json"
config_first
.
to_json_file
(
json_file_path
)
config_second
=
TransfoXLConfig
.
from_json_file
(
json_file_path
)
os
.
remove
(
json_file_path
)
self
.
assertEqual
(
config_second
.
to_dict
(),
config_first
.
to_dict
())
def
run_tester
(
self
,
tester
):
config_and_inputs
=
tester
.
prepare_config_and_inputs
()
...
...
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