Skip to content

Commit 26fdf99

Browse files
committed
[FixBug] Fix bug with loading pretrained lstm and cnn models
1 parent 776dfec commit 26fdf99

4 files changed

Lines changed: 22 additions & 14 deletions

File tree

textattack/attacker.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,6 @@ def _attack_parallel(self):
392392
def attack_dataset(self):
393393
"""Attack the dataset.
394394
395-
396395
Returns:
397396
:obj:`list[AttackResult]` - List of :class:`~textattack.attack_results.AttackResult` obtained after attacking the given dataset..
398397
"""

textattack/model_args.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,24 +90,27 @@
9090

9191
#
9292
# Models hosted by textattack.
93+
# `models` vs `models_v2`: `models_v2` is simply a new dir in S3 that contains models' `config.json`.
94+
# Fixes issue https://github.com/QData/TextAttack/issues/485
95+
# Model parameters has not changed.
9396
#
9497
TEXTATTACK_MODELS = {
9598
#
9699
# LSTMs
97100
#
98-
"lstm-ag-news": "models/classification/lstm/ag-news",
99-
"lstm-imdb": "models/classification/lstm/imdb",
100-
"lstm-mr": "models/classification/lstm/mr",
101-
"lstm-sst2": "models/classification/lstm/sst2",
102-
"lstm-yelp": "models/classification/lstm/yelp",
101+
"lstm-ag-news": "models_v2/classification/lstm/ag-news",
102+
"lstm-imdb": "models_v2/classification/lstm/imdb",
103+
"lstm-mr": "models_v2/classification/lstm/mr",
104+
"lstm-sst2": "models_v2/classification/lstm/sst2",
105+
"lstm-yelp": "models_v2/classification/lstm/yelp",
103106
#
104107
# CNNs
105108
#
106-
"cnn-ag-news": "models/classification/cnn/ag-news",
107-
"cnn-imdb": "models/classification/cnn/imdb",
108-
"cnn-mr": "models/classification/cnn/rotten-tomatoes",
109-
"cnn-sst2": "models/classification/cnn/sst",
110-
"cnn-yelp": "models/classification/cnn/yelp",
109+
"cnn-ag-news": "models_v2/classification/cnn/ag-news",
110+
"cnn-imdb": "models_v2/classification/cnn/imdb",
111+
"cnn-mr": "models_v2/classification/cnn/rotten-tomatoes",
112+
"cnn-sst2": "models_v2/classification/cnn/sst",
113+
"cnn-yelp": "models_v2/classification/cnn/yelp",
111114
#
112115
# T5 for translation
113116
#

textattack/models/helpers/lstm_for_classification.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,17 @@ def from_pretrained(cls, name_or_path):
101101
"""Load trained LSTM model by name or from path.
102102
103103
Args:
104-
name_or_path (str): Name of the model (e.g. "lstm-imdb") or model saved via `save_pretrained`.
104+
name_or_path (:obj:`str`): Name of the model (e.g. "lstm-imdb") or model saved via :meth:`save_pretrained`.
105+
Returns:
106+
:class:`~textattack.models.helpers.LSTMForClassification` model
105107
"""
106108
if name_or_path in TEXTATTACK_MODELS:
107109
path = utils.download_if_needed(TEXTATTACK_MODELS[name_or_path])
108110
else:
109111
path = name_or_path
110112

111113
config_path = os.path.join(path, "config.json")
114+
112115
if os.path.exists(config_path):
113116
with open(config_path, "r") as f:
114117
config = json.load(f)

textattack/models/helpers/word_cnn_for_classification.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,17 @@ def from_pretrained(cls, name_or_path):
8787
"""Load trained LSTM model by name or from path.
8888
8989
Args:
90-
name_or_path (str): Name of the model (e.g. "cnn-imdb") or model saved via `save_pretrained`.
90+
name_or_path (:obj:`str`): Name of the model (e.g. "cnn-imdb") or model saved via :meth:`save_pretrained`.
91+
Returns:
92+
:class:`~textattack.models.helpers.WordCNNForClassification` model
9193
"""
92-
if name_or_path != "cnn" and name_or_path in TEXTATTACK_MODELS:
94+
if name_or_path in TEXTATTACK_MODELS:
9395
path = utils.download_if_needed(TEXTATTACK_MODELS[name_or_path])
9496
else:
9597
path = name_or_path
9698

9799
config_path = os.path.join(path, "config.json")
100+
98101
if os.path.exists(config_path):
99102
with open(config_path, "r") as f:
100103
config = json.load(f)

0 commit comments

Comments
 (0)